Sha256: a6660e4dfe9cbbbb4988e284d972dc28fedd24022e32f68e0d6a68453bcbebdc

Contents?: true

Size: 1.81 KB

Versions: 22

Compression:

Stored size: 1.81 KB

Contents

module Bourdain
  module Commands

    class BumpCommand < Command
      usage :command, <<-END
        Bump a VERSION file (patch by default)
        bump [<options>] [<version-spec=major|minor|patch|x.y.z>]
      END

      def initialize argv
        super
        version_spec    = argv.shift
        version_spec    = 'patch' if version_spec.nil?
        verify! version_spec
        return unless require_versionfile!
        current_version = get_cookbook_version
        new_version     = bump_version version_spec, current_version
        set_cookbook_version new_version
        log.info "Bumped to #{new_version}."
      end


    private
      def set_cookbook_version version
        File.open('VERSION', 'w') { |f| f.write version }
      end

      def get_cookbook_version
        File.open('VERSION').read.strip
      end

      def verify! version_spec
        case version_spec
        when /major/
          :major
        when /minor/
          :minor
        when /patch/
          :patch
        when /\d+\.\d+\.\d+/
          :manual
        else
          Trollop::die 'Invalid <version-spec> provided'
        end
      end

      def bump_version version_spec, version
        type = verify! version_spec

        # I'm not really that thinky, so I stole this from knife-spork:
        #
        # https://github.com/jonlives/knife-spork/blob/master/lib/chef/knife/spork-bump.rb
        #
        if type == :manual
          version_array = version_spec.split('.')

        else
          field = case type
          when :patch ; 2
          when :minor ; 1
          when :major ; 0
          end

          version_array = version.split('.').map(&:to_i)
          version_array[field] += 1
          ((field + 1)..2).each { |i| version_array[i] = 0 }
        end

        version_array.join('.')
      end
    end

  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
bourdain-1.2.14 lib/bourdain/resources/commands/bump.rb
bourdain-1.2.13 lib/bourdain/resources/commands/bump.rb