Establish a convention for Requirement names

The name attribute of requirements is used when generating options for
the :optional and :recommended dependency tags.

Unless otherwise specified, the name attribute of a Requirement will be
populated by stripping any module prefixes from the beginning and
"Dependency" or "Requirement" from end of the class name and downcasing
the result.

Closes Homebrew/homebrew#17759.
This commit is contained in:
Jack Nagel 2013-02-12 16:24:30 -06:00
parent d71c8beac9
commit 71f85300b4
4 changed files with 22 additions and 6 deletions

View File

@ -85,9 +85,9 @@ private
Dependency.new(spec.to_s, tag) Dependency.new(spec.to_s, tag)
when :x11 then X11Dependency.new(spec.to_s, tag) when :x11 then X11Dependency.new(spec.to_s, tag)
when :xcode then XcodeDependency.new(tag) when :xcode then XcodeDependency.new(tag)
when :mysql then MysqlInstalled.new(tag) when :mysql then MysqlDependency.new(tag)
when :postgresql then PostgresqlInstalled.new(tag) when :postgresql then PostgresqlDependency.new(tag)
when :tex then TeXInstalled.new(tag) when :tex then TeXDependency.new(tag)
when :clt then CLTDependency.new(tag) when :clt then CLTDependency.new(tag)
else else
raise "Unsupported special dependency #{spec}" raise "Unsupported special dependency #{spec}"

View File

@ -13,6 +13,7 @@ class Requirement
def initialize(*tags) def initialize(*tags)
@tags = tags.flatten.compact @tags = tags.flatten.compact
@tags << :build if self.class.build @tags << :build if self.class.build
@name ||= infer_name
end end
# The message to show when the requirement is not met. # The message to show when the requirement is not met.
@ -55,6 +56,13 @@ class Requirement
private private
def infer_name
klass = self.class.to_s
klass.sub!(/(Dependency|Requirement)$/, '')
klass.sub!(/^(\w+::){0,}/, '')
klass.downcase
end
def infer_env_modification(o) def infer_env_modification(o)
case o case o
when Pathname when Pathname

View File

@ -253,7 +253,7 @@ class XcodeDependency < Requirement
end end
end end
class MysqlInstalled < Requirement class MysqlDependency < Requirement
fatal true fatal true
satisfy { which 'mysql_config' } satisfy { which 'mysql_config' }
@ -274,7 +274,7 @@ class MysqlInstalled < Requirement
end end
end end
class PostgresqlInstalled < Requirement class PostgresqlDependency < Requirement
fatal true fatal true
satisfy { which 'pg_config' } satisfy { which 'pg_config' }
@ -292,7 +292,7 @@ class PostgresqlInstalled < Requirement
end end
end end
class TeXInstalled < Requirement class TeXDependency < Requirement
fatal true fatal true
satisfy { which('tex') || which('latex') } satisfy { which('tex') || which('latex') }

View File

@ -92,4 +92,12 @@ class RequirementTests < Test::Unit::TestCase
req = Class.new(Requirement) { build true }.new req = Class.new(Requirement) { build true }.new
assert req.build? assert req.build?
end end
def test_infer_name_from_class
klass, const = self.class, :FooRequirement
klass.const_set(const, Class.new(Requirement))
assert_equal "foo", klass.const_get(const).new.name
ensure
klass.send(:remove_const, const) if klass.const_defined?(const)
end
end end