134 lines
3.0 KiB
Ruby
Raw Normal View History

2018-06-07 14:42:58 +02:00
require "hbc/cask_loader"
require "hbc/config"
2016-08-18 22:11:42 +03:00
require "hbc/dsl"
require "hbc/metadata"
2018-06-07 14:42:58 +02:00
require "searchable"
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
module Hbc
class Cask
2018-04-14 10:28:28 +02:00
extend Enumerable
2016-09-24 13:52:43 +02:00
extend Forwardable
2018-06-07 14:42:58 +02:00
extend Searchable
include Metadata
2016-08-18 22:11:42 +03:00
attr_reader :token, :sourcefile_path, :config
2017-07-29 16:27:54 +02:00
2018-04-14 10:28:28 +02:00
def self.each
return to_enum unless block_given?
Tap.flat_map(&:cask_files).each do |f|
yield CaskLoader::FromTapPathLoader.new(f).load
end
end
2017-07-29 16:27:54 +02:00
def tap
return super if block_given? # Object#tap
@tap
end
def initialize(token, sourcefile_path: nil, tap: nil, config: Config.global, &block)
2016-09-24 13:52:43 +02:00
@token = token
@sourcefile_path = sourcefile_path
2017-07-29 16:27:54 +02:00
@tap = tap
@config = config
@dsl = DSL.new(self)
2016-10-23 14:26:17 +02:00
return unless block_given?
@dsl.instance_eval(&block)
@dsl.language_eval
2016-09-24 13:52:43 +02:00
end
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
DSL::DSL_METHODS.each do |method_name|
define_method(method_name) { |&block| @dsl.send(method_name, &block) }
2016-09-24 13:52:43 +02:00
end
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
def timestamped_versions
Pathname.glob(metadata_timestamped_path(version: "*", timestamp: "*"))
.map { |p| p.relative_path_from(p.parent.parent) }
2016-09-24 13:52:43 +02:00
.sort_by(&:basename) # sort by timestamp
.map { |p| p.split.map(&:to_s) }
2016-09-24 13:52:43 +02:00
end
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
def versions
timestamped_versions.map(&:first)
.reverse
.uniq
.reverse
end
2016-08-18 22:11:42 +03:00
def full_name
2018-04-14 11:32:29 +02:00
return token if tap.nil?
2018-06-30 06:03:51 +02:00
return token if tap.user == "Homebrew"
2018-04-14 11:32:29 +02:00
"#{tap.name}/#{token}"
end
2016-09-24 13:52:43 +02:00
def installed?
!versions.empty?
end
2016-08-18 22:11:42 +03:00
def installed_caskfile
installed_version = timestamped_versions.last
metadata_master_container_path.join(*installed_version, "Casks", "#{token}.rb")
end
2017-02-27 22:33:34 +02:00
def outdated?(greedy = false)
!outdated_versions(greedy).empty?
end
def outdated_versions(greedy = false)
# special case: tap version is not available
return [] if version.nil?
if greedy
return versions if version.latest?
elsif auto_updates
return []
end
installed = versions
current = installed.last
# not outdated unless there is a different version on tap
return [] if current == version
# collect all installed versions that are different than tap version and return them
2017-05-29 18:24:52 +01:00
installed.reject { |v| v == version }
2017-02-27 22:33:34 +02:00
end
2016-09-24 13:52:43 +02:00
def to_s
@token
end
2016-08-18 22:11:42 +03:00
2017-06-28 17:53:59 +02:00
def hash
token.hash
end
def eql?(other)
token == other.token
end
alias == eql?
2016-09-24 13:52:43 +02:00
def dumpcask
odebug "Cask instance dumps in YAML:"
odebug "Cask instance toplevel:", to_yaml
[
:name,
:homepage,
:url,
:appcast,
:version,
: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
2016-08-18 22:11:42 +03:00
end
end
end