PageRenderTime 30ms CodeModel.GetById 15ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/scalate-jruby/src/main/resources/haml-3.0.25/lib/haml/version.rb

http://github.com/scalate/scalate
Ruby | 106 lines | 58 code | 11 blank | 37 comment | 9 complexity | ac207ad8194857f78230ed85174b7569 MD5 | raw file
  1require 'haml/util'
  2
  3module Haml
  4  # Handles Haml version-reporting.
  5  # Haml not only reports the standard three version numbers,
  6  # but its Git revision hash as well,
  7  # if it was installed from Git.
  8  module Version
  9    include Haml::Util
 10
 11    # Returns a hash representing the version of Haml.
 12    # The `:major`, `:minor`, and `:teeny` keys have their respective numbers as Fixnums.
 13    # The `:name` key has the name of the version.
 14    # The `:string` key contains a human-readable string representation of the version.
 15    # The `:number` key is the major, minor, and teeny keys separated by periods.
 16    # If Haml is checked out from Git, the `:rev` key will have the revision hash.
 17    # For example:
 18    #
 19    #     {
 20    #       :string => "2.1.0.9616393",
 21    #       :rev    => "9616393b8924ef36639c7e82aa88a51a24d16949",
 22    #       :number => "2.1.0",
 23    #       :major  => 2, :minor => 1, :teeny => 0
 24    #     }
 25    #
 26    # If a prerelease version of Haml is being used,
 27    # the `:string` and `:number` fields will reflect the full version
 28    # (e.g. `"2.2.beta.1"`), and the `:teeny` field will be `-1`.
 29    # A `:prerelease` key will contain the name of the prerelease (e.g. `"beta"`),
 30    # and a `:prerelease_number` key will contain the rerelease number.
 31    # For example:
 32    #
 33    #     {
 34    #       :string => "3.0.beta.1",
 35    #       :number => "3.0.beta.1",
 36    #       :major => 3, :minor => 0, :teeny => -1,
 37    #       :prerelease => "beta",
 38    #       :prerelease_number => 1
 39    #     }
 40    #
 41    # @return [{Symbol => String/Fixnum}] The version hash
 42    def version
 43      return @@version if defined?(@@version)
 44
 45      #data = File.read(scope('VERSION'))
 46      data = "3.0.25"
 47      numbers = data.strip.split('.').
 48        map {|n| n =~ /^[0-9]+$/ ? n.to_i : n}
 49      #data = File.read(scope('VERSION_NAME'))
 50      data = "Classy Cassidy"
 51      name = data.strip
 52      @@version = {
 53        :major => numbers[0],
 54        :minor => numbers[1],
 55        :teeny => numbers[2],
 56        :name => name
 57      }
 58
 59      if numbers[3].is_a?(String)
 60        @@version[:teeny] = -1
 61        @@version[:prerelease] = numbers[3]
 62        @@version[:prerelease_number] = numbers[4]
 63      end
 64
 65      @@version[:number] = numbers.join('.')
 66      @@version[:string] = @@version[:number].dup
 67
 68      if rev = revision_number
 69        @@version[:rev] = rev
 70        unless rev[0] == ?(
 71          @@version[:string] << "." << rev[0...7]
 72        end
 73      end
 74
 75      @@version[:string] << " (#{name})"
 76      @@version
 77    end
 78
 79    private
 80
 81    def revision_number
 82      if File.exists?(scope('REVISION'))
 83        rev = File.read(scope('REVISION')).strip
 84        return rev unless rev =~ /^([a-f0-9]+|\(.*\))$/ || rev == '(unknown)'
 85      end
 86
 87      return unless File.exists?(scope('.git/HEAD'))
 88      rev = File.read(scope('.git/HEAD')).strip
 89      return rev unless rev =~ /^ref: (.*)$/
 90
 91      ref_name = $1
 92      ref_file = scope(".git/#{ref_name}")
 93      info_file = scope(".git/info/refs")
 94      return File.read(ref_file).strip if File.exists?(ref_file)
 95      return unless File.exists?(info_file)
 96      File.open(info_file) do |f|
 97        f.each do |l|
 98          sha, ref = l.strip.split("\t", 2)
 99          next unless ref == ref_name
100          return sha
101        end
102      end
103      return nil
104    end
105  end
106end