-
Notifications
You must be signed in to change notification settings - Fork 3
/
release
executable file
·123 lines (112 loc) · 4.89 KB
/
release
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
#!/usr/bin/env escript
%%!
%-*-Mode:erlang;coding:utf-8;tab-width:4;c-basic-offset:4;indent-tabs-mode:()-*-
% ex: set ft=erlang fenc=utf-8 sts=4 ts=4 sw=4 et:
%%%
%%%------------------------------------------------------------------------
%%% MIT License
%%%
%%% Copyright (c) 2013-2019 Michael Truog <mjtruog at protonmail dot com>
%%%
%%% Permission is hereby granted, free of charge, to any person obtaining a
%%% copy of this software and associated documentation files (the "Software"),
%%% to deal in the Software without restriction, including without limitation
%%% the rights to use, copy, modify, merge, publish, distribute, sublicense,
%%% and/or sell copies of the Software, and to permit persons to whom the
%%% Software is furnished to do so, subject to the following conditions:
%%%
%%% The above copyright notice and this permission notice shall be included in
%%% all copies or substantial portions of the Software.
%%%
%%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
%%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
%%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
%%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
%%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
%%% FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
%%% DEALINGS IN THE SOFTWARE.
%%%------------------------------------------------------------------------
-author('mjtruog at protonmail dot com').
-mode(compile).
main(_) ->
{ok,
[{sys, _} = RelToolConfig,
{target_dir, TargetDir},
{overlay, OverlayConfig}]} = file:consult("reltool.config"),
{ok, Spec} = reltool:get_target_spec([RelToolConfig]),
case file:make_dir(TargetDir) of
ok ->
ok;
{error, eexist} ->
io:format("release already exists? (~p)~n", [TargetDir]),
exit_code(1)
end,
ok = reltool:eval_target_spec(Spec, code:root_dir(), TargetDir),
ok = process_overlay(RelToolConfig, TargetDir, OverlayConfig),
exit_code(0).
shell(Command, Arguments) ->
Shell = erlang:open_port({spawn_executable, "/bin/sh"},
[{args, ["-"]},
stream, binary, stderr_to_stdout, exit_status]),
Exec = io_lib:format(Command, Arguments),
true = erlang:port_command(Shell, ["exec ", Exec, "\n"]),
case shell_output(Shell, []) of
{0, _} ->
ok;
{Status, Output} ->
io:format("~s\"~s\" failed! exit ~w~n", [Output, Exec, Status]),
error
end.
shell_output(Shell, Output) ->
receive
{Shell, {data, Data}} ->
shell_output(Shell, [Data | Output]);
{Shell, {exit_status, Status}} ->
{Status, lists:reverse(Output)}
end.
boot_rel_vsn({sys, Config} = _RelToolConfig) ->
case proplists:lookup(rel, Config) of
{rel, _Name, Version, _Applications} ->
Version;
{rel, _Name, Version, _Applications, _Options} ->
Version
end.
%% minimal parsing for handling mustache syntax
mustache(Body, Context) ->
mustache(Body, "", Context).
mustache([], Result, _Context) ->
lists:reverse(Result);
mustache([${, ${ | KeyStr], Result, Context) ->
mustache_key(KeyStr, "", Result, Context);
mustache([C | Rest], Result, Context) ->
mustache(Rest, [C | Result], Context).
mustache_key([$}, $} | Rest], KeyStr, Result, Context) ->
Key = erlang:list_to_existing_atom(lists:reverse(KeyStr)),
{ok, Value} = dict:find(Key, Context),
mustache(Rest, lists:reverse(Value) ++ Result, Context);
mustache_key([C | Rest], KeyStr, Result, Context) ->
mustache_key(Rest, [C | KeyStr], Result, Context).
%% support minimal overlay based on rebar overlays
process_overlay(RelToolConfig, TargetDir, OverlayConfig) ->
BootRelVsn = boot_rel_vsn(RelToolConfig),
OverlayVars =
dict:from_list([{erts_vsn, "erts-" ++ erlang:system_info(version)},
{rel_vsn, BootRelVsn},
{target_dir, TargetDir},
{hostname, net_adm:localhost()}]),
{ok, BaseDir} = file:get_cwd(),
execute_overlay(OverlayConfig, OverlayVars, BaseDir, TargetDir).
execute_overlay([], _Vars, _BaseDir, _TargetDir) ->
ok;
execute_overlay([{mkdir, Out} | Rest], Vars, BaseDir, TargetDir) ->
OutDir = mustache(filename:join(TargetDir, Out), Vars),
ok = shell("mkdir -p \"~s\"", [OutDir]),
execute_overlay(Rest, Vars, BaseDir, TargetDir);
execute_overlay([{copy, In, Out} | Rest], Vars, BaseDir, TargetDir) ->
InFile = mustache(filename:join(BaseDir, In), Vars),
OutFile = mustache(filename:join(TargetDir, Out), Vars),
true = filelib:is_file(InFile),
ok = shell("cp -R \"~s\" \"~s\"", [InFile, OutFile]),
execute_overlay(Rest, Vars, BaseDir, TargetDir).
exit_code(ExitCode) ->
erlang:halt(ExitCode, [{flush, true}]).