Clean up the version parsing code.

This commit is contained in:
Chris Eppstein 2009-02-07 23:43:52 -08:00
parent 29666e177c
commit 8a0d2b900c

View File

@ -11,38 +11,56 @@ module Compass
def version
return @version if defined?(@version)
@version = {
:string => File.read(scope('VERSION')).strip
}
dotted_string, label = @version[:string].split(/-/, 2)
numbers = dotted_string.split('.').map { |n| n.to_i }
@version[:major] = numbers[0]
@version[:minor] = numbers[1]
@version[:teeny] = numbers[2]
@version[:label] = label
read_version_file
parse_version
if File.exists?(scope('REVISION'))
rev = File.read(scope('REVISION')).strip
rev = nil if rev !~ /[a-f0-9]+/
end
if rev.nil? && File.exists?(scope('.git/HEAD'))
rev = File.read(scope('.git/HEAD')).strip
if rev =~ /^ref: (.*)$/
rev = File.read(scope(".git/#{$1}")).strip
end
end
if rev
@version[:rev] = rev
@version[:string] << " [#{rev[0...7]}]"
if r = revision
@version[:rev] = r
@version[:string] << " [#{r[0...7]}]"
end
@version
end
protected
def scope(file) # :nodoc:
File.join(File.dirname(__FILE__), '..', '..', file)
end
def read_version_file
@version = {
:string => File.read(scope('VERSION')).strip
}
end
def parse_version
dotted_string, @version[:label] = @version[:string].split(/-/, 2)
numbers = dotted_string.split('.').map { |n| n.to_i }
[:major, :minor, :teeny].zip(numbers).each do |attr, value|
@version[attr] = value
end
end
def revision
revision_from_git || revision_from_file
end
def revision_from_file
if File.exists?(scope('REVISION'))
rev = File.read(scope('REVISION')).strip
rev = nil if rev !~ /[a-f0-9]+/
end
end
def revision_from_git
if File.exists?(scope('.git/HEAD'))
rev = File.read(scope('.git/HEAD')).strip
if rev =~ /^ref: (.*)$/
rev = File.read(scope(".git/#{$1}")).strip
end
end
end
end
end