Adding support for local casks outside of the casks directory

This commit is contained in:
Shane Delmore 2013-07-16 15:12:16 -07:00
parent a983cd8200
commit 429eb80cbe
2 changed files with 46 additions and 6 deletions

View file

@ -41,6 +41,10 @@ class Cask
@default_tap = _tap
end
def self._file_source?(cask_title)
%r{(^file://|^\./|\.rb$)} =~ cask_title
end
def self.init
HOMEBREW_CACHE.mkpath unless HOMEBREW_CACHE.exist?
unless caskroom.exist?
@ -69,13 +73,43 @@ class Cask
end
end
def self.load(cask_title)
cask_path = path(cask_title)
unless cask_path.exist?
raise CaskUnavailableError, cask_title
def self._load_from_tap(cask_title)
if cask_title.include?('/')
cask_with_tap = cask_title
else
cask_with_tap = all_titles.grep(/#{cask_title}$/).first
end
require cask_path
const_get(cask_title.split('/').last.split('-').map(&:capitalize).join).new
if cask_with_tap
tap, cask = cask_with_tap.split('/')
source = tapspath.join(tap, 'Casks', "#{cask}.rb")
else
source = tapspath.join(default_tap, 'Casks', "#{cask_title}.rb")
end
raise CaskUnavailableError, cask_title unless source.exist?
_load_from_file(source)
end
def self._load_from_path(cask_title)
_load_from_file(Pathname.new(File::expand_path(cask_title)))
end
def self._load_from_file(source)
raise CaskUnavailableError, source unless source.exist?
require source
const_get(cask_class_name(source)).new
end
def self.load(cask_title)
if _file_source?(cask_title)
_load_from_path(cask_title)
else
_load_from_tap(cask_title)
end
end
def self.cask_class_name(pathname)
pathname.basename.to_s.sub(/\.rb/, '').split('-').map(&:capitalize).join
end
def self.title

View file

@ -7,6 +7,12 @@ describe "Cask" do
c.must_be_kind_of(Cask)
c.must_be_instance_of(Adium)
end
it "returns an instance of the cask from a specific file location" do
c = Cask.load("./Casks/dia.rb")
c.must_be_kind_of(Cask)
c.must_be_instance_of(Dia)
end
it "raises an error when attempting to load a cask that doesn't exist" do
lambda {