diff --git a/Library/Homebrew/cask/cmd/list.rb b/Library/Homebrew/cask/cmd/list.rb index 563f2a59d9..391e175633 100644 --- a/Library/Homebrew/cask/cmd/list.rb +++ b/Library/Homebrew/cask/cmd/list.rb @@ -3,58 +3,69 @@ module Cask class Cmd class List < AbstractCommand - option "-1", :one, false - option "--versions", :versions, false - option "--full-name", :full_name, false + option "-1", :one, false + option "--versions", :versions, false + option "--full-name", :full_name, false + option "--json", :json, false - def run - args.any? ? list : list_installed + def self.usage + <<~EOS + `cask list`, `cask ls` [] [] + + -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 are provided. + --full-name - Print casks with fully-qualified names. + --json - Print a JSON representation of . See the docs for examples of using the JSON + output: + + List all installed casks. + + If are provided, limit information to just those casks. + EOS 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| 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 + casks end def self.list_artifacts(cask) cask.artifacts.group_by(&:class).each do |klass, artifacts| next unless klass.respond_to?(:english_description) - ohai 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)) + return "==> #{klass.english_description}", artifacts.map(&:summarize_installed) end end def self.format_versioned(cask) cask.to_s.concat(cask.versions.map(&:to_s).join(" ").prepend(" ")) end - - def self.help - "with no args, lists installed Casks; given installed Casks, lists staged files" - end end end end diff --git a/Library/Homebrew/test/cask/cmd/list_spec.rb b/Library/Homebrew/test/cask/cmd/list_spec.rb index bc285acb7b..82a486b0d5 100644 --- a/Library/Homebrew/test/cask/cmd/list_spec.rb +++ b/Library/Homebrew/test/cask/cmd/list_spec.rb @@ -20,6 +20,26 @@ describe Cask::Cmd::List, :cask do EOS 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 casks = %w[ local-caffeine @@ -66,6 +86,31 @@ describe Cask::Cmd::List, :cask do 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 let(:caffeine) { Cask::CaskLoader.load(cask_path("local-caffeine")) } let(:transmission) { Cask::CaskLoader.load(cask_path("local-transmission")) }