Refactoring. (#23821)

* Refactoring.

* Refactor pre-/postflight block artifacts.

* Use Homebrew’s `Tap` class.

* Overwrite TAP_DIRECTORY constant.
This commit is contained in:
Markus Reiter 2016-08-17 16:33:35 +02:00 committed by GitHub
parent 8478e2e884
commit 38c7e64226
12 changed files with 73 additions and 134 deletions

View File

@ -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

View File

@ -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)

View File

@ -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?

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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")

View File

@ -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")