Make usage of ObserverPathnameExtension more obvious

Remove use of globals.

Closes Homebrew/homebrew#21795.
This commit is contained in:
Jack Nagel 2013-08-09 21:09:48 -05:00
parent 23fbe23426
commit 966e82663f
3 changed files with 30 additions and 22 deletions

View File

@ -2,12 +2,9 @@ require 'keg'
require 'cmd/tap' require 'cmd/tap'
module Homebrew extend self module Homebrew extend self
# $n and $d are used by the ObserverPathnameExtension to keep track of
# certain filesystem actions.
def prune def prune
$n = 0 ObserverPathnameExtension.reset_counts!
$d = 0
dirs = [] dirs = []
Keg::PRUNEABLE_DIRECTORIES.select(&:directory?).each do |dir| Keg::PRUNEABLE_DIRECTORIES.select(&:directory?).each do |dir|
@ -41,11 +38,13 @@ module Homebrew extend self
repair_taps unless ARGV.dry_run? repair_taps unless ARGV.dry_run?
if $n == 0 && $d == 0 n, d = ObserverPathnameExtension.counts
if ObserverPathnameExtension.total.zero?
puts "Nothing pruned" if ARGV.verbose? puts "Nothing pruned" if ARGV.verbose?
else else
print "Pruned #{$n} symbolic links " print "Pruned #{n} symbolic links "
print "and #{$d} directories " if $d > 0 print "and #{d} directories " if d > 0
puts "from #{HOMEBREW_PREFIX}" puts "from #{HOMEBREW_PREFIX}"
end unless ARGV.dry_run? end unless ARGV.dry_run?
end end

View File

@ -433,21 +433,36 @@ class Pathname
end end
end end
# sets $n and $d so you can observe creation of stuff
module ObserverPathnameExtension module ObserverPathnameExtension
class << self
attr_accessor :n, :d
def reset_counts!
@n = @d = 0
end
def total
n + d
end
def counts
[n, d]
end
end
def unlink def unlink
super super
puts "rm #{to_s}" if ARGV.verbose? puts "rm #{to_s}" if ARGV.verbose?
$n+=1 ObserverPathnameExtension.n += 1
end end
def rmdir def rmdir
super super
puts "rmdir #{to_s}" if ARGV.verbose? puts "rmdir #{to_s}" if ARGV.verbose?
$d+=1 ObserverPathnameExtension.d += 1
end end
def make_relative_symlink src def make_relative_symlink src
super super
$n+=1 ObserverPathnameExtension.n += 1
end end
def install_info def install_info
super super
@ -458,6 +473,3 @@ module ObserverPathnameExtension
puts "uninfo #{to_s}" if ARGV.verbose? puts "uninfo #{to_s}" if ARGV.verbose?
end end
end end
$n=0
$d=0

View File

@ -33,9 +33,7 @@ class Keg < Pathname
end end
def unlink def unlink
# these are used by the ObserverPathnameExtension to count the number ObserverPathnameExtension.reset_counts!
# of files and directories linked
$n=$d=0
dirs = [] dirs = []
@ -62,7 +60,7 @@ class Keg < Pathname
dirs.reverse_each(&:rmdir_if_possible) dirs.reverse_each(&:rmdir_if_possible)
$n+$d ObserverPathnameExtension.total
end end
def fname def fname
@ -108,8 +106,7 @@ class Keg < Pathname
def link mode=OpenStruct.new def link mode=OpenStruct.new
raise "Cannot link #{fname}\nAnother version is already linked: #{linked_keg_record.realpath}" if linked_keg_record.directory? raise "Cannot link #{fname}\nAnother version is already linked: #{linked_keg_record.realpath}" if linked_keg_record.directory?
$n=0 ObserverPathnameExtension.reset_counts!
$d=0
share_mkpaths = %w[aclocal doc info locale man] share_mkpaths = %w[aclocal doc info locale man]
share_mkpaths.concat((1..8).map { |i| "man/man#{i}" }) share_mkpaths.concat((1..8).map { |i| "man/man#{i}" })
@ -175,7 +172,7 @@ class Keg < Pathname
optlink optlink
end end
return $n + $d ObserverPathnameExtension.total
rescue Exception rescue Exception
opoo "Could not link #{fname}. Unlinking..." opoo "Could not link #{fname}. Unlinking..."
unlink unlink