From f800db03831e3fbc9825ce4b6a5c7336b26d2ff8 Mon Sep 17 00:00:00 2001 From: Roland Walker Date: Thu, 4 Dec 2014 17:42:00 -0500 Subject: [PATCH] Add `name` stanza for proper name Documentation is in a separate PR so that authors don't start using this feature before release. --- lib/cask/cli/create.rb | 1 + lib/cask/cli/info.rb | 8 ++++++- lib/cask/cli/internal_stanza.rb | 3 ++- lib/cask/dsl.rb | 23 +++++++++++++++++++ lib/cask/utils.rb | 5 ++++- test/cask/cli/create_test.rb | 1 + test/cask/cli/info_test.rb | 5 +++++ test/cask/dsl_test.rb | 39 +++++++++++++++++++++++++++++++++ 8 files changed, 82 insertions(+), 3 deletions(-) diff --git a/lib/cask/cli/create.rb b/lib/cask/cli/create.rb index 7ee0166ee4d6..6a5b39fc478c 100644 --- a/lib/cask/cli/create.rb +++ b/lib/cask/cli/create.rb @@ -24,6 +24,7 @@ def self.template(cask_token) sha256 '' url 'https://' + name '' homepage '' license :unknown diff --git a/lib/cask/cli/info.rb b/lib/cask/cli/info.rb index 911c020ab2d2..8f99d0780107 100644 --- a/lib/cask/cli/info.rb +++ b/lib/cask/cli/info.rb @@ -20,9 +20,10 @@ def self.info(cask) else "Not installed" end - + # todo completely reformat the info report <<-PURPOSE #{cask}: #{cask.version} +#{formatted_full_name(cask) } #{cask.homepage or 'No Homepage'} #{installation} #{github_info(cask) or 'No GitHub URL'} @@ -30,6 +31,11 @@ def self.info(cask) PURPOSE end + def self.formatted_full_name(cask) + # todo transitional: make name a required stanza, and then stop substituting cask.token here + cask.full_name.empty? ? cask.token : cask.full_name.join(', ') + end + def self.github_info(cask) cask_token = cask.token cask_token = cask.class.all_tokens.detect { |t| t.split("/").last == cask_token } unless cask_token =~ /\// diff --git a/lib/cask/cli/internal_stanza.rb b/lib/cask/cli/internal_stanza.rb index f459170efd41..3c342e6c2491 100644 --- a/lib/cask/cli/internal_stanza.rb +++ b/lib/cask/cli/internal_stanza.rb @@ -69,7 +69,8 @@ def self.run(*arguments) def self.print_stanzas(stanza, format=nil, table=nil, quiet=nil, *cask_tokens) count = 0 - stanza = :sums if stanza == :sha256 + stanza = :sums if stanza == :sha256 + stanza = :full_name if stanza == :name if ARTIFACTS.include?(stanza) artifact_name = stanza stanza = :artifacts diff --git a/lib/cask/dsl.rb b/lib/cask/dsl.rb index 865efdeaf72e..ffec19994961 100644 --- a/lib/cask/dsl.rb +++ b/lib/cask/dsl.rb @@ -22,6 +22,8 @@ def self.included(base) base.extend(ClassMethods) end + def full_name; self.class.full_name; end + def homepage; self.class.homepage; end def url; self.class.url; end @@ -49,6 +51,27 @@ def artifacts; self.class.artifacts; end def caveats; self.class.caveats; end module ClassMethods + + # A quite fragile shim to allow "full_name" be exposed as simply "name" + # in the DSL. We detect the difference with the already-existing "name" + # method by arity, and use "full_name" exclusively in backend code. + def name(*args) + if args.empty? + super + else + self.full_name(args) + end + end + + def full_name(_full_name=nil) + @full_name ||= [] + if _full_name + # todo this idiom may be preferred to << if it behaves the same on Ruby 1.8 and 2.x + @full_name.concat(Array(*_full_name)) + end + @full_name + end + def homepage(homepage=nil) if @homepage and !homepage.nil? raise CaskInvalidError.new(self.token, "'homepage' stanza may only appear once") diff --git a/lib/cask/utils.rb b/lib/cask/utils.rb index 0d74afb57cc3..f1c228e6e9a1 100644 --- a/lib/cask/utils.rb +++ b/lib/cask/utils.rb @@ -85,6 +85,7 @@ def dumpcask odebug "Cask instance dumps in YAML:" odebug "Cask instance toplevel:", self.to_yaml [ + :full_name, :homepage, :url, :appcast, @@ -99,7 +100,9 @@ def dumpcask :container, :gpg, ].each do |method| - odebug "Cask instance method '#{method}':", self.send(method).to_yaml + printable_method = method.to_s + printable_method = "name" if printable_method == "full_name" + odebug "Cask instance method '#{printable_method}':", self.send(method).to_yaml end end end diff --git a/test/cask/cli/create_test.rb b/test/cask/cli/create_test.rb index 54d820325988..65e6a7c3ff1b 100644 --- a/test/cask/cli/create_test.rb +++ b/test/cask/cli/create_test.rb @@ -41,6 +41,7 @@ def self.editor_commands sha256 '' url 'https://' + name '' homepage '' license :unknown diff --git a/test/cask/cli/info_test.rb b/test/cask/cli/info_test.rb index 7a30f3e718e9..b889b6b97dbc 100644 --- a/test/cask/cli/info_test.rb +++ b/test/cask/cli/info_test.rb @@ -6,6 +6,7 @@ Cask::CLI::Info.run('local-caffeine') }.must_output <<-CLIOUTPUT.undent local-caffeine: 1.2.3 + local-caffeine http://example.com/local-caffeine Not installed https://github.com/caskroom/homebrew-testcasks/blob/master/Casks/local-caffeine.rb @@ -18,12 +19,14 @@ before do @expected_output = <<-CLIOUTPUT.undent local-caffeine: 1.2.3 + local-caffeine http://example.com/local-caffeine Not installed https://github.com/caskroom/homebrew-testcasks/blob/master/Casks/local-caffeine.rb ==> Contents Caffeine.app (app) local-transmission: 2.61 + local-transmission http://example.com/local-transmission Not installed https://github.com/caskroom/homebrew-testcasks/blob/master/Casks/local-transmission.rb @@ -50,6 +53,7 @@ Cask::CLI::Info.run('with-caveats') }.must_output <<-CLIOUTPUT.undent with-caveats: 1.2.3 + with-caveats http://example.com/local-caffeine Not installed https://github.com/caskroom/homebrew-testcasks/blob/master/Casks/with-caveats.rb @@ -74,6 +78,7 @@ Cask::CLI::Info.run('with-conditional-caveats') }.must_output <<-CLIOUTPUT.undent with-conditional-caveats: 1.2.3 + with-conditional-caveats http://example.com/local-caffeine Not installed https://github.com/caskroom/homebrew-testcasks/blob/master/Casks/with-conditional-caveats.rb diff --git a/test/cask/dsl_test.rb b/test/cask/dsl_test.rb index c77a9376c027..511baca0d6d3 100644 --- a/test/cask/dsl_test.rb +++ b/test/cask/dsl_test.rb @@ -57,6 +57,45 @@ end end + describe "name stanza" do + it "lets you set the full name via a name stanza" do + NameCask = Class.new(Cask) + NameCask.class_eval do + name 'Proper Name' + end + instance = NameCask.new + instance.full_name.must_equal [ + 'Proper Name', + ] + end + + it "Accepts an array value to the name stanza" do + ArrayNameCask = Class.new(Cask) + ArrayNameCask.class_eval do + name ['Proper Name', 'Alternate Name'] + end + instance = ArrayNameCask.new + instance.full_name.must_equal [ + 'Proper Name', + 'Alternate Name', + ] + end + + it "Accepts multiple name stanzas" do + MultiNameCask = Class.new(Cask) + MultiNameCask.class_eval do + name 'Proper Name' + name 'Alternate Name' + end + instance = MultiNameCask.new + # the sort is a hack to deal with Ruby 1.8 oddities + instance.full_name.sort.must_equal [ + 'Proper Name', + 'Alternate Name', + ].sort + end + end + describe "sha256 stanza" do it "lets you set checksum via sha256" do ChecksumCask = Class.new(Cask)