From 429eb80cbecc4e609a2017fe80c956d2a5dd0fb7 Mon Sep 17 00:00:00 2001 From: Shane Delmore Date: Tue, 16 Jul 2013 15:12:16 -0700 Subject: [PATCH] Adding support for local casks outside of the casks directory --- lib/cask.rb | 46 ++++++++++++++++++++++++++++++++++++++++------ test/cask_test.rb | 6 ++++++ 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/lib/cask.rb b/lib/cask.rb index 04bd93aac..bd6d1479b 100644 --- a/lib/cask.rb +++ b/lib/cask.rb @@ -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 diff --git a/test/cask_test.rb b/test/cask_test.rb index d51cea140..f4d56c6cf 100644 --- a/test/cask_test.rb +++ b/test/cask_test.rb @@ -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 {