/
mixbump
executable file
·66 lines (60 loc) · 2.07 KB
/
mixbump
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
#!/usr/local/bin/elixir
# get mix.exs path
if length(System.argv) > 0 do
if length(System.argv) == 1 do
# No command specified, just bump
[mix_exs_path | _] = System.argv
command = nil
else
[command, mix_exs_path | _] = System.argv
if command == "a", do: command = "add"
if command == "c", do: command = "commit"
end
# Allow specifying path without mix.exs if it contains it.
if !Regex.match?(~r/mix.exs$/, mix_exs_path) && File.exists?("#{mix_exs_path}/mix.exs") do
mix_exs_path = Path.join [mix_exs_path, "mix.exs"]
end
else
mix_exs_path = nil
end
if command && command != "add" && command != "commit" do
mix_exs_path = nil
IO.puts "Unrecognized command #{command}; \"add\" and \"commit\" are supported"
end
if mix_exs_path && File.exists?(mix_exs_path) do
# Update mix.exs in-place.
IO.puts "Bumping #{mix_exs_path}"
File.write mix_exs_path, (
File.read!(mix_exs_path)
|> String.split("\n")
|> (Enum.map fn(line) ->
Regex.replace(~r/version:\s*"(\d+)\.(\d+)\.(\d+)"/, line, fn(_, major, minor, sub) ->
{sub, _} = Integer.parse(sub)
old_version = "#{major}.#{minor}.#{sub}"
new_version = "#{major}.#{minor}.#{sub + 1}"
IO.puts "Bumping from #{old_version} to #{new_version}"
"version: \"#{major}.#{minor}.#{sub + 1}\""
end)
end)
|> Enum.join("\n")
)
case command do
"add" ->
System.cmd "git", ~w[add mix.exs], cd: Path.dirname(mix_exs_path)
{output, _} = System.cmd "git", ~w[diff --cached], cd: Path.dirname(mix_exs_path)
IO.puts output
"commit" ->
System.cmd "git", ~w[add mix.exs], cd: Path.dirname(mix_exs_path)
System.cmd "git", ~w[commit -m] ++ ["Version bump"], cd: Path.dirname(mix_exs_path)
{output, _} = System.cmd "git", ~w[diff HEAD~], cd: Path.dirname(mix_exs_path)
IO.puts output
_ ->
{output, _} = System.cmd "git", ~w[diff mix.exs], cd: Path.dirname(mix_exs_path)
IO.puts output
end
else
IO.puts """
Please specify the path to a valid mix.exs file.
Usage: mixbump path/to/mix.exs
"""
end