Browse Source

Add the capability of compiling erlydtl templates

Sina Samavati 11 years ago
parent
commit
5fe9b8b8cf
1 changed files with 54 additions and 5 deletions
  1. 54 5
      src/mad_compile.erl

+ 54 - 5
src/mad_compile.erl

@@ -91,7 +91,11 @@ app(Dir, ConfigFile) ->
             Opts = mad_utils:get_value(erl_opts, Conf1, []),
             lists:foreach(compile_fun(IncDir, EbinDir, Opts), Files),
             ok
-    end.
+    end,
+    ErlyDtlOpts = mad_utils:get_value(erlydtl_opts, Conf1, []),
+    ErlyDtlOpts1 = validate_erlydtl_opts(Dir, ErlyDtlOpts),
+    compile_erlydtl_files(ErlyDtlOpts1),
+    ok.
 
 -spec validate_property({atom(), term()}, term()) -> {atom(), term()}.
 validate_property({modules, _}, Modules) ->
@@ -105,7 +109,8 @@ compile_fun(IncDir, EbinDir, Opts) ->
     fun(File) ->
             case is_app_src(File) of
                 false ->
-                    Compiled = is_compiled(EbinDir, File),
+                    BeamFile = erl_to_beam(EbinDir, File),
+                    Compiled = is_compiled(BeamFile, File),
                     if Compiled =:= false ->
                             io:format("Compiling ~s~n", [File]),
                             Opts1 = ?COMPILE_OPTS(IncDir, EbinDir, Opts),
@@ -152,9 +157,8 @@ erl_to_beam(EbinDir, Filename) ->
     filename:join(EbinDir, filename:basename(Filename, ".erl") ++ ".beam").
 
 -spec is_compiled(directory(), file:name()) -> boolean().
-is_compiled(EbinDir, ErlFile) ->
-    BeamFile = erl_to_beam(EbinDir, ErlFile),
-    mad_utils:last_modified(BeamFile) > mad_utils:last_modified(ErlFile).
+is_compiled(BeamFile, File) ->
+    mad_utils:last_modified(BeamFile) > mad_utils:last_modified(File).
 
 -spec add_modules_property([{atom(), term()}]) -> [{atom(), term()}].
 add_modules_property(Properties) ->
@@ -207,3 +211,48 @@ is_behaviour(File) ->
         _ ->
             false
     end.
+
+get_kv(K, Opts, Default) ->
+    V = mad_utils:get_value(K, Opts, Default),
+    KV = {K, V},
+    {KV, Opts -- [KV]}.
+
+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 = erl_to_beam(OutDir, atom_to_list(ModuleName)),
+                      Compiled = is_compiled(BeamFile, F),
+                      if Compiled =:= false ->
+                              io:format("Compiling ~s~n", [F]),
+                              erlydtl:compile(F, ModuleName, Opts3);
+                         true ->
+                              ok
+                      end
+              end,
+    lists:foreach(Compile, Files).