diff --git a/Library/Homebrew/cask/cmd/info.rb b/Library/Homebrew/cask/cmd/info.rb index 9667f2bfc21b77eca55b46ffa91b33d9d29515bb..250269683a5781ecf37d0c6b72677f0c5c6b9d20 100644 --- a/Library/Homebrew/cask/cmd/info.rb +++ b/Library/Homebrew/cask/cmd/info.rb @@ -18,14 +18,38 @@ module Cask def self.parser super do - flag "--json=", - description: "Output information in JSON format." + flag "--json=", + 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 def run if args.json == "v1" 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 casks.each_with_index do |cask, i| puts unless i.zero? diff --git a/Library/Homebrew/cmd/info.rb b/Library/Homebrew/cmd/info.rb index c204f6467f3e39f6557a2784d864d250b0305c5b..0807ee16bed628f7fff02c766605b5f7c766cbdd 100644 --- a/Library/Homebrew/cmd/info.rb +++ b/Library/Homebrew/cmd/info.rb @@ -86,7 +86,7 @@ module Homebrew elsif args.github? 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 print_info(args: args) end @@ -148,7 +148,11 @@ module Homebrew def github_info(f) if f.tap 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) else f.path