diff options
| author | Max Howell | 2009-08-21 20:20:44 +0100 |
|---|---|---|
| committer | Max Howell | 2009-08-24 01:04:48 +0100 |
| commit | c532d11e7aab8821a4e548208d5ef09c42d77e86 (patch) | |
| tree | 57fa9e95142b31fd4c311c72fe0e2e8d5bff3808 /Library | |
| parent | b115f26870fe128294a4662f017fe28be69cf7d5 (diff) | |
| download | brew-c532d11e7aab8821a4e548208d5ef09c42d77e86.tar.bz2 | |
Refactor away AbstractFormula
We'd gotten to the stage where Formula was so lean, it was pointless to
separate it.
Diffstat (limited to 'Library')
| -rw-r--r-- | Library/Homebrew/formula.rb | 97 | ||||
| -rwxr-xr-x | Library/Homebrew/unittest.rb | 14 |
2 files changed, 53 insertions, 58 deletions
diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index 577ea0f0a..0fea6a607 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -125,18 +125,20 @@ class FormulaUnavailableError <RuntimeError end -# the base class variety of formula, you don't get a prefix, so it's not -# useful. See the derived classes for fun and games. -class AbstractFormula - def initialize noop=nil - @version=self.class.version unless @version +# Derive and define at least @url, see Library/Formula for examples +class Formula + # Homebrew determines the name + def initialize name=nil @url=self.class.url unless @url - @homepage=self.class.homepage unless @homepage - @md5=self.class.md5 unless @md5 - @sha1=self.class.sha1 unless @sha1 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 + @md5=self.class.md5 unless @md5 + @sha1=self.class.sha1 unless @sha1 end # if the dir is there, but it's empty we consider it not installed @@ -153,7 +155,7 @@ class AbstractFormula end def path - Formula.path name + self.class.path name end attr_reader :url, :version, :homepage, :name @@ -211,6 +213,31 @@ class AbstractFormula 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 # Pretty titles the command and buffers stdout/stderr # Throws if there's an error @@ -307,56 +334,17 @@ private 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 raise 'You cannot override Formula.brew' if method == 'brew' end + + class <<self + attr_reader :url, :svnurl, :version, :homepage, :md5, :sha1 + end end # see ack.rb for an example usage -class ScriptFileFormula <AbstractFormula - def initialize name=nil - super - @name=name - end +class ScriptFileFormula <Formula def install bin.install Dir['*'] end @@ -365,8 +353,7 @@ end # see flac.rb for example usage class GithubGistFormula <ScriptFileFormula def initialize name=nil - super - @name=name @version=File.basename(File.dirname(url))[0,6] + super name end end diff --git a/Library/Homebrew/unittest.rb b/Library/Homebrew/unittest.rb index f3ea7c930..4e387bcbf 100755 --- a/Library/Homebrew/unittest.rb +++ b/Library/Homebrew/unittest.rb @@ -26,7 +26,7 @@ class MockFormula <Formula end end -class MostlyAbstractFormula <AbstractFormula +class MostlyAbstractFormula <Formula @url='' end @@ -58,6 +58,10 @@ class TestBallInvalidMd5 <TestBall @md5='61aa838a9e4050d1876a295a9e62cbe6' end +class TestBadVersion <TestBall + @version="versions can't have spaces" +end + class TestBallOverrideBrew <Formula def initialize super "foo" @@ -192,6 +196,10 @@ class BeerTasting <Test::Unit::TestCase end end + def test_bad_version + assert_raises(RuntimeError) {f=TestBadVersion.new} + end + def test_install f=TestBall.new @@ -267,8 +275,8 @@ class BeerTasting <Test::Unit::TestCase end def test_abstract_formula - f=MostlyAbstractFormula.new - assert_nil f.name + f=MostlyAbstractFormula.new '' + assert_equal '', f.name assert_raises(RuntimeError) { f.prefix } nostdout { assert_raises(ExecutionError) { f.brew } } end |
