version.rb 3.5 KB

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