Better version extraction and more flexible funcs

This commit is contained in:
Max Howell 2009-06-08 11:42:28 +01:00
parent b3b14a7e0a
commit cbbc7b0f0f
3 changed files with 53 additions and 39 deletions

View File

@ -49,6 +49,29 @@ def appsupport
return appsupport return appsupport
end end
class BuildError <RuntimeError
def initialize cmd
super "Build failed during: #{cmd}"
end
end
# 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 $1 if $1
# eg. boost_1_39_0
/^[^0-9]*((\d+_)*\d+)$/.match basename
return $1.gsub('_', '.') if $1
# eg. (erlang) otp_src_R13B
/^.*[-_.](.*)$/.match basename
return $1 if $1
end
# make our code neater # make our code neater
class Pathname class Pathname
@ -62,6 +85,17 @@ class Pathname
FileUtils.cp_r to_s, dst FileUtils.cp_r to_s, dst
end end
end end
def extname
/\.(zip|tar\.(gz|bz2)|tgz)$/.match to_s
return ".#{$1}" if $1
return File.extname(to_s)
end
# for files we support, basename without extension
def stem
return File.basename(to_s, extname)
end
end end
@ -109,24 +143,6 @@ class AbstractFormula
# end ruby is weird section # end ruby is weird section
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
private private
def maybe_mkpath path def maybe_mkpath path
path.mkpath unless path.exist? path.mkpath unless path.exist?
@ -265,12 +281,6 @@ class UnidentifiedFormula <AbstractFormula
end end
private 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}"`
@ -297,7 +307,7 @@ end
class Formula <UnidentifiedFormula class Formula <UnidentifiedFormula
def initialize name def initialize name
super name super name
extract_version File.basename(@url, extname) @version=extract_version Pathname.new(File.basename(@url)).stem
end end
end end
@ -338,14 +348,17 @@ def system cmd
IO.popen("#{cmd} 2>&1") do |f| IO.popen("#{cmd} 2>&1") do |f|
until f.eof? until f.eof?
s=f.gets s=f.gets
if ARGV.include? '--verbose'
puts s
else
out+=s out+=s
puts s if ARGV.include? '--verbose' end
end end
end end
unless $? == 0 unless $? == 0
puts out unless ARGV.include? '--verbose' #already did that above puts out unless ARGV.include? '--verbose' #already did that above
raise "Failure during: #{cmd}" raise BuildError.new(cmd)
end end
end end

View File

@ -71,8 +71,6 @@ class BeerTasting <Test::Unit::TestCase
TestFormula.new 'test-0.1.tgz' TestFormula.new 'test-0.1.tgz'
TestFormula.new 'test-0.1.zip' TestFormula.new 'test-0.1.zip'
end end
assert_raise(RuntimeError) {TestFormula.new 'test-0.1.7'}
assert_raise(RuntimeError) {TestFormula.new 'test-0.1.arse'}
end end
def test_prefix def test_prefix

View File

@ -235,19 +235,20 @@ begin
shift_formulae_from_ARGV.each {|name| rm name} shift_formulae_from_ARGV.each {|name| rm name}
when 'mk' when 'mk'
require 'brewkit' require 'brewkit'
class Template <Formula url=ARGV.shift
@url=ARGV.shift version=extract_version File.basename(url, Pathname.new(url).extname)
end
t=Template.new /(.*?)[-_.]?#{version}/.match File.basename(url)
/(.*)[-_.]#{t.version}/.match File.basename(t.url) raise "Couldn't parse name from #{url}" if $1.nil?
t.name = $1
f=File.new $formula+(t.name+'.rb'), 'w' path=$formula+($1+'.rb')
raise "#{path} already exists!" if File.exist? path
f=File.new path, 'w'
f.puts "require 'brewkit'" f.puts "require 'brewkit'"
f.puts f.puts
f.puts "class #{__class t.name} <Formula" f.puts "class #{__class $1} <Formula"
f.puts " @url=#{t.url}" f.puts " @url='#{url}'"
f.puts " @md5=''" f.puts " @md5=''"
f.puts " @homepage=''" f.puts " @homepage=''"
f.puts f.puts
@ -257,6 +258,8 @@ begin
f.puts " end" f.puts " end"
f.puts "end" f.puts "end"
f.close f.close
puts path
when 'info' when 'info'
o=__obj shift_formulae_from_ARGV[0] o=__obj shift_formulae_from_ARGV[0]
puts "#{o.name} #{o.version}" puts "#{o.name} #{o.version}"