Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for SemVer extensions #18

Draft
wants to merge 4 commits into
base: feature/1.1.0-release
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<img src="https://user-images.githubusercontent.com/5007/160249855-dc0eb32f-f77d-4c5a-a995-93ac46408a68.png" alt="incr" />
</a>
<br />
incr is a tool to help you easily increment the version number of your NPM or Mix packages.
incr is designed to streamline the process of incrementing the version number of your NPM or Mix packages.
<br /><br />
<a href="https://rubygems.org/gems/incr"><img src="http://img.shields.io/gem/v/incr.svg" /></a>
</p>
Expand All @@ -12,13 +12,13 @@

The process is detailed as follow:

* Find the relevant file(s) (e.g.: `package.json` and `package-lock.json` or `mix.exs`).
* Determine the existing version number.
* Increment the specified segment. If you increment the minor segment, the patch segment is set to 0 and the same goes for the major segment, the minor and patch segments are set to 0.
* Write the newly incremented version number in the relevant file(s).
* Create a new `git commit` with the relevant file with the version number as the default message (e.g.: 0.2.1).
* Create a new annotated `git tag` pointing to the new `git commit` with the version number prefixed by a 'v' as the name (e.g.: v0.2.1).
* 💥
- Find the relevant file(s) (e.g.: `package.json` and `package-lock.json` or `mix.exs`).
- Determine the existing version number.
- Increment the specified segment. If you increment the minor segment, the patch segment is set to 0 and the same goes for the major segment, the minor and patch segments are set to 0.
- Write the newly incremented version number in the relevant file(s).
- Create a new `git commit` with the relevant file with the version number as the default message (e.g.: 0.2.1).
- Create a new annotated `git tag` pointing to the new `git commit` with the version number prefixed by a 'v' as the name (e.g.: v0.2.1).
- 💥

## Installation

Expand All @@ -42,12 +42,19 @@ To increment the minor segment of your Mix package version number:
~> incr mix minor
```

To increment the prerelease segment of your NPM package version number, while specifying an identifier (`rc`):

```shell
~> incr -i rc mix prerelease
```

### Arguments

Here are some arguments that can be used with `incr`:

- `-d` : Directory where to search for the version files (default: `.`)
- `-t` : Tag name pattern, where `%s` will be replaced with the new version (default: `v%s`)
- `-i` : Identifier for the prerelease segment (default: `alpha`)
- `--[no-]commit` : Commit changes. (default: enabled)
- `--[no-]tag` : Create a git tag. (default: enabled)

Expand All @@ -57,7 +64,7 @@ Example:
~> incr --no-tag -d ./subprojects/web/ -t my-custom-tag-prefix/%s npm patch
```

This will :
This will:

- Search for `package.json` and `package-lock.json` files inside `./subprojects/web/` and update the patch version
- Commit the changes under the message `my-custom-tag-prefix/2.3.4`
Expand All @@ -69,4 +76,4 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/jcoutu

## License

The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
This lovely gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
5 changes: 3 additions & 2 deletions bin/incr
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ version(Incr::VERSION)

flag([:d, :version_file_dir], default_value: '.', desc: 'Directory where to search for version file')
flag([:t, :tag_name_pattern], default_value: 'v%s', desc: 'Pattern for the tag name')
flag([:i, :identifier], default_value: 'alpha', desc: 'Identifier for the prerelease segment')

switch(:commit, default_value: true, desc: 'Commit changes')
switch(:tag, default_value: true, desc: 'Create a git tag')

pre do |global, command, options, args|
if args.length != 1 || !['major', 'minor', 'patch'].any? {|segment| args.include?(segment)}
warn('expecting a single argument: major, minor or patch.')
if args.length != 1 || !['major', 'minor', 'patch', 'prerelease'].any? {|segment| args.include?(segment)}
warn('expecting a single argument: major, minor, patch or prerelease.')
return false
end
true
Expand Down
3 changes: 2 additions & 1 deletion lib/incr/command/mix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def initialize(args, global_options)
@commit = global_options[:commit]
@tag = global_options[:tag]
@noop = global_options[:noop]
@identifier = global_options[:identifier]
end

def execute
Expand All @@ -29,7 +30,7 @@ def execute

file_version = file_content.match(VERSION_REGEX)[1]
old_version = SemVersion.new(file_version)
new_version = Incr::Service::Version.increment_segment(old_version, @segment)
new_version = Incr::Service::Version.increment_segment(old_version, @segment, @identifier)
replace_file_version(old_version, new_version)

