Merge pull request #2325 from reitermarkus/better-cask-loading

Use a `Formulary`-like approach to load Casks.
This commit is contained in:
Markus Reiter 2017-03-16 22:58:21 +01:00 committed by GitHub
commit bfb5bf1d70
49 changed files with 244 additions and 387 deletions

View File

@ -7,7 +7,6 @@ require "hbc/auditor"
require "hbc/cache" require "hbc/cache"
require "hbc/cask" require "hbc/cask"
require "hbc/cask_loader" require "hbc/cask_loader"
require "hbc/without_source"
require "hbc/caskroom" require "hbc/caskroom"
require "hbc/checkable" require "hbc/checkable"
require "hbc/cli" require "hbc/cli"
@ -24,7 +23,6 @@ require "hbc/macos"
require "hbc/pkg" require "hbc/pkg"
require "hbc/qualified_token" require "hbc/qualified_token"
require "hbc/scopes" require "hbc/scopes"
require "hbc/source"
require "hbc/staged" require "hbc/staged"
require "hbc/system_command" require "hbc/system_command"
require "hbc/topological_hash" require "hbc/topological_hash"
@ -44,11 +42,4 @@ module Hbc
Cache.ensure_cache_exists Cache.ensure_cache_exists
Caskroom.ensure_caskroom_exists Caskroom.ensure_caskroom_exists
end end
def self.load(query)
odebug "Loading Cask definitions"
cask = Source.for_query(query).load
cask.dumpcask
cask
end
end end

View File

@ -15,7 +15,7 @@ module Hbc
walk = lambda do |acc, deps| walk = lambda do |acc, deps|
deps.each do |dep| deps.each do |dep|
next if acc.key?(dep) next if acc.key?(dep)
succs = deps_in.call Hbc.load(dep) succs = deps_in.call CaskLoader.load(dep)
acc[dep] = succs acc[dep] = succs
walk.call(acc, succs) walk.call(acc, succs)
end end

View File

@ -1,44 +1,167 @@
module Hbc module Hbc
class CaskLoader module CaskLoader
def self.load_from_file(path) class FromContentLoader
raise CaskError, "File '#{path}' does not exist" unless path.exist? def initialize(content)
raise CaskError, "File '#{path}' is not readable" unless path.readable? @content = content
raise CaskError, "File '#{path}' is not a plain file" unless path.file?
token = path.basename(".rb").to_s
content = IO.read(path).force_encoding("UTF-8")
new(token, content, path).load
end
def self.load_from_string(token, content)
new(token, content).load
end
def load
instance_eval(@content, __FILE__, __LINE__)
rescue CaskError, StandardError, ScriptError => e
e.message.concat(" while loading '#{@token}'")
e.message.concat(" from '#{@path}'") unless @path.nil?
raise e, e.message
end
private
def initialize(token, content, path = nil)
@token = token
@content = content
@path = path unless path.nil?
end
def cask(header_token, &block)
raise CaskTokenDoesNotMatchError.new(@token, header_token) unless @token == header_token
if @path.nil?
Cask.new(@token, &block)
else
Cask.new(@token, sourcefile_path: @path, &block)
end end
def load
instance_eval(@content.force_encoding("UTF-8"), __FILE__, __LINE__)
end
private
def cask(header_token, &block)
Cask.new(header_token, &block)
end
end
class FromPathLoader < FromContentLoader
def self.can_load?(ref)
path = Pathname.new(ref)
path.extname == ".rb" && path.expand_path.exist?
end
attr_reader :token, :path
def initialize(path)
path = Pathname.new(path).expand_path
@token = path.basename(".rb").to_s
@path = path
end
def load
raise CaskError, "'#{@path}' does not exist." unless @path.exist?
raise CaskError, "'#{@path}' is not readable." unless @path.readable?
raise CaskError, "'#{@path}' is not a file." unless @path.file?
@content = IO.read(@path)
super
end
private
def cask(header_token, &block)
if @token != header_token
raise CaskTokenDoesNotMatchError.new(@token, header_token)
end
Cask.new(header_token, sourcefile_path: @path, &block)
end
end
class FromURILoader < FromPathLoader
def self.can_load?(ref)
!(ref.to_s !~ ::URI.regexp)
end
def initialize(url)
@url = url
uri = URI(url)
super Hbc.cache/File.basename(uri.path)
end
def load
Hbc.cache.mkpath
FileUtils.rm_f @path
begin
ohai "Downloading #{@url}."
curl @url, "-o", @path
rescue ErrorDuringExecution
raise CaskUnavailableError, @url
end
super
end
end
class FromTapLoader < FromPathLoader
def self.can_load?(ref)
!(ref.to_s !~ HOMEBREW_TAP_CASK_REGEX)
end
def initialize(tapped_name)
user, repo, token = tapped_name.split("/", 3).map(&:downcase)
@tap = Tap.fetch(user, repo)
super @tap.cask_dir/"#{token}.rb"
end
def load
@tap.install unless @tap.installed?
super
end
end
class NullLoader < FromPathLoader
def self.can_load?(*)
true
end
def initialize(ref)
@token = File.basename(ref, ".rb")
super CaskLoader.default_path(@token)
end
def load
raise CaskUnavailableError, @token
end
end
def self.load_from_file(path)
FromPathLoader.new(path).load
end
def self.load_from_string(content)
FromContentLoader.new(content).load
end
def self.path(ref)
self.for(ref).path
end
def self.load(ref)
self.for(ref).load
end
def self.for(ref)
[
FromURILoader,
FromTapLoader,
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))
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)
end
possible_installed_cask = Cask.new(ref)
if possible_installed_cask.installed?
return FromPathLoader.new(possible_installed_cask.installed_caskfile)
end
NullLoader.new(ref)
end
def self.default_path(token)
Hbc.default_tap.cask_dir/"#{token.to_s.downcase}.rb"
end
def self.tap_paths(token)
Tap.map { |t| t.cask_dir/"#{token.to_s.downcase}.rb" }
.select(&:exist?)
end end
end end
end end

