From a8d3aca169c7d6d83790ff52eb5cd973b54446e7 Mon Sep 17 00:00:00 2001 From: Jack Nagel Date: Sat, 21 Sep 2013 19:27:24 -0500 Subject: [PATCH] Move options to SoftwareSpec --- Library/Homebrew/formula.rb | 31 ++++++++------------- Library/Homebrew/software_spec.rb | 11 +++++++- Library/Homebrew/test/test_software_spec.rb | 18 ++++++++++++ 3 files changed, 40 insertions(+), 20 deletions(-) diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index 10e546d2f8..4011a62101 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -17,7 +17,7 @@ class Formula include Utils::Inreplace extend BuildEnvironmentDSL - attr_reader :name, :path, :homepage, :downloader + attr_reader :name, :path, :homepage, :downloader, :build attr_reader :stable, :bottle, :devel, :head, :active_spec # The current working directory during builds and tests. @@ -48,12 +48,7 @@ class Formula @active_spec = determine_active_spec validate_attributes :url, :name, :version @downloader = active_spec.downloader - - # Combine DSL `option` and `def options` - options.each do |opt, desc| - # make sure to strip "--" from the start of options - self.class.build.add opt[/--(.+)$/, 1], desc - end + @build = determine_build_options @pin = FormulaPin.new(self) end @@ -89,9 +84,13 @@ class Formula end def default_build? - build = self.class.build.dup - build.concat(stable.options) - build.used_options.empty? + self.class.build.used_options.empty? + end + + def determine_build_options + build = active_spec.build + options.each { |opt, desc| build.add(opt, desc) } + build end def url; active_spec.url; end @@ -173,9 +172,6 @@ class Formula def plist_manual; self.class.plist_manual end def plist_startup; self.class.plist_startup end - # Defined and active build-time options. - def build; self.class.build; end - def opt_prefix Pathname.new("#{HOMEBREW_PREFIX}/opt/#{name}") end @@ -667,7 +663,8 @@ class Formula end def build - @build ||= BuildOptions.new(ARGV.options_only) + @stable ||= create_spec(SoftwareSpec) + @stable.build end def url val, specs={} @@ -732,11 +729,7 @@ class Formula end def option name, description=nil - # Support symbols - name = name.to_s - raise "Option name is required." if name.empty? - raise "Options should not start with dashes." if name[0, 1] == "-" - build.add name, description + specs.each { |spec| spec.option(name, description) } end def plist_options options diff --git a/Library/Homebrew/software_spec.rb b/Library/Homebrew/software_spec.rb index 7b4004ecbb..81adc83ff3 100644 --- a/Library/Homebrew/software_spec.rb +++ b/Library/Homebrew/software_spec.rb @@ -2,11 +2,12 @@ require 'forwardable' require 'resource' require 'checksum' require 'version' +require 'build_options' class SoftwareSpec extend Forwardable - attr_reader :resources, :owner + attr_reader :build, :resources, :owner def_delegators :@resource, :stage, :fetch def_delegators :@resource, :download_strategy, :verify_download_integrity @@ -16,6 +17,7 @@ class SoftwareSpec def initialize url=nil, version=nil @resource = Resource.new(:default, url, version) @resources = {} + @build = BuildOptions.new(ARGV.options_only) end def owner= owner @@ -35,6 +37,13 @@ class SoftwareSpec resources.fetch(name) { raise ResourceMissingError.new(owner, name) } end end + + def option name, description=nil + name = name.to_s if Symbol === name + raise "Option name is required." if name.empty? + raise "Options should not start with dashes." if name[0, 1] == "-" + build.add(name, description) + end end class HeadSoftwareSpec < SoftwareSpec diff --git a/Library/Homebrew/test/test_software_spec.rb b/Library/Homebrew/test/test_software_spec.rb index febd33b398..c534cd0083 100644 --- a/Library/Homebrew/test/test_software_spec.rb +++ b/Library/Homebrew/test/test_software_spec.rb @@ -29,6 +29,24 @@ class SoftwareSpecTests < Test::Unit::TestCase @spec.owner = owner @spec.resources.each_value { |r| assert_equal owner, r.owner } end + + def test_option + @spec.option('foo') + assert @spec.build.has_option? 'foo' + end + + def test_option_raises_when_begins_with_dashes + assert_raises(RuntimeError) { @spec.option('--foo') } + end + + def test_option_raises_when_name_empty + assert_raises(RuntimeError) { @spec.option('') } + end + + def test_option_accepts_symbols + @spec.option(:foo) + assert @spec.build.has_option? 'foo' + end end class HeadSoftwareSpecTests < Test::Unit::TestCase