From 33a6732f04a3bd835458e324061cbafa30c0a054 Mon Sep 17 00:00:00 2001 From: Tristan Sloughter Date: Thu, 9 May 2019 07:31:17 -0600 Subject: [PATCH 1/2] add init release template --- priv/templates/init-release.template | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 priv/templates/init-release.template diff --git a/priv/templates/init-release.template b/priv/templates/init-release.template new file mode 100644 index 000000000..15c1bd68f --- /dev/null +++ b/priv/templates/init-release.template @@ -0,0 +1,19 @@ +{description, "OTP Release structure for executable programs"}. +{variables, [ + {name, "myapp", "Name of the OTP release. An app with this name will also be created."}, + {desc, "An OTP application", "Short description of the release's main app's purpose"} +]}. +{template, "sys.config", "config/sys.config"}. +{template, "vm.args", "config/vm.args"}. +{message, "Add relx configuration to rebar.config:\n\n{relx, [{release, {@@name@@, \"0.1.0\"}, + [@@name@@, + sasl]}, + + {sys_config, \"./config/sys.config\"}, + {vm_args, \"./config/vm.args\"}, + + {dev_mode, true}, + {include_erts, false}, + + {extended_start_script, true}] +}."}. \ No newline at end of file From f6a8e125b64b01d3924f7d1975bec80977675b51 Mon Sep 17 00:00:00 2001 From: Tristan Sloughter Date: Sun, 12 May 2019 11:55:05 -0600 Subject: [PATCH 2/2] use template for default value --- priv/templates/init-release.template | 6 ++--- src/rebar_templater.erl | 33 ++++++++++++++++------------ 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/priv/templates/init-release.template b/priv/templates/init-release.template index 15c1bd68f..52246f27e 100644 --- a/priv/templates/init-release.template +++ b/priv/templates/init-release.template @@ -1,12 +1,12 @@ {description, "OTP Release structure for executable programs"}. {variables, [ - {name, "myapp", "Name of the OTP release. An app with this name will also be created."}, + {name, "{{root_name}}", "Name of the OTP release. An app with this name will also be created."}, {desc, "An OTP application", "Short description of the release's main app's purpose"} ]}. {template, "sys.config", "config/sys.config"}. {template, "vm.args", "config/vm.args"}. -{message, "Add relx configuration to rebar.config:\n\n{relx, [{release, {@@name@@, \"0.1.0\"}, - [@@name@@, +{message, "Add relx configuration to rebar.config:\n\n{relx, [{release, {{{root_name}}, \"0.1.0\"}, + [{{name}}, sasl]}, {sys_config, \"./config/sys.config\"}, diff --git a/src/rebar_templater.erl b/src/rebar_templater.erl index bc79db02d..7c37e0d0f 100644 --- a/src/rebar_templater.erl +++ b/src/rebar_templater.erl @@ -83,28 +83,32 @@ get_template_vars(TemplateTerms, State) -> {_, Value} -> Value; false -> [] end, - override_vars(Vars, override_vars(global_variables(State), default_variables())). + override_vars(Vars, override_vars(global_variables(State), default_variables(State))). %% Provide a way to merge a set of variables with another one. The left-hand %% set of variables takes precedence over the right-hand set. %% In the case where left-hand variable description contains overriden defaults, but %% the right-hand one contains additional data such as documentation, the resulting %% variable description will contain the widest set of information possible. -override_vars([], General) -> General; -override_vars([{Var, Default} | Rest], General) -> - case lists:keytake(Var, 1, General) of - {value, {Var, _Default, Doc}, NewGeneral} -> - [{Var, Default, Doc} | override_vars(Rest, NewGeneral)]; - {value, {Var, _Default}, NewGeneral} -> - [{Var, Default} | override_vars(Rest, NewGeneral)]; - false -> - [{Var, Default} | override_vars(Rest, General)] - end; -override_vars([{Var, Default, Doc} | Rest], General) -> - [{Var, Default, Doc} | override_vars(Rest, lists:keydelete(Var, 1, General))]. +override_vars(Vars, General) -> + {NewGeneral, VarsAcc} = + lists:foldl(fun({Var, Default}, {General1, Acc}) -> + case lists:keytake(Var, 1, General1) of + {value, {Var, _Default, Doc}, NewGeneral1} -> + {NewGeneral1, [{Var, expand_path(Default, General1), Doc} | Acc]}; + {value, {Var, _Default}, NewGeneral1} -> + {NewGeneral1, [{Var, expand_path(Default, General1)}]}; + false -> + {General1, [{Var, expand_path(Default, General1)} | Acc]} + end; + ({Var, Default, Doc}, {General1, Acc}) -> + {lists:keydelete(Var, 1, General1), [{Var, rebar_utils:to_list(render(Default, General1)), Doc} | Acc]} + end, {General, []}, Vars), + NewGeneral ++ VarsAcc. %% Default variables, generated dynamically. -default_variables() -> +default_variables(State) -> + RootName = filename:basename(filename:rootname(rebar_dir:root_dir(State))), {DefaultAuthor, DefaultEmail} = default_author_and_email(), {{Y,M,D},{H,Min,S}} = calendar:universal_time(), [{date, lists:flatten(io_lib:format("~4..0w-~2..0w-~2..0w",[Y,M,D]))}, @@ -112,6 +116,7 @@ default_variables() -> {author_name, DefaultAuthor}, {author_email, DefaultEmail}, {copyright_year, integer_to_list(Y)}, + {root_name, RootName}, {apps_dir, "apps", "Directory where applications will be created if needed"}]. default_author_and_email() ->