# encoding: utf-8 require 'fedux_org_stdlib/version_management/rubygem_version_file' require 'fedux_org_stdlib/rake/task' require 'fedux_org_stdlib/roles/versionable' module FeduxOrgStdlib module Rake # Version Bump Task # # @example Create new version task for bumping the major number X.0.0 # FeduxOrgStdlib::Rake::VersionBumpTask.new( # name: 'version:bump:major', # description: 'Bump major version part' # ) { |t| t.bump_version :major } # # @example Create new version task for bumping the minor number 0.X.0 # FeduxOrgStdlib::Rake::VersionBumpTask.new( # name: 'version:bump:minor', # description: 'Bump minor version part' # ) { |t| t.bump_version :minor } # # @example Create new version task for bumping the tiny number 0.0.X # FeduxOrgStdlib::Rake::VersionBumpTask.new( # name: 'version:bump:tiny', # description: 'Bump tiny version part' # ) { |t| t.bump_version :tiny } # # @see Rakefile class VersionBumpTask < Task include Roles::Versionable # @private def bump_version(type) version_update do |file| file.bump(type) end end # @private def version(new_version) fail Exception, "You need to define a version via \"VERSION=\" or \"version=\"." if new_version.blank? begin version_update do |file| file.version = new_version end rescue StandardError => e logger.fatal("Nothing has changed since your last commit or has been added to the index. Therefor nothing needs to be commited: #{e.message}.") end end private def add_to_repo(file, version) logger.info('Adding new version file to repository.') sh "git add #{file}" sh 'git add Gemfile.lock' if File.exist? 'Gemfile.lock' sh "git commit -m 'Version bump to #{version}'" end def version_update(&block) file = FeduxOrgStdlib::VersionManagement::RubygemVersionFile.read(version_file) old_version = file.version block.call(file) new_version = file.version file.write(version_file) logger.info("Updated version. It was \"#{old_version}\". It is now \"#{new_version}\".") add_to_repo(version_file, file.version) end end end end