Class: ParanoidVersioning

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/paranoid_versioning.rb

Constant Summary collapse

APP_VERSION =
ParanoidVersioning.load "#{(Rails.root.to_s)}/config/version.yml"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = nil) ⇒ ParanoidVersioning

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”



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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

Instance Attribute Details

#branchObject

Returns the value of attribute branch.



6
7
8
# File 'lib/paranoid_versioning.rb', line 6

def branch
  @branch
end

#buildObject

Returns the value of attribute build.



6
7
8
# File 'lib/paranoid_versioning.rb', line 6

def build
  @build
end

#build_dateObject

Returns the value of attribute build_date.



6
7
8
# File 'lib/paranoid_versioning.rb', line 6

def build_date
  @build_date
end

#commiterObject

Returns the value of attribute commiter.



6
7
8
# File 'lib/paranoid_versioning.rb', line 6

def commiter
  @commiter
end

#formatObject

Returns the value of attribute format.



6
7
8
# File 'lib/paranoid_versioning.rb', line 6

def format
  @format
end

#majorObject

Returns the value of attribute major.



6
7
8
# File 'lib/paranoid_versioning.rb', line 6

def major
  @major
end

#milestoneObject

Returns the value of attribute milestone.



6
7
8
# File 'lib/paranoid_versioning.rb', line 6

def milestone
  @milestone
end

#minorObject

Returns the value of attribute minor.



6
7
8
# File 'lib/paranoid_versioning.rb', line 6

def minor
  @minor
end

#patchObject

Returns the value of attribute patch.



6
7
8
# File 'lib/paranoid_versioning.rb', line 6

def patch
  @patch
end

Class Method Details

.get_versionObject



142
143
144
# File 'lib/paranoid_versioning.rb', line 142

def self.get_version
  ParanoidVersioning.load "#{(Rails.root.to_s)}/config/version.yml"
end

.load(path) ⇒ Object

Loads the version information from a YAML file



87
88
89
90
91
92
93
94
95
96
97
# 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

.parse(version) ⇒ Object

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

Raises:

  • (ArgumentError)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# 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

Instance Method Details

#get_branch_name_from_gitObject



132
133
134
135
136
# 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_gitObject



126
127
128
129
130
# 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_gitObject



114
115
116
117
118
# 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_gitObject



120
121
122
123
124
# 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_sObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# 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