2018-06-07 14:42:58 +02:00
|
|
|
require "hbc/cask_loader"
|
2018-06-09 12:20:58 +02:00
|
|
|
require "hbc/config"
|
2016-08-18 22:11:42 +03:00
|
|
|
require "hbc/dsl"
|
2017-04-20 10:48:32 +02:00
|
|
|
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
|
2017-04-20 10:48:32 +02:00
|
|
|
include Metadata
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2017-12-03 21:21:31 +01: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
|
|
|
|
|
2017-12-03 21:21:31 +01:00
|
|
|
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
|
2017-12-03 21:21:31 +01:00
|
|
|
@config = config
|
2017-04-06 00:33:31 +02:00
|
|
|
@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|
|
2017-11-24 17:44:01 +01:00
|
|
|
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
|
2017-04-20 10:48:32 +02:00
|
|
|
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
|
2017-03-06 22:50:22 +01:00
|
|
|
.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
|
|
|
|
2017-07-08 19:33:44 -07: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}"
|
2017-07-08 19:33:44 -07:00
|
|
|
end
|
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def installed?
|
|
|
|
!versions.empty?
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2017-03-12 21:59:13 +01: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
|
2018-07-02 09:05:49 +01:00
|
|
|
|
2018-07-17 11:12:04 +01:00
|
|
|
def to_h
|
2018-07-17 10:04:17 +01:00
|
|
|
{
|
2018-07-02 09:05:49 +01:00
|
|
|
"name" => name,
|
|
|
|
"homepage" => homepage,
|
|
|
|
"url" => url,
|
|
|
|
"appcast" => appcast,
|
|
|
|
"version" => version,
|
|
|
|
"sha256" => sha256,
|
2018-07-04 15:08:32 +01:00
|
|
|
"artifacts" => artifacts.map do |a|
|
2018-07-17 10:56:44 +01:00
|
|
|
if a.respond_to? :to_h
|
2018-07-04 15:08:32 +01:00
|
|
|
a.to_h
|
2018-07-17 10:56:44 +01:00
|
|
|
elsif a.respond_to? :to_a
|
|
|
|
a.to_a
|
2018-07-04 15:08:32 +01:00
|
|
|
else
|
|
|
|
a
|
|
|
|
end
|
|
|
|
end,
|
2018-07-02 09:05:49 +01:00
|
|
|
"caveats" => caveats,
|
|
|
|
"depends_on" => depends_on,
|
2018-07-04 15:08:32 +01:00
|
|
|
"conflicts_with" => conflicts_with.to_a,
|
2018-07-02 09:05:49 +01:00
|
|
|
"container" => container,
|
|
|
|
"gpg" => gpg,
|
|
|
|
"accessibility_access" => accessibility_access,
|
2018-07-02 10:46:41 +01:00
|
|
|
"auto_updates" => auto_updates,
|
2018-07-02 09:05:49 +01:00
|
|
|
}
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
end
|
|
|
|
end
|