aboutsummaryrefslogtreecommitdiffstats
path: root/src/rlx_util.erl
diff options
context:
space:
mode:
authorJordan Wilberding <[email protected]>2015-05-08 15:57:05 -0700
committerJordan Wilberding <[email protected]>2015-05-08 15:57:05 -0700
commit2e59b1c95575b3c104cc191e954c82baadc43c6c (patch)
treed18fb57579cb3e58984599bdb2c926db9314d294 /src/rlx_util.erl
parentf8af9f5cd9f70ff43a043fe274dda4e56be82a2d (diff)
parent58fec57db3d99ffb5aefd0d5751d7bbda6762f66 (diff)
downloadrelx-2e59b1c95575b3c104cc191e954c82baadc43c6c.tar.gz
relx-2e59b1c95575b3c104cc191e954c82baadc43c6c.tar.bz2
relx-2e59b1c95575b3c104cc191e954c82baadc43c6c.zip
Merge pull request #339 from tsloughter/mustache
use mustache instead of erlydtl for overlays
Diffstat (limited to 'src/rlx_util.erl')
-rw-r--r--src/rlx_util.erl62
1 files changed, 61 insertions, 1 deletions
diff --git a/src/rlx_util.erl b/src/rlx_util.erl
index 9c4dcc2..2251b4f 100644
--- a/src/rlx_util.erl
+++ b/src/rlx_util.erl
@@ -32,7 +32,12 @@
error_reason/1,
indent/1,
optional_to_string/1,
- wildcard_paths/1]).
+ wildcard_paths/1,
+ render/1,
+ render/2,
+ load_file/3,
+ template_files/0,
+ escript_foldl/3]).
-define(ONE_LEVEL_INDENT, " ").
%%============================================================================
@@ -147,6 +152,61 @@ wildcard(Path) when is_list(Path) ->
Paths -> Paths
end.
+render(Template) ->
+ render(Template, []).
+
+render(Template, Data) ->
+ {ok, mustache:render(ec_cnv:to_binary(Template), Data, [{key_type, atom}])}.
+
+load_file(Files, escript, Name) ->
+ {Name, Bin} = lists:keyfind(Name, 1, Files),
+ Bin;
+load_file(_Files, file, Name) ->
+ {ok, Bin} = file:read_file(Name),
+ Bin.
+
+template_files() ->
+ find_priv_templates() ++ escript_files().
+
+find_priv_templates() ->
+ Files = filelib:wildcard(filename:join([code:priv_dir(relx), "templates", "*"])),
+ lists:map(fun(File) ->
+ {ok, Bin} = file:read_file(File),
+ {filename:basename(File), Bin}
+ end, Files).
+
+%% Scan the current escript for available files
+escript_files() ->
+ try
+ {ok, Files} = escript_foldl(
+ fun(Name, _, GetBin, Acc) ->
+ [{filename:basename(Name), GetBin()} | Acc]
+ end, [], filename:absname(escript:script_name())),
+ Files
+ catch
+ _:_ ->
+ []
+ end.
+
+escript_foldl(Fun, Acc, File) ->
+ case escript:extract(File, [compile_source]) of
+ {ok, [_Shebang, _Comment, _EmuArgs, Body]} ->
+ case Body of
+ {source, BeamCode} ->
+ GetInfo = fun() -> file:read_file_info(File) end,
+ GetBin = fun() -> BeamCode end,
+ {ok, Fun(".", GetInfo, GetBin, Acc)};
+ {beam, BeamCode} ->
+ GetInfo = fun() -> file:read_file_info(File) end,
+ GetBin = fun() -> BeamCode end,
+ {ok, Fun(".", GetInfo, GetBin, Acc)};
+ {archive, ArchiveBin} ->
+ zip:foldl(Fun, Acc, {File, ArchiveBin})
+ end;
+ {error, _} = Error ->
+ Error
+ end.
+
%%%===================================================================
%%% Test Functions
%%%===================================================================