new_tag = @tag_pattern % new_version.to_s
Expand Down
3 changes: 2 additions & 1 deletion lib/incr/command/npm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def initialize(args, global_options)
@commit = global_options[:commit]
@tag = global_options[:tag]
@noop = global_options[:noop]
@identifier = global_options[:identifier]
end

def execute
Expand All @@ -42,7 +43,7 @@ def execute

file_version = package_json['version']
old_version = SemVersion.new(file_version)
new_version = Incr::Service::Version.increment_segment(old_version, @segment)
new_version = Incr::Service::Version.increment_segment(old_version, @segment, @identifier)

replace_file_version(@package_json_filename, new_version.to_s)
replace_file_version(@package_json_lock_filename, new_version.to_s)
Expand Down
45 changes: 30 additions & 15 deletions lib/incr/service/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,39 @@
module Incr
module Service
class Version
def self.increment_segment(version, segment)
SEGMENT_OPERATIONS = {
'major' => ->(v, _) { v.major += 1; v.minor = 0; v.patch = 0; v.prerelease = nil },
'minor' => ->(v, _) { v.minor += 1; v.patch = 0; v.prerelease = nil },
'patch' => ->(v, _) { v.patch += 1; v.prerelease = nil },
'prerelease' => ->(v, id) { handle_prerelease(v, id) },
}.freeze

def self.increment_segment(version, segment, identifier = nil)
incremented_version = version.clone

case segment
when 'major'
incremented_version.major = version.major + 1
incremented_version.minor = 0
incremented_version.patch = 0
when 'minor'
incremented_version.minor = version.minor + 1
incremented_version.patch = 0
when 'patch'
incremented_version.patch = version.patch + 1

operation = SEGMENT_OPERATIONS[segment]
raise ArgumentError, "Unknown segment: #{segment}" unless operation

operation.call(incremented_version, identifier)

incremented_version
end

def self.handle_prerelease(version, identifier)
if version.prerelease.nil?
version.patch += 1
identifier = 'alpha' if identifier.nil?
version.prerelease = "#{identifier}.1"
else
raise ArgumentError, "Unknown segment: #{segment}"
version.prerelease = increment_prerelease(version.prerelease, identifier)
end

incremented_version
end

def self.increment_prerelease(value, identifier)
parts = value.split('.')
parts[-1] = identifier.nil? ? parts[-1].to_i + 1 : 1
parts[0] = identifier if identifier
parts.join('.')
end
end
end
Expand Down
48 changes: 48 additions & 0 deletions spec/incr/service/version_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,53 @@
}.to raise_error(NoMethodError)
end
end

context 'without a prerelease version' do
let(:version) { SemVersion.new('1.0.0') }

it 'increments the prerelease segment' do
expected = '1.0.1-alpha.1'
result = Incr::Service::Version.increment_segment(version, 'prerelease', 'alpha')
expect(result.to_s).to eql(expected)
end
end

context 'with a prerelease version' do
let(:version) { SemVersion.new('1.0.0-alpha.1') }

it 'increments the prerelease segment' do
expected = '1.0.0-alpha.2'
result = Incr::Service::Version.increment_segment(version, 'prerelease')
expect(result.to_s).to eql(expected)
end

it 'increments the prerelease segment with a custom identifier' do
expected = '1.0.0-rc.1'
result = Incr::Service::Version.increment_segment(version, 'prerelease', 'rc')
expect(result.to_s).to eql(expected)
end

it 'increments the prerelease segment with a custom identifier and resets the prerelease segment' do
expected = '1.0.0-rc.1'
result = Incr::Service::Version.increment_segment(version, 'prerelease', 'rc')
expect(result.to_s).to eql(expected)
end

it 'increments the prerelease segment with a custom identifier and resets the prerelease segment' do
expected = '1.0.0-rc.1'
result = Incr::Service::Version.increment_segment(version, 'prerelease', 'rc')
expect(result.to_s).to eql(expected)
end

it 'increments the minor segment' do
expected = '1.1.0'
result = Incr::Service::Version.increment_segment(version, 'minor')
expect(result.to_s).to eql(expected)
end

it 'handles empty custom identifier' do
expect { Incr::Service::Version.increment_segment(version, 'prerelease', '') }.to raise_error(ArgumentError)
end
end
end
end