This commit is contained in:
@@ -0,0 +1,163 @@
|
||||
[](https://travis-ci.org/gregorym/bump)
|
||||
[](http://badge.fury.io/rb/bump)
|
||||
|
||||
A gem to bump versions of gems and chef-cookbooks.
|
||||
|
||||
- bumps version major / minor / patch / pre
|
||||
- bundles
|
||||
- commits changes
|
||||
|
||||
# Installation
|
||||
|
||||
gem install bump
|
||||
|
||||
# Usage
|
||||
|
||||
### Show current version
|
||||
|
||||
bump current
|
||||
|
||||
> 0.1.2
|
||||
|
||||
### Bump (major, minor, patch, pre)
|
||||
|
||||
bump patch
|
||||
|
||||
> Bump version 0.1.2 to 0.1.3
|
||||
|
||||
### Show next version
|
||||
|
||||
bump show-next patch
|
||||
|
||||
> 0.1.3
|
||||
|
||||
### Show version file path
|
||||
|
||||
bump file
|
||||
|
||||
> lib/foo/version.rb
|
||||
|
||||
## Options
|
||||
|
||||
### `--no-commit`
|
||||
|
||||
Do not commit after bumping.
|
||||
|
||||
bump patch --no-commit
|
||||
|
||||
### `--tag`
|
||||
|
||||
Will add a git tag like `v1.2.3` (if the current project is a git repository and `--no-commit` has not been given).
|
||||
|
||||
bump patch --tag
|
||||
|
||||
The `--tag-prefix` option can change the tag prefix:
|
||||
|
||||
bump patch --tag --tag-prefix v- # tag as v-1.2.3
|
||||
bump patch --tag --tag-prefix "" # tag as 1.2.3
|
||||
|
||||
### `--no-bundle`
|
||||
|
||||
Do not run `bundle` command after bumping.
|
||||
|
||||
bump patch --no-bundle
|
||||
|
||||
### `--replace-in`
|
||||
|
||||
Bump the version in additional files.
|
||||
|
||||
bump patch --replace-in Readme.md
|
||||
|
||||
### `--commit-message [MSG], -m [MSG]`
|
||||
|
||||
Append additional information to the commit message.
|
||||
|
||||
bump patch --commit-message "Something extra"
|
||||
|
||||
or
|
||||
|
||||
bump patch -m "Something extra"
|
||||
|
||||
### `--changelog`
|
||||
|
||||
Updates `CHANGELOG.md` when bumping.
|
||||
This requires a heading (starting with `##`) that includes the previous version and a heading above that, for example:
|
||||
|
||||
```markdown
|
||||
### Next
|
||||
- Added bar
|
||||
|
||||
### v0.0.0 - 2019-12-24
|
||||
- Added foo
|
||||
```
|
||||
|
||||
### `--edit-changelog`
|
||||
|
||||
Updates CHANGELOG.md when bumping (see above), and
|
||||
opens the changelog in an editor specified in `$EDITOR` (or `vi`),
|
||||
then waits for the editor to be closed and continues.
|
||||
|
||||
```bash
|
||||
EDITOR="subl -n -w" bump patch --edit-changelog
|
||||
```
|
||||
|
||||
## Rake
|
||||
|
||||
```ruby
|
||||
# Rakefile
|
||||
require "bump/tasks"
|
||||
|
||||
#
|
||||
# do not always tag the version
|
||||
# Bump.tag_by_default = false
|
||||
#
|
||||
# bump the version in additional files
|
||||
# Bump.replace_in_default = ["Readme.md"]
|
||||
#
|
||||
# Maintain changelog:
|
||||
# Bump.changelog = true
|
||||
# Opens the changelog in an editor when bumping
|
||||
# Bump.changelog = :editor
|
||||
```
|
||||
|
||||
rake bump:current # display current version
|
||||
rake bump:show-next INCREMENT=minor # display next minor version
|
||||
rake bump:file # display version file path
|
||||
|
||||
# bumping using defaults for `COMMIT`, `TAG`, and `BUNDLE`
|
||||
rake bump:major
|
||||
rake bump:patch
|
||||
rake bump:minor
|
||||
rake bump:pre
|
||||
|
||||
# bumping with option(s)
|
||||
rake bump:patch TAG=false BUNDLE=false # commit, but don't tag or run `bundle`
|
||||
rake bump:patch TAG=true TAG_PREFIX=v- # tag with a prefix 'v-' ex. the tag will look like v-0.0.1
|
||||
rake bump:patch COMMIT=false TAG=false # don't commit, don't tag
|
||||
rake bump:minor BUNDLE=false # don't run `bundle`
|
||||
|
||||
## Ruby
|
||||
|
||||
```ruby
|
||||
require "bump"
|
||||
Bump::Bump.current # -> "1.2.3"
|
||||
Bump::Bump.next_version("patch") # -> "1.2.4"
|
||||
Bump::Bump.file # -> "lib/foo/version.rb"
|
||||
Bump::Bump.run("patch") # -> version changed
|
||||
Bump::Bump.run("patch", tag: true, tag_prefix: 'v-') # -> version changed with tagging with '-v' as prefix
|
||||
Bump::Bump.run("patch", commit: false, bundle:false, tag:false) # -> version changed with options
|
||||
Bump::Bump.run("patch", commit_message: '[no ci]') # -> creates a commit message with 'v1.2.3 [no ci]' instead of default: 'v1.2.3'
|
||||
```
|
||||
|
||||
# Supported locations
|
||||
|
||||
- `VERSION` file with `1.2.3`
|
||||
- `gemspec` with `gem.version = "1.2.3"` or `Gem:Specification.new "gem-name", "1.2.3" do`
|
||||
- `lib/**/version.rb` file with `VERSION = "1.2.3"`
|
||||
- `metadata.rb` with `version "1.2.3"`
|
||||
- `VERSION = "1.2.3"` in `lib/**/*.rb`
|
||||
|
||||
# Author
|
||||
|
||||
Gregory<br>
|
||||
License: MIT
|
||||
Executable
+45
@@ -0,0 +1,45 @@
|
||||
#!/usr/bin/env ruby
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'optparse'
|
||||
require 'shellwords'
|
||||
|
||||
options = {}
|
||||
OptionParser.new do |opts|
|
||||
opts.banner = <<-BANNER.gsub(/^ /, "")
|
||||
Bump your gem version.
|
||||
|
||||
Usage:
|
||||
bump current # show current version
|
||||
bump show-next INCREMENT # show next (pre|patch|minor|major) version of your gem
|
||||
bump file # show version file path
|
||||
bump pre # increase prerelease version of your gem (1.0.0-X) [alpha, beta, rc, ]
|
||||
bump patch # increase patch version of your gem (1.0.X)
|
||||
bump minor # increase minor version of your gem (1.X.0)
|
||||
bump major # increase major version of your gem (X.0.0)
|
||||
bump set 1.2.3 # set the version number to the given value
|
||||
|
||||
Options:
|
||||
BANNER
|
||||
opts.on("--no-commit", "Do not make a commit.") { options[:commit] = false }
|
||||
opts.on("-m", "--commit-message MSG", String, "Append MSG to the commit message.") { |msg| options[:commit_message] = msg }
|
||||
opts.on("--no-bundle", "Do not bundle.") { options[:bundle] = false }
|
||||
opts.on("--tag", "Create git tag from version (only if commit is true).") { options[:tag] = true }
|
||||
opts.on("--tag-prefix TAG_PREFIX", "Prefix the tag with this string, ex. 'v'") { |tag_prefix| options[:tag_prefix] = tag_prefix }
|
||||
opts.on("--replace-in FILE", String, "Replace old version with the new version additionally in this file") { |f| (options[:replace_in] ||= []) << f }
|
||||
opts.on("--changelog", "Update CHANGELOG.md") { options[:changelog] = true }
|
||||
opts.on("--edit-changelog", "Use $EDITOR to open changelog before committing, e.g. 'subl -n -w' or 'nano'.") { options[:changelog] = :editor }
|
||||
opts.on("--value-only", "Do not prefix the output with any descriptive text") { options[:value_only] = true }
|
||||
opts.on("-h", "--help", "Show this.") { puts opts; exit }
|
||||
end.parse!
|
||||
|
||||
valid_argv = ["set", "show-next"].include?(ARGV.first) ? 2 : 1
|
||||
abort "Usage instructions: bump --help" unless ARGV.size == valid_argv
|
||||
|
||||
options[:version] = ARGV[1] if ARGV[0] == "set"
|
||||
options[:increment] = ARGV[1] if ARGV[0] == "show-next"
|
||||
|
||||
require File.dirname(__FILE__) + '/../lib/bump'
|
||||
output, status = Bump::Bump.run(ARGV.first, options)
|
||||
puts output
|
||||
exit status
|
||||
@@ -0,0 +1,312 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Bump
|
||||
class InvalidIncrementError < StandardError; end
|
||||
class InvalidOptionError < StandardError; end
|
||||
class InvalidVersionError < StandardError; end
|
||||
class UnfoundVersionError < StandardError; end
|
||||
class TooManyVersionFilesError < StandardError; end
|
||||
class UnfoundVersionFileError < StandardError; end
|
||||
class RakeArgumentsDeprecatedError < StandardError; end
|
||||
|
||||
class <<self
|
||||
attr_accessor :tag_by_default, :replace_in_default, :changelog
|
||||
end
|
||||
|
||||
class Bump
|
||||
BUMPS = ["major", "minor", "patch", "pre"].freeze
|
||||
PRERELEASE = ["alpha", "beta", "rc", nil].freeze
|
||||
OPTIONS = BUMPS | ["set", "current", "file"]
|
||||
VERSION_REGEX = /(\d+\.\d+\.\d+(?:-(?:#{PRERELEASE.compact.join('|')}))?)/.freeze
|
||||
|
||||
class << self
|
||||
def defaults
|
||||
{
|
||||
tag: ::Bump.tag_by_default,
|
||||
tag_prefix: 'v',
|
||||
commit: true,
|
||||
changelog: ::Bump.changelog || false, # TODO: default to true with opt-out once it gets more stable
|
||||
bundle: File.exist?("Gemfile"),
|
||||
replace_in: ::Bump.replace_in_default || []
|
||||
}
|
||||
end
|
||||
|
||||
def run(bump, options = {})
|
||||
options = defaults.merge(options)
|
||||
options[:commit] = false unless File.directory?(".git")
|
||||
|
||||
case bump
|
||||
when *BUMPS
|
||||
bump_part(bump, options)
|
||||
when "set"
|
||||
raise InvalidVersionError unless options[:version]
|
||||
|
||||
bump_set(options[:version], options)
|
||||
when "current"
|
||||
[current, 0]
|
||||
when "show-next"
|
||||
increment = options[:increment]
|
||||
raise InvalidIncrementError unless BUMPS.include?(increment)
|
||||
|
||||
[next_version(increment), 0]
|
||||
when "file"
|
||||
[file, 0]
|
||||
else
|
||||
raise InvalidOptionError
|
||||
end
|
||||
rescue InvalidIncrementError
|
||||
["Invalid increment. Choose between #{BUMPS.join(',')}.", 1]
|
||||
rescue InvalidOptionError
|
||||
["Invalid option. Choose between #{OPTIONS.join(',')}.", 1]
|
||||
rescue InvalidVersionError
|
||||
["Invalid version number given.", 1]
|
||||
rescue UnfoundVersionError
|
||||
["Unable to find your gem version", 1]
|
||||
rescue UnfoundVersionFileError
|
||||
["Unable to find a file with the gem version", 1]
|
||||
rescue TooManyVersionFilesError
|
||||
["More than one version file found (#{$!.message})", 1]
|
||||
end
|
||||
|
||||
def current
|
||||
current_info.first
|
||||
end
|
||||
|
||||
def next_version(increment, current = Bump.current)
|
||||
current, prerelease = current.split('-')
|
||||
major, minor, patch, *other = current.split('.')
|
||||
case increment
|
||||
when "major"
|
||||
major = major.succ
|
||||
minor = 0
|
||||
patch = 0
|
||||
prerelease = nil
|
||||
when "minor"
|
||||
minor = minor.succ
|
||||
patch = 0
|
||||
prerelease = nil
|
||||
when "patch"
|
||||
patch = patch.succ
|
||||
when "pre"
|
||||
prerelease.strip! if prerelease.respond_to? :strip
|
||||
prerelease = PRERELEASE[PRERELEASE.index(prerelease).succ % PRERELEASE.length]
|
||||
else
|
||||
raise InvalidIncrementError
|
||||
end
|
||||
version = [major, minor, patch, *other].compact.join('.')
|
||||
[version, prerelease].compact.join('-')
|
||||
end
|
||||
|
||||
def file
|
||||
current_info.last
|
||||
end
|
||||
|
||||
def parse_cli_options!(options)
|
||||
options.each do |key, value|
|
||||
options[key] = parse_cli_options_value(value)
|
||||
end
|
||||
options.delete_if { |_key, value| value.nil? }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def parse_cli_options_value(value)
|
||||
case value
|
||||
when "true" then true
|
||||
when "false" then false
|
||||
when "nil" then nil
|
||||
else
|
||||
value
|
||||
end
|
||||
end
|
||||
|
||||
def bump(file, current, next_version, options)
|
||||
# bump in files that need to change
|
||||
[file, *options[:replace_in]].each do |f|
|
||||
return ["Unable to find version #{current} in #{f}", 1] unless replace f, current, next_version
|
||||
|
||||
git_add f if options[:commit]
|
||||
end
|
||||
|
||||
# bundle if needed
|
||||
if options[:bundle] && Dir.glob('*.gemspec').any? && under_version_control?("Gemfile.lock")
|
||||
bundler_with_clean_env do
|
||||
return ["Bundle error", 1] unless system("bundle")
|
||||
|
||||
git_add "Gemfile.lock" if options[:commit]
|
||||
end
|
||||
end
|
||||
|
||||
# changelog if needed
|
||||
if options[:changelog]
|
||||
log = Dir["CHANGELOG.md"].first
|
||||
return ["Did not find CHANGELOG.md", 1] unless log
|
||||
|
||||
error = bump_changelog(log, next_version)
|
||||
return [error, 1] if error
|
||||
|
||||
open_changelog(log) if options[:changelog] == :editor
|
||||
|
||||
git_add log if options[:commit]
|
||||
end
|
||||
|
||||
# commit staged changes
|
||||
commit next_version, options if options[:commit]
|
||||
|
||||
# tell user the result
|
||||
[next_version, 0]
|
||||
end
|
||||
|
||||
def open_changelog(log)
|
||||
editor = ENV['EDITOR'] || "vi"
|
||||
system "#{editor} #{log}"
|
||||
end
|
||||
|
||||
def bundler_with_clean_env(&block)
|
||||
if defined?(Bundler)
|
||||
if Bundler.respond_to?(:with_unbundled_env)
|
||||
Bundler.with_unbundled_env(&block)
|
||||
else
|
||||
Bundler.with_clean_env(&block)
|
||||
end
|
||||
else
|
||||
yield
|
||||
end
|
||||
end
|
||||
|
||||
def bump_part(increment, options)
|
||||
current, file = current_info
|
||||
next_version = next_version(increment, current)
|
||||
bump(file, current, next_version, options)
|
||||
end
|
||||
|
||||
def bump_set(next_version, options)
|
||||
current, file = current_info
|
||||
bump(file, current, next_version, options)
|
||||
end
|
||||
|
||||
def bump_changelog(file, current)
|
||||
parts = File.read(file).split(/(^##+.*)/) # headlines and their content
|
||||
prev_index = parts.index { |p| p =~ /(^##+.*(\d+\.\d+\.\d+(\.[a-z]+)?).*)/ } # position of previous version
|
||||
return "Unable to find previous version in CHANGELOG.md" unless prev_index
|
||||
|
||||
# reuse the same style by just swapping the numbers
|
||||
new_heading = parts[prev_index].sub($2, current)
|
||||
# add current date if previous heading used that
|
||||
new_heading.sub!(/\d\d\d\d-\d\d-\d\d/, Time.now.strftime('%Y-%m-%d'))
|
||||
|
||||
if prev_index < 2
|
||||
# previous version is first '##' element (no '## Next' present), add line feed after version to avoid
|
||||
# '## v1.0.1## v1.0.0'
|
||||
parts.insert prev_index - 1, new_heading + "\n"
|
||||
else
|
||||
# put our new heading underneath the "Next" heading, which should be above the last version
|
||||
parts.insert prev_index - 1, "\n" + new_heading
|
||||
end
|
||||
|
||||
File.write file, parts.join("")
|
||||
nil
|
||||
end
|
||||
|
||||
def commit_message(version, options)
|
||||
tag = "#{options[:tag_prefix]}#{version}"
|
||||
options[:commit_message] ? "#{tag} #{options[:commit_message]}" : tag
|
||||
end
|
||||
|
||||
def commit(version, options)
|
||||
tag = "#{options[:tag_prefix]}#{version}"
|
||||
system("git", "commit", "-m", commit_message(version, options))
|
||||
system("git", "tag", "-a", "-m", "Bump to #{tag}", tag) if options[:tag]
|
||||
end
|
||||
|
||||
def git_add(file)
|
||||
system("git", "add", "--update", file)
|
||||
end
|
||||
|
||||
def replace(file, old, new)
|
||||
content = File.read(file)
|
||||
return unless content.sub!(old, new)
|
||||
|
||||
File.write(file, content)
|
||||
end
|
||||
|
||||
def current_info
|
||||
version, file = (
|
||||
version_from_version ||
|
||||
version_from_version_rb ||
|
||||
version_from_gemspec ||
|
||||
version_from_lib_rb ||
|
||||
version_from_chef ||
|
||||
raise(UnfoundVersionFileError)
|
||||
)
|
||||
raise UnfoundVersionError unless version
|
||||
|
||||
[version, file]
|
||||
end
|
||||
|
||||
def version_from_gemspec
|
||||
return unless file = find_version_file("*.gemspec")
|
||||
|
||||
content = File.read(file)
|
||||
version = (
|
||||
content[/\.version\s*=\s*["']#{VERSION_REGEX}["']/, 1] ||
|
||||
File.read(file)[/Gem::Specification.new.+ ["']#{VERSION_REGEX}["']/, 1]
|
||||
)
|
||||
return unless version
|
||||
|
||||
[version, file]
|
||||
end
|
||||
|
||||
def version_from_version_rb
|
||||
files = Dir.glob("lib/**/version.rb")
|
||||
files.detect do |file|
|
||||
if version_and_file = extract_version_from_file(file)
|
||||
return version_and_file
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def version_from_version
|
||||
return unless file = find_version_file("VERSION")
|
||||
|
||||
extract_version_from_file(file)
|
||||
end
|
||||
|
||||
def version_from_lib_rb
|
||||
files = Dir.glob("lib/**/*.rb")
|
||||
file = files.detect do |f|
|
||||
File.read(f) =~ /^\s+VERSION = ['"](#{VERSION_REGEX})['"]/i
|
||||
end
|
||||
[Regexp.last_match(1), file] if file
|
||||
end
|
||||
|
||||
def version_from_chef
|
||||
file = find_version_file("metadata.rb")
|
||||
return unless file && File.read(file) =~ /^version\s+(['"])(#{VERSION_REGEX})['"]/
|
||||
|
||||
[Regexp.last_match(2), file]
|
||||
end
|
||||
|
||||
def extract_version_from_file(file)
|
||||
return unless version = File.read(file)[VERSION_REGEX]
|
||||
|
||||
[version, file]
|
||||
end
|
||||
|
||||
def find_version_file(pattern)
|
||||
files = Dir.glob(pattern)
|
||||
case files.size
|
||||
when 0 then nil
|
||||
when 1 then files.first
|
||||
else
|
||||
raise TooManyVersionFilesError, files.join(", ")
|
||||
end
|
||||
end
|
||||
|
||||
def under_version_control?(file)
|
||||
@all_files ||= `git ls-files`.split(/\r?\n/)
|
||||
@all_files.include?(file)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,45 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "bump"
|
||||
|
||||
namespace :bump do
|
||||
run_bump = lambda do |bump, options|
|
||||
output, status = Bump::Bump.run(bump, options)
|
||||
puts output
|
||||
abort unless status == 0
|
||||
end
|
||||
|
||||
(Bump::Bump::BUMPS + ["current", "file", "show-next"]).each do |bump|
|
||||
if bump == "current"
|
||||
desc "Show current gem version"
|
||||
elsif bump == "show-next"
|
||||
desc "Show next #{Bump::Bump::BUMPS.join('|')} version."
|
||||
elsif bump == "file"
|
||||
desc "Show version file path"
|
||||
else
|
||||
desc "Bump #{bump} part of gem version"
|
||||
end
|
||||
|
||||
task bump, :no_args do |_task, args|
|
||||
if args.no_args
|
||||
raise(
|
||||
RakeArgumentsDeprecatedError,
|
||||
"rake arguments are deprecated, use TAG=false to disable tagging"
|
||||
)
|
||||
end
|
||||
options = {
|
||||
tag: ENV['TAG'],
|
||||
tag_prefix: ENV['TAG_PREFIX'],
|
||||
commit: ENV['COMMIT'],
|
||||
bundle: ENV['BUNDLE'],
|
||||
increment: ENV['INCREMENT']
|
||||
}
|
||||
run_bump.call(bump, Bump::Bump.parse_cli_options!(options))
|
||||
end
|
||||
end
|
||||
|
||||
desc "Sets the version number using the VERSION environment variable"
|
||||
task :set do
|
||||
run_bump.call("set", version: ENV['VERSION'])
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user