forked from Homebrew/brew
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cask.rb
220 lines (177 loc) · 5.01 KB
/
cask.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# typed: false
# frozen_string_literal: true
require "cask/cask_loader"
require "cask/config"
require "cask/dsl"
require "cask/metadata"
require "searchable"
module Cask
# An instance of a cask.
#
# @api private
class Cask
extend T::Sig
extend Enumerable
extend Forwardable
extend Searchable
include Metadata
attr_reader :token, :sourcefile_path, :config, :default_config
def self.each(&block)
return to_enum unless block_given?
Tap.flat_map(&:cask_files).each do |f|
block.call CaskLoader::FromTapPathLoader.new(f).load(config: nil)
rescue CaskUnreadableError => e
opoo e.message
end
end
def tap
return super if block_given? # Object#tap
@tap
end
def initialize(token, sourcefile_path: nil, tap: nil, config: nil, &block)
@token = token
@sourcefile_path = sourcefile_path
@tap = tap
@block = block
@default_config = config || Config.new
self.config = if config_path.exist?
Config.from_json(File.read(config_path))
else
@default_config
end
end
def config=(config)
@config = config
@dsl = DSL.new(self)
return unless @block
@dsl.instance_eval(&@block)
@dsl.language_eval
end
DSL::DSL_METHODS.each do |method_name|
define_method(method_name) { |&block| @dsl.send(method_name, &block) }
end
sig { returns(T::Array[[String, String]]) }
def timestamped_versions
Pathname.glob(metadata_timestamped_path(version: "*", timestamp: "*"))
.map { |p| p.relative_path_from(p.parent.parent) }
.sort_by(&:basename) # sort by timestamp
.map { |p| p.split.map(&:to_s) }
end
def versions
timestamped_versions.map(&:first)
.reverse
.uniq
.reverse
end
def full_name
return token if tap.nil?
return token if tap.user == "Homebrew"
"#{tap.name}/#{token}"
end
def installed?
!versions.empty?
end
sig { returns(T.nilable(Time)) }
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
def config_path
metadata_master_container_path/"config.json"
end
def caskroom_path
@caskroom_path ||= Caskroom.path.join(token)
end
def outdated?(greedy: false)
!outdated_versions(greedy: 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
installed.reject { |v| v == version }
end
def outdated_info(greedy, verbose, json)
return token if !verbose && !json
installed_versions = outdated_versions(greedy: greedy).join(", ")
if json
{
name: token,
installed_versions: installed_versions,
current_version: version,
}
else
"#{token} (#{installed_versions}) != #{version}"
end
end
def to_s
@token
end
def hash
token.hash
end
def eql?(other)
token == other.token
end
alias == eql?
def to_h
{
"token" => token,
"name" => name,
"desc" => desc,
"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?),
"depends_on" => depends_on,
"conflicts_with" => conflicts_with,
"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.transform_values do |value|
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
end
end