Current section
Files
Jump to
Current section
Files
src/compile/mad_dtl.erl
-module(mad_dtl).
-copyright('Sina Samavati').
-compile(export_all).
compile(Dir,Config) ->
case mad_utils:get_value(erlydtl_opts, Config, []) of
[] -> false;
X -> compile_erlydtl_files(validate_erlydtl_opts(Dir,X)) end.
get_kv(K, Opts, Default) ->
V = mad_utils:get_value(K, Opts, Default),
KV = {K, V},
{KV, Opts -- [KV]}.
file_to_beam(Bin, Filename) -> filename:join(Bin, filename:basename(Filename) ++ ".beam").
validate_erlydtl_opts(Cwd, Opts) ->
DefaultDocRoot = filename:join("priv", "templates"),
{DocRoot, Opts1} = get_kv(doc_root, Opts, DefaultDocRoot),
{OutDir, Opts2} = get_kv(out_dir, Opts1, "ebin"),
{CompilerOpts, Opts3} = get_kv(compiler_options, Opts2, []),
{SourceExt, Opts4} = get_kv(source_ext, Opts3, ".dtl"),
{ModuleExt, Opts5} = get_kv(module_ext, Opts4, ""),
{_, DocRootDir} = DocRoot,
DocRoot1 = {doc_root, filename:join(Cwd, DocRootDir)},
{_, OutDir1} = OutDir,
OutDir2 = {out_dir, filename:join(Cwd, OutDir1)},
[DocRoot1, OutDir2, CompilerOpts, SourceExt, ModuleExt|Opts5].
module_name(File, Ext, NewExt) ->
list_to_atom(filename:basename(File, Ext) ++ NewExt).
compile_erlydtl_files(Opts) ->
{{_, DocRoot}, Opts1} = get_kv(doc_root, Opts, ""),
{{_, SourceExt}, Opts2} = get_kv(source_ext, Opts1, ""),
{{_, ModuleExt}, Opts3} = get_kv(module_ext, Opts2, ""),
{{_, OutDir}, _} = get_kv(out_dir, Opts3, ""),
Files = filelib:fold_files(DocRoot, SourceExt, true,
fun(F, Acc) -> [F|Acc] end, []),
Compile = fun(F) ->
ModuleName = module_name(F, SourceExt, ModuleExt),
BeamFile = file_to_beam(OutDir, atom_to_list(ModuleName)),
Compiled = mad_compile:is_compiled(BeamFile, F),
if Compiled =:= false ->
io:format("DTL Compiling ~s~n\r", [F -- mad_utils:cwd()]),
erlydtl:compile(F, ModuleName, Opts3);
true -> ok end
end,
lists:any(fun({error,_}) -> true; (ok) -> false end,[Compile(F) || F <- Files]).