-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmix.exs
207 lines (179 loc) · 5.74 KB
/
mix.exs
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
defmodule GatewayApi.MixProject do
use Mix.Project
@version "0.0.1-beta.1"
def project do
[
aliases: aliases(),
apps_path: "apps",
deps: deps(),
description: description(),
docs: docs(),
elixirc_options: [warnings_as_errors: warnings_as_errors(Mix.env())],
homepage_url: "https://api.gateway.com.ua/",
name: "APIGateway",
preferred_cli_env: cli_env_for(:test, ["coveralls", "coveralls.detail", "coveralls.post", "coveralls.html", "coveralls.json", "test.reset", "test.watch"]),
source_url: "https://github.com/kapranov/api_gateway",
start_permanent: Mix.env() == :prod,
test_coverage: [tool: ExCoveralls],
updated: update_version(@version),
version: version(@version)
]
end
defp aliases do
[
bless: [&bless/1],
"deps.get": [
"deps.get",
&update_version/1
],
"test.cover": &run_default_coverage/1,
"test.cover.html": &run_html_coverage/1,
"test.no.start": ["test --no-start"],
]
end
defp deps do
[
{:credo, "~> 1.7", only: [:dev, :test], runtime: false},
{:ex_doc, "~> 0.30.6", runtime: false},
{:ex_spec, "~> 2.0", only: [:test]},
{:ex_unit_notifier, "~> 1.3", [env: :prod, hex: "ex_unit_notifier", repo: "hexpm"]},
{:excoveralls, "~> 0.17.1", only: [:test]},
{:junit_formatter, "~> 3.3"},
{:mix_test_watch, "~> 1.1", only: [:dev, :test], runtime: false}
]
end
defp description do
contents = "An easy way to manage GraphQL Channel and much more for eHealth Inc. Kyiv"
Mix.shell().info("Synopsis version with: #{inspect(contents)}")
end
defp docs do
[
name: "APIGateway",
source_url: "https://github.com/kapranov/api_gateway",
homepage_url: "htts://api.gateway.me:4001/docs",
docs: [
main: "APIGateway",
logo: "",
extras: ["README.md"]
]
]
end
defp cli_env_for(env, tasks) do
Enum.reduce(tasks, [], fn(key, acc) -> Keyword.put(acc, :"#{key}", env) end)
end
defp warnings_as_errors(:prod), do: false
defp warnings_as_errors(_), do: true
defp update_version(_) do
contents = [
version(@version),
get_commit_sha(),
get_commit_date()
]
Mix.shell().info("Updating version with: #{inspect(contents)}")
File.write("VERSION", Enum.join(contents, "\n"), [:write])
end
defp version(version) do
identifier_filter = ~r/[^0-9a-z\-]+/i
git_pre_release =
with {tag, 0} <-
System.cmd("git", ["describe", "--tags", "--abbrev=0"], stderr_to_stdout: true),
{describe, 0} <- System.cmd("git", ["describe", "--tags", "--abbrev=8"]) do
describe
|> String.trim()
|> String.replace(String.trim(tag), "")
|> String.trim_leading("-")
|> String.trim()
else
_ ->
{commit_hash, 0} = System.cmd("git", ["rev-parse", "--short", "HEAD"])
"0-g" <> String.trim(commit_hash)
end
branch_name =
with {branch_name, 0} <- System.cmd("git", ["rev-parse", "--abbrev-ref", "HEAD"]),
branch_name <- String.trim(branch_name),
branch_name <- System.get_env("GATEWAY_BUILD_BRANCH") || branch_name,
true <-
!Enum.any?(["master", "HEAD", "release/", "stable"], fn name ->
String.starts_with?(name, branch_name)
end) do
branch_name =
branch_name
|> String.trim()
|> String.replace(identifier_filter, "-")
branch_name
end
build_name =
cond do
name = Application.get_env(:api_gateway, :build_name) -> name
name = System.get_env("COMMUNITY_BUILD_NAME") -> name
true -> nil
end
env_name = if Mix.env() != :prod, do: to_string(Mix.env())
env_override = System.get_env("COMMUNITY_BUILD_NAME")
env_name =
case env_override do
nil -> env_name
env_override when env_override in ["", "prod"] -> nil
env_override -> env_override
end
pre_release =
[git_pre_release, branch_name]
|> Enum.filter(fn string -> string && string != "" end)
|> Enum.join(".")
|> (fn
"" -> nil
string -> "-" <> String.replace(string, identifier_filter, "-")
end).()
build_metadata =
[build_name, env_name]
|> Enum.filter(fn string -> string && string != "" end)
|> Enum.join(".")
|> (fn
"" -> nil
string -> "+" <> String.replace(string, identifier_filter, "-")
end).()
[version, pre_release, build_metadata]
|> Enum.filter(fn string -> string && string != "" end)
|> Enum.join()
end
defp bless(_) do
[
{"compile", ["--warnings-as-errors", "--force"]},
{"coveralls.html", []},
{"format", ["--check-formatted"]},
{"credo", []}
]
|> Enum.each(fn {task, args} ->
IO.ANSI.format([:cyan, "Running #{task} with args #{inspect(args)}"])
|> IO.puts()
Mix.Task.run(task, args)
end)
end
defp run_default_coverage(args), do: run_coverage("coveralls", args)
defp run_html_coverage(args), do: run_coverage("coveralls.html", args)
defp run_coverage(task, args) do
{_, res} =
System.cmd(
"mix",
[task | args],
into: IO.binstream(:stdio, :line),
env: [{"MIX_ENV", "test"}]
)
if res > 0 do
System.at_exit(fn _ -> exit({:shutdown, 1}) end)
end
end
defp get_commit_sha do
System.cmd("git", ["rev-parse", "HEAD"])
|> elem(0)
|> String.trim()
end
defp get_commit_date do
[sec, tz] =
System.cmd("git", ~w|log -1 --date=raw --format=%cd|)
|> elem(0)
|> String.split(~r/\s+/, trim: true)
|> Enum.map(&String.to_integer/1)
DateTime.from_unix!(sec + tz * 36) |> to_string
end
end