Skip to content

Commit f6a8e12

Browse files
committed
use template for default value
1 parent 33a6732 commit f6a8e12

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

priv/templates/init-release.template

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{description, "OTP Release structure for executable programs"}.
22
{variables, [
3-
{name, "myapp", "Name of the OTP release. An app with this name will also be created."},
3+
{name, "{{root_name}}", "Name of the OTP release. An app with this name will also be created."},
44
{desc, "An OTP application", "Short description of the release's main app's purpose"}
55
]}.
66
{template, "sys.config", "config/sys.config"}.
77
{template, "vm.args", "config/vm.args"}.
8-
{message, "Add relx configuration to rebar.config:\n\n{relx, [{release, {@@name@@, \"0.1.0\"},
9-
[@@name@@,
8+
{message, "Add relx configuration to rebar.config:\n\n{relx, [{release, {{{root_name}}, \"0.1.0\"},
9+
[{{name}},
1010
sasl]},
1111

1212
{sys_config, \"./config/sys.config\"},

src/rebar_templater.erl

+19-14
Original file line numberDiff line numberDiff line change
@@ -83,35 +83,40 @@ get_template_vars(TemplateTerms, State) ->
8383
{_, Value} -> Value;
8484
false -> []
8585
end,
86-
override_vars(Vars, override_vars(global_variables(State), default_variables())).
86+
override_vars(Vars, override_vars(global_variables(State), default_variables(State))).
8787

8888
%% Provide a way to merge a set of variables with another one. The left-hand
8989
%% set of variables takes precedence over the right-hand set.
9090
%% In the case where left-hand variable description contains overriden defaults, but
9191
%% the right-hand one contains additional data such as documentation, the resulting
9292
%% variable description will contain the widest set of information possible.
93-
override_vars([], General) -> General;
94-
override_vars([{Var, Default} | Rest], General) ->
95-
case lists:keytake(Var, 1, General) of
96-
{value, {Var, _Default, Doc}, NewGeneral} ->
97-
[{Var, Default, Doc} | override_vars(Rest, NewGeneral)];
98-
{value, {Var, _Default}, NewGeneral} ->
99-
[{Var, Default} | override_vars(Rest, NewGeneral)];
100-
false ->
101-
[{Var, Default} | override_vars(Rest, General)]
102-
end;
103-
override_vars([{Var, Default, Doc} | Rest], General) ->
104-
[{Var, Default, Doc} | override_vars(Rest, lists:keydelete(Var, 1, General))].
93+
override_vars(Vars, General) ->
94+
{NewGeneral, VarsAcc} =
95+
lists:foldl(fun({Var, Default}, {General1, Acc}) ->
96+
case lists:keytake(Var, 1, General1) of
97+
{value, {Var, _Default, Doc}, NewGeneral1} ->
98+
{NewGeneral1, [{Var, expand_path(Default, General1), Doc} | Acc]};
99+
{value, {Var, _Default}, NewGeneral1} ->
100+
{NewGeneral1, [{Var, expand_path(Default, General1)}]};
101+
false ->
102+
{General1, [{Var, expand_path(Default, General1)} | Acc]}
103+
end;
104+
({Var, Default, Doc}, {General1, Acc}) ->
105+
{lists:keydelete(Var, 1, General1), [{Var, rebar_utils:to_list(render(Default, General1)), Doc} | Acc]}
106+
end, {General, []}, Vars),
107+
NewGeneral ++ VarsAcc.
105108

106109
%% Default variables, generated dynamically.
107-
default_variables() ->
110+
default_variables(State) ->
111+
RootName = filename:basename(filename:rootname(rebar_dir:root_dir(State))),
108112
{DefaultAuthor, DefaultEmail} = default_author_and_email(),
109113
{{Y,M,D},{H,Min,S}} = calendar:universal_time(),
110114
[{date, lists:flatten(io_lib:format("~4..0w-~2..0w-~2..0w",[Y,M,D]))},
111115
{datetime, lists:flatten(io_lib:format("~4..0w-~2..0w-~2..0wT~2..0w:~2..0w:~2..0w+00:00",[Y,M,D,H,Min,S]))},
112116
{author_name, DefaultAuthor},
113117
{author_email, DefaultEmail},
114118
{copyright_year, integer_to_list(Y)},
119+
{root_name, RootName},
115120
{apps_dir, "apps", "Directory where applications will be created if needed"}].
116121

117122
default_author_and_email() ->

0 commit comments

Comments
 (0)