View File

@ -40,7 +40,7 @@ module Hbc
if cask_tokens.empty? if cask_tokens.empty?
Hbc.all Hbc.all
else else
cask_tokens.map { |token| Hbc.load(token) } cask_tokens.map { |token| CaskLoader.load(token) }
end end
end end

View File

@ -6,7 +6,7 @@ module Hbc
raise CaskUnspecifiedError if cask_tokens.empty? raise CaskUnspecifiedError if cask_tokens.empty?
# only respects the first argument # only respects the first argument
cask_token = cask_tokens.first.sub(/\.rb$/i, "") cask_token = cask_tokens.first.sub(/\.rb$/i, "")
cask_path = Hbc.path(cask_token) cask_path = CaskLoader.path(cask_token)
raise CaskUnavailableError, cask_token.to_s unless cask_path.exist? raise CaskUnavailableError, cask_token.to_s unless cask_path.exist?
puts File.open(cask_path, &:read) puts File.open(cask_path, &:read)
end end

View File

@ -5,7 +5,7 @@ module Hbc
cask_tokens = cask_tokens_from(args) cask_tokens = cask_tokens_from(args)
raise CaskUnspecifiedError if cask_tokens.empty? raise CaskUnspecifiedError if cask_tokens.empty?
cask_token = cask_tokens.first.sub(/\.rb$/i, "") cask_token = cask_tokens.first.sub(/\.rb$/i, "")
cask_path = Hbc.path(cask_token) cask_path = CaskLoader.path(cask_token)
odebug "Creating Cask #{cask_token}" odebug "Creating Cask #{cask_token}"
raise CaskAlreadyCreatedError, cask_token if cask_path.exist? raise CaskAlreadyCreatedError, cask_token if cask_path.exist?

View File

