diff --git a/lib/compass/version.rb b/lib/compass/version.rb index 43bee2eb..13cf60c5 100644 --- a/lib/compass/version.rb +++ b/lib/compass/version.rb @@ -11,38 +11,56 @@ module Compass def version return @version if defined?(@version) + read_version_file + parse_version + + 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 } - 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 + 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 - if rev.nil? && File.exists?(scope('.git/HEAD')) + 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 - - if rev - @version[:rev] = rev - @version[:string] << " [#{rev[0...7]}]" - end - - @version - end - - def scope(file) # :nodoc: - File.join(File.dirname(__FILE__), '..', '..', file) end + end end