module FalkorLib::Versioning

Semantic Versioning Management @see falkorlib/tasks/versioning.rake

Public Instance Methods

bump(oldversion, level) click to toggle source
Return a new version number based on

@param oldversion the old version (format: x.y.z) @param level the level of bumping (either :major, :minor, :patch)

# File lib/falkorlib/versioning.rb, line 163
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 = {}) click to toggle source

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

# File lib/falkorlib/versioning.rb, line 76
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] )
    metadata = JSON.parse( IO.read( jsonfile ) )
    version  = metadata["version"]
  end
  version
end
major(version) click to toggle source

extract the major part of the version

# File lib/falkorlib/versioning.rb, line 50
def major(version)
  res = 0
  res = Regexp.last_match(1) if version =~ /^\s*(\d+)\.\d+\.\d+/
  res
end
minor(version) click to toggle source

extract the minor part of the version

# File lib/falkorlib/versioning.rb, line 57
def minor(version)
  res = 0
  res = Regexp.last_match(1) if version =~ /^\s*\d+\.(\d+)\.\d+/
  res
end
patch(version) click to toggle source

extract the patch part of the version

# File lib/falkorlib/versioning.rb, line 64
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 = {}) click to toggle source

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

# File lib/falkorlib/versioning.rb, line 103
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]}"
    metadata = JSON.parse( IO.read( versionfile ) )
    metadata["version"] = version
    File.open(versionfile, "w") do |f|
      f.write JSON.pretty_generate( metadata )
    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