Refactor away AbstractFormula

We'd gotten to the stage where Formula was so lean, it was pointless to
separate it.
This commit is contained in:
Max Howell 2009-08-21 20:20:44 +01:00
parent b115f26870
commit c532d11e7a
2 changed files with 53 additions and 58 deletions

View File

@ -125,18 +125,20 @@ class FormulaUnavailableError <RuntimeError
end end
# the base class variety of formula, you don't get a prefix, so it's not # Derive and define at least @url, see Library/Formula for examples
# useful. See the derived classes for fun and games. class Formula
class AbstractFormula # Homebrew determines the name
def initialize noop=nil def initialize name=nil
@version=self.class.version unless @version
@url=self.class.url unless @url @url=self.class.url unless @url
raise if @url.nil?
@name=name
raise if @name =~ /\s/
@version=self.class.version unless @version
@version=Pathname.new(@url).version unless @version
raise if @version =~ /\s/
@homepage=self.class.homepage unless @homepage @homepage=self.class.homepage unless @homepage
@md5=self.class.md5 unless @md5 @md5=self.class.md5 unless @md5
@sha1=self.class.sha1 unless @sha1 @sha1=self.class.sha1 unless @sha1
raise if @url.nil?
raise if @name =~ /\s/
raise if @version =~ /\s/
end end
# if the dir is there, but it's empty we consider it not installed # if the dir is there, but it's empty we consider it not installed
@ -153,7 +155,7 @@ class AbstractFormula
end end
def path def path
Formula.path name self.class.path name
end end
attr_reader :url, :version, :homepage, :name attr_reader :url, :version, :homepage, :name
@ -211,6 +213,31 @@ class AbstractFormula
end end
end end
# we don't have a std_autotools variant because autotools is a lot less
# consistent and the standard parameters are more memorable
# really Homebrew should determine what works inside brew() then
# we could add --disable-dependency-tracking when it will work
def std_cmake_parameters
# The None part makes cmake use the environment's CFLAGS etc. settings
"-DCMAKE_INSTALL_PREFIX='#{prefix}' -DCMAKE_BUILD_TYPE=None"
end
def self.class name
#remove invalid characters and camelcase
name.capitalize.gsub(/[-_\s]([a-zA-Z0-9])/) { $1.upcase }
end
def self.factory name
require self.path(name)
return eval(self.class(name)).new(name)
rescue LoadError
raise FormulaUnavailableError.new(name)
end
def self.path name
HOMEBREW_PREFIX+'Library'+'Formula'+"#{name.downcase}.rb"
end
protected protected
# Pretty titles the command and buffers stdout/stderr # Pretty titles the command and buffers stdout/stderr
# Throws if there's an error # Throws if there's an error
@ -307,56 +334,17 @@ private
end end
end end
class <<self
attr_reader :url, :version, :homepage, :md5, :sha1
end
end
# This is the meat. See the examples.
class Formula <AbstractFormula
def initialize name=nil
super
@name=name
@version=Pathname.new(@url).version unless @version
end
def self.class name
#remove invalid characters and camelcase
name.capitalize.gsub(/[-_\s]([a-zA-Z0-9])/) { $1.upcase }
end
def self.factory name
require self.path(name)
return eval(self.class(name)).new(name)
rescue LoadError
raise FormulaUnavailableError.new(name)
end
def self.path name
HOMEBREW_PREFIX+'Library'+'Formula'+"#{name.downcase}.rb"
end
# we don't have a std_autotools variant because autotools is a lot less
# consistent and the standard parameters are more memorable
# really Homebrew should determine what works inside brew() then
# we could add --disable-dependency-tracking when it will work
def std_cmake_parameters
# The None part makes cmake use the environment's CFLAGS etc. settings
"-DCMAKE_INSTALL_PREFIX='#{prefix}' -DCMAKE_BUILD_TYPE=None"
end
private
def method_added method def method_added method
raise 'You cannot override Formula.brew' if method == 'brew' raise 'You cannot override Formula.brew' if method == 'brew'
end end
class <<self
attr_reader :url, :svnurl, :version, :homepage, :md5, :sha1
end
end end
# see ack.rb for an example usage # see ack.rb for an example usage
class ScriptFileFormula <AbstractFormula class ScriptFileFormula <Formula
def initialize name=nil
super
@name=name
end
def install def install
bin.install Dir['*'] bin.install Dir['*']
end end
@ -365,8 +353,7 @@ end
# see flac.rb for example usage # see flac.rb for example usage
class GithubGistFormula <ScriptFileFormula class GithubGistFormula <ScriptFileFormula
def initialize name=nil def initialize name=nil
super
@name=name
@version=File.basename(File.dirname(url))[0,6] @version=File.basename(File.dirname(url))[0,6]
super name
end end
end end

View File

@ -26,7 +26,7 @@ class MockFormula <Formula
end end
end end
class MostlyAbstractFormula <AbstractFormula class MostlyAbstractFormula <Formula
@url='' @url=''
end end
@ -58,6 +58,10 @@ class TestBallInvalidMd5 <TestBall
@md5='61aa838a9e4050d1876a295a9e62cbe6' @md5='61aa838a9e4050d1876a295a9e62cbe6'
end end
class TestBadVersion <TestBall
@version="versions can't have spaces"
end
class TestBallOverrideBrew <Formula class TestBallOverrideBrew <Formula
def initialize def initialize
super "foo" super "foo"
@ -192,6 +196,10 @@ class BeerTasting <Test::Unit::TestCase
end end
end end
def test_bad_version
assert_raises(RuntimeError) {f=TestBadVersion.new}
end
def test_install def test_install
f=TestBall.new f=TestBall.new
@ -267,8 +275,8 @@ class BeerTasting <Test::Unit::TestCase
end end
def test_abstract_formula def test_abstract_formula
f=MostlyAbstractFormula.new f=MostlyAbstractFormula.new ''
assert_nil f.name assert_equal '', f.name
assert_raises(RuntimeError) { f.prefix } assert_raises(RuntimeError) { f.prefix }
nostdout { assert_raises(ExecutionError) { f.brew } } nostdout { assert_raises(ExecutionError) { f.brew } }
end end