Detect Tap in CaskLoader.

This commit is contained in:
Markus Reiter 2017-07-29 16:27:54 +02:00
parent 627b1dae70
commit 6d6ce7cf0a
7 changed files with 55 additions and 26 deletions

View File

@ -7,9 +7,16 @@ module Hbc
include Metadata
attr_reader :token, :sourcefile_path
def initialize(token, sourcefile_path: nil, &block)
def tap
return super if block_given? # Object#tap
@tap
end
def initialize(token, sourcefile_path: nil, tap: nil, &block)
@token = token
@sourcefile_path = sourcefile_path
@tap = tap
@dsl = DSL.new(@token)
return unless block_given?
@dsl.instance_eval(&block)

View File

@ -13,8 +13,8 @@ module Hbc
private
def cask(header_token, &block)
Cask.new(header_token, &block)
def cask(header_token, **options, &block)
Cask.new(header_token, **options, &block)
end
end
@ -45,12 +45,12 @@ module Hbc
private
def cask(header_token, &block)
def cask(header_token, **options, &block)
if token != header_token
raise CaskTokenMismatchError.new(token, header_token)
end
Cask.new(header_token, sourcefile_path: path, &block)
super(header_token, **options, sourcefile_path: path, &block)
end
end
@ -80,18 +80,33 @@ module Hbc
end
end
class FromTapLoader < FromPathLoader
class FromTapPathLoader < FromPathLoader
def self.can_load?(ref)
ref.to_s.match?(HOMEBREW_TAP_CASK_REGEX)
ref.to_s.match?(HOMEBREW_TAP_PATH_REGEX) && super
end
attr_reader :tap
def initialize(tap_path)
@tap = Tap.from_path(tap_path)
super tap_path
end
private
def cask(*args, &block)
super(*args, tap: tap, &block)
end
end
class FromTapLoader < FromTapPathLoader
def self.can_load?(ref)
ref.to_s.match?(HOMEBREW_TAP_CASK_REGEX)
end
def initialize(tapped_name)
user, repo, token = tapped_name.split("/", 3)
@tap = Tap.fetch(user, repo)
super @tap.cask_dir/"#{token}.rb"
super Tap.fetch(user, repo).cask_dir/"#{token}.rb"
end
def load
@ -136,19 +151,26 @@ module Hbc
[
FromURILoader,
FromTapLoader,
FromTapPathLoader,
FromPathLoader,
].each do |loader_class|
return loader_class.new(ref) if loader_class.can_load?(ref)
end
if FromPathLoader.can_load?(default_path(ref))
return FromPathLoader.new(default_path(ref))
if FromTapPathLoader.can_load?(default_path(ref))
return FromTapPathLoader.new(default_path(ref))
end
possible_tap_casks = tap_paths(ref)
if possible_tap_casks.count == 1
possible_tap_cask = possible_tap_casks.first
return FromPathLoader.new(possible_tap_cask)
case (possible_tap_casks = tap_paths(ref)).count
when 1
return FromTapPathLoader.new(possible_tap_casks.first)
when 2..Float::INFINITY
loaders = possible_tap_casks.map(&FromTapPathLoader.method(:new))
raise CaskError, <<-EOS.undent
Cask #{ref} exists in multiple taps:
#{loaders.map { |loader| " #{loader.tap}/#{loader.token}" }.join("\n")}
EOS
end
possible_installed_cask = Cask.new(ref)

View File

@ -1,13 +1,13 @@
module CaskLoaderCompatibilityLayer
private
def cask(header_token, &block)
def cask(header_token, **options, &block)
if header_token.is_a?(Hash) && header_token.key?(:v1)
odeprecated %q("cask :v1 => 'token'"), %q("cask 'token'")
header_token = header_token[:v1]
end
super(header_token, &block)
super(header_token, **options, &block)
end
end

View File

@ -181,8 +181,8 @@ class TapFormulaAmbiguityError < RuntimeError
@name = name
@paths = paths
@formulae = paths.map do |path|
path.to_s =~ HOMEBREW_TAP_PATH_REGEX
"#{Tap.fetch(Regexp.last_match(1), Regexp.last_match(2))}/#{path.basename(".rb")}"
match = path.to_s.match(HOMEBREW_TAP_PATH_REGEX)
"#{Tap.fetch(match[:user], match[:repo])}/#{path.basename(".rb")}"
end
super <<-EOS.undent

View File

@ -177,8 +177,8 @@ class Formula
@tap = if path == Formulary.core_path(name)
CoreTap.instance
elsif path.to_s =~ HOMEBREW_TAP_PATH_REGEX
Tap.fetch(Regexp.last_match(1), Regexp.last_match(2))
elsif match = path.to_s.match(HOMEBREW_TAP_PATH_REGEX)
Tap.fetch(match[:user], match[:repo])
end
@full_name = full_name_with_optional_tap(name)

View File

@ -42,9 +42,9 @@ class Tap
end
def self.from_path(path)
path.to_s =~ HOMEBREW_TAP_PATH_REGEX
raise "Invalid tap path '#{path}'" unless Regexp.last_match(1)
fetch(Regexp.last_match(1), Regexp.last_match(2))
match = path.to_s.match(HOMEBREW_TAP_PATH_REGEX)
raise "Invalid tap path '#{path}'" unless match
fetch(match[:user], match[:repo])
rescue
# No need to error as a nil tap is sufficient to show failure.
nil

View File

@ -3,7 +3,7 @@ HOMEBREW_TAP_FORMULA_REGEX = %r{^([\w-]+)/([\w-]+)/([\w+-.@]+)$}
# match taps' casks, e.g. someuser/sometap/somecask
HOMEBREW_TAP_CASK_REGEX = %r{^([\w-]+)/([\w-]+)/([a-z0-9\-]+)$}
# match taps' directory paths, e.g. HOMEBREW_LIBRARY/Taps/someuser/sometap
HOMEBREW_TAP_DIR_REGEX = %r{#{Regexp.escape(HOMEBREW_LIBRARY)}/Taps/([\w-]+)/([\w-]+)}
HOMEBREW_TAP_DIR_REGEX = %r{#{Regexp.escape(HOMEBREW_LIBRARY)}/Taps/(?<user>[\w-]+)/(?<repo>[\w-]+)}
# match taps' formula paths, e.g. HOMEBREW_LIBRARY/Taps/someuser/sometap/someformula
HOMEBREW_TAP_PATH_REGEX = Regexp.new(HOMEBREW_TAP_DIR_REGEX.source + %r{/(.*)}.source)
# match the default and the versions brew-cask tap e.g. Caskroom/cask or Caskroom/versions