diff options
| author | AnastasiaSulyagina | 2016-08-18 22:11:42 +0300 |
|---|---|---|
| committer | AnastasiaSulyagina | 2016-08-19 14:50:14 +0300 |
| commit | e81f4ab7deeb40308f240be5ea00091fc8786d7a (patch) | |
| tree | b5418f9149de71c0f05f90cb2b39ab47f46e27b4 /Library/Homebrew/cask/lib/hbc/source | |
| parent | 5c7c9de669025bbe4cad9829be39c5cf3b31ad25 (diff) | |
| download | brew-e81f4ab7deeb40308f240be5ea00091fc8786d7a.tar.bz2 | |
init
Diffstat (limited to 'Library/Homebrew/cask/lib/hbc/source')
8 files changed, 186 insertions, 0 deletions
diff --git a/Library/Homebrew/cask/lib/hbc/source/gone.rb b/Library/Homebrew/cask/lib/hbc/source/gone.rb new file mode 100644 index 000000000..2b9f2b5f2 --- /dev/null +++ b/Library/Homebrew/cask/lib/hbc/source/gone.rb @@ -0,0 +1,19 @@ +class Hbc::Source::Gone + def self.me?(query) + Hbc::WithoutSource.new(query).installed? + end + + attr_reader :query + + def initialize(query) + @query = query + end + + def load + Hbc::WithoutSource.new(query) + end + + def to_s + "" + end +end diff --git a/Library/Homebrew/cask/lib/hbc/source/path_base.rb b/Library/Homebrew/cask/lib/hbc/source/path_base.rb new file mode 100644 index 000000000..bbb413fd3 --- /dev/null +++ b/Library/Homebrew/cask/lib/hbc/source/path_base.rb @@ -0,0 +1,65 @@ +require "rubygems" + +class Hbc::Source::PathBase + # derived classes must define method self.me? + + def self.path_for_query(query) + query_string = query.to_s + Pathname.new(query_string.end_with?(".rb") ? query_string : query_string + ".rb") + end + + attr_reader :path + + def initialize(path) + @path = Pathname(path).expand_path + end + + def load + raise Hbc::CaskError, "File '#{path}' does not exist" unless path.exist? + raise Hbc::CaskError, "File '#{path}' is not readable" unless path.readable? + raise Hbc::CaskError, "File '#{path}' is not a plain file" unless path.file? + load_cask + end + + def to_s + # stringify to fully-resolved location + path.to_s + end + + private + + def load_cask + instance_eval(cask_contents, __FILE__, __LINE__) + rescue Hbc::CaskError, StandardError, ScriptError => e + # bug: e.message.concat doesn't work with Hbc::CaskError exceptions + raise e, e.message.concat(" while loading '#{path}'") + end + + def cask_contents + File.open(path, "rb") do |handle| + contents = handle.read + if defined?(Encoding) + contents.force_encoding("UTF-8") + else + contents + end + end + end + + def cask(header_token, &block) + build_cask(Hbc::Cask, header_token, &block) + end + + def test_cask(header_token, &block) + build_cask(Hbc::TestCask, header_token, &block) + end + + def build_cask(cask_class, header_token, &block) + raise Hbc::CaskTokenDoesNotMatchError.new(cask_token, header_token) unless cask_token == header_token + cask_class.new(cask_token, sourcefile_path: path, &block) + end + + def cask_token + path.basename.to_s.sub(%r{\.rb}, "") + end +end diff --git a/Library/Homebrew/cask/lib/hbc/source/path_slash_optional.rb b/Library/Homebrew/cask/lib/hbc/source/path_slash_optional.rb new file mode 100644 index 000000000..fb34c481a --- /dev/null +++ b/Library/Homebrew/cask/lib/hbc/source/path_slash_optional.rb @@ -0,0 +1,8 @@ +require "hbc/source/path_base" + +class Hbc::Source::PathSlashOptional < Hbc::Source::PathBase + def self.me?(query) + path = path_for_query(query) + path.exist? + end +end diff --git a/Library/Homebrew/cask/lib/hbc/source/path_slash_required.rb b/Library/Homebrew/cask/lib/hbc/source/path_slash_required.rb new file mode 100644 index 000000000..0c533a8a5 --- /dev/null +++ b/Library/Homebrew/cask/lib/hbc/source/path_slash_required.rb @@ -0,0 +1,8 @@ +require "hbc/source/path_base" + +class Hbc::Source::PathSlashRequired < Hbc::Source::PathBase + def self.me?(query) + path = path_for_query(query) + path.to_s.include?("/") && path.exist? + end +end diff --git a/Library/Homebrew/cask/lib/hbc/source/tapped.rb b/Library/Homebrew/cask/lib/hbc/source/tapped.rb new file mode 100644 index 000000000..da9366840 --- /dev/null +++ b/Library/Homebrew/cask/lib/hbc/source/tapped.rb @@ -0,0 +1,35 @@ +class Hbc::Source::Tapped + def self.me?(query) + path_for_query(query).exist? + end + + def self.path_for_query(query) + # Repeating Hbc.all_tokens is very slow for operations such as + # brew cask list, but memoizing the value might cause breakage + # elsewhere, given that installation and tap status is permitted + # to change during the course of an invocation. + 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("/") + Tap.fetch(user, repo).cask_dir.join("#{token}.rb") + else + Hbc.default_tap.cask_dir.join(query.sub(%r{(\.rb)?$}i, ".rb")) + end + end + + attr_reader :token + + def initialize(token) + @token = token + end + + def load + path = self.class.path_for_query(token) + Hbc::Source::PathSlashOptional.new(path).load + end + + def to_s + # stringify to fully-resolved location + self.class.path_for_query(token).expand_path.to_s + end +end diff --git a/Library/Homebrew/cask/lib/hbc/source/tapped_qualified.rb b/Library/Homebrew/cask/lib/hbc/source/tapped_qualified.rb new file mode 100644 index 000000000..48f8501e5 --- /dev/null +++ b/Library/Homebrew/cask/lib/hbc/source/tapped_qualified.rb @@ -0,0 +1,12 @@ +require "hbc/source/tapped" + +class Hbc::Source::TappedQualified < Hbc::Source::Tapped + def self.me?(query) + !Hbc::QualifiedToken.parse(query).nil? && path_for_query(query).exist? + end + + def self.path_for_query(query) + user, repo, token = Hbc::QualifiedToken.parse(query) + Tap.new(user, repo).cask_dir.join(token.sub(%r{(\.rb)?$}i, ".rb")) + end +end diff --git a/Library/Homebrew/cask/lib/hbc/source/untapped_qualified.rb b/Library/Homebrew/cask/lib/hbc/source/untapped_qualified.rb new file mode 100644 index 000000000..361919bb3 --- /dev/null +++ b/Library/Homebrew/cask/lib/hbc/source/untapped_qualified.rb @@ -0,0 +1,11 @@ +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) + + tap = Tap.fetch(user, repo) + tap.install unless tap.installed? + tap.cask_dir.join(token.sub(%r{(\.rb)?$}i, ".rb")) + end +end diff --git a/Library/Homebrew/cask/lib/hbc/source/uri.rb b/Library/Homebrew/cask/lib/hbc/source/uri.rb new file mode 100644 index 000000000..99bc50688 --- /dev/null +++ b/Library/Homebrew/cask/lib/hbc/source/uri.rb @@ -0,0 +1,28 @@ +class Hbc::Source::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 Hbc::CaskUnavailableError, uri + end + Hbc::Source::PathSlashOptional.new(path).load + end + + def to_s + uri.to_s + end +end |