@ -6,7 +6,7 @@ module Hbc
raise CaskUnspecifiedError if cask_tokens.empty? raise CaskUnspecifiedError if cask_tokens.empty?
# only respects the first argument # only respects the first argument
cask_token = cask_tokens.first.sub(/\.rb$/i, "") cask_token = cask_tokens.first.sub(/\.rb$/i, "")
cask_path = Hbc.path(cask_token) cask_path = CaskLoader.path(cask_token)
odebug "Opening editor for Cask #{cask_token}" odebug "Opening editor for Cask #{cask_token}"
unless cask_path.exist? unless cask_path.exist?
raise CaskUnavailableError, %Q(#{cask_token}, run "brew cask create #{cask_token}" to create a new Cask) raise CaskUnavailableError, %Q(#{cask_token}, run "brew cask create #{cask_token}" to create a new Cask)

View File

@ -8,7 +8,7 @@ module Hbc
cask_tokens.each do |cask_token| cask_tokens.each do |cask_token|
ohai "Downloading external files for Cask #{cask_token}" ohai "Downloading external files for Cask #{cask_token}"
cask = Hbc.load(cask_token) cask = CaskLoader.load(cask_token)
downloaded_path = Download.new(cask, force: force).perform downloaded_path = Download.new(cask, force: force).perform
Verify.all(cask, downloaded_path) Verify.all(cask, downloaded_path)
ohai "Success! Downloaded to -> #{downloaded_path}" ohai "Success! Downloaded to -> #{downloaded_path}"

View File

@ -8,7 +8,7 @@ module Hbc
else else
cask_tokens.each do |cask_token| cask_tokens.each do |cask_token|
odebug "Opening homepage for Cask #{cask_token}" odebug "Opening homepage for Cask #{cask_token}"
cask = Hbc.load(cask_token) cask = CaskLoader.load(cask_token)
system "/usr/bin/open", "--", cask.homepage system "/usr/bin/open", "--", cask.homepage
end end
end end

View File

@ -6,7 +6,7 @@ module Hbc
raise CaskUnspecifiedError if cask_tokens.empty? raise CaskUnspecifiedError if cask_tokens.empty?
cask_tokens.each do |cask_token| cask_tokens.each do |cask_token|
odebug "Getting info for Cask #{cask_token}" odebug "Getting info for Cask #{cask_token}"
cask = Hbc.load(cask_token) cask = CaskLoader.load(cask_token)
info(cask) info(cask)
end end

View File

@ -18,7 +18,7 @@ module Hbc
count = 0 count = 0
cask_tokens.each do |cask_token| cask_tokens.each do |cask_token|
begin begin
cask = Hbc.load(cask_token) cask = CaskLoader.load(cask_token)
Installer.new(cask, Installer.new(cask,
force: force, force: force,
skip_cask_deps: skip_cask_deps, skip_cask_deps: skip_cask_deps,

View File

@ -24,7 +24,7 @@ module Hbc
count = 0 count = 0
cask_tokens.each do |cask_token| cask_tokens.each do |cask_token|
cask = Hbc.load(cask_token) cask = CaskLoader.load(cask_token)
if cask.appcast.nil? if cask.appcast.nil?
opoo "Cask '#{cask}' is missing an `appcast` stanza." opoo "Cask '#{cask}' is missing an `appcast` stanza."

View File

@ -84,7 +84,7 @@ module Hbc
def modified_casks def modified_casks
return @modified_casks if defined? @modified_casks return @modified_casks if defined? @modified_casks
@modified_casks = modified_cask_files.map { |f| Hbc.load(f) } @modified_casks = modified_cask_files.map { |f| CaskLoader.load(f) }
if @modified_casks.any? if @modified_casks.any?
num_modified = @modified_casks.size num_modified = @modified_casks.size
ohai "#{Formatter.pluralize(num_modified, "modified cask")}: " \ ohai "#{Formatter.pluralize(num_modified, "modified cask")}: " \

View File

@ -2,7 +2,7 @@ module Hbc
class CLI class CLI
class InternalCheckurl < InternalUseBase class InternalCheckurl < InternalUseBase
def self.run(*args) def self.run(*args)
casks_to_check = args.empty? ? Hbc.all : args.map { |arg| Hbc.load(arg) } casks_to_check = args.empty? ? Hbc.all : args.map { |arg| CaskLoader.load(arg) }
casks_to_check.each do |cask| casks_to_check.each do |cask|
odebug "Checking URL for Cask #{cask}" odebug "Checking URL for Cask #{cask}"
checker = UrlChecker.new(cask) checker = UrlChecker.new(cask)

View File

@ -16,7 +16,7 @@ module Hbc
count = 0 count = 0
cask_tokens.each do |cask_token| cask_tokens.each do |cask_token|
begin begin
cask = Hbc.load(cask_token) cask = CaskLoader.load(cask_token)
count += 1 count += 1
cask.dumpcask cask.dumpcask
rescue StandardError => e rescue StandardError => e

View File

@ -84,7 +84,7 @@ module Hbc
print "#{cask_token}\t" if table print "#{cask_token}\t" if table
begin begin
cask = Hbc.load(cask_token) cask = CaskLoader.load(cask_token)
rescue StandardError rescue StandardError
opoo "Cask '#{cask_token}' was not found" unless quiet opoo "Cask '#{cask_token}' was not found" unless quiet
puts "" puts ""

View File

@ -28,7 +28,7 @@ module Hbc
cask_tokens.each do |cask_token| cask_tokens.each do |cask_token|
odebug "Listing files for Cask #{cask_token}" odebug "Listing files for Cask #{cask_token}"
begin begin
cask = Hbc.load(cask_token) cask = CaskLoader.load(cask_token)
if cask.installed? if cask.installed?
if @options[:one] if @options[:one]

View File

@ -9,7 +9,7 @@ module Hbc
casks_to_check = if cask_tokens.empty? casks_to_check = if cask_tokens.empty?
Hbc.installed Hbc.installed
else else
cask_tokens.map { |token| Hbc.load(token) } cask_tokens.map { |token| CaskLoader.load(token) }
end end
casks_to_check.each do |cask| casks_to_check.each do |cask|

View File

@ -5,7 +5,7 @@ module Hbc
count = 0 count = 0
cask_tokens.each do |cask_token| cask_tokens.each do |cask_token|
begin begin
cask = Hbc.load(cask_token) cask = CaskLoader.load(cask_token)
installer = Installer.new(cask, installer = Installer.new(cask,
force: force, force: force,

View File

@ -39,7 +39,7 @@ module Hbc
elsif cask_tokens.any? { |file| File.exist?(file) } elsif cask_tokens.any? { |file| File.exist?(file) }
cask_tokens cask_tokens
else else
cask_tokens.map { |token| Hbc.path(token) } cask_tokens.map { |token| CaskLoader.path(token) }
end end
end end

View File

@ -8,7 +8,7 @@ module Hbc
cask_tokens.each do |cask_token| cask_tokens.each do |cask_token|
odebug "Uninstalling Cask #{cask_token}" odebug "Uninstalling Cask #{cask_token}"
cask = Hbc.load(cask_token) cask = CaskLoader.load(cask_token)
raise CaskNotInstalledError, cask unless cask.installed? || force raise CaskNotInstalledError, cask unless cask.installed? || force

View File

@ -6,7 +6,7 @@ module Hbc
raise CaskUnspecifiedError if cask_tokens.empty? raise CaskUnspecifiedError if cask_tokens.empty?
cask_tokens.each do |cask_token| cask_tokens.each do |cask_token|
odebug "Zapping Cask #{cask_token}" odebug "Zapping Cask #{cask_token}"
cask = Hbc.load(cask_token) cask = CaskLoader.load(cask_token)
Installer.new(cask).zap Installer.new(cask).zap
end end
end end

View File

@ -229,7 +229,7 @@ module Hbc
deps = CaskDependencies.new(@cask) deps = CaskDependencies.new(@cask)
deps.sorted.each do |dep_token| deps.sorted.each do |dep_token|
puts "#{dep_token} ..." puts "#{dep_token} ..."
dep = Hbc.load(dep_token) dep = CaskLoader.load(dep_token)
if dep.installed? if dep.installed?
puts "already installed" puts "already installed"
else else

View File

@ -109,34 +109,6 @@ module Hbc
@default_tap ||= Tap.fetch("caskroom", "homebrew-cask") @default_tap ||= Tap.fetch("caskroom", "homebrew-cask")
end end
def path(query)
query_path = Pathname.new(query)
return query_path if query_path.absolute?
return query_path if query_path.exist? && query_path.extname == ".rb"
query_without_extension = query.sub(/\.rb$/i, "")
token_with_tap = if query =~ %r{\A[^/]+/[^/]+/[^/]+\Z}
query_without_extension
else
all_tokens.detect do |tap_and_token|
tap_and_token.split("/")[2] == query_without_extension
end
end
if token_with_tap
user, repo, token = token_with_tap.split("/")
tap = Tap.fetch(user, repo)
else
token = query_without_extension
tap = Hbc.default_tap
end
return query_path if tap.cask_dir.nil?
tap.cask_dir.join("#{token}.rb")
end
def tcc_db def tcc_db
@tcc_db ||= Pathname.new("/Library/Application Support/com.apple.TCC/TCC.db") @tcc_db ||= Pathname.new("/Library/Application Support/com.apple.TCC/TCC.db")
end end

View File

@ -11,22 +11,22 @@ module Hbc
end end
def all_tapped_cask_dirs def all_tapped_cask_dirs
Tap.map(&:cask_dir).compact Tap.map(&:cask_dir).select(&:directory?)
end end
def all_tokens def all_tokens
Tap.map do |t| Tap.flat_map do |t|
t.cask_files.map do |p| t.cask_files.map do |p|
"#{t.name}/#{File.basename(p, ".rb")}" "#{t.name}/#{File.basename(p, ".rb")}"
end end
end.flatten end
end end
def installed def installed
# Hbc.load has some DWIM which is slow. Optimize here # CaskLoader.load has some DWIM which is slow. Optimize here
# by spoon-feeding Hbc.load fully-qualified paths. # by spoon-feeding CaskLoader.load fully-qualified paths.
# TODO: speed up Hbc::Source::Tapped (main perf drag is calling Hbc.all_tokens repeatedly) # TODO: speed up Hbc::Source::Tapped (main perf drag is calling Hbc.all_tokens repeatedly)
# TODO: ability to specify expected source when calling Hbc.load (minor perf benefit) # TODO: ability to specify expected source when calling CaskLoader.load (minor perf benefit)
Pathname.glob(caskroom.join("*")) Pathname.glob(caskroom.join("*"))
.map do |caskroom_path| .map do |caskroom_path|
token = caskroom_path.basename.to_s token = caskroom_path.basename.to_s
@ -36,9 +36,9 @@ module Hbc
end end
if path_to_cask if path_to_cask
Hbc.load(path_to_cask.join("#{token}.rb")) CaskLoader.load(path_to_cask.join("#{token}.rb"))
else else
Hbc.load(token) CaskLoader.load(token)
end end
end end
end end

View File

@ -1,37 +0,0 @@
require "hbc/source/gone"
require "hbc/source/path_slash_required"
require "hbc/source/path_slash_optional"
require "hbc/source/tapped_qualified"
require "hbc/source/untapped_qualified"
require "hbc/source/tapped"
require "hbc/source/uri"
module Hbc
module Source
def self.sources
[
URI,
PathSlashRequired,
TappedQualified,
UntappedQualified,
Tapped,
PathSlashOptional,
Gone,
]
end
def self.for_query(query)
odebug "Translating '#{query}' into a valid Cask source"
raise CaskUnavailableError, query if query.to_s =~ /^\s*$/
source = sources.find do |s|
odebug "Testing source class #{s}"
s.me?(query)
end
raise CaskUnavailableError, query unless source
odebug "Success! Using source class #{source}"
resolved_cask_source = source.new(query)
odebug "Resolved Cask URI or file source to '#{resolved_cask_source}'"
resolved_cask_source
end
end
end

View File

@ -1,23 +0,0 @@
module Hbc
module Source
class Gone
def self.me?(query)
WithoutSource.new(query).installed?
end
attr_reader :query
def initialize(query)
@query = query
end
def load
WithoutSource.new(query)
end
def to_s
""
end
end
end
end

View File

@ -1,29 +0,0 @@
require "rubygems"
require "hbc/cask_loader"
module Hbc
module Source
class PathBase
# derived classes must define method self.me?
def self.path_for_query(query)
Pathname.new(query).sub(/(\.rb)?$/, ".rb")
end
attr_reader :path
def initialize(path)
@path = Pathname.new(path).expand_path
end
def load
CaskLoader.load_from_file(@path)
end
def to_s
# stringify to fully-resolved location
@path.to_s
end
end
end
end

View File

@ -1,12 +0,0 @@
require "hbc/source/path_base"
module Hbc
module Source
class PathSlashOptional < PathBase
def self.me?(query)
path = path_for_query(query)
path.exist?
end
end
end
end

View File

@ -1,12 +0,0 @@
require "hbc/source/path_base"
module Hbc
module Source
class PathSlashRequired < PathBase
def self.me?(query)
path = path_for_query(query)
path.to_s.include?("/") && path.exist?
end
end
end
end

View File

@ -1,24 +0,0 @@
module Hbc
module Source
class Tapped
def self.me?(query)
Hbc.path(query).exist?
end
attr_reader :token
def initialize(token)
@token = token
end
def load
PathSlashOptional.new(Hbc.path(token)).load
end
def to_s
# stringify to fully-resolved location
Hbc.path(token).expand_path.to_s
end
end
end
end

View File

@ -1,21 +0,0 @@
require "hbc/source/tapped"
module Hbc
module Source
class TappedQualified < Tapped
def self.me?(query)
return if (tap = tap_for_query(query)).nil?
tap.installed? && Hbc.path(query).exist?
end
def self.tap_for_query(query)
qualified_token = QualifiedToken.parse(query)
return if qualified_token.nil?
user, repo = qualified_token[0..1]
Tap.fetch(user, repo)
end
end
end
end

View File

@ -1,14 +0,0 @@
require "hbc/source/tapped_qualified"
module Hbc
module Source
class UntappedQualified < TappedQualified
def self.me?(query)
return if (tap = tap_for_query(query)).nil?
tap.install
tap.installed? && Hbc.path(query).exist?
end
end
end
end

View File

@ -1,32 +0,0 @@
module Hbc
module Source
class URI
def self.me?(query)
!(query.to_s =~ ::URI.regexp).nil?
end
attr_reader :uri
def initialize(uri)
@uri = uri
end
def load
Hbc.cache.mkpath
path = Hbc.cache.join(File.basename(uri))
ohai "Downloading #{uri}"
odebug "Download target -> #{path}"
begin
curl(uri, "-o", path.to_s)
rescue ErrorDuringExecution
raise CaskUnavailableError, uri
end
PathSlashOptional.new(path).load
end
def to_s
uri.to_s
end
end
end
end

View File

@ -1,17 +0,0 @@
module Hbc
class WithoutSource < Cask
# Override from `Hbc::DSL` because we don't have a cask source file to work
# with, so we don't know the cask's `version`.
def staged_path
(caskroom_path.children - [metadata_master_container_path]).first
end
def to_s
"#{token} (!)"
end
def installed?
caskroom_path.exist?
end
end
end

View File

@ -19,6 +19,11 @@ class String
end end
end end
def cask
$LOAD_PATH.unshift("#{HOMEBREW_LIBRARY_PATH}/cask/lib")
require "hbc"
end
module Homebrew module Homebrew
module_function module_function

View File

@ -12,7 +12,11 @@ module CaskLoaderCompatibilityLayer
end end
module Hbc module Hbc
class CaskLoader module CaskLoader
prepend CaskLoaderCompatibilityLayer class FromContentLoader; end
class FromPathLoader < FromContentLoader
prepend CaskLoaderCompatibilityLayer
end
end end
end end

View File

@ -285,7 +285,7 @@ class Tap
# path to the directory of all {Formula} files for this {Tap}. # path to the directory of all {Formula} files for this {Tap}.
def formula_dir def formula_dir
@formula_dir ||= potential_formula_dirs.detect(&:directory?) @formula_dir ||= potential_formula_dirs.detect(&:directory?) || path/"Formula"
end end
def potential_formula_dirs def potential_formula_dirs
@ -294,12 +294,12 @@ class Tap
# path to the directory of all {Cask} files for this {Tap}. # path to the directory of all {Cask} files for this {Tap}.
def cask_dir def cask_dir
@cask_dir ||= [path/"Casks"].detect(&:directory?) @cask_dir ||= path/"Casks"
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 formula_dir @formula_files ||= if formula_dir.directory?
formula_dir.children.select(&method(:formula_file?)) formula_dir.children.select(&method(:formula_file?))
else else
[] []
@ -308,7 +308,7 @@ class Tap
# an array of all {Cask} files of this {Tap}. # an array of all {Cask} files of this {Tap}.
def cask_files def cask_files
@cask_files ||= if cask_dir @cask_files ||= if cask_dir.directory?
cask_dir.children.select(&method(:cask_file?)) cask_dir.children.select(&method(:cask_file?))
else else
[] []

View File

@ -79,7 +79,7 @@ describe Hbc::Audit, :cask do
end end
describe "#run!" do describe "#run!" do
let(:cask) { Hbc.load(cask_token) } let(:cask) { Hbc::CaskLoader.load(cask_token) }
subject { audit.run! } subject { audit.run! }
describe "required stanzas" do describe "required stanzas" do
@ -320,7 +320,7 @@ describe Hbc::Audit, :cask do
describe "audit of downloads" do describe "audit of downloads" do
let(:cask_token) { "with-binary" } let(:cask_token) { "with-binary" }
let(:cask) { Hbc.load(cask_token) } let(:cask) { Hbc::CaskLoader.load(cask_token) }
let(:download) { instance_double(Hbc::Download) } let(:download) { instance_double(Hbc::Download) }
let(:verify) { class_double(Hbc::Verify).as_stubbed_const } let(:verify) { class_double(Hbc::Verify).as_stubbed_const }
let(:error_msg) { "Download Failed" } let(:error_msg) { "Download Failed" }

View File

@ -27,20 +27,20 @@ describe Hbc::Cask, :cask do
let(:relative_tap_path) { tap_path.relative_path_from(file_dirname) } let(:relative_tap_path) { tap_path.relative_path_from(file_dirname) }
it "returns an instance of the Cask for the given token" do it "returns an instance of the Cask for the given token" do
c = Hbc.load("local-caffeine") c = Hbc::CaskLoader.load("local-caffeine")
expect(c).to be_kind_of(Hbc::Cask) expect(c).to be_kind_of(Hbc::Cask)
expect(c.token).to eq("local-caffeine") expect(c.token).to eq("local-caffeine")
end end
it "returns an instance of the Cask from a specific file location" do it "returns an instance of the Cask from a specific file location" do
c = Hbc.load("#{tap_path}/Casks/local-caffeine.rb") c = Hbc::CaskLoader.load("#{tap_path}/Casks/local-caffeine.rb")
expect(c).to be_kind_of(Hbc::Cask) expect(c).to be_kind_of(Hbc::Cask)
expect(c.token).to eq("local-caffeine") expect(c.token).to eq("local-caffeine")
end end
it "returns an instance of the Cask from a url" do it "returns an instance of the Cask from a url" do
c = shutup do c = shutup do
Hbc.load("file://#{tap_path}/Casks/local-caffeine.rb") Hbc::CaskLoader.load("file://#{tap_path}/Casks/local-caffeine.rb")
end end
expect(c).to be_kind_of(Hbc::Cask) expect(c).to be_kind_of(Hbc::Cask)
expect(c.token).to eq("local-caffeine") expect(c.token).to eq("local-caffeine")
@ -50,25 +50,25 @@ describe Hbc::Cask, :cask do
expect { expect {
url = "file://#{tap_path}/Casks/notacask.rb" url = "file://#{tap_path}/Casks/notacask.rb"
shutup do shutup do
Hbc.load(url) Hbc::CaskLoader.load(url)
end end
}.to raise_error(Hbc::CaskUnavailableError) }.to raise_error(Hbc::CaskUnavailableError)
end end
it "returns an instance of the Cask from a relative file location" do it "returns an instance of the Cask from a relative file location" do
c = Hbc.load(relative_tap_path/"Casks/local-caffeine.rb") c = Hbc::CaskLoader.load(relative_tap_path/"Casks/local-caffeine.rb")
expect(c).to be_kind_of(Hbc::Cask) expect(c).to be_kind_of(Hbc::Cask)
expect(c.token).to eq("local-caffeine") expect(c.token).to eq("local-caffeine")
end end
it "uses exact match when loading by token" do it "uses exact match when loading by token" do
expect(Hbc.load("test-opera").token).to eq("test-opera") expect(Hbc::CaskLoader.load("test-opera").token).to eq("test-opera")
expect(Hbc.load("test-opera-mail").token).to eq("test-opera-mail") expect(Hbc::CaskLoader.load("test-opera-mail").token).to eq("test-opera-mail")
end end
it "raises an error when attempting to load a Cask that doesn't exist" do it "raises an error when attempting to load a Cask that doesn't exist" do
expect { expect {
Hbc.load("notacask") Hbc::CaskLoader.load("notacask")
}.to raise_error(Hbc::CaskUnavailableError) }.to raise_error(Hbc::CaskUnavailableError)
end end
end end
@ -84,7 +84,7 @@ describe Hbc::Cask, :cask do
describe "metadata" do describe "metadata" do
it "proposes a versioned metadata directory name for each instance" do it "proposes a versioned metadata directory name for each instance" do
cask_token = "local-caffeine" cask_token = "local-caffeine"
c = Hbc.load(cask_token) c = Hbc::CaskLoader.load(cask_token)
metadata_path = Hbc.caskroom.join(cask_token, ".metadata", c.version) metadata_path = Hbc.caskroom.join(cask_token, ".metadata", c.version)
expect(c.metadata_versioned_container_path.to_s).to eq(metadata_path.to_s) expect(c.metadata_versioned_container_path.to_s).to eq(metadata_path.to_s)
end end
@ -92,13 +92,13 @@ describe Hbc::Cask, :cask do
describe "outdated" do describe "outdated" do
it "ignores the Casks that have auto_updates true (without --greedy)" do it "ignores the Casks that have auto_updates true (without --greedy)" do
c = Hbc.load("auto-updates") c = Hbc::CaskLoader.load("auto-updates")
expect(c).not_to be_outdated expect(c).not_to be_outdated
expect(c.outdated_versions).to be_empty expect(c.outdated_versions).to be_empty
end end
it "ignores the Casks that have version :latest (without --greedy)" do it "ignores the Casks that have version :latest (without --greedy)" do
c = Hbc.load("version-latest-string") c = Hbc::CaskLoader.load("version-latest-string")
expect(c).not_to be_outdated expect(c).not_to be_outdated
expect(c.outdated_versions).to be_empty expect(c.outdated_versions).to be_empty
end end

View File

@ -13,7 +13,7 @@ describe Hbc::CLI::Audit, :cask do
it "audits specified Casks if tokens are given" do it "audits specified Casks if tokens are given" do
cask_token = "nice-app" cask_token = "nice-app"
expect(Hbc).to receive(:load).with(cask_token).and_return(cask) expect(Hbc::CaskLoader).to receive(:load).with(cask_token).and_return(cask)
expect(auditor).to receive(:audit).with(cask, audit_download: false, check_token_conflicts: false) expect(auditor).to receive(:audit).with(cask, audit_download: false, check_token_conflicts: false)
@ -23,14 +23,14 @@ describe Hbc::CLI::Audit, :cask do
describe "rules for downloading a Cask" do describe "rules for downloading a Cask" do
it "does not download the Cask per default" do it "does not download the Cask per default" do
allow(Hbc).to receive(:load).and_return(cask) allow(Hbc::CaskLoader).to receive(:load).and_return(cask)
expect(auditor).to receive(:audit).with(cask, audit_download: false, check_token_conflicts: false) expect(auditor).to receive(:audit).with(cask, audit_download: false, check_token_conflicts: false)
run_audit(["casktoken"], auditor) run_audit(["casktoken"], auditor)
end end
it "download a Cask if --download flag is set" do it "download a Cask if --download flag is set" do
allow(Hbc).to receive(:load).and_return(cask) allow(Hbc::CaskLoader).to receive(:load).and_return(cask)
expect(auditor).to receive(:audit).with(cask, audit_download: true, check_token_conflicts: false) expect(auditor).to receive(:audit).with(cask, audit_download: true, check_token_conflicts: false)
run_audit(["casktoken", "--download"], auditor) run_audit(["casktoken", "--download"], auditor)
@ -39,14 +39,14 @@ describe Hbc::CLI::Audit, :cask do
describe "rules for checking token conflicts" do describe "rules for checking token conflicts" do
it "does not check for token conflicts per default" do it "does not check for token conflicts per default" do
allow(Hbc).to receive(:load).and_return(cask) allow(Hbc::CaskLoader).to receive(:load).and_return(cask)
expect(auditor).to receive(:audit).with(cask, audit_download: false, check_token_conflicts: false) expect(auditor).to receive(:audit).with(cask, audit_download: false, check_token_conflicts: false)
run_audit(["casktoken"], auditor) run_audit(["casktoken"], auditor)
end end
it "checks for token conflicts if --token-conflicts flag is set" do it "checks for token conflicts if --token-conflicts flag is set" do
allow(Hbc).to receive(:load).and_return(cask) allow(Hbc::CaskLoader).to receive(:load).and_return(cask)
expect(auditor).to receive(:audit).with(cask, audit_download: false, check_token_conflicts: true) expect(auditor).to receive(:audit).with(cask, audit_download: false, check_token_conflicts: true)
run_audit(["casktoken", "--token-conflicts"], auditor) run_audit(["casktoken", "--token-conflicts"], auditor)

View File

@ -24,7 +24,7 @@ describe Hbc::CLI::Create, :cask do
after(:each) do after(:each) do
%w[new-cask additional-cask another-cask yet-another-cask local-caff].each do |cask| %w[new-cask additional-cask another-cask yet-another-cask local-caff].each do |cask|
path = Hbc.path(cask) path = Hbc::CaskLoader.path(cask)
path.delete if path.exist? path.delete if path.exist?
end end
end end
@ -32,13 +32,13 @@ describe Hbc::CLI::Create, :cask do
it "opens the editor for the specified Cask" do it "opens the editor for the specified Cask" do
Hbc::CLI::Create.run("new-cask") Hbc::CLI::Create.run("new-cask")
expect(Hbc::CLI::Create.editor_commands).to eq [ expect(Hbc::CLI::Create.editor_commands).to eq [
[Hbc.path("new-cask")], [Hbc::CaskLoader.path("new-cask")],
] ]
end end
it "drops a template down for the specified Cask" do it "drops a template down for the specified Cask" do
Hbc::CLI::Create.run("new-cask") Hbc::CLI::Create.run("new-cask")
template = File.read(Hbc.path("new-cask")) template = File.read(Hbc::CaskLoader.path("new-cask"))
expect(template).to eq <<-EOS.undent expect(template).to eq <<-EOS.undent
cask 'new-cask' do cask 'new-cask' do
version '' version ''
@ -56,14 +56,14 @@ describe Hbc::CLI::Create, :cask do
it "throws away additional Cask arguments and uses the first" do it "throws away additional Cask arguments and uses the first" do
Hbc::CLI::Create.run("additional-cask", "another-cask") Hbc::CLI::Create.run("additional-cask", "another-cask")
expect(Hbc::CLI::Create.editor_commands).to eq [ expect(Hbc::CLI::Create.editor_commands).to eq [
[Hbc.path("additional-cask")], [Hbc::CaskLoader.path("additional-cask")],
] ]
end end
it "throws away stray options" do it "throws away stray options" do
Hbc::CLI::Create.run("--notavalidoption", "yet-another-cask") Hbc::CLI::Create.run("--notavalidoption", "yet-another-cask")
expect(Hbc::CLI::Create.editor_commands).to eq [ expect(Hbc::CLI::Create.editor_commands).to eq [
[Hbc.path("yet-another-cask")], [Hbc::CaskLoader.path("yet-another-cask")],
] ]
end end
@ -76,7 +76,7 @@ describe Hbc::CLI::Create, :cask do
it "allows creating Casks that are substrings of existing Casks" do it "allows creating Casks that are substrings of existing Casks" do
Hbc::CLI::Create.run("local-caff") Hbc::CLI::Create.run("local-caff")
expect(Hbc::CLI::Create.editor_commands).to eq [ expect(Hbc::CLI::Create.editor_commands).to eq [
[Hbc.path("local-caff")], [Hbc::CaskLoader.path("local-caff")],
] ]
end end

View File

@ -25,14 +25,14 @@ describe Hbc::CLI::Edit, :cask do
it "opens the editor for the specified Cask" do it "opens the editor for the specified Cask" do
Hbc::CLI::Edit.run("local-caffeine") Hbc::CLI::Edit.run("local-caffeine")
expect(Hbc::CLI::Edit.editor_commands).to eq [ expect(Hbc::CLI::Edit.editor_commands).to eq [
[Hbc.path("local-caffeine")], [Hbc::CaskLoader.path("local-caffeine")],
] ]
end end
it "throws away additional arguments and uses the first" do it "throws away additional arguments and uses the first" do
Hbc::CLI::Edit.run("local-caffeine", "local-transmission") Hbc::CLI::Edit.run("local-caffeine", "local-transmission")
expect(Hbc::CLI::Edit.editor_commands).to eq [ expect(Hbc::CLI::Edit.editor_commands).to eq [
[Hbc.path("local-caffeine")], [Hbc::CaskLoader.path("local-caffeine")],
] ]
end end

View File

@ -1,6 +1,6 @@
describe Hbc::CLI::List, :cask do describe Hbc::CLI::List, :cask do
it "lists the installed Casks in a pretty fashion" do it "lists the installed Casks in a pretty fashion" do
casks = %w[local-caffeine local-transmission].map { |c| Hbc.load(c) } casks = %w[local-caffeine local-transmission].map { |c| Hbc::CaskLoader.load(c) }
casks.each do |c| casks.each do |c|
InstallHelper.install_with_caskfile(c) InstallHelper.install_with_caskfile(c)
@ -24,7 +24,7 @@ describe Hbc::CLI::List, :cask do
} }
before(:each) do before(:each) do
casks.map(&Hbc.method(:load)).each(&InstallHelper.method(:install_with_caskfile)) casks.map(&Hbc::CaskLoader.method(:load)).each(&InstallHelper.method(:install_with_caskfile))
end end
it "of all installed Casks" do it "of all installed Casks" do
@ -40,23 +40,6 @@ describe Hbc::CLI::List, :cask do
end end
end end
describe "when Casks have been renamed" do
let(:caskroom_path) { Hbc.caskroom.join("ive-been-renamed") }
let(:staged_path) { caskroom_path.join("latest") }
before do
staged_path.mkpath
end
it "lists installed Casks without backing ruby files (due to renames or otherwise)" do
expect {
Hbc::CLI::List.run
}.to output(<<-EOS.undent).to_stdout
ive-been-renamed (!)
EOS
end
end
describe "given a set of installed Casks" do describe "given a set of installed Casks" do
let(:caffeine) { Hbc::CaskLoader.load_from_file(TEST_FIXTURE_DIR/"cask/Casks/local-caffeine.rb") } let(:caffeine) { Hbc::CaskLoader.load_from_file(TEST_FIXTURE_DIR/"cask/Casks/local-caffeine.rb") }
let(:transmission) { Hbc::CaskLoader.load_from_file(TEST_FIXTURE_DIR/"cask/Casks/local-transmission.rb") } let(:transmission) { Hbc::CaskLoader.load_from_file(TEST_FIXTURE_DIR/"cask/Casks/local-transmission.rb") }

View File

@ -130,7 +130,7 @@ describe Hbc::CLI::Style, :cask do
end end
it "tries to find paths for all tokens" do it "tries to find paths for all tokens" do
expect(Hbc).to receive(:path).twice expect(Hbc::CaskLoader).to receive(:path).twice
subject subject
end end
end end

View File

@ -32,7 +32,7 @@ describe Hbc::CLI::Zap, :cask do
# The above tests that implicitly. # The above tests that implicitly.
# #
# it "dispatches both uninstall and zap stanzas" do # it "dispatches both uninstall and zap stanzas" do
# with_zap = Hbc.load('with-zap') # with_zap = Hbc::CaskLoader.load('with-zap')
# #
# shutup do # shutup do
# Hbc::Installer.new(with_zap).install # Hbc::Installer.new(with_zap).install

View File

@ -17,7 +17,7 @@ describe "Satisfy Dependencies and Requirements", :cask do
context do context do
let(:cask) { Hbc::CaskLoader.load_from_file(TEST_FIXTURE_DIR/"cask/Casks/with-depends-on-cask.rb") } let(:cask) { Hbc::CaskLoader.load_from_file(TEST_FIXTURE_DIR/"cask/Casks/with-depends-on-cask.rb") }
let(:dependency) { Hbc.load(cask.depends_on.cask.first) } let(:dependency) { Hbc::CaskLoader.load(cask.depends_on.cask.first) }
it "installs the dependency of a Cask and the Cask itself" do it "installs the dependency of a Cask and the Cask itself" do
expect(subject).not_to raise_error expect(subject).not_to raise_error

View File

@ -291,7 +291,7 @@ describe "download strategies", :cask do
# does not work yet, because (for unknown reasons), the tar command # does not work yet, because (for unknown reasons), the tar command
# returns an error code when running under the test suite # returns an error code when running under the test suite
# it 'creates a tarball matching the expected checksum' do # it 'creates a tarball matching the expected checksum' do
# cask = Hbc.load('svn-download-check-cask') # cask = Hbc::CaskLoader.load('svn-download-check-cask')
# downloader = Hbc::SubversionDownloadStrategy.new(cask) # downloader = Hbc::SubversionDownloadStrategy.new(cask)
# # special mocking required for tar to have something to work with # # special mocking required for tar to have something to work with
# def downloader.fetch_repo(target, url, revision = nil, ignore_externals=false) # def downloader.fetch_repo(target, url, revision = nil, ignore_externals=false)

View File

@ -1,15 +1,15 @@
describe Hbc::Scopes, :cask do describe Hbc::Scopes, :cask do
describe "installed" do describe "installed" do
it "returns a list installed Casks by loading Casks for all the dirs that exist in the caskroom" do it "returns a list installed Casks by loading Casks for all the dirs that exist in the caskroom" do
allow(Hbc).to receive(:load) { |token| "loaded-#{token}" } allow(Hbc::CaskLoader).to receive(:load) { |token| "loaded-#{token}" }
Hbc.caskroom.join("cask-bar").mkpath Hbc.caskroom.join("cask-bar").mkpath
Hbc.caskroom.join("cask-foo").mkpath Hbc.caskroom.join("cask-foo").mkpath
installed_casks = Hbc.installed installed_casks = Hbc.installed
expect(Hbc).to have_received(:load).with("cask-bar") expect(Hbc::CaskLoader).to have_received(:load).with("cask-bar")
expect(Hbc).to have_received(:load).with("cask-foo") expect(Hbc::CaskLoader).to have_received(:load).with("cask-foo")
expect(installed_casks).to eq( expect(installed_casks).to eq(
%w[ %w[
loaded-cask-bar loaded-cask-bar
@ -18,11 +18,11 @@ describe Hbc::Scopes, :cask do
) )
end end
it "optimizes performance by resolving to a fully qualified path before calling Hbc.load" do it "optimizes performance by resolving to a fully qualified path before calling Hbc::CaskLoader.load" do
fake_tapped_cask_dir = Pathname.new(Dir.mktmpdir).join("Casks") fake_tapped_cask_dir = Pathname.new(Dir.mktmpdir).join("Casks")
absolute_path_to_cask = fake_tapped_cask_dir.join("some-cask.rb") absolute_path_to_cask = fake_tapped_cask_dir.join("some-cask.rb")
allow(Hbc).to receive(:load) allow(Hbc::CaskLoader).to receive(:load)
allow(Hbc).to receive(:all_tapped_cask_dirs) { [fake_tapped_cask_dir] } allow(Hbc).to receive(:all_tapped_cask_dirs) { [fake_tapped_cask_dir] }
Hbc.caskroom.join("some-cask").mkdir Hbc.caskroom.join("some-cask").mkdir
@ -31,7 +31,7 @@ describe Hbc::Scopes, :cask do
Hbc.installed Hbc.installed
expect(Hbc).to have_received(:load).with(absolute_path_to_cask) expect(Hbc::CaskLoader).to have_received(:load).with(absolute_path_to_cask)
end end
end end
end end