Refactor class heirarchy

This commit is contained in:
Max Howell 2009-06-05 23:39:56 +01:00
parent 0ab4b3bf12
commit b3b14a7e0a
2 changed files with 96 additions and 66 deletions

View File

@ -45,7 +45,7 @@ end
def appsupport def appsupport
appsupport = File.expand_path "~/Library/Application Support/Homebrew" appsupport = File.expand_path "~/Library/Application Support/Homebrew"
FileUtils.mkpath appsupport unless File.exist? appsupport FileUtils.mkpath appsupport
return appsupport return appsupport
end end
@ -65,11 +65,14 @@ class Pathname
end 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 'find'
require 'fileutils' require 'fileutils'
# fuck knows, ruby is weird # fuck knows, ruby is weird
# TODO please fix!
def self.url def self.url
@url @url
end end
@ -90,26 +93,38 @@ class Formula
end end
# end ruby is weird section # 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 # fuck knows, ruby is weird
@url=url if @url.nil? @url=url if @url.nil?
raise "@url.nil?" if @url.nil? raise "@url.nil?" if @url.nil?
@md5=md5 if @md5.nil? @md5=md5 if @md5.nil?
# end ruby is weird section # end ruby is weird section
# 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 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 end
private private
@ -140,22 +155,12 @@ public
maybe_mkpath prefix+'include' maybe_mkpath prefix+'include'
end end
def name=name
raise "Name assigned twice, I'm not letting you do that!" if @name
@name=name
end
protected
def caveats def caveats
nil nil
end end
public # yields self with current working directory set to the uncompressed tarball
#yields a Pathname object for the installation prefix
def brew def brew
# disabled until the regexp makes sense :P
#raise "@name does not validate to our regexp" unless /^\w+$/ =~ @name
ohai "Downloading #{@url}" ohai "Downloading #{@url}"
Dir.chdir appsupport do Dir.chdir appsupport do
@ -215,14 +220,15 @@ public
end end
end end
def version protected
@version def cache?
true
end end
def name def uncompress path
@name path.dirname
end end
protected private
def fetch def fetch
%r[http://(www.)?github.com/.*/(zip|tar)ball/].match @url %r[http://(www.)?github.com/.*/(zip|tar)ball/].match @url
if $2 if $2
@ -246,6 +252,25 @@ protected
return tgz return tgz
end 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) def uncompress(path)
if path.extname == '.zip' if path.extname == '.zip'
`unzip -qq "#{path}"` `unzip -qq "#{path}"`
@ -266,22 +291,18 @@ protected
Dir.pwd Dir.pwd
end end
end end
end
def cache? # this is what you will mostly use, reimplement install, prefix won't raise
true class Formula <UnidentifiedFormula
end def initialize name
super name
private extract_version File.basename(@url, extname)
def method_added(method)
raise 'You cannot override Formula.brew' if method == 'brew'
end end
end end
# see ack.rb for an example usage # see ack.rb for an example usage
class UncompressedScriptFormula <Formula class ScriptFileFormula <AbstractFormula
def uncompress path
path.dirname
end
def cache? def cache?
false false
end end
@ -291,17 +312,11 @@ class UncompressedScriptFormula <Formula
end end
end end
class GithubGistFormula <UncompressedScriptFormula class GithubGistFormula <ScriptFileFormula
def initialize def initialize
super super File.basename(url)
@name=File.basename url
@version=File.basename(File.dirname(url))[0,6] @version=File.basename(File.dirname(url))[0,6]
end end
def install
FileUtils.cp @name, bin
(bin+@name).chmod 0544
end
end end
def inreplace(path, before, after) def inreplace(path, before, after)

View File

@ -1,65 +1,80 @@
#!/usr/bin/ruby #!/usr/bin/ruby
$:.unshift File.dirname __FILE__ $:.unshift File.dirname(__FILE__)
require 'test/unit' require 'test/unit'
require 'brewkit' require 'brewkit'
class TestFormula <Formula class TestFormula <Formula
def initialize url, md5 def initialize url, md5='nomd5'
@url=url @url=url
@md5=md5 @md5=md5
@name='test' super 'test'
super()
end end
end end
class BeerTasting <Test::Unit::TestCase class BeerTasting <Test::Unit::TestCase
def test_version_all_dots 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 assert_equal '1.14', r.version
end end
def test_version_underscore_separator 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 assert_equal '1.1', r.version
end end
def test_version_underscores_all_the_way def test_boost_version_style
r=TestFormula.new "http://example.com/boost_1_39_0.tar.bz2", 'nomd5' r=TestFormula.new "http://example.com/boost_1_39_0.tar.bz2"
assert_equal '1.39.0', r.version assert_equal '1.39.0', r.version
end 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 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 assert_equal '1.1-2', r.version
end end
def test_version_single_digit 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 assert_equal '45', r.version
end end
def test_noseparator_single_digit 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 assert_equal '45', r.version
end end
def test_version_developer_that_hates_us_format 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 assert_equal '1.2.3', r.version
end end
def test_version_regular 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 assert_equal '1.21', r.version
end end
def test_yet_another_version 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 assert_equal '0.15.1b', r.version
end 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 def test_prefix
url='http://www.methylblue.com/test-0.1.tar.gz' url='http://www.methylblue.com/test-0.1.tar.gz'
md5='d496ea538a21dc4bb8524a8888baf88c' md5='d496ea538a21dc4bb8524a8888baf88c'