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)



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

def bump(oldversion, level)
    major = minor = patch = 0
    if oldversion =~ /^(\d+)\.(\d+)\.(\d+)$/
        major = $1.to_i
        minor = $2.to_i
        patch = $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('.')
end

.get_version(rootdir = 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



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/falkorlib/versioning.rb', line 75

def get_version(rootdir = Dir.pwd, options = {})
    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 ]
    case type
    when 'file'
        versionfile = File.join( rootdir, source[:filename] )
        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



49
50
51
52
53
# File 'lib/falkorlib/versioning.rb', line 49

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

.minor(version) ⇒ Object

extract the minor part of the version



56
57
58
59
60
# File 'lib/falkorlib/versioning.rb', line 56

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

.patch(version) ⇒ Object

extract the patch part of the version



63
64
65
66
67
# File 'lib/falkorlib/versioning.rb', line 63

def patch(version)
    res = 0
    res = $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



99
100
101
102
103
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
# File 'lib/falkorlib/versioning.rb', line 99

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