class ParanoidVersioning

Constants

APP_VERSION

Attributes

branch[RW]
build[RW]
build_date[RW]
commiter[RW]
format[RW]
major[RW]
milestone[RW]
minor[RW]
patch[RW]

Public Class Methods

get_version() click to toggle source
# File lib/paranoid_versioning.rb, line 142
def self.get_version
  ParanoidVersioning.load "#{(Rails.root.to_s)}/config/version.yml"
end
load(path) click to toggle source

Loads the version information from a YAML file

# File lib/paranoid_versioning.rb, line 87
def self.load(path)
  if File.exist?(path)
    ParanoidVersioning.new YAML::load(File.open(path))
  else 
    recipe = { "major" => 1, "minor" => 0 }
    template_yml = File.read(File.join(File.dirname(__FILE__), 'templates/version.yml'))
    File.open(path, "w+") { |f| f.write template_yml } #Store
    File.open(path, "a") { |f| f << recipe.to_yaml } #Store
    ParanoidVersioning.new YAML::load(File.open(path))
  end
end
new(args = nil) click to toggle source

Creates a new instance of the version class using information in the passed Hash to construct the version number

ParanoidVersioning.new(:major => 1, :minor => 0) #=> “1.0”

# File lib/paranoid_versioning.rb, line 18
def initialize(args = nil)
  if args && args.is_a?(Hash) 
    args.keys.reject { |key| key.is_a?(Symbol) }.each{ |key| args[key.to_sym] = args.delete(key) }

    [:major, :minor].each do |param|
      raise ArgumentError.new("The #{param.to_s} parameter is required") if args[param].nil?
    end
  end

  @major     = args[:major].to_s
  @minor     = args[:minor].to_s
  @patch     = args[:patch].to_s unless args[:patch].nil?
  @milestone = args[:milestone].to_s unless args[:milestone].nil?
  @branch    = args[:branch].to_s unless args[:branch].nil?
  @commiter  = args[:commiter].to_s unless args[:commiter].nil?
  @format    = args[:format].to_s unless args[:format].nil?

  unless args[:build_date].nil?
    get_date = case args[:build_date]
                      when 'git-revdate', ''
                        get_revdate_from_git
                      else 
                        args[:build_date].to_s
                      end
    @build_date = Date.parse(get_date)
  end

  unless args[:branch].nil?
    @branch = get_branch_name_from_git
  end

  @build = case args[:build]
            when 'git-revcount'
              get_revcount_from_git
            when 'git-hash'
              get_hash_from_git
            when nil, ''
              unless args[:build].nil?
                args.delete(:build)
              end
            else 
              args[:build].to_s
            end
end
parse(version) click to toggle source

Parses a version string to create an instance of the Version class

# File lib/paranoid_versioning.rb, line 64
def self.parse(version)
  m = version.match(/(\d+)\.(\d+)(?:\.(\d+))?(?:\sM(\d+))?(?:\sof\s(\w+))?(?:\sby\s(\w+))?(?:\son\s(\S+))?/)

  raise ArgumentError.new("The version '#{version}' is unparsable") if m.nil?

  version = ParanoidVersioning.new :major     => m[1],
                           :minor     => m[2],
                           :patch     => m[3],
                           :milestone => m[4],
                           :build     => m[5],
                           :branch    => m[6],
                           :commiter  => m[7]

  if (m[8] && m[8] != '')
    date = Date.parse(m[8])
    version.build_date = date
  end

  return version

end

Public Instance Methods

get_branch_name_from_git() click to toggle source
# File lib/paranoid_versioning.rb, line 132
def get_branch_name_from_git
  if File.exist?(".git")
    `git rev-parse --abbrev-ref HEAD`
  end
end
get_hash_from_git() click to toggle source
# File lib/paranoid_versioning.rb, line 126
def get_hash_from_git
  if File.exist?(".git")
    `git show --pretty=format:%H`.split("\n")[0].strip[0..5]
  end
end
get_revcount_from_git() click to toggle source
# File lib/paranoid_versioning.rb, line 114
def get_revcount_from_git
  if File.exist?(".git")
    `git rev-list --count HEAD`.strip
  end
end
get_revdate_from_git() click to toggle source
# File lib/paranoid_versioning.rb, line 120
def get_revdate_from_git
  if File.exist?(".git")
    `git show --date=short --pretty=format:%cd`.split("\n")[0].strip
  end
end
to_s() click to toggle source
# File lib/paranoid_versioning.rb, line 99
def to_s
  if @format 
    str = eval(@format.to_s.inspect)
  else  
    str = "#{major}.#{minor}"
    str << ".#{patch}" unless patch.nil?
    str << ".#{milestone} " unless milestone.nil?
    str << "(#{build}) " unless build.nil?
    str << " of #{branch}" unless branch.nil?
    str << " by #{commiter} " unless commiter.nil?
    str << " on #{build_date.strftime('%d/%m/%Y')}" unless build_date.nil?
  end
  str
end