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/cask.rb | |
| parent | 687f0fcf721c8e36f32570ed72d0988a6eaf986f (diff) | |
| download | brew-b86c8efb79b3ed835d552c4d7416640ef10caf21.tar.bz2 | |
Cask: Use nested classes and modules.
Diffstat (limited to 'Library/Homebrew/cask/lib/hbc/cask.rb')
| -rw-r--r-- | Library/Homebrew/cask/lib/hbc/cask.rb | 188 |
1 files changed, 95 insertions, 93 deletions
diff --git a/Library/Homebrew/cask/lib/hbc/cask.rb b/Library/Homebrew/cask/lib/hbc/cask.rb index f19e5b81f..756b05b83 100644 --- a/Library/Homebrew/cask/lib/hbc/cask.rb +++ b/Library/Homebrew/cask/lib/hbc/cask.rb @@ -2,114 +2,116 @@ require "forwardable" require "hbc/dsl" -class Hbc::Cask - extend Forwardable +module Hbc + class Cask + extend Forwardable - attr_reader :token, :sourcefile_path - def initialize(token, sourcefile_path: nil, dsl: nil, &block) - @token = token - @sourcefile_path = sourcefile_path - @dsl = dsl || Hbc::DSL.new(@token) - @dsl.instance_eval(&block) if block_given? - end - - Hbc::DSL::DSL_METHODS.each do |method_name| - define_method(method_name) { @dsl.send(method_name) } - end - - METADATA_SUBDIR = ".metadata".freeze + attr_reader :token, :sourcefile_path + def initialize(token, sourcefile_path: nil, dsl: nil, &block) + @token = token + @sourcefile_path = sourcefile_path + @dsl = dsl || DSL.new(@token) + @dsl.instance_eval(&block) if block_given? + end - def metadata_master_container_path - @metadata_master_container_path ||= caskroom_path.join(METADATA_SUBDIR) - end + DSL::DSL_METHODS.each do |method_name| + define_method(method_name) { @dsl.send(method_name) } + end - def metadata_versioned_container_path - cask_version = version ? version : :unknown - metadata_master_container_path.join(cask_version.to_s) - end + METADATA_SUBDIR = ".metadata".freeze - def metadata_path(timestamp = :latest, create = false) - return nil unless metadata_versioned_container_path.respond_to?(:join) - if create && timestamp == :latest - raise Hbc::CaskError, "Cannot create metadata path when timestamp is :latest" - end - path = if timestamp == :latest - Pathname.glob(metadata_versioned_container_path.join("*")).sort.last - elsif timestamp == :now - Hbc::Utils.nowstamp_metadata_path(metadata_versioned_container_path) - else - metadata_versioned_container_path.join(timestamp) - end - if create - odebug "Creating metadata directory #{path}" - FileUtils.mkdir_p path + def metadata_master_container_path + @metadata_master_container_path ||= caskroom_path.join(METADATA_SUBDIR) end - path - end - def metadata_subdir(leaf, timestamp = :latest, create = false) - if create && timestamp == :latest - raise Hbc::CaskError, "Cannot create metadata subdir when timestamp is :latest" + def metadata_versioned_container_path + cask_version = version ? version : :unknown + metadata_master_container_path.join(cask_version.to_s) end - unless leaf.respond_to?(:length) && !leaf.empty? - raise Hbc::CaskError, "Cannot create metadata subdir for empty leaf" + + def metadata_path(timestamp = :latest, create = false) + return nil unless metadata_versioned_container_path.respond_to?(:join) + if create && timestamp == :latest + raise CaskError, "Cannot create metadata path when timestamp is :latest" + end + path = if timestamp == :latest + Pathname.glob(metadata_versioned_container_path.join("*")).sort.last + elsif timestamp == :now + Utils.nowstamp_metadata_path(metadata_versioned_container_path) + else + metadata_versioned_container_path.join(timestamp) + end + if create + odebug "Creating metadata directory #{path}" + FileUtils.mkdir_p path + end + path end - parent = metadata_path(timestamp, create) - return nil unless parent.respond_to?(:join) - subdir = parent.join(leaf) - if create - odebug "Creating metadata subdirectory #{subdir}" - FileUtils.mkdir_p subdir + + def metadata_subdir(leaf, timestamp = :latest, create = false) + if create && timestamp == :latest + raise CaskError, "Cannot create metadata subdir when timestamp is :latest" + end + unless leaf.respond_to?(:length) && !leaf.empty? + raise CaskError, "Cannot create metadata subdir for empty leaf" + end + parent = metadata_path(timestamp, create) + return nil unless parent.respond_to?(:join) + subdir = parent.join(leaf) + if create + odebug "Creating metadata subdirectory #{subdir}" + FileUtils.mkdir_p subdir + end + subdir end - subdir - end - def timestamped_versions - Pathname.glob(metadata_master_container_path.join("*", "*")) - .map { |p| p.relative_path_from(metadata_master_container_path) } - .sort_by(&:basename) # sort by timestamp - .map(&:split) - end + def timestamped_versions + Pathname.glob(metadata_master_container_path.join("*", "*")) + .map { |p| p.relative_path_from(metadata_master_container_path) } + .sort_by(&:basename) # sort by timestamp + .map(&:split) + end - def versions - timestamped_versions.map(&:first) - .reverse - .uniq - .reverse - end + def versions + timestamped_versions.map(&:first) + .reverse + .uniq + .reverse + end - def installed? - !versions.empty? - end + def installed? + !versions.empty? + end - def to_s - @token - end + def to_s + @token + end - def dumpcask - return unless Hbc.respond_to?(:debug) - return unless Hbc.debug + def dumpcask + return unless Hbc.respond_to?(:debug) + return unless Hbc.debug - odebug "Cask instance dumps in YAML:" - odebug "Cask instance toplevel:", to_yaml - [ - :name, - :homepage, - :url, - :appcast, - :version, - :license, - :sha256, - :artifacts, - :caveats, - :depends_on, - :conflicts_with, - :container, - :gpg, - :accessibility_access, - :auto_updates, - ].each do |method| - odebug "Cask instance method '#{method}':", send(method).to_yaml + odebug "Cask instance dumps in YAML:" + odebug "Cask instance toplevel:", to_yaml + [ + :name, + :homepage, + :url, + :appcast, + :version, + :license, + :sha256, + :artifacts, + :caveats, + :depends_on, + :conflicts_with, + :container, + :gpg, + :accessibility_access, + :auto_updates, + ].each do |method| + odebug "Cask instance method '#{method}':", send(method).to_yaml + end end end end |
