cask: add list --json
This commit is contained in:
parent
bab7c7e190
commit
832073a3c7
@ -3,58 +3,69 @@
|
|||||||
module Cask
|
module Cask
|
||||||
class Cmd
|
class Cmd
|
||||||
class List < AbstractCommand
|
class List < AbstractCommand
|
||||||
option "-1", :one, false
|
option "-1", :one, false
|
||||||
option "--versions", :versions, false
|
option "--versions", :versions, false
|
||||||
option "--full-name", :full_name, false
|
option "--full-name", :full_name, false
|
||||||
|
option "--json", :json, false
|
||||||
|
|
||||||
def run
|
def self.usage
|
||||||
args.any? ? list : list_installed
|
<<~EOS
|
||||||
|
`cask list`, `cask ls` [<options>] [<casks>]
|
||||||
|
|
||||||
|
-1 - Force output to be one entry per line.
|
||||||
|
This is the default when output is not to a terminal.
|
||||||
|
--versions - Show the version number for installed formulae, or only the specified
|
||||||
|
casks if <casks> are provided.
|
||||||
|
--full-name - Print casks with fully-qualified names.
|
||||||
|
--json - Print a JSON representation of <cask>. See the docs for examples of using the JSON
|
||||||
|
output: <https://docs.brew.sh/Querying-Brew>
|
||||||
|
|
||||||
|
List all installed casks.
|
||||||
|
|
||||||
|
If <casks> are provided, limit information to just those casks.
|
||||||
|
EOS
|
||||||
end
|
end
|
||||||
|
|
||||||
def list
|
def self.help
|
||||||
|
"lists installed Casks or the casks provided in the arguments"
|
||||||
|
end
|
||||||
|
|
||||||
|
def run
|
||||||
|
output = args.any? ? provided_list : Caskroom.casks
|
||||||
|
|
||||||
|
if json?
|
||||||
|
puts JSON.generate(output.map(&:to_h))
|
||||||
|
elsif one?
|
||||||
|
puts output.map(&:to_s)
|
||||||
|
elsif full_name?
|
||||||
|
puts output.map(&:full_name).sort(&tap_and_name_comparison)
|
||||||
|
elsif versions?
|
||||||
|
puts output.map(&self.class.method(:format_versioned))
|
||||||
|
elsif !output.empty? && args.any?
|
||||||
|
puts output.map(&self.class.method(:list_artifacts))
|
||||||
|
elsif !output.empty?
|
||||||
|
puts Formatter.columns(output.map(&:to_s))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def provided_list
|
||||||
casks.each do |cask|
|
casks.each do |cask|
|
||||||
raise CaskNotInstalledError, cask unless cask.installed?
|
raise CaskNotInstalledError, cask unless cask.installed?
|
||||||
|
|
||||||
if one?
|
|
||||||
puts cask.token
|
|
||||||
elsif versions?
|
|
||||||
puts self.class.format_versioned(cask)
|
|
||||||
else
|
|
||||||
cask = CaskLoader.load(cask.installed_caskfile)
|
|
||||||
self.class.list_artifacts(cask)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
casks
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.list_artifacts(cask)
|
def self.list_artifacts(cask)
|
||||||
cask.artifacts.group_by(&:class).each do |klass, artifacts|
|
cask.artifacts.group_by(&:class).each do |klass, artifacts|
|
||||||
next unless klass.respond_to?(:english_description)
|
next unless klass.respond_to?(:english_description)
|
||||||
|
|
||||||
ohai klass.english_description, artifacts.map(&:summarize_installed)
|
return "==> #{klass.english_description}", artifacts.map(&:summarize_installed)
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def list_installed
|
|
||||||
installed_casks = Caskroom.casks
|
|
||||||
|
|
||||||
if one?
|
|
||||||
puts installed_casks.map(&:to_s)
|
|
||||||
elsif versions?
|
|
||||||
puts installed_casks.map(&self.class.method(:format_versioned))
|
|
||||||
elsif full_name?
|
|
||||||
puts installed_casks.map(&:full_name).sort(&tap_and_name_comparison)
|
|
||||||
elsif !installed_casks.empty?
|
|
||||||
puts Formatter.columns(installed_casks.map(&:to_s))
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.format_versioned(cask)
|
def self.format_versioned(cask)
|
||||||
cask.to_s.concat(cask.versions.map(&:to_s).join(" ").prepend(" "))
|
cask.to_s.concat(cask.versions.map(&:to_s).join(" ").prepend(" "))
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.help
|
|
||||||
"with no args, lists installed Casks; given installed Casks, lists staged files"
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -20,6 +20,26 @@ describe Cask::Cmd::List, :cask do
|
|||||||
EOS
|
EOS
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "lists oneline" do
|
||||||
|
casks = %w[
|
||||||
|
local-caffeine
|
||||||
|
third-party/tap/third-party-cask
|
||||||
|
local-transmission
|
||||||
|
].map { |c| Cask::CaskLoader.load(c) }
|
||||||
|
|
||||||
|
casks.each do |c|
|
||||||
|
InstallHelper.install_with_caskfile(c)
|
||||||
|
end
|
||||||
|
|
||||||
|
expect {
|
||||||
|
described_class.run("-1")
|
||||||
|
}.to output(<<~EOS).to_stdout
|
||||||
|
local-caffeine
|
||||||
|
local-transmission
|
||||||
|
third-party-cask
|
||||||
|
EOS
|
||||||
|
end
|
||||||
|
|
||||||
it "lists full names" do
|
it "lists full names" do
|
||||||
casks = %w[
|
casks = %w[
|
||||||
local-caffeine
|
local-caffeine
|
||||||
@ -66,6 +86,31 @@ describe Cask::Cmd::List, :cask do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "lists json" do
|
||||||
|
let(:casks) { ["local-caffeine", "local-transmission"] }
|
||||||
|
let(:expected_output) {
|
||||||
|
<<~EOS
|
||||||
|
[{"token":"local-caffeine","name":[],"homepage":"https://brew.sh/","url":"file:///usr/local/Homebrew/Library/Homebrew/test/support/fixtures/cask/caffeine.zip","appcast":null,"version":"1.2.3","sha256":"67cdb8a02803ef37fdbf7e0be205863172e41a561ca446cd84f0d7ab35a99d94","artifacts":[["Caffeine.app"]],"caveats":null,"depends_on":{},"conflicts_with":null,"container":null,"auto_updates":null},{"token":"local-transmission","name":["Transmission"],"homepage":"https://brew.sh/","url":"file:///usr/local/Homebrew/Library/Homebrew/test/support/fixtures/cask/transmission-2.61.dmg","appcast":null,"version":"2.61","sha256":"e44ffa103fbf83f55c8d0b1bea309a43b2880798dae8620b1ee8da5e1095ec68","artifacts":[["Transmission.app"]],"caveats":null,"depends_on":{},"conflicts_with":null,"container":null,"auto_updates":null}]
|
||||||
|
EOS
|
||||||
|
}
|
||||||
|
|
||||||
|
before do
|
||||||
|
casks.map(&Cask::CaskLoader.method(:load)).each(&InstallHelper.method(:install_with_caskfile))
|
||||||
|
end
|
||||||
|
|
||||||
|
it "of all installed Casks" do
|
||||||
|
expect {
|
||||||
|
described_class.run("--json")
|
||||||
|
}.to output(expected_output).to_stdout
|
||||||
|
end
|
||||||
|
|
||||||
|
it "of given Casks" do
|
||||||
|
expect {
|
||||||
|
described_class.run("--json", "local-caffeine", "local-transmission")
|
||||||
|
}.to output(expected_output).to_stdout
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe "given a set of installed Casks" do
|
describe "given a set of installed Casks" do
|
||||||
let(:caffeine) { Cask::CaskLoader.load(cask_path("local-caffeine")) }
|
let(:caffeine) { Cask::CaskLoader.load(cask_path("local-caffeine")) }
|
||||||
let(:transmission) { Cask::CaskLoader.load(cask_path("local-transmission")) }
|
let(:transmission) { Cask::CaskLoader.load(cask_path("local-transmission")) }
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user