Add JSON support to brew cask outdated

* brew outdated already has JSON support, now users and tools can get
similar results with brew cask outdated --json
This commit is contained in:
Frank Lam 2020-04-26 21:13:19 +08:00
parent 65ff9155f8
commit f2fa2c5d30
No known key found for this signature in database
GPG Key ID: 771022A2327531C7
2 changed files with 107 additions and 16 deletions

View File

@ -5,30 +5,21 @@ module Cask
class Outdated < AbstractCommand
option "--greedy", :greedy, false
option "--quiet", :quiet, false
option "--json", :json, false
def initialize(*)
super
self.verbose = ($stdout.tty? || verbose?) && !quiet?
@outdated_casks = casks(alternative: -> { Caskroom.casks }).select do |cask|
odebug "Checking update info of Cask #{cask}"
cask.outdated?(greedy?)
end
end
def run
casks(alternative: -> { Caskroom.casks }).each do |cask|
odebug "Checking update info of Cask #{cask}"
self.class.list_if_outdated(cask, greedy?, verbose?)
end
end
output = @outdated_casks.map { |cask| cask.outdated_info(greedy?, verbose?, json?) }
def self.list_if_outdated(cask, greedy, verbose)
return unless cask.outdated?(greedy)
if verbose
outdated_versions = cask.outdated_versions(greedy)
outdated_info = "#{cask.token} (#{outdated_versions.join(", ")})"
current_version = cask.version.to_s
puts "#{outdated_info} != #{current_version}"
else
puts cask.token
end
puts json? ? JSON.generate(output) : output
end
def self.help

View File

@ -88,4 +88,104 @@ describe Cask::Cmd::Outdated, :cask do
EOS
end
end
describe "--json" do
it "lists outdated Casks in JSON format" do
result = [
{
name: "local-caffeine",
installed_versions: "1.2.2",
current_version: "1.2.3"
},
{
name: "local-transmission",
installed_versions: "2.60",
current_version: "2.61"
}
].to_json
expect {
described_class.run("--json")
}.to output(result + "\n").to_stdout
end
end
describe "--json overrides --quiet" do
it "ignores --quiet and lists outdated Casks in JSON format" do
result = [
{
name: "local-caffeine",
installed_versions: "1.2.2",
current_version: "1.2.3"
},
{
name: "local-transmission",
installed_versions: "2.60",
current_version: "2.61"
}
].to_json
expect {
described_class.run("--json", "--quiet")
}.to output(result + "\n").to_stdout
end
end
describe "--json and --greedy" do
it 'includes the Casks with "auto_updates true" or "version latest" in JSON format' do
result = [
{
name: "auto-updates",
installed_versions: "2.57",
current_version: "2.61"
},
{
name: "local-caffeine",
installed_versions: "1.2.2",
current_version: "1.2.3"
},
{
name: "local-transmission",
installed_versions: "2.60",
current_version: "2.61"
},
{
name: "version-latest-string",
installed_versions: "latest",
current_version: "latest"
}
].to_json
expect {
described_class.run("--json", "--greedy")
}.to output(result + "\n").to_stdout
end
it 'does not include the Casks with "auto_updates true" with no version change in JSON format' do
cask = Cask::CaskLoader.load(cask_path("auto-updates"))
InstallHelper.install_with_caskfile(cask)
result = [
{
name: "local-caffeine",
installed_versions: "1.2.2",
current_version: "1.2.3"
},
{
name: "local-transmission",
installed_versions: "2.60",
current_version: "2.61"
},
{
name: "version-latest-string",
installed_versions: "latest",
current_version: "latest"
}
].to_json
expect {
described_class.run("--json", "--greedy")
}.to output(result + "\n").to_stdout
end
end
end