189 lines
4.2 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require "cask/cask_loader"
require "cask/config"
require "cask/dsl"
require "cask/metadata"
2018-06-07 14:42:58 +02:00
require "searchable"
2016-08-18 22:11:42 +03:00
2018-09-06 08:29:14 +02:00
module Cask
2016-09-24 13:52:43 +02:00
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
rescue CaskUnreadableError => e
opoo e.message
2018-04-14 10:28:28 +02:00
end
end
2017-07-29 16:27:54 +02:00
def tap
return super if block_given? # Object#tap
2018-09-17 02:45:00 +02:00
2017-07-29 16:27:54 +02:00
@tap
end
def initialize(token, sourcefile_path: nil, tap: nil, &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
@block = block
self.config = Config.for_cask(self)
end
def config=(config)
@config = config
@dsl = DSL.new(self)
return unless @block
2018-09-17 02:45:00 +02:00
@dsl.instance_eval(&@block)
2016-10-23 14:26:17 +02:00
@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-09-17 02:45:00 +02:00
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
2019-05-31 20:38:28 +02:00
def install_time
_, time = timestamped_versions.last
return unless time
Time.strptime(time, Metadata::TIMESTAMP_FORMAT)
end
def installed_caskfile
installed_version = timestamped_versions.last
metadata_master_container_path.join(*installed_version, "Casks", "#{token}.rb")
end
2019-02-02 17:11:37 +01:00
def config_path
2019-02-08 03:00:27 +01:00
metadata_master_container_path/"config.json"
2019-02-02 17:11:37 +01:00
end
def caskroom_path
@caskroom_path ||= Caskroom.path.join(token)
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?
2018-07-17 11:12:04 +01:00
def to_h
2018-07-17 10:04:17 +01:00
{
2019-07-09 20:43:45 +02:00
"token" => token,
2018-11-02 17:18:07 +00:00
"name" => name,
"homepage" => homepage,
"url" => url,
"appcast" => appcast,
"version" => version,
"sha256" => sha256,
"artifacts" => artifacts.map(&method(:to_h_gsubs)),
"caveats" => (to_h_string_gsubs(caveats) unless caveats.empty?),
2018-11-02 17:18:07 +00:00
"depends_on" => depends_on,
"conflicts_with" => conflicts_with,
2018-11-02 17:18:07 +00:00
"container" => container,
"auto_updates" => auto_updates,
}
end
private
def to_h_string_gsubs(string)
string.to_s
.gsub(ENV["HOME"], "$HOME")
.gsub(HOMEBREW_PREFIX, "$(brew --prefix)")
end
def to_h_array_gsubs(array)
array.to_a.map do |value|
to_h_gsubs(value)
end
end
def to_h_hash_gsubs(hash)
hash.to_h.each_with_object({}) do |(key, value), h|
h[key] = to_h_gsubs(value)
end
rescue TypeError
to_h_array_gsubs(hash)
end
def to_h_gsubs(value)
if value.respond_to? :to_h
to_h_hash_gsubs(value)
elsif value.respond_to? :to_a
to_h_array_gsubs(value)
else
to_h_string_gsubs(value)
end
end
2016-08-18 22:11:42 +03:00
end
end