Add option to the DSL

Closes Homebrew/homebrew#9982
This commit is contained in:
Adam Vandenberg 2012-07-30 11:32:56 -07:00
parent 0df4c6a703
commit d1d52b3467
3 changed files with 101 additions and 5 deletions

View File

@ -19,9 +19,9 @@ end
module Homebrew extend self module Homebrew extend self
def options def options
ff.each do |f| ff.each do |f|
next if f.options.empty? next if f.build.empty?
if ARGV.include? '--compact' if ARGV.include? '--compact'
puts f.options.collect {|o| o[0]} * " " puts f.build.collect {|k,v| k} * " "
else else
puts f.name if ff.length > 1 puts f.name if ff.length > 1
dump_options_for_formula f dump_options_for_formula f
@ -31,9 +31,9 @@ module Homebrew extend self
end end
def dump_options_for_formula f def dump_options_for_formula f
f.options.each do |o| f.options.each do |k,v|
puts o[0] puts k
puts "\t"+o[1] puts "\t"+v
end end
end end
end end

View File

@ -59,6 +59,9 @@ class Formula
# If we got an explicit path, use that, else determine from the name # If we got an explicit path, use that, else determine from the name
@path = path.nil? ? self.class.path(name) : Pathname.new(path) @path = path.nil? ? self.class.path(name) : Pathname.new(path)
@downloader = download_strategy.new(name, @active_spec) @downloader = download_strategy.new(name, @active_spec)
# Combine DSL `option` and `def options`
options.each {|o| self.class.build.add(o[0], o[1]) }
end end
# Derive specs from class ivars # Derive specs from class ivars
@ -160,6 +163,10 @@ class Formula
def plist_name; 'homebrew.mxcl.'+name end def plist_name; 'homebrew.mxcl.'+name end
def plist_path; prefix+(plist_name+'.plist') end def plist_path; prefix+(plist_name+'.plist') end
def build
self.class.build
end
# Use the @active_spec to detect the download strategy. # Use the @active_spec to detect the download strategy.
# Can be overriden to force a custom download strategy # Can be overriden to force a custom download strategy
def download_strategy def download_strategy
@ -579,6 +586,10 @@ private
} }
end end
def build
@build ||= BuildOptions.new(ARGV)
end
def url val=nil, specs=nil def url val=nil, specs=nil
if val.nil? if val.nil?
return @stable.url if @stable return @stable.url if @stable
@ -630,6 +641,14 @@ private
dependencies.add(dep) dependencies.add(dep)
end 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
end
def conflicts_with formula, opts={} def conflicts_with formula, opts={}
message = <<-EOS.undent message = <<-EOS.undent
#{formula} cannot be installed alongside #{name.downcase}. #{formula} cannot be installed alongside #{name.downcase}.

View File

@ -151,3 +151,80 @@ EOS
end end
end end
end end
# This class holds the build-time options defined for a Formula,
# and provides named access to those options during install.
class BuildOptions
def initialize args
# Take a copy of the args (any string array, actually)
@args = Array.new(args)
# Extend it into an ARGV extension
@args.extend(HomebrewArgvExtension)
@options = []
end
def add name, description=nil
if description.nil?
case name
when :universal, "universal"
description = "Build a universal binary."
when "32-bit"
description = "Build 32-bit only."
else
description = ""
end
end
@options << [name, description]
end
def has_option? name
@options.any? {|o| o[0] == name}
end
def empty?
@options.empty?
end
def collect
@options.collect {|o| yield o[0], o[1]}
end
def each
@options.each {|o| yield o[0], o[1]}
end
def include? name
@args.include? '--' + name
end
def using? name
@args.include? '--' + name
end
def head?
@args.flag? '--HEAD'
end
def devel?
@args.include? '--devel'
end
def stable?
not (head? or devel?)
end
# True if the user requested a universal build.
def universal?
@args.include? '--universal'
end
# Request a 32-bit only build.
# This is needed for some use-cases though we prefer to build Universal
# when a 32-bit version is needed.
def build_32_bit?
@args.include? '--32-bit'
end
end