show cask info on browser

This commit is contained in:
hyuraku 2020-09-28 13:10:06 +09:00
parent 48c050371d
commit 137d33290f
2 changed files with 32 additions and 4 deletions

View File

@ -18,14 +18,38 @@ module Cask
def self.parser def self.parser
super do super do
flag "--json=", flag "--json=",
description: "Output information in JSON format." description: "Output information in JSON format."
switch "--github",
description: "Open the GitHub source page for <Cask> in a browser. "
end
end
def github_info(cask)
sourcefile_path = cask.sourcefile_path
dir = cask.tap.path
path = sourcefile_path.relative_path_from(dir)
remote = cask.tap.remote
github_remote_path(remote, path)
end
def github_remote_path(remote, path)
if remote =~ %r{^(?:https?://|git(?:@|://))github\.com[:/](.+)/(.+?)(?:\.git)?$}
"https://github.com/#{Regexp.last_match(1)}/#{Regexp.last_match(2)}/blob/HEAD/#{path}"
else
"#{remote}/#{path}"
end end
end end
def run def run
if args.json == "v1" if args.json == "v1"
puts JSON.generate(casks.map(&:to_h)) puts JSON.generate(casks.map(&:to_h))
elsif args.github?
raise CaskUnspecifiedError if args.no_named?
args.named.to_formulae_and_casks.map do |cask|
exec_browser(github_info(cask))
end
else else
casks.each_with_index do |cask, i| casks.each_with_index do |cask, i|
puts unless i.zero? puts unless i.zero?

View File

@ -86,7 +86,7 @@ module Homebrew
elsif args.github? elsif args.github?
raise FormulaUnspecifiedError if args.no_named? raise FormulaUnspecifiedError if args.no_named?
exec_browser(*args.named.to_formulae.map { |f| github_info(f) }) exec_browser(*args.named.to_formulae_and_casks.map { |f| github_info(f) })
else else
print_info(args: args) print_info(args: args)
end end
@ -148,7 +148,11 @@ module Homebrew
def github_info(f) def github_info(f)
if f.tap if f.tap
if remote = f.tap.remote if remote = f.tap.remote
path = f.path.relative_path_from(f.tap.path) path = if f.class.superclass == Formula
f.path.relative_path_from(f.tap.path)
elsif f.is_a?(Cask::Cask)
f.sourcefile_path.relative_path_from(f.tap.path)
end
github_remote_path(remote, path) github_remote_path(remote, path)
else else
f.path f.path