/
behaviours.ex
104 lines (81 loc) · 2.65 KB
/
behaviours.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
defmodule Versioce.PreHook do
@moduledoc """
Behaviour for pre hooks in versioce.
A hook can be defined by simply using `Versioce.PreHook` in a module:
defmodule MyProj.Versioce.PreHook do
use Versioce.PreHook
def run(args) do
IO.inspect(args)
{:ok, args}
end
end
The `run/1` function will receive all arguments passed to the `bump` mix task.
It can modify them and make required side-effects but it *should* return them
of form `{:ok, args}` as result of one hook is piped into the next one, unless
`{:error, reason}` is returned, then the hook pipe is halted.
"""
@doc """
A hook needs to implement `run` which receives
a list of command line args for `bump` task.
It should return them in form `{:ok, args} | {:error, reason}`
"""
@callback run([String.t()]) :: {:ok, [String.t()]} | {:error, String.t()}
@doc false
defmacro __using__(_opts) do
quote do
@behaviour Versioce.PreHook
end
end
end
defmodule Versioce.PostHook do
@moduledoc """
Behaviour for post hooks in versioce.
A hook can be defined by simply using `Versioce.PostHook` in a module:
defmodule MyProj.Versioce.PreHook do
use Versioce.PostHook
def run(version) do
IO.inspect(version)
{:ok, version}
end
end
The `run/1` function will receive a version that project was bumped to.
It can modify it and make required side-effects but it *should* return it
of form `{:ok, version}` as result of one hook is piped into the next one,
unless `{:error, reason}` is returned, then the hook pipe is halted.
"""
@doc """
A hook needs to implement `run` which receives
the new version of the project.
It should return it in form `{:ok, version} | {:error, reason}`
"""
@callback run(String.t()) :: {:ok, String.t()} | {:error, String.t()}
@doc false
defmacro __using__(_opts) do
quote do
@behaviour Versioce.PostHook
end
end
end
defmodule Versioce.OK do
@moduledoc false
@type ok_tuple :: {:ok, any} | {:error, any}
@spec bind(ok_tuple, fun) :: ok_tuple()
def bind({:ok, value}, func), do: func.(value)
def bind({:error, reason}, _), do: {:error, reason}
@spec unit(any) :: ok_tuple()
def unit({:ok, _} = value), do: value
def unit({:error, _} = value), do: value
def unit(value), do: {:ok, value}
end
defmodule Versioce.Hooks do
@moduledoc false
@spec run(any, [module()]) :: Versioce.OK.ok_tuple()
def run(params, []), do: Versioce.OK.unit(params)
def run(params, hooks) do
[h | tail] = hooks
params
|> Versioce.OK.unit()
|> Versioce.OK.bind(&h.run/1)
|> run(tail)
end
end