aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMax Howell2009-06-05 23:39:56 +0100
committerMax Howell2009-06-05 23:39:56 +0100
commit02878497790a072df8b65647c2025155c9ea8660 (patch)
treebb6f2d32e2b9249a643d00dd03e2b16f9949041f
parentf8813207aa3a76e680f2ce6866eb2322578999a5 (diff)
downloadhomebrew-02878497790a072df8b65647c2025155c9ea8660.tar.bz2
Refactor class heirarchy
-rw-r--r--Library/Formula/ack.rb2
-rw-r--r--Library/Formula/git.rb2
-rw-r--r--Library/Formula/term.rb2
-rw-r--r--Library/Homebrew/brewkit.rb119
-rwxr-xr-xLibrary/Homebrew/unittest.rb43
5 files changed, 99 insertions, 69 deletions
diff --git a/Library/Formula/ack.rb b/Library/Formula/ack.rb
index 4bab09c89..dbc8faf08 100644
--- a/Library/Formula/ack.rb
+++ b/Library/Formula/ack.rb
@@ -1,6 +1,6 @@
require 'brewkit'
-class Ack <UncompressedScriptFormula
+class Ack <ScriptFileFormula
def initialize
@version='1.88'
@url="http://ack.googlecode.com/svn/tags/#{@version}/ack"
diff --git a/Library/Formula/git.rb b/Library/Formula/git.rb
index 56cf84e95..fd82b0512 100644
--- a/Library/Formula/git.rb
+++ b/Library/Formula/git.rb
@@ -1,6 +1,6 @@
require 'brewkit'
-class GitManuals <Formula
+class GitManuals <UnidentifiedFormula
@url='http://kernel.org/pub/software/scm/git/git-manpages-1.6.3.1.tar.bz2'
@md5='971d573e8f261feb83290a59728c2b33'
end
diff --git a/Library/Formula/term.rb b/Library/Formula/term.rb
index 427d3607a..1d99f205e 100644
--- a/Library/Formula/term.rb
+++ b/Library/Formula/term.rb
@@ -1,6 +1,6 @@
require 'brewkit'
-class Term <UncompressedScriptFormula
+class Term <ScriptFileFormula
def initialize
@url='http://github.com/liyanage/macosx-shell-scripts/raw/e29f7eaa1eb13d78056dec85dc517626ab1d93e3/term'
@md5='1bbf4509a50224b27ac8c20d3fe8682e'
diff --git a/Library/Homebrew/brewkit.rb b/Library/Homebrew/brewkit.rb
index 3236a28d3..619e97cec 100644
--- a/Library/Homebrew/brewkit.rb
+++ b/Library/Homebrew/brewkit.rb
@@ -45,7 +45,7 @@ end
def appsupport
appsupport = File.expand_path "~/Library/Application Support/Homebrew"
- FileUtils.mkpath appsupport unless File.exist? appsupport
+ FileUtils.mkpath appsupport
return appsupport
end
@@ -65,11 +65,14 @@ class Pathname
end
-class Formula
+# the base class variety of formula, you don't get a prefix, so it's not really
+# useful. See the derived classes for fun and games.
+class AbstractFormula
require 'find'
require 'fileutils'
# fuck knows, ruby is weird
+ # TODO please fix!
def self.url
@url
end
@@ -90,26 +93,38 @@ class Formula
end
# end ruby is weird section
- def initialize
+ def version
+ @version
+ end
+ def name
+ @name
+ end
+
+ def initialize name=nil
+ @name=name
# fuck knows, ruby is weird
@url=url if @url.nil?
raise "@url.nil?" if @url.nil?
@md5=md5 if @md5.nil?
# end ruby is weird section
+ end
- # pls improve this version extraction crap
- filename=File.basename @url
- i=filename.index /[-_]\d/
- unless i.nil?
- /^((\d+[._])*(\d+-)?\d+[abc]?)/.match filename[i+1,1000] #1000 because rubysucks
- @version=$1
- # if there are no dots replace underscores, boost do this, the bastards!
- @version.gsub!('_', '.') unless @version.include? '.'
- else
- # no divisor or a '.' divisor, eg. dmd.1.11.zip
- /^[a-zA-Z._-]*((\d+\.)*\d+)/.match filename
- @version = $1
- end
+protected
+ # pass in the basename of the filename _without_ any file extension
+ def extract_version basename
+ # eg. foobar4.5.1
+ # eg. foobar-4.5.1
+ # eg. foobar-4.5.1b
+ /^[^0-9]*((\d+\.)*(\d+-)?\d+[abc]?)$/.match basename
+ return @version=$1 if $1
+
+ # eg. boost_1_39_0
+ /^[^0-9]*((\d+_)*\d+)$/.match basename
+ return @version=$1.gsub('_', '.') if $1
+
+ # eg. (erlang) otp_src_R13B
+ /^.*[-_.](.*)$/.match basename
+ return @version=$1 if $1
end
private
@@ -140,22 +155,12 @@ public
maybe_mkpath prefix+'include'
end
- def name=name
- raise "Name assigned twice, I'm not letting you do that!" if @name
- @name=name
- end
-
-protected
def caveats
nil
end
-public
- #yields a Pathname object for the installation prefix
+ # yields self with current working directory set to the uncompressed tarball
def brew
- # disabled until the regexp makes sense :P
- #raise "@name does not validate to our regexp" unless /^\w+$/ =~ @name
-
ohai "Downloading #{@url}"
Dir.chdir appsupport do
@@ -215,14 +220,15 @@ public
end
end
- def version
- @version
+protected
+ def cache?
+ true
end
- def name
- @name
+ def uncompress path
+ path.dirname
end
-protected
+private
def fetch
%r[http://(www.)?github.com/.*/(zip|tar)ball/].match @url
if $2
@@ -246,6 +252,25 @@ protected
return tgz
end
+ def method_added method
+ raise 'You cannot override Formula.brew' if method == 'brew'
+ end
+end
+
+# somewhat useful, it'll raise if you call prefix, but it'll unpack a tar/zip
+# for you, check the md5, and allow you to yield from brew
+class UnidentifiedFormula <AbstractFormula
+ def initialize name=nil
+ super name
+ end
+
+private
+ def extname
+ /\.(zip|tar\.(gz|bz2)|tgz)$/.match @url
+ return ".#{$1}" if $1
+ raise "Only tarballs and zips are supported by this class"
+ end
+
def uncompress(path)
if path.extname == '.zip'
`unzip -qq "#{path}"`
@@ -265,23 +290,19 @@ protected
# if there's more than one dir, then this is the build directory already
Dir.pwd
end
- end
-
- def cache?
- true
- end
+ end
+end
-private
- def method_added(method)
- raise 'You cannot override Formula.brew' if method == 'brew'
+# this is what you will mostly use, reimplement install, prefix won't raise
+class Formula <UnidentifiedFormula
+ def initialize name
+ super name
+ extract_version File.basename(@url, extname)
end
end
# see ack.rb for an example usage
-class UncompressedScriptFormula <Formula
- def uncompress path
- path.dirname
- end
+class ScriptFileFormula <AbstractFormula
def cache?
false
end
@@ -291,17 +312,11 @@ class UncompressedScriptFormula <Formula
end
end
-class GithubGistFormula <UncompressedScriptFormula
+class GithubGistFormula <ScriptFileFormula
def initialize
- super
- @name=File.basename url
+ super File.basename(url)
@version=File.basename(File.dirname(url))[0,6]
end
-
- def install
- FileUtils.cp @name, bin
- (bin+@name).chmod 0544
- end
end
def inreplace(path, before, after)
diff --git a/Library/Homebrew/unittest.rb b/Library/Homebrew/unittest.rb
index 08a95e1f3..d619b9601 100755
--- a/Library/Homebrew/unittest.rb
+++ b/Library/Homebrew/unittest.rb
@@ -1,65 +1,80 @@
#!/usr/bin/ruby
-$:.unshift File.dirname __FILE__
+$:.unshift File.dirname(__FILE__)
require 'test/unit'
require 'brewkit'
class TestFormula <Formula
- def initialize url, md5
+ def initialize url, md5='nomd5'
@url=url
@md5=md5
- @name='test'
- super()
+ super 'test'
end
end
class BeerTasting <Test::Unit::TestCase
def test_version_all_dots
- r=TestFormula.new "http://example.com/foo.bar.la.1.14.zip", 'nomd5'
+ r=TestFormula.new "http://example.com/foo.bar.la.1.14.zip"
assert_equal '1.14', r.version
end
def test_version_underscore_separator
- r=TestFormula.new "http://example.com/grc_1.1.tar.gz", 'nomd5'
+ r=TestFormula.new "http://example.com/grc_1.1.tar.gz"
assert_equal '1.1', r.version
end
- def test_version_underscores_all_the_way
- r=TestFormula.new "http://example.com/boost_1_39_0.tar.bz2", 'nomd5'
+ def test_boost_version_style
+ r=TestFormula.new "http://example.com/boost_1_39_0.tar.bz2"
assert_equal '1.39.0', r.version
end
+ def test_erlang_version_style
+ r=TestFormula.new "http://erlang.org/download/otp_src_R13B.tar.gz"
+ assert_equal 'R13B', r.version
+ end
+
def test_version_internal_dash
- r=TestFormula.new "http://example.com/foo-arse-1.1-2.tar.gz", 'nomd5'
+ r=TestFormula.new "http://example.com/foo-arse-1.1-2.tar.gz"
assert_equal '1.1-2', r.version
end
def test_version_single_digit
- r=TestFormula.new "http://example.com/foo_bar.45.tar.gz", 'nomd5'
+ r=TestFormula.new "http://example.com/foo_bar.45.tar.gz"
assert_equal '45', r.version
end
def test_noseparator_single_digit
- r=TestFormula.new "http://example.com/foo_bar45.tar.gz", 'nomd5'
+ r=TestFormula.new "http://example.com/foo_bar45.tar.gz"
assert_equal '45', r.version
end
def test_version_developer_that_hates_us_format
- r=TestFormula.new "http://example.com/foo-bar-la.1.2.3.tar.gz", 'nomd5'
+ r=TestFormula.new "http://example.com/foo-bar-la.1.2.3.tar.gz"
assert_equal '1.2.3', r.version
end
def test_version_regular
- r=TestFormula.new "http://example.com/foo_bar-1.21.tar.gz", 'nomd5'
+ r=TestFormula.new "http://example.com/foo_bar-1.21.tar.gz"
assert_equal '1.21', r.version
end
def test_yet_another_version
- r=TestFormula.new "http://example.com/mad-0.15.1b.tar.gz", 'nomd5'
+ r=TestFormula.new "http://example.com/mad-0.15.1b.tar.gz"
assert_equal '0.15.1b', r.version
end
+ def test_supported_compressed_types
+ assert_nothing_raised do
+ TestFormula.new 'test-0.1.tar.gz'
+ TestFormula.new 'test-0.1.tar.bz2'
+ TestFormula.new 'test-0.1.tgz'
+ TestFormula.new 'test-0.1.zip'
+ end
+ assert_raise(RuntimeError) {TestFormula.new 'test-0.1.7'}
+ assert_raise(RuntimeError) {TestFormula.new 'test-0.1.arse'}
+ end
+
def test_prefix
url='http://www.methylblue.com/test-0.1.tar.gz'
md5='d496ea538a21dc4bb8524a8888baf88c'