Skip to content

Commit e59ada5

Browse files
authored
Merge pull request #1894 from reitermarkus/appcast-checkpoint
Add internal command to calculate appcast checkpoint.
2 parents 4459669 + 2076b49 commit e59ada5

File tree

7 files changed

+104
-11
lines changed

7 files changed

+104
-11
lines changed

Library/Homebrew/cask/lib/hbc/audit.rb

+9-10
Original file line numberDiff line numberDiff line change
@@ -133,20 +133,19 @@ def check_appcast_http_code
133133

134134
def check_appcast_checkpoint_accuracy
135135
odebug "Verifying appcast checkpoint is accurate"
136-
result = @command.run("/usr/bin/curl", args: ["--compressed", "--location", "--user-agent", URL::FAKE_USER_AGENT, cask.appcast], print_stderr: false)
137-
if result.success?
138-
processed_appcast_text = result.stdout.gsub(%r{<pubDate>[^<]*</pubDate>}, "")
139-
# This step is necessary to replicate running `sed` from the command line
140-
processed_appcast_text << "\n" unless processed_appcast_text.end_with?("\n")
136+
result = cask.appcast.calculate_checkpoint
137+
138+
actual_checkpoint = result[:checkpoint]
139+
140+
if actual_checkpoint.nil?
141+
add_warning "error retrieving appcast: #{result[:command_result].stderr}"
142+
else
141143
expected = cask.appcast.checkpoint
142-
actual = Digest::SHA2.hexdigest(processed_appcast_text)
143-
add_warning <<-EOS.undent unless expected == actual
144+
add_warning <<-EOS.undent unless expected == actual_checkpoint
144145
appcast checkpoint mismatch
145146
Expected: #{expected}
146-
Actual: #{actual}
147+
Actual: #{actual_checkpoint}
147148
EOS
148-
else
149-
add_warning "error retrieving appcast: #{result.stderr}"
150149
end
151150
end
152151

Library/Homebrew/cask/lib/hbc/cli.rb

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
require "hbc/cli/internal_use_base"
2525
require "hbc/cli/internal_audit_modified_casks"
26+
require "hbc/cli/internal_appcast_checkpoint"
2627
require "hbc/cli/internal_checkurl"
2728
require "hbc/cli/internal_dump"
2829
require "hbc/cli/internal_help"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
module Hbc
2+
class CLI
3+
class InternalAppcastCheckpoint < InternalUseBase
4+
def self.run(*args)
5+
calculate = args.include? "--calculate"
6+
cask_tokens = cask_tokens_from(args)
7+
raise CaskUnspecifiedError if cask_tokens.empty?
8+
9+
if cask_tokens.all? { |t| t =~ %r{^https?://} && t !~ /\.rb$/ }
10+
appcask_checkpoint_for_url(cask_tokens)
11+
else
12+
appcask_checkpoint(cask_tokens, calculate)
13+
end
14+
end
15+
16+
def self.appcask_checkpoint_for_url(urls)
17+
urls.each do |url|
18+
appcast = DSL::Appcast.new(url)
19+
puts appcast.calculate_checkpoint[:checkpoint]
20+
end
21+
end
22+
23+
def self.appcask_checkpoint(cask_tokens, calculate)
24+
count = 0
25+
26+
cask_tokens.each do |cask_token|
27+
cask = Hbc.load(cask_token)
28+
29+
if cask.appcast.nil?
30+
opoo "Cask '#{cask}' is missing an `appcast` stanza."
31+
else
32+
if calculate
33+
result = cask.appcast.calculate_checkpoint
34+
35+
checkpoint = result[:checkpoint]
36+
else
37+
checkpoint = cask.appcast.checkpoint
38+
end
39+
40+
if checkpoint.nil?
41+
onoe "Could not retrieve `appcast` checkpoint for cask '#{cask}': #{result[:command_result].stderr}"
42+
else
43+
puts cask_tokens.count > 1 ? "#{checkpoint} #{cask}": checkpoint
44+
count += 1
45+
end
46+
end
47+
end
48+
49+
count == cask_tokens.count
50+
end
51+
52+
def self.help
53+
"prints or calculates a given Cask's or URL's appcast checkpoint"
54+
end
55+
56+
def self.needs_init?
57+
true
58+
end
59+
end
60+
end
61+
end

Library/Homebrew/cask/lib/hbc/dsl/appcast.rb

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require "hbc/system_command"
2+
13
module Hbc
24
class DSL
35
class Appcast
@@ -9,6 +11,20 @@ def initialize(uri, parameters = {})
911
@checkpoint = @parameters[:checkpoint]
1012
end
1113

14+
def calculate_checkpoint
15+
result = SystemCommand.run("/usr/bin/curl", args: ["--compressed", "--location", "--user-agent", URL::FAKE_USER_AGENT, @uri], print_stderr: false)
16+
17+
checkpoint = if result.success?
18+
processed_appcast_text = result.stdout.gsub(%r{<pubDate>[^<]*</pubDate>}m, "")
19+
Digest::SHA2.hexdigest(processed_appcast_text)
20+
end
21+
22+
{
23+
checkpoint: checkpoint,
24+
command_result: result,
25+
}
26+
end
27+
1228
def to_yaml
1329
[@uri, @parameters].to_yaml
1430
end

Library/Homebrew/cask/spec/cask/audit_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@
162162

163163
before do
164164
allow(audit).to receive(:check_appcast_http_code)
165-
allow(fake_system_command).to receive(:run).and_return(fake_curl_result)
165+
allow(Hbc::SystemCommand).to receive(:run).and_return(fake_curl_result)
166166
allow(fake_curl_result).to receive(:success?).and_return(success)
167167
end
168168

Library/Homebrew/manpages/brew-cask.1.md

+7
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,13 @@ names, and other aspects of this manual are still subject to change.
120120

121121
**`zap` may remove files which are shared between applications.**
122122

123+
## INTERNAL COMMANDS
124+
125+
* `_appcast_checkpoint` [--calculate] [ <token> ... | <URL> ... ]:
126+
Given a `token`, returns the current appcast checkpoint, or calculates
127+
the appcast checkpoint if the `--calculate` flag is specified.
128+
Given a `URL`, calculates the appcast checkpoint for it.
129+
123130
## OPTIONS
124131

125132
To make these options persistent, see the ENVIRONMENT section, below.

manpages/brew-cask.1

+9
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,15 @@ If the Cask definition contains a \fBzap\fR stanza, performs additional \fBzap\f
105105
.
106106
.IP "" 0
107107
.
108+
.SH "INTERNAL COMMANDS"
109+
.
110+
.TP
111+
\fB_appcast_checkpoint\fR [\-\-calculate] [ \fItoken\fR \.\.\. | \fIURL\fR \.\.\. ]
112+
Given a \fBtoken\fR, returns the current appcast checkpoint, or calculates the appcast checkpoint if the \fB\-\-calculate\fR flag is specified\.
113+
.
114+
.br
115+
Given a \fBURL\fR, calculates the appcast checkpoint for it\.
116+
.
108117
.SH "OPTIONS"
109118
To make these options persistent, see the ENVIRONMENT section, below\.
110119
.

0 commit comments

Comments
 (0)