Module: FalkorLib::Versioning

Defined in:
lib/falkorlib/versioning.rb

Overview

Semantic Versioning Management

See Also:

  • falkorlib/tasks/versioningfalkorlib/tasks/versioning.rake

Defined Under Namespace

Modules: Gem, Puppet

Class Method Summary collapse

Class Method Details

.bump(oldversion, level) ⇒ Object

Return a new version number based on

Parameters:

  • oldversion

    the old version (format: x.y.z)

  • level

    the level of bumping (either :major, :minor, :patch)



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/falkorlib/versioning.rb', line 164

def bump(oldversion, level)
  major = minor = patch = 0
  if oldversion =~ /^(\d+)\.(\d+)\.(\d+)$/
    major = Regexp.last_match(1).to_i
    minor = Regexp.last_match(2).to_i
    patch = Regexp.last_match(3).to_i
  end
  case level.to_sym
  when :major
    major += 1
    minor = 0
    patch = 0
  when :minor
    minor += 1
    patch = 0
  when :patch
    patch += 1
  end
  version = [major, minor, patch].compact.join('.')
  version
end

.get_version(path = Dir.pwd, options = {}) ⇒ Object

Get the current version Supported options:

  • :default [string] default version

  • :type in [‘file’,‘gem’,‘puppet_module’] type of versionning mechanism

  • :source [Hash] information on the way to retrieve the information



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/falkorlib/versioning.rb', line 77

def get_version(path = Dir.pwd, options = {})
  rootdir = normalized_path(path)
  version = (options[:default]) ? options[:default] : FalkorLib.config[:versioning][:default]
  type    = (options[:type])    ? options[:type]    : FalkorLib.config[:versioning][:type]
  source  = (options[:source])  ? options[:source]  : FalkorLib.config[:versioning][:source][ type ]
  puts "type = '#{type}'"
  case type
  when 'file'
    versionfile = File.join( rootdir, source[:filename] )
    puts "versionfile = '#{versionfile}'"
    version = File.read( versionfile ).chomp if File.exist?( versionfile )
  when 'gem'
    getmethod = source[:getmethod ]
    version = eval( getmethod ) unless (getmethod.nil? || getmethod.empty?)
  when 'puppet_module'
    jsonfile = File.join( rootdir, source[:filename] )
     = JSON.parse( IO.read( jsonfile ) )
    version  = ["version"]
  end
  version
end

.major(version) ⇒ Object

extract the major part of the version



51
52
53
54
55
# File 'lib/falkorlib/versioning.rb', line 51

def major(version)
  res = 0
  res = Regexp.last_match(1) if version =~ /^\s*(\d+)\.\d+\.\d+/
  res
end

.minor(version) ⇒ Object

extract the minor part of the version



58
59
60
61
62
# File 'lib/falkorlib/versioning.rb', line 58

def minor(version)
  res = 0
  res = Regexp.last_match(1) if version =~ /^\s*\d+\.(\d+)\.\d+/
  res
end

.patch(version) ⇒ Object

extract the patch part of the version



65
66
67
68
69
# File 'lib/falkorlib/versioning.rb', line 65

def patch(version)
  res = 0
  res = Regexp.last_match(1) if version =~ /^\s*\d+\.\d+\.(\d+)/
  res
end

.set_version(version, rootdir = Dir.pwd, options = {}) ⇒ Object

Set the version Supported options:

  • :type in [‘file’,‘gem’,‘puppet_module’] type of versionning mechanism

  • :source [Hash] information on the way to retrieve the information



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/falkorlib/versioning.rb', line 104

def set_version(version, rootdir = Dir.pwd, options = {})
  exit_status = 0
  type    = (options[:type])    ? options[:type]    : FalkorLib.config[:versioning][:type]
  source  = (options[:source])  ? options[:source]  : FalkorLib.config[:versioning][:source][ type ]
  versionfile = File.join( rootdir, source[:filename] ) unless source[:filename].nil?
  major, minor, patch = major(version), minor(version), patch(version)
  #tocommit = ""
  case type
  when 'file'
    info "writing version changes in #{source[:filename]}"
    File.open(versionfile, 'w') { |f| f.puts version } #if File.exist? ( versionfile )
  when 'gem'
    info "=> writing version changes in #{source[:filename]}"
    File.open(versionfile, 'r+') do |f|
      text = f.read
      text.gsub!(/^(\s*)MAJOR\s*,\s*MINOR,\s*PATCH\s*=\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)(.*)$/,
                 '\1' + "MAJOR, MINOR, PATCH = #{major}, #{minor}, #{patch}" + '\5')
      f.rewind
      f.write(text)
    end
  when 'puppet_module'
    info "=> writing version changes in #{source[:filename]}"
     = JSON.parse( IO.read( versionfile ) )
    ["version"] = version
    File.open(versionfile, "w") do |f|
      f.write JSON.pretty_generate(  )
    end
    #exit 1
  end
  if FalkorLib::Git.init?(rootdir)
    filelist = FalkorLib::Git.list_files( rootdir )
    Dir.chdir( rootdir ) do
      next if source[:filename].nil?
      unless filelist.include?( source[:filename] )
        warning "The version file #{source[:filename]} is not part of the Git repository"
        answer = ask("Adding the file to the repository? (Y|n)", 'Yes')
        next if answer =~ /n.*/i
        exit_status = FalkorLib::Git.add(versionfile, "Adding the version file '#{source[:filename]}', inialized to the '#{version}' version" )
        next
      end
      run %( git diff #{source[:filename]} )
      answer = ask(cyan("=> Commit the changes of the version file to the repository? (Y|n)"), 'Yes')
      next if answer =~ /n.*/i
      run %( git commit -s -m "bump to version '#{version}'" #{source[:filename]} )
      exit_status = $?.to_i
      # if (type == 'gem' && File.exists?(File.join(rootdir, 'Gemfile')) )
      #     run %{
      #        sleep 2
      #        bundle update falkorlib
      #        git commit -s -m "Update Gemfile.lock accordingly" Gemfile.lock
      #     } if command?( 'bundle' )
      # end
    end
  end
  exit_status
end