aboutsummaryrefslogtreecommitdiffstats
path: root/src/rlx_util.erl
diff options
context:
space:
mode:
authorTristan Sloughter <[email protected]>2015-04-28 22:36:44 -0500
committerTristan Sloughter <[email protected]>2015-05-08 11:16:53 -0500
commit88d1f29257869b0de9369881236477163804125a (patch)
treec14b95ae8efdebcf074d314b6b0afb662f3454fb /src/rlx_util.erl
parenta36609536aaf7d98b5bd5d3364cab739dc6689b5 (diff)
downloadrelx-88d1f29257869b0de9369881236477163804125a.tar.gz
relx-88d1f29257869b0de9369881236477163804125a.tar.bz2
relx-88d1f29257869b0de9369881236477163804125a.zip
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..4639ffa 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)}.
+
+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
%%%===================================================================