Skip to content

Commit

Permalink
Merge 87908de into 4511d4e
Browse files Browse the repository at this point in the history
  • Loading branch information
smolijar committed Mar 4, 2019
2 parents 4511d4e + 87908de commit 1b1d614
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ gem install vump
## Usage

```
vump <major|minor|patch> [...options]
vump [<major|minor|patch|<semver-string>>] [...options]
```

| Option | Example value | Explanation |
Expand Down
6 changes: 5 additions & 1 deletion lib/vump/cli/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ def self.parse_arg(args)
:major
when /^mi.*/
:minor
else
when /^p/
:patch
else
args.first
end
end

Expand All @@ -49,6 +51,8 @@ def self.parse_options(options)
verbose: false,
version: false,
tag_prefix: 'v',
build: nil,
pre: nil,
path: Dir.pwd,
}
options.keys.each_with_object(defaults) do |k, acc|
Expand Down
2 changes: 1 addition & 1 deletion lib/vump/cli/reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def add_read_version(mod, version)
# rubocop:disable Metrics/AbcSize
# rubocop:disable Metrics/LineLength
def help
puts 'vump'.bold.yellow + ' <major|minor|patch> [...options]'.yellow
puts 'vump'.bold.yellow + ' [<major|minor|patch|<semver-string>>] [...options]'.yellow
header title: 'Available options:'
table(border: false) do
[
Expand Down
22 changes: 13 additions & 9 deletions lib/vump/semver/semver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,23 @@ def initialize(string = nil)
end

def load(string)
# <numeral>[-<sufix>]
version, sufix = string
.match(/([\d\.]+)(?:\-)?(.*)?/)
.captures
# <sufix>:= [<pre>][+<build>]
@pre, @build = sufix.split('+', 2).map { |s| s.empty? ? false : s }
# <numeral>[-<pre>][+<build>]
version, @pre, @build = string
.match(/([\d\.]+)(?:\-)?([^\+]*)(?:\+)?(.*)?/)
.to_a
.drop(1)
# <numeral>:= <major>.<minor>.<patch>
@major, @minor, @patch = version
.match(/(\d+)\.(\d+)\.(\d+)/)
.captures
.to_a
.drop(1)
.map(&:to_i)
end

def valid?
[@major, @minor, @patch].all? { |v| v.is_a?(Numeric) }
end

def reset(what)
levels = %i[@build @pre @patch @minor @major]
# tag to false, version to 0
Expand Down Expand Up @@ -59,8 +63,8 @@ def bump(what)

def to_s
str = "#{@major}.#{@minor}.#{@patch}"
str << "-#{@pre}" if @pre
str << "+#{@build}" if @build
str << "-#{@pre}" if @pre && @pre != ''
str << "+#{@build}" if @build && @build != ''
str
end
end
Expand Down
23 changes: 17 additions & 6 deletions lib/vump/vump.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,25 @@ def commit(modules, version)
@git.tag(version) if @git.commit(version)
end

def bump(arg = false, pre = false, build = false)
def compose_version(latest, arg, pre, build)
semver = Semver.new(latest)
semver.bump(arg) if arg
# if arg is string, overwrite
semver = Semver.new(arg) if arg.is_a?(String)
semver.pre = pre
semver.build = build
semver
end

def bump(arg = nil, pre = nil, build = nil)
modules = load_modules
version = select_version(read_versions(modules))
semver = Semver.new(version)
semver.bump(arg) if arg
semver.pre = pre if pre
semver.build = build if build
write_versions(modules, semver.to_s)
semver = compose_version(version, arg, pre, build)
if semver.valid?
write_versions(modules, semver.to_s)
else
@logger.error('Provided version is not a valid semver string')
end
end

def help
Expand Down
11 changes: 7 additions & 4 deletions spec/vump/cli/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@
it 'minor with typo' do
expect(cli.parse_arg(%w[minro])).to eql(:minor)
end
it 'patch on invalid' do
expect(cli.parse_arg(%w[brekeke])).to eql(:patch)
it 'patch' do
expect(cli.parse_arg(%w[patch])).to eql(:patch)
end
it 'patch on default' do
expect(cli.parse_arg([])).to eql(:patch)
it 'keep on invalid' do
expect(cli.parse_arg(%w[brekeke])).to eql('brekeke')
end
it 'nil on default' do
expect(cli.parse_arg([])).to eql(nil)
end
end
context 'parse_inputs' do
Expand Down
12 changes: 12 additions & 0 deletions spec/vump/vump_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ def relevant?
after :all do
FileUtils.rm_rf(sandbox_dir)
end
it 'set custom version' do
File.write(version_path, "0.0.0\n")
v = Vump::Vump.new(sandbox_dir, '0.2.0', silent: true)
v.bump('0.2.0')
expect(File.read(version_path)).to eql("0.2.0\n")
end
it 'set custom invalid version and no change' do
File.write(version_path, "0.0.0\n")
v = Vump::Vump.new(sandbox_dir, '0.foo.1', silent: true)
v.bump('0.foo.1')
expect(File.read(version_path)).to eql("0.0.0\n")
end
it 'flow with version' do
git = Git.open(sandbox_dir)
File.write(version_path, "0.0.0\n")
Expand Down

0 comments on commit 1b1d614

Please sign in to comment.