Tap: use method instead of variable

This commit is contained in:
Xu Cheng 2015-12-06 20:57:28 +08:00
parent d20cd244f4
commit 89bd945bb7

View File

@ -55,7 +55,7 @@ class Tap
def remote def remote
@remote ||= if installed? @remote ||= if installed?
if git? if git?
@path.cd do path.cd do
Utils.popen_read("git", "config", "--get", "remote.origin.url").chomp Utils.popen_read("git", "config", "--get", "remote.origin.url").chomp
end end
end end
@ -66,7 +66,7 @@ class Tap
# True if this {Tap} is a git repository. # True if this {Tap} is a git repository.
def git? def git?
(@path/".git").exist? (path/".git").exist?
end end
def to_s def to_s
@ -75,13 +75,13 @@ class Tap
# True if this {Tap} is an official Homebrew tap. # True if this {Tap} is an official Homebrew tap.
def official? def official?
@user == "Homebrew" user == "Homebrew"
end end
# True if the remote of this {Tap} is a private repository. # True if the remote of this {Tap} is a private repository.
def private? def private?
return true if custom_remote? return true if custom_remote?
GitHub.private_repo?(@user, "homebrew-#{@repo}") GitHub.private_repo?(user, "homebrew-#{repo}")
rescue GitHub::HTTPNotFoundError rescue GitHub::HTTPNotFoundError
true true
rescue GitHub::Error rescue GitHub::Error
@ -90,7 +90,7 @@ class Tap
# True if this {Tap} has been installed. # True if this {Tap} has been installed.
def installed? def installed?
@path.directory? path.directory?
end end
# install this {Tap}. # install this {Tap}.
@ -104,8 +104,8 @@ class Tap
# ensure git is installed # ensure git is installed
Utils.ensure_git_installed! Utils.ensure_git_installed!
ohai "Tapping #{name}" ohai "Tapping #{name}"
remote = options[:clone_target] || "https://github.com/#{@user}/homebrew-#{@repo}" remote = options[:clone_target] || "https://github.com/#{user}/homebrew-#{repo}"
args = %W[clone #{remote} #{@path}] args = %W[clone #{remote} #{path}]
args << "--depth=1" unless options.fetch(:full_clone, false) args << "--depth=1" unless options.fetch(:full_clone, false)
begin begin
@ -113,13 +113,13 @@ class Tap
rescue Interrupt, ErrorDuringExecution rescue Interrupt, ErrorDuringExecution
ignore_interrupts do ignore_interrupts do
sleep 0.1 # wait for git to cleanup the top directory when interrupt happens. sleep 0.1 # wait for git to cleanup the top directory when interrupt happens.
@path.parent.rmdir_if_possible path.parent.rmdir_if_possible
end end
raise raise
end end
formula_count = formula_files.size formula_count = formula_files.size
puts "Tapped #{formula_count} formula#{plural(formula_count, "e")} (#{@path.abv})" puts "Tapped #{formula_count} formula#{plural(formula_count, "e")} (#{path.abv})"
Descriptions.cache_formulae(formula_names) Descriptions.cache_formulae(formula_names)
if !options[:clone_target] && private? if !options[:clone_target] && private?
@ -127,8 +127,8 @@ class Tap
It looks like you tapped a private repository. To avoid entering your It looks like you tapped a private repository. To avoid entering your
credentials each time you update, you can use git HTTP credential credentials each time you update, you can use git HTTP credential
caching or issue the following command: caching or issue the following command:
cd #{@path} cd #{path}
git remote set-url origin git@github.com:#{@user}/homebrew-#{@repo}.git git remote set-url origin git@github.com:#{user}/homebrew-#{repo}.git
EOS EOS
end end
end end
@ -137,24 +137,24 @@ class Tap
def uninstall def uninstall
raise TapUnavailableError, name unless installed? raise TapUnavailableError, name unless installed?
puts "Untapping #{name}... (#{@path.abv})" puts "Untapping #{name}... (#{path.abv})"
unpin if pinned? unpin if pinned?
formula_count = formula_files.size formula_count = formula_files.size
Descriptions.uncache_formulae(formula_names) Descriptions.uncache_formulae(formula_names)
@path.rmtree path.rmtree
@path.dirname.rmdir_if_possible path.dirname.rmdir_if_possible
puts "Untapped #{formula_count} formula#{plural(formula_count, "e")}" puts "Untapped #{formula_count} formula#{plural(formula_count, "e")}"
end end
# True if the {#remote} of {Tap} is customized. # True if the {#remote} of {Tap} is customized.
def custom_remote? def custom_remote?
return true unless remote return true unless remote
remote.casecmp("https://github.com/#{@user}/homebrew-#{@repo}") != 0 remote.casecmp("https://github.com/#{user}/homebrew-#{repo}") != 0
end end
# an array of all {Formula} files of this {Tap}. # an array of all {Formula} files of this {Tap}.
def formula_files def formula_files
@formula_files ||= if dir = [@path/"Formula", @path/"HomebrewFormula", @path].detect(&:directory?) @formula_files ||= if dir = [path/"Formula", path/"HomebrewFormula", path].detect(&:directory?)
dir.children.select { |p| p.extname == ".rb" } dir.children.select { |p| p.extname == ".rb" }
else else
[] []
@ -209,7 +209,7 @@ class Tap
# path to the pin record for this {Tap}. # path to the pin record for this {Tap}.
# @private # @private
def pinned_symlink_path def pinned_symlink_path
HOMEBREW_LIBRARY/"PinnedTaps/#{@name}" HOMEBREW_LIBRARY/"PinnedTaps/#{name}"
end end
# True if this {Tap} has been pinned. # True if this {Tap} has been pinned.
@ -222,7 +222,7 @@ class Tap
def pin def pin
raise TapUnavailableError, name unless installed? raise TapUnavailableError, name unless installed?
raise TapPinStatusError.new(name, true) if pinned? raise TapPinStatusError.new(name, true) if pinned?
pinned_symlink_path.make_relative_symlink(@path) pinned_symlink_path.make_relative_symlink(path)
@pinned = true @pinned = true
end end
@ -237,10 +237,10 @@ class Tap
def to_hash def to_hash
hash = { hash = {
"name" => @name, "name" => name,
"user" => @user, "user" => user,
"repo" => @repo, "repo" => repo,
"path" => @path.to_s, "path" => path.to_s,
"installed" => installed?, "installed" => installed?,
"official" => official?, "official" => official?,
"formula_names" => formula_names, "formula_names" => formula_names,