diff options
| author | Markus Reiter | 2016-09-24 13:52:43 +0200 |
|---|---|---|
| committer | Markus Reiter | 2016-09-24 16:00:58 +0200 |
| commit | b86c8efb79b3ed835d552c4d7416640ef10caf21 (patch) | |
| tree | 7e1edc8a8f339e4d2781f43576d40c9c79aebcdc /Library/Homebrew/cask/lib/hbc/source | |
| parent | 687f0fcf721c8e36f32570ed72d0988a6eaf986f (diff) | |
| download | brew-b86c8efb79b3ed835d552c4d7416640ef10caf21.tar.bz2 | |
Cask: Use nested classes and modules.
Diffstat (limited to 'Library/Homebrew/cask/lib/hbc/source')
| -rw-r--r-- | Library/Homebrew/cask/lib/hbc/source/gone.rb | 30 | ||||
| -rw-r--r-- | Library/Homebrew/cask/lib/hbc/source/path_base.rb | 108 | ||||
| -rw-r--r-- | Library/Homebrew/cask/lib/hbc/source/path_slash_optional.rb | 12 | ||||
| -rw-r--r-- | Library/Homebrew/cask/lib/hbc/source/path_slash_required.rb | 12 | ||||
| -rw-r--r-- | Library/Homebrew/cask/lib/hbc/source/tapped.rb | 60 | ||||
| -rw-r--r-- | Library/Homebrew/cask/lib/hbc/source/tapped_qualified.rb | 32 | ||||
| -rw-r--r-- | Library/Homebrew/cask/lib/hbc/source/untapped_qualified.rb | 14 | ||||
| -rw-r--r-- | Library/Homebrew/cask/lib/hbc/source/uri.rb | 48 |
8 files changed, 174 insertions, 142 deletions
diff --git a/Library/Homebrew/cask/lib/hbc/source/gone.rb b/Library/Homebrew/cask/lib/hbc/source/gone.rb index 2b9f2b5f2..1c744d0db 100644 --- a/Library/Homebrew/cask/lib/hbc/source/gone.rb +++ b/Library/Homebrew/cask/lib/hbc/source/gone.rb @@ -1,19 +1,23 @@ -class Hbc::Source::Gone - def self.me?(query) - Hbc::WithoutSource.new(query).installed? - end +module Hbc + module Source + class Gone + def self.me?(query) + WithoutSource.new(query).installed? + end - attr_reader :query + attr_reader :query - def initialize(query) - @query = query - end + def initialize(query) + @query = query + end - def load - Hbc::WithoutSource.new(query) - end + def load + WithoutSource.new(query) + end - def to_s - "" + def to_s + "" + end + end end end diff --git a/Library/Homebrew/cask/lib/hbc/source/path_base.rb b/Library/Homebrew/cask/lib/hbc/source/path_base.rb index bd85ab425..5725380ec 100644 --- a/Library/Homebrew/cask/lib/hbc/source/path_base.rb +++ b/Library/Homebrew/cask/lib/hbc/source/path_base.rb @@ -1,69 +1,73 @@ require "rubygems" -class Hbc::Source::PathBase - # derived classes must define method self.me? +module Hbc + module Source + class 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 + 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 + attr_reader :path - def initialize(path) - @path = Pathname(path).expand_path - end + 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 load + raise CaskError, "File '#{path}' does not exist" unless path.exist? + raise CaskError, "File '#{path}' is not readable" unless path.readable? + raise 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 + def to_s + # stringify to fully-resolved location + path.to_s + end - private + 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 load_cask + instance_eval(cask_contents, __FILE__, __LINE__) + rescue CaskError, StandardError, ScriptError => e + # bug: e.message.concat doesn't work with 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 + 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 - end - end - def cask(header_token, &block) - build_cask(Hbc::Cask, header_token, &block) - end + def cask(header_token, &block) + build_cask(Cask, header_token, &block) + end - def test_cask(header_token, &block) - build_cask(Hbc::TestCask, header_token, &block) - end + def test_cask(header_token, &block) + build_cask(TestCask, header_token, &block) + end - def build_cask(cask_class, header_token, &block) - if header_token.is_a?(Hash) - # Cask file is using old `cask :v1 => 'token'` syntax - header_token = header_token.values.first - end - raise Hbc::CaskTokenDoesNotMatchError.new(cask_token, header_token) unless cask_token == header_token - cask_class.new(cask_token, sourcefile_path: path, &block) - end + def build_cask(cask_class, header_token, &block) + if header_token.is_a?(Hash) + # Cask file is using old `cask :v1 => 'token'` syntax + header_token = header_token.values.first + end + raise 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}, "") + def cask_token + path.basename.to_s.sub(%r{\.rb}, "") + end + end 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 index fb34c481a..d96a41130 100644 --- a/Library/Homebrew/cask/lib/hbc/source/path_slash_optional.rb +++ b/Library/Homebrew/cask/lib/hbc/source/path_slash_optional.rb @@ -1,8 +1,12 @@ require "hbc/source/path_base" -class Hbc::Source::PathSlashOptional < Hbc::Source::PathBase - def self.me?(query) - path = path_for_query(query) - path.exist? +module Hbc + module Source + class PathSlashOptional < PathBase + def self.me?(query) + path = path_for_query(query) + path.exist? + end + end 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 index 0c533a8a5..2753b1710 100644 --- a/Library/Homebrew/cask/lib/hbc/source/path_slash_required.rb +++ b/Library/Homebrew/cask/lib/hbc/source/path_slash_required.rb @@ -1,8 +1,12 @@ 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? +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 diff --git a/Library/Homebrew/cask/lib/hbc/source/tapped.rb b/Library/Homebrew/cask/lib/hbc/source/tapped.rb index da9366840..6716e3a35 100644 --- a/Library/Homebrew/cask/lib/hbc/source/tapped.rb +++ b/Library/Homebrew/cask/lib/hbc/source/tapped.rb @@ -1,35 +1,39 @@ -class Hbc::Source::Tapped - def self.me?(query) - path_for_query(query).exist? - end +module Hbc + module Source + class 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 + 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 + attr_reader :token - def initialize(token) - @token = token - end + def initialize(token) + @token = token + end - def load - path = self.class.path_for_query(token) - Hbc::Source::PathSlashOptional.new(path).load - end + def load + path = self.class.path_for_query(token) + PathSlashOptional.new(path).load + end - def to_s - # stringify to fully-resolved location - self.class.path_for_query(token).expand_path.to_s + def to_s + # stringify to fully-resolved location + self.class.path_for_query(token).expand_path.to_s + end + end end end diff --git a/Library/Homebrew/cask/lib/hbc/source/tapped_qualified.rb b/Library/Homebrew/cask/lib/hbc/source/tapped_qualified.rb index 97a35defe..52191f279 100644 --- a/Library/Homebrew/cask/lib/hbc/source/tapped_qualified.rb +++ b/Library/Homebrew/cask/lib/hbc/source/tapped_qualified.rb @@ -1,22 +1,26 @@ require "hbc/source/tapped" -class Hbc::Source::TappedQualified < Hbc::Source::Tapped - def self.me?(query) - return if (tap = tap_for_query(query)).nil? +module Hbc + module Source + class TappedQualified < Tapped + def self.me?(query) + return if (tap = tap_for_query(query)).nil? - tap.installed? && path_for_query(query).exist? - end + tap.installed? && path_for_query(query).exist? + end - def self.tap_for_query(query) - qualified_token = Hbc::QualifiedToken.parse(query) - return if qualified_token.nil? + 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 + user, repo = qualified_token[0..1] + Tap.fetch(user, repo) + end - def self.path_for_query(query) - user, repo, token = Hbc::QualifiedToken.parse(query) - Tap.fetch(user, repo).cask_dir.join(token.sub(%r{(\.rb)?$}i, ".rb")) + def self.path_for_query(query) + user, repo, token = QualifiedToken.parse(query) + Tap.fetch(user, repo).cask_dir.join(token.sub(%r{(\.rb)?$}i, ".rb")) + end + end end end diff --git a/Library/Homebrew/cask/lib/hbc/source/untapped_qualified.rb b/Library/Homebrew/cask/lib/hbc/source/untapped_qualified.rb index 7188d34ce..6041605d8 100644 --- a/Library/Homebrew/cask/lib/hbc/source/untapped_qualified.rb +++ b/Library/Homebrew/cask/lib/hbc/source/untapped_qualified.rb @@ -1,10 +1,14 @@ require "hbc/source/tapped_qualified" -class Hbc::Source::UntappedQualified < Hbc::Source::TappedQualified - def self.me?(query) - return if (tap = tap_for_query(query)).nil? +module Hbc + module Source + class UntappedQualified < TappedQualified + def self.me?(query) + return if (tap = tap_for_query(query)).nil? - tap.install - tap.installed? && path_for_query(query).exist? + tap.install + tap.installed? && path_for_query(query).exist? + end + end end end diff --git a/Library/Homebrew/cask/lib/hbc/source/uri.rb b/Library/Homebrew/cask/lib/hbc/source/uri.rb index 99bc50688..09fab4bd0 100644 --- a/Library/Homebrew/cask/lib/hbc/source/uri.rb +++ b/Library/Homebrew/cask/lib/hbc/source/uri.rb @@ -1,28 +1,32 @@ -class Hbc::Source::URI - def self.me?(query) - !(query.to_s =~ URI.regexp).nil? - end +module Hbc + module Source + class URI + def self.me?(query) + !(query.to_s =~ ::URI.regexp).nil? + end - attr_reader :uri + attr_reader :uri - def initialize(uri) - @uri = uri - end + 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 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 + def to_s + uri.to_s + end + end end end |
