Sha256: 64c581e9609197145ff89de9b967e3fcebbf9c3a7ba2d389931cc47ab26535ea

Contents?: true

Size: 1.39 KB

Versions: 5

Compression:

Stored size: 1.39 KB

Contents

require 'core_ext/kernel/silence'

module GemRelease
  class VersionFile
    include GemRelease::Helpers

    VERSION_PATTERN = /(VERSION\s*=\s*(?:"|'))(\d+\.\d+\.\d+)("|')/
    NUMBER_PATTERN  = /(\d+)\.(\d+)\.(\d+)/

    attr_reader :target

    def initialize(options = {})
      @target = options[:target] || :patch
    end

    def bump!
      File.open(filename, 'w+') { |f| f.write(bumped_content) }
      require_version
    end

    def new_number
      @new_number ||= old_number.sub(NUMBER_PATTERN) do
        respond_to?(target) ? send(target, $1, $2, $3) : target
      end
    end

    def old_number
      @old_number ||= content =~ VERSION_PATTERN && $2
    end

    def filename
      File.expand_path("lib/#{gem_module_path}/version.rb")
    end

    protected

      def require_version
        $: << 'lib' unless $:.include?('lib')
        require "#{gem_module_path}/version"
      end

      def force_load!
        silence { load(filename) }
      end

      def major(major, minor, patch)
        "#{major.to_i + 1}.0.0"
      end

      def minor(major, minor, patch)
        "#{major}.#{minor.to_i + 1}.0"
      end

      def patch(major, minor, patch)
        "#{major}.#{minor}.#{patch.to_i + 1}"
      end

      def content
        @content ||= File.read(filename)
      end

      def bumped_content
        content.sub(VERSION_PATTERN) { "#{$1}#{new_number}#{$3}" }
      end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
gem-release-0.0.22 lib/gem_release/version_file.rb
gem-release-0.0.21 lib/gem_release/version_file.rb
gem-release-0.0.20 lib/gem_release/version_file.rb
gem-release-0.0.19 lib/gem_release/version_file.rb
gem-release-0.0.17 lib/gem_release/version_file.rb