From 38c7e642263877d99d46ae4d1a9b9210488ec232 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Wed, 17 Aug 2016 16:33:35 +0200 Subject: [PATCH] Refactoring. (#23821) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Refactoring. * Refactor pre-/postflight block artifacts. * Use Homebrew’s `Tap` class. * Overwrite TAP_DIRECTORY constant. --- lib/hbc/cli/doctor.rb | 21 ++++----------- lib/hbc/cli/info.rb | 19 ++------------ lib/hbc/cli/style.rb | 2 +- lib/hbc/locations.rb | 20 +++------------ lib/hbc/qualified_token.rb | 36 +++++++++----------------- lib/hbc/scopes.rb | 12 +++------ lib/hbc/source/tapped.rb | 4 +-- lib/hbc/source/tapped_qualified.rb | 4 +-- lib/hbc/source/untapped_qualified.rb | 14 +++------- lib/hbc/version.rb | 2 +- spec/spec_helper.rb | 38 ++++++++++++++++------------ test/test_helper.rb | 35 +++++++++++++------------ 12 files changed, 73 insertions(+), 134 deletions(-) diff --git a/lib/hbc/cli/doctor.rb b/lib/hbc/cli/doctor.rb index 91037cc1837..d2feb1e06be 100644 --- a/lib/hbc/cli/doctor.rb +++ b/lib/hbc/cli/doctor.rb @@ -14,7 +14,7 @@ class Hbc::CLI::Doctor < Hbc::CLI::Base ohai "Homebrew-Cask Install Location:", render_install_location ohai "Homebrew-Cask Staging Location:", render_staging_location(Hbc.caskroom) ohai "Homebrew-Cask Cached Downloads:", render_cached_downloads - ohai "Homebrew-Cask Default Tap Path:", render_tap_paths(fq_default_tap) + ohai "Homebrew-Cask Default Tap Path:", render_tap_paths(Hbc.default_tap.path) ohai "Homebrew-Cask Alternate Cask Taps:", render_tap_paths(alt_taps) ohai "Homebrew-Cask Default Tap Cask Count:", render_with_none_as_error(default_cask_count) ohai "Contents of $LOAD_PATH:", render_load_path($LOAD_PATH) @@ -32,28 +32,17 @@ class Hbc::CLI::Doctor < Hbc::CLI::Base ohai "Running As Privileged User:", render_with_none_as_error(privileged_uid) end - def self.fq_default_tap - return @fq_default_tap if @fq_default_tap - @fq_default_tap = homebrew_repository.join "Library", "Taps", Hbc.default_tap - rescue StandardError - @fq_default_tap = notfound_string - end - def self.alt_taps - alt_taps = Pathname.glob(homebrew_repository - .join("Library", "Taps", "*", "*", "Casks")) - .map(&:dirname) - [fq_default_tap] - nil if alt_taps.empty? - rescue StandardError - notfound_string + Tap.select { |t| t.cask_dir.directory? && t != Hbc.default_tap } + .map(&:path) end def self.default_cask_count default_cask_count = notfound_string begin - default_cask_count = homebrew_repository.join(fq_default_tap, "Casks").children.count(&:file?) + default_cask_count = Hbc.default_tap.cask_dir.children.count(&:file?) rescue StandardError - default_cask_count = "0 #{error_string "Error reading #{fq_default_tap}"}" + default_cask_count = "0 #{error_string "Error reading #{Hbc.default_tap.path}"}" end default_cask_count end diff --git a/lib/hbc/cli/info.rb b/lib/hbc/cli/info.rb index 196f05fad01..dda4057056f 100644 --- a/lib/hbc/cli/info.rb +++ b/lib/hbc/cli/info.rb @@ -49,23 +49,8 @@ class Hbc::CLI::Info < Hbc::CLI::Base end def self.github_info(cask) - cask_token = cask.token - cask_token = Hbc.all_tokens.detect { |t| t.split("/").last == cask_token } unless cask_token.include?("/") - return nil unless cask_token.respond_to?(:length) && !cask_token.empty? - path_elements = cask_token.split "/" - if path_elements.count == 2 - # eg caskroom-cask/google-chrome. - # Not certain this form is needed, but it was supported in the past. - token = path_elements[1] - dash_elements = path_elements[0].split("-") - repo = dash_elements.pop - dash_elements.pop if dash_elements.count > 1 && dash_elements[-1] + "-" == repo_prefix - user = dash_elements.join("-") - else - user, repo, token = path_elements - end - repo.sub!(%r{^homebrew-}i, "") - "https://github.com/#{user}/homebrew-#{repo}/blob/master/Casks/#{token}.rb" + user, repo, token = Hbc::QualifiedToken.parse(Hbc.all_tokens.detect { |t| t.split("/").last == cask.token }) + "#{Tap.fetch(user, repo).default_remote}/blob/master/Casks/#{token}.rb" end def self.artifact_info(cask) diff --git a/lib/hbc/cli/style.rb b/lib/hbc/cli/style.rb index 082f024f7d4..ac7cbfb44b7 100644 --- a/lib/hbc/cli/style.rb +++ b/lib/hbc/cli/style.rb @@ -60,7 +60,7 @@ class Hbc::CLI::Style < Hbc::CLI::Base end def rubocop_config - "#{Hbc.default_tappath}/Casks/.rubocop.yml" + Hbc.default_tap.cask_dir.join(".rubocop.yml") end def fix? diff --git a/lib/hbc/locations.rb b/lib/hbc/locations.rb index 5796047504c..e4d88f31878 100644 --- a/lib/hbc/locations.rb +++ b/lib/hbc/locations.rb @@ -126,11 +126,7 @@ module Hbc::Locations attr_writer :default_tap def default_tap - @default_tap ||= "caskroom/homebrew-cask" - end - - def default_tappath - @default_tappath ||= homebrew_tapspath.join(default_tap) + @default_tap ||= Tap.fetch("caskroom/homebrew-cask") end def path(query) @@ -145,11 +141,9 @@ module Hbc::Locations if token_with_tap user, repo, token = token_with_tap.split("/") - # FIXME/TODO: handle old-style 1-slash form: phinze-cask/token - repo = "homebrew-" + repo unless repo =~ %r{^homebrew-} - homebrew_tapspath.join(user, repo, "Casks", "#{token}.rb") + Tap.fetch(user, repo).cask_dir.join("#{token}.rb") else - homebrew_tapspath.join(default_tap, "Casks", "#{query}.rb") + default_tap.cask_dir.join("#{query}.rb") end end @@ -198,13 +192,5 @@ module Hbc::Locations def homebrew_repository=(path) @homebrew_repository = path ? Pathname.new(path) : path end - - def homebrew_tapspath - @homebrew_tapspath ||= homebrew_repository.join(*%w[Library Taps]) - end - - def homebrew_tapspath=(path) - @homebrew_tapspath = path ? Pathname.new(path) : path - end end end diff --git a/lib/hbc/qualified_token.rb b/lib/hbc/qualified_token.rb index 0f7442505da..635e1cb3d6c 100644 --- a/lib/hbc/qualified_token.rb +++ b/lib/hbc/qualified_token.rb @@ -1,33 +1,21 @@ module Hbc::QualifiedToken - def self.repo_prefix - "homebrew-" - end + REPO_PREFIX = "homebrew-".freeze - def self.user_regexp - # per https://github.com/Homebrew/homebrew/blob/4c7bc9ec3bca729c898ee347b6135ba692ee0274/Library/Homebrew/cmd/tap.rb#L121 - %r{[a-z_\-]+} - end + # per https://github.com/Homebrew/homebrew/blob/4c7bc9ec3bca729c898ee347b6135ba692ee0274/Library/Homebrew/cmd/tap.rb#L121 + USER_REGEX = %r{[a-z_\-]+} - def self.repo_regexp - # per https://github.com/Homebrew/homebrew/blob/4c7bc9ec3bca729c898ee347b6135ba692ee0274/Library/Homebrew/cmd/tap.rb#L121 - %r{(?:#{repo_prefix})?\w+} - end + # per https://github.com/Homebrew/homebrew/blob/4c7bc9ec3bca729c898ee347b6135ba692ee0274/Library/Homebrew/cmd/tap.rb#L121 + REPO_REGEX = %r{(?:#{REPO_PREFIX})?\w+} - def self.token_regexp - # per https://github.com/caskroom/homebrew-cask/blob/master/CONTRIBUTING.md#generating-a-token-for-the-cask - %r{[a-z0-9\-]+} - end + # per https://github.com/caskroom/homebrew-cask/blob/master/CONTRIBUTING.md#generating-a-token-for-the-cask + TOKEN_REGEX = %r{[a-z0-9\-]+} - def self.tap_regexp - %r{#{user_regexp}[/\-]#{repo_regexp}} - end + TAP_REGEX = %r{#{USER_REGEX}[/\-]#{REPO_REGEX}} - def self.qualified_token_regexp - @qualified_token_regexp ||= %r{#{tap_regexp}/#{token_regexp}} - end + QUALIFIED_TOKEN_REGEX ||= %r{#{TAP_REGEX}/#{TOKEN_REGEX}} def self.parse(arg) - return nil unless arg.is_a?(String) && arg.downcase =~ %r{^#{qualified_token_regexp}$} + return nil unless arg.is_a?(String) && arg.downcase =~ %r{^#{QUALIFIED_TOKEN_REGEX}$} path_elements = arg.downcase.split("/") if path_elements.count == 2 # eg phinze-cask/google-chrome. @@ -35,14 +23,14 @@ module Hbc::QualifiedToken token = path_elements[1] dash_elements = path_elements[0].split("-") repo = dash_elements.pop - dash_elements.pop if dash_elements.count > 1 && dash_elements[-1] + "-" == repo_prefix + dash_elements.pop if dash_elements.count > 1 && dash_elements[-1] + "-" == REPO_PREFIX user = dash_elements.join("-") else # eg caskroom/cask/google-chrome # per https://github.com/Homebrew/homebrew/wiki/brew-tap user, repo, token = path_elements end - repo.sub!(%r{^#{repo_prefix}}, "") + repo.sub!(%r{^#{REPO_PREFIX}}, "") odebug "[user, repo, token] might be [#{user}, #{repo}, #{token}]" [user, repo, token] end diff --git a/lib/hbc/scopes.rb b/lib/hbc/scopes.rb index 55b328f457d..3fbb59d26d0 100644 --- a/lib/hbc/scopes.rb +++ b/lib/hbc/scopes.rb @@ -10,15 +10,9 @@ module Hbc::Scopes end def all_tapped_cask_dirs - return @all_tapped_cask_dirs unless @all_tapped_cask_dirs.nil? - fq_default_tap = Hbc.homebrew_tapspath.join(default_tap, "Casks") - @all_tapped_cask_dirs = Dir.glob(Hbc.homebrew_tapspath.join("*", "*", "Casks")).map { |d| Pathname.new(d) } - # optimization: place the default Tap first - if @all_tapped_cask_dirs.include? fq_default_tap - @all_tapped_cask_dirs -= [fq_default_tap] - @all_tapped_cask_dirs.unshift fq_default_tap - end - @all_tapped_cask_dirs + @all_tapped_cask_dirs ||= Tap.names.map(&Tap.method(:fetch)).map(&:cask_dir) + .unshift(default_tap.cask_dir) # optimization: place the default Tap first + .uniq end def reset_all_tapped_cask_dirs diff --git a/lib/hbc/source/tapped.rb b/lib/hbc/source/tapped.rb index b597ebc60a2..da9366840cd 100644 --- a/lib/hbc/source/tapped.rb +++ b/lib/hbc/source/tapped.rb @@ -11,9 +11,9 @@ class Hbc::Source::Tapped token_with_tap = Hbc.all_tokens.find { |t| t.split("/").last == query.sub(%r{\.rb$}i, "") } if token_with_tap user, repo, token = token_with_tap.split("/") - Hbc.homebrew_tapspath.join(user, repo, "Casks", "#{token}.rb") + Tap.fetch(user, repo).cask_dir.join("#{token}.rb") else - Hbc.homebrew_tapspath.join(Hbc.default_tap, "Casks", "#{query.sub(%r{\.rb$}i, '')}.rb") + Hbc.default_tap.cask_dir.join(query.sub(%r{(\.rb)?$}i, ".rb")) end end diff --git a/lib/hbc/source/tapped_qualified.rb b/lib/hbc/source/tapped_qualified.rb index d140d8a82f7..48f8501e543 100644 --- a/lib/hbc/source/tapped_qualified.rb +++ b/lib/hbc/source/tapped_qualified.rb @@ -7,8 +7,6 @@ class Hbc::Source::TappedQualified < Hbc::Source::Tapped def self.path_for_query(query) user, repo, token = Hbc::QualifiedToken.parse(query) - token.sub!(%r{\.rb$}i, "") - tap = "#{user}/homebrew-#{repo}" - Hbc.homebrew_tapspath.join(tap, "Casks", "#{token}.rb") + Tap.new(user, repo).cask_dir.join(token.sub(%r{(\.rb)?$}i, ".rb")) end end diff --git a/lib/hbc/source/untapped_qualified.rb b/lib/hbc/source/untapped_qualified.rb index ab5d03e7e9e..361919bb383 100644 --- a/lib/hbc/source/untapped_qualified.rb +++ b/lib/hbc/source/untapped_qualified.rb @@ -3,15 +3,9 @@ require "hbc/source/tapped_qualified" class Hbc::Source::UntappedQualified < Hbc::Source::TappedQualified def self.path_for_query(query) user, repo, token = Hbc::QualifiedToken.parse(query) - token.sub!(%r{\.rb$}i, "") - tap = "#{user}/homebrew-#{repo}" - unless Hbc.homebrew_tapspath.join(tap).exist? - ohai "Adding new tap '#{tap}'" - result = Hbc::SystemCommand.run!(Hbc.homebrew_executable, - args: ["tap", "#{user}/#{repo}"]) - puts result.stdout - $stderr.puts result.stderr - end - Hbc.homebrew_tapspath.join(tap, "Casks", "#{token}.rb") + + tap = Tap.fetch(user, repo) + tap.install unless tap.installed? + tap.cask_dir.join(token.sub(%r{(\.rb)?$}i, ".rb")) end end diff --git a/lib/hbc/version.rb b/lib/hbc/version.rb index aa41daf0071..471fd1999df 100644 --- a/lib/hbc/version.rb +++ b/lib/hbc/version.rb @@ -3,7 +3,7 @@ HBC_VERSION = "0.60.0".freeze module Hbc def self.full_version @full_version ||= begin - revision, commit = Dir.chdir(Hbc.default_tappath) do + revision, commit = Dir.chdir(Hbc.default_tap.path) do [`git rev-parse --short=4 --verify -q HEAD 2>/dev/null`.chomp, `git show -s --format="%cr" HEAD 2>/dev/null`.chomp] end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 0965b8ec8d5..b1ffbbbc0db 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -10,27 +10,26 @@ end # just in case raise "brew-cask: Ruby 2.0 or greater is required." if RUBY_VERSION.to_i < 2 -project_root = Pathname(File.expand_path("../..", __FILE__)) +project_root = Pathname.new(File.expand_path("../..", __FILE__)) -Dir["#{project_root}/spec/support/*.rb"].each(&method(:require)) +# add Homebrew to load path +$LOAD_PATH.unshift(File.expand_path("#{ENV['HOMEBREW_REPOSITORY']}/Library/Homebrew")) + +require "global" +require "extend/pathname" + +# add Homebrew-Cask to load path +$LOAD_PATH.push(project_root.join("lib").to_s) # force some environment variables ENV["HOMEBREW_NO_EMOJI"] = "1" ENV["HOMEBREW_CASK_OPTS"] = nil +Dir["#{project_root}/spec/support/*.rb"].each(&method(:require)) + # from Homebrew. Provides expects method. require "mocha/api" -# add homebrew to load path -homebrew_repo = `brew --repository`.chomp -$LOAD_PATH.unshift(File.expand_path("#{homebrew_repo}/Library/Homebrew")) - -require "global" -require "extend/pathname" - -# add homebrew-cask lib to load path -$LOAD_PATH.push(project_root.join("lib").to_s) - require "hbc" class Hbc::TestCask < Hbc::Cask; end @@ -43,13 +42,20 @@ end # override Homebrew locations Hbc.homebrew_prefix = Pathname.new(TEST_TMPDIR).join("prefix") Hbc.homebrew_repository = Hbc.homebrew_prefix -Hbc.homebrew_tapspath = nil Hbc.binarydir = Hbc.homebrew_prefix.join("binarydir", "bin") Hbc.appdir = Pathname.new(TEST_TMPDIR).join("appdir") -# Look for Casks in testcasks by default. It is elsewhere required that -# the string "test" appear in the directory name. -Hbc.default_tap = project_root.join("spec", "support") +# Override Tap::TAP_DIRECTORY to use our test Tap directory. +class Tap + send(:remove_const, :TAP_DIRECTORY) + TAP_DIRECTORY = Hbc.homebrew_repository.join("Library", "Taps") +end + +Hbc.default_tap = Tap.fetch("caskroom", "speccasks") +Hbc.default_tap.path.dirname.mkpath + +# also jack in some test Casks +FileUtils.ln_s project_root.join("spec", "support"), Tap::TAP_DIRECTORY.join("caskroom", "homebrew-speccasks") # create cache directory Hbc.homebrew_cache = Pathname.new(TEST_TMPDIR).join("cache") diff --git a/test/test_helper.rb b/test/test_helper.rb index 7f011f1effe..58b72201bbd 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -10,16 +10,16 @@ end # just in case raise "brew-cask: Ruby 2.0 or greater is required." if RUBY_VERSION.to_i < 2 +project_root = Pathname.new(File.expand_path("../..", __FILE__)) + # add Homebrew to load path $LOAD_PATH.unshift(File.expand_path("#{ENV['HOMEBREW_REPOSITORY']}/Library/Homebrew")) require "global" require "extend/pathname" -# add homebrew-cask lib to load path -brew_cask_path = Pathname.new(File.expand_path(__FILE__ + "/../../")) -lib_path = brew_cask_path.join("lib") -$LOAD_PATH.push(lib_path) +# add Homebrew-Cask to load path +$LOAD_PATH.push(project_root.join("lib").to_s) # force some environment variables ENV["HOMEBREW_NO_EMOJI"] = "1" @@ -68,11 +68,20 @@ require "hbc" # override Homebrew locations Hbc.homebrew_prefix = Pathname.new(TEST_TMPDIR).join("prefix") Hbc.homebrew_repository = Hbc.homebrew_prefix -Hbc.homebrew_tapspath = nil -# Look for Casks in testcasks by default. It is elsewhere required that -# the string "test" appear in the directory name. -Hbc.default_tap = "caskroom/homebrew-testcasks" +# Override Tap::TAP_DIRECTORY to use our test Tap directory. +class Tap + send(:remove_const, :TAP_DIRECTORY) + TAP_DIRECTORY = Hbc.homebrew_prefix.join("Library", "Taps") +end + +Hbc.default_tap = Tap.fetch("caskroom", "testcasks") + +# also jack in some test Casks +FileUtils.ln_s project_root.join("test", "support"), Tap::TAP_DIRECTORY.join("caskroom").tap(&:mkpath).join("homebrew-testcasks") + +# pretend that the caskroom/cask Tap is installed +FileUtils.ln_s project_root, Tap::TAP_DIRECTORY.join("caskroom").tap(&:mkpath).join("homebrew-cask") # create cache directory Hbc.homebrew_cache = Pathname.new(TEST_TMPDIR).join("cache") @@ -163,16 +172,9 @@ require "support/never_sudo_system_command" require "tmpdir" require "tempfile" -# pretend like we installed the homebrew-cask tap -project_root = Pathname.new(File.expand_path("#{File.dirname(__FILE__)}/../")) -taps_dest = Hbc.homebrew_prefix.join(*%w[Library Taps caskroom]) - # create directories -FileUtils.mkdir_p taps_dest FileUtils.mkdir_p Hbc.homebrew_prefix.join("bin") -FileUtils.ln_s project_root, taps_dest.join("homebrew-cask") - # Common superclass for test Casks for when we need to filter them out class Hbc::TestCask < Hbc::Cask; end @@ -182,6 +184,3 @@ FileUtils.ln_s "/usr/local/bin/unar", Hbc.homebrew_prefix.join("bin/unar") FileUtils.ln_s "/usr/local/bin/unlzma", Hbc.homebrew_prefix.join("bin/unlzma") FileUtils.ln_s "/usr/local/bin/unxz", Hbc.homebrew_prefix.join("bin/unxz") FileUtils.ln_s "/usr/local/bin/lsar", Hbc.homebrew_prefix.join("bin/lsar") - -# also jack in some test Casks -FileUtils.ln_s project_root.join("test", "support"), taps_dest.join("homebrew-testcasks")