71 lines
2.1 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2018-09-06 08:29:14 +02:00
module Cask
2018-09-04 08:45:48 +01:00
class Cmd
2017-05-20 19:08:03 +02:00
class List < AbstractCommand
2020-08-01 02:30:46 +02:00
def self.description
"Lists installed casks or the casks provided in the arguments."
2020-07-09 22:58:48 +02:00
end
2020-08-01 02:30:46 +02:00
def self.parser
super do
switch "-1",
description: "Force output to be one entry per line."
switch "--versions",
description: "Show the version number the listed casks."
switch "--full-name",
description: "Print casks with fully-qualified names."
switch "--json",
description: "Print a JSON representation of the listed casks. "
end
2020-07-09 22:58:48 +02:00
end
2017-05-20 03:30:46 +02:00
def run
self.class.list_casks(
*casks,
2020-08-01 02:30:46 +02:00
json: args.json?,
one: args.public_send(:'1?'),
full_name: args.full_name?,
versions: args.versions?,
)
end
def self.list_casks(*casks, json: false, one: false, full_name: false, versions: false)
output = if casks.any?
casks.each do |cask|
raise CaskNotInstalledError, cask unless cask.installed?
end
else
Caskroom.casks
end
2020-07-09 22:58:48 +02:00
if json
2020-07-09 22:58:48 +02:00
puts JSON.generate(output.map(&:to_h))
elsif one
2020-07-09 22:58:48 +02:00
puts output.map(&:to_s)
elsif full_name
2020-07-09 22:58:48 +02:00
puts output.map(&:full_name).sort(&tap_and_name_comparison)
elsif versions
puts output.map(&method(:format_versioned))
elsif !output.empty? && casks.any?
puts output.map(&method(:list_artifacts))
2020-07-09 22:58:48 +02:00
elsif !output.empty?
puts Formatter.columns(output.map(&:to_s))
end
2016-09-24 13:52:43 +02:00
end
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
def self.list_artifacts(cask)
2017-10-04 17:08:35 +02:00
cask.artifacts.group_by(&:class).each do |klass, artifacts|
next unless klass.respond_to?(:english_description)
2018-09-17 02:45:00 +02:00
2020-07-09 22:58:48 +02:00
return "==> #{klass.english_description}", artifacts.map(&:summarize_installed)
2016-09-24 13:52:43 +02:00
end
end
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
def self.format_versioned(cask)
cask.to_s.concat(cask.versions.map(&:to_s).join(" ").prepend(" "))
end
end
end
2016-08-18 22:11:42 +03:00
end