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 drop pre task #26

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
40 changes: 29 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ It is heavily inspired by [bumpversion](https://github.com/peritus/bumpversion).
- [Pre Hooks](#pre-hooks)
- [Post Hooks](#post-hooks)
- [Bump your versions!](#bump-your-versions)
- [Pre and Build params](#pre-and-build-params)
- [CalVer](#calver)
- [Changelog generation](#changelog-generation)
- [The name](#the-name)
Expand Down Expand Up @@ -101,7 +102,6 @@ Running pre-hooks: [MyProj.Versioce.PreHook]
Bumping version from 0.1.0:
0.1.1
Running post-hooks: []
Done.
```

#### Post hooks
Expand Down Expand Up @@ -135,11 +135,12 @@ Bumping version from 0.1.0:
0.1.1
Running post-hooks: [MyProj.Versioce.PostHook]
"0.1.1"
Done.
```

### Bump your versions!

Check documentation of the [mix bump task](Mix.Tasks.Bump.html)

Simply run `mix bump` with the preferred binding or a ready version.
```
> mix bump.version
Expand All @@ -149,37 +150,55 @@ Running pre-hooks: []
Bumping version from 0.1.0:
0.1.1
Running post-hooks: []
Done.
> mix bump major
Running pre-hooks: []
Bumping version 0.1.1:
1.0.0
Running post-hooks: []
Done.
> mix bump 2.0.2
Running pre-hooks: []
Bumping version from 1.1.1:
2.0.2
Running post-hooks: []
Done.
```

#### Pre and Build params

You can also add pre-release or build information easily with `--pre` or `--build`
```
> mix bump.version
0.1.0
> mix bump --pre alpha.3
Running pre-hooks: []
Bumping version from 0.1.0:
0.1.1-alpha.3
0.1.0-alpha.3
Running post-hooks: []
Done.
> mix bump --build 20210101011700.amd64
Running pre-hooks: []
Bumping version from 0.1.1-alpha.3:
0.1.1-alpha.3+20210101011700.amd64
Bumping version from 0.1.0-alpha.3:
0.1.0-alpha.3+20210101011700.amd64
Running post-hooks: []
```

After using `--pre` you can bump to the full version or next iteration via `bump next`
```
> mix bump.version
0.1.0
> mix bump --pre alpha.3
Running pre-hooks: []
Bumping version from 0.1.0:
0.1.0-alpha.3
Running post-hooks: []
> mix bump next --pre alpha.4
Running pre-hooks: []
Bumping version from 0.1.0-alpha.3:
0.1.0-alpha.4
Running post-hooks: []
> mix bump next
Running pre-hooks: []
Bumping version from 0.1.0-alpha.4:
0.1.0
Running post-hooks: []
Done.
```

#### CalVer
Expand All @@ -199,7 +218,6 @@ Running pre-hooks: []
Bumping version from 0.1.0:
2022.11.28
Running post-hooks: []
Done.
```

### Changelog generation
Expand Down
69 changes: 41 additions & 28 deletions lib/Bumper/bumper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,34 @@ defmodule Versioce.Bumper do
end

@doc """
Bumps versions in all the files specified in config + mix.exs
Bumps versions in all the files specified in config.

## Example

iex> Versioce.Bumper.bump({[], ["minor"]}, "0.0.1")
iex> Versioce.Bumper.bump("0.0.1", "0.1.0")
"0.1.0"
"""
@spec bump({OptionParser.parsed(), OptionParser.argv()}, String.t()) :: String.t()
def bump({[], []}, from) do
@spec bump(from :: String.t(), to :: String.t()) :: String.t()
def bump(from, to) do
Files.update_version_files(from, to)
to
end

@doc """
Get next project version based on options passed.

## Example

iex> iex> Versioce.Bumper.get_new_version({[], ["minor"]}, "0.0.1")
"0.1.0"
"""
@spec get_new_version({OptionParser.parsed(), OptionParser.argv()}, String.t()) :: String.t()
def get_new_version({[], []}, from) do
IO.puts("Nothing to do")
from
end

def bump({options, ["patch"]}, from) do
def get_new_version({options, ["patch"]}, from) do
from_v = Version.parse!(from)

new_version =
Expand All @@ -51,12 +65,10 @@ defmodule Versioce.Bumper do
|> to_string

Version.parse!(new_version)

Files.update_version_files(from, new_version)
new_version
end

def bump({options, ["minor"]}, from) do
def get_new_version({options, ["minor"]}, from) do
from_v = Version.parse!(from)

new_version =
Expand All @@ -68,12 +80,10 @@ defmodule Versioce.Bumper do
|> to_string

Version.parse!(new_version)

Files.update_version_files(from, new_version)
new_version
end

def bump({options, ["major"]}, from) do
def get_new_version({options, ["major"]}, from) do
from_v = Version.parse!(from)

new_version =
Expand All @@ -86,38 +96,41 @@ defmodule Versioce.Bumper do
|> to_string

Version.parse!(new_version)
new_version
end

Files.update_version_files(from, new_version)
def get_new_version({options, ["next"]}, from) do
from_v = Version.parse!(from)

new_version =
Map.merge(from_v, %{
pre: []
})
|> add_build_pre(options)
|> to_string

Version.parse!(new_version)
new_version
end

def bump({_options, ["calver"]}, from) do
def get_new_version({_options, ["calver"]}, _from) do
today = Date.utc_today()
format = Config.calver_format()

{:ok, new_version} = CalverFormattingParser.parse_format(format, today)

Files.update_version_files(from, new_version)
new_version
end

def bump({_, [version]}, from) do
new_version = Version.parse!(version) |> to_string

Files.update_version_files(from, new_version)
new_version
def get_new_version({_, [version]}, _from) do
Version.parse!(version) |> to_string
end

def bump({options, []}, from) do
def get_new_version({options, []}, from) do
from_v = Version.parse!(from)

new_version =
from_v
|> add_build_pre(options)
|> to_string

Files.update_version_files(from, new_version)
new_version
from_v
|> add_build_pre(options)
|> to_string
end

# Adds 'build' and 'pre' values to the `Version` struct of they were passed in options.
Expand Down
46 changes: 36 additions & 10 deletions lib/Tasks/mix_bump.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
@moduledoc """
A task that bumps your projects version.

> mix bump major|minor|patch|calver [--pre :string] [--build :string] [--no-pre-hooks] [--no-post-hooks]
> mix bump major|minor|patch|calver|next [--pre :string] [--build :string] [--no-pre-hooks] [--no-post-hooks]

major|minor|patch - bumps corresponding part of the project version
calver - bumps to current calver version
next - bumps to the next version from a pre-release

CalVer does not support --pre and --build. Both will be ignored

Expand All @@ -14,6 +18,24 @@
Bumping version from 0.0.2:
"0.0.3-alpha"
Running post-hooks: []

$> mix bump next --pre beta
Running pre-hooks: []
Bumping version from 0.0.3-alpha:
"0.0.3-beta"
Running post-hooks: []

$> mix bump next
Running pre-hooks: []
Bumping version from 0.0.3-beta:
"0.0.3"
Running post-hooks: []

$> mix bump major
Running pre-hooks: []
Bumping version from 0.0.3:
"1.0.0"
Running post-hooks: []
"""
use Mix.Task
alias Versioce.Bumper
Expand Down Expand Up @@ -50,12 +72,10 @@
end
end

defp bump(options, current_version) do
defp bump(current_version, new_version) do
IO.puts("Bumping version from #{current_version}:")

new_version =
Bumper.bump(options, current_version)
|> IO.inspect()
Bumper.bump(current_version, new_version) |> IO.inspect()

{:ok, new_version}
end
Expand All @@ -72,12 +92,18 @@
end

def run(options, {:ok, current_version}) do
with {:ok, _} <- run_pre_hooks(options),
{:ok, new_version} <- bump(options, current_version),
{:ok, _} <- run_post_hooks({options, new_version}) do
{:ok, new_version}
new_version = Bumper.get_new_version(options, current_version)

if new_version === current_version do
IO.puts("Bumping to the same version, nothing to do")

Check warning on line 98 in lib/Tasks/mix_bump.ex

View check run for this annotation

Codecov / codecov/patch

lib/Tasks/mix_bump.ex#L98

Added line #L98 was not covered by tests
else
{:error, reason} -> IO.puts(reason)
with {:ok, _} <- run_pre_hooks(options),
{:ok, new_version} <- bump(current_version, new_version),
{:ok, _} <- run_post_hooks({options, new_version}) do
{:ok, new_version}
else
{:error, reason} -> IO.puts(reason)
end
end
end

Expand Down
14 changes: 12 additions & 2 deletions test/bumper_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ defmodule VersioceTest.Bumper do
use Mimic

defp helper_bump(options, version) do
options
options = options
|> Bump.parse()
|> Bumper.bump(version)

new_version = Bumper.get_new_version(options, version)

Bumper.bump(version, new_version)
end

defp test_versioning(binding, new_vers) do
Expand Down Expand Up @@ -79,6 +82,13 @@ defmodule VersioceTest.Bumper do
test_build_pre("minor", "0.2.0")
end

test "Bump with next" do
assert helper_bump(["next"], "0.1.0-alpha") == "0.1.0"
assert helper_bump(["next", "--pre", "beta"], "0.1.0-alpha") == "0.1.0-beta"
assert helper_bump(["next", "--pre", "beta"], "0.1.0") == "0.1.0-beta"
test_build_pre("next", "0.1.0")
end

test "Bump with major" do
test_versioning("major", "1.0.0")
test_build_pre("major", "1.0.0")
Expand Down
Loading