Specify multiple locations of DTL template files

Added a backward compartible feature to specify `erlydtl_opts' options
for the DTL template compiler to allow inclusion of templates in different
directories with different compilation settings for each. E.g.:

    {erlydtl_opts, [
          [{doc_root, "src/view"}, {module_ext, "_dtl_vw"}]
        , [{doc_root, "src"},      {module_ext, ""}, {recursive, false}]

        , {out_dir,   "ebin"}
        , {compiler_options, [verbose, debug_info]}
    ]}.

The definition above is identical to this (the last two options
are duplicated in each list):

    {erlydtl_opts, [
          [{doc_root,   "src/view"}
          ,{module_ext, "_dtl_vw"}
          ,{out_dir,    "ebin"}
          ,{compiler_options, [verbose, debug_info]}]

        , [{doc_root,   "src"}
          ,{module_ext, ""}
          ,{out_dir,    "ebin"}
          ,{compiler_options, [verbose, debug_info]}
          ,{recursive, false}]
    ]}.

In this case "src/view" and "src" directories containing template files
will be compiled.  A new `recursive' option tells rebar_erlydtl_compiler
to search files recursively from a given doc_root.  In the example above
the "src" directory won't be scanned recursively, and the target template
name for target beam modules won't have "_dtl_vw" suffix.
This commit is contained in:
Serge Aleynikov 2012-10-21 06:13:35 +04:00
parent d896fbffa7
commit 38902e9a52
2 changed files with 75 additions and 26 deletions

View file

@ -62,9 +62,9 @@ run(Config, FirstFiles, SourceDir, SourceExt, TargetDir, TargetExt,
%% Convert simple extension to proper regex %% Convert simple extension to proper regex
SourceExtRe = ".*\\" ++ SourceExt ++ [$$], SourceExtRe = ".*\\" ++ SourceExt ++ [$$],
Recursive = proplists:get_value(recursive, Opts, true),
%% Find all possible source files %% Find all possible source files
FoundFiles = rebar_utils:find_files(SourceDir, SourceExtRe), FoundFiles = rebar_utils:find_files(SourceDir, SourceExtRe, Recursive),
%% Remove first files from found files %% Remove first files from found files
RestFiles = [Source || Source <- FoundFiles, RestFiles = [Source || Source <- FoundFiles,
not lists:member(Source, FirstFiles)], not lists:member(Source, FirstFiles)],

View file

@ -31,7 +31,11 @@
%% to ebin/*_dtl.beam. %% to ebin/*_dtl.beam.
%% %%
%% Configuration options should be placed in rebar.config under %% Configuration options should be placed in rebar.config under
%% 'erlydtl_opts'. Available options include: %% 'erlydtl_opts'. It can be a list of name-value tuples or a list of
%% lists of name-value tuples if you have multiple template directories
%% that need to have different settings (see example below).
%%
%% Available options include:
%% %%
%% doc_root: where to find templates to compile %% doc_root: where to find templates to compile
%% "templates" by default %% "templates" by default
@ -45,6 +49,9 @@
%% module_ext: characters to append to the template's module name %% module_ext: characters to append to the template's module name
%% "_dtl" by default %% "_dtl" by default
%% %%
%% recursive: boolean that determines if doc_root(s) need to be
%% scanned recursively for matching template file names
%% (default: true).
%% For example, if you had: %% For example, if you had:
%% /t_src/ %% /t_src/
%% base.html %% base.html
@ -70,6 +77,21 @@
%% {source_ext, ".dtl"}, %% {source_ext, ".dtl"},
%% {module_ext, "_dtl"} %% {module_ext, "_dtl"}
%% ]}. %% ]}.
%%
%% The following example will compile the following templates:
%% "src/*.dtl" files into "ebin/*_dtl.beam" and
%% "templates/*.html" into "ebin/*.beam". Note that any tuple option
%% (such as 'out_dir') in the outer list is added to each inner list:
%% {erlydtl_opts, [
%% {out_dir, "ebin"},
%% {recursive, false},
%% [
%% {doc_root, "src"}, {module_ext, "_dtl"}
%% ],
%% [
%% {doc_root, "templates", {module_ext, ""}, {source_ext, ".html"}
%% ]
%% ]}.
-module(rebar_erlydtl_compiler). -module(rebar_erlydtl_compiler).
-export([compile/2]). -export([compile/2]).
@ -81,16 +103,23 @@
%% =================================================================== %% ===================================================================
compile(Config, _AppFile) -> compile(Config, _AppFile) ->
DtlOpts = erlydtl_opts(Config), MultiDtlOpts = erlydtl_opts(Config),
OrigPath = code:get_path(), OrigPath = code:get_path(),
true = code:add_path(rebar_utils:ebin_dir()), true = code:add_path(rebar_utils:ebin_dir()),
Result = rebar_base_compiler:run(Config, [],
Result = lists:foldl(fun(DtlOpts, _) ->
rebar_base_compiler:run(Config, [],
option(doc_root, DtlOpts), option(doc_root, DtlOpts),
option(source_ext, DtlOpts), option(source_ext, DtlOpts),
option(out_dir, DtlOpts), option(out_dir, DtlOpts),
option(module_ext, DtlOpts) ++ ".beam", option(module_ext, DtlOpts) ++ ".beam",
fun compile_dtl/3, fun(S, T, _C) ->
[{check_last_mod, false}]), compile_dtl(S, T, DtlOpts)
end,
[{check_last_mod, false},
{recursive, option(recursive, DtlOpts)}])
end, ok, MultiDtlOpts),
true = code:set_path(OrigPath), true = code:set_path(OrigPath),
Result. Result.
@ -100,7 +129,18 @@ compile(Config, _AppFile) ->
%% =================================================================== %% ===================================================================
erlydtl_opts(Config) -> erlydtl_opts(Config) ->
rebar_config:get(Config, erlydtl_opts, []). Opts = rebar_config:get(Config, erlydtl_opts, []),
Tuples = [{K,V} || {K,V} <- Opts],
case [L || L <- Opts, is_list(L), not io_lib:printable_list(L)] of
[] ->
lists:keysort(1, [Tuples]);
Lists ->
lists:map(fun(L) ->
lists:keysort(1, lists:foldl(fun({K,T}, Acc) ->
lists:keystore(K, 1, Acc, {K, T})
end, Tuples, L))
end, Lists)
end.
option(Opt, DtlOpts) -> option(Opt, DtlOpts) ->
proplists:get_value(Opt, DtlOpts, default(Opt)). proplists:get_value(Opt, DtlOpts, default(Opt)).
@ -109,9 +149,11 @@ default(doc_root) -> "templates";
default(out_dir) -> "ebin"; default(out_dir) -> "ebin";
default(source_ext) -> ".dtl"; default(source_ext) -> ".dtl";
default(module_ext) -> "_dtl"; default(module_ext) -> "_dtl";
default(custom_tags_dir) -> "". default(custom_tags_dir) -> "";
default(compiler_options) -> [report, return];
default(recursive) -> true.
compile_dtl(Source, Target, Config) -> compile_dtl(Source, Target, DtlOpts) ->
case code:which(erlydtl) of case code:which(erlydtl) of
non_existing -> non_existing ->
?ERROR("~n===============================================~n" ?ERROR("~n===============================================~n"
@ -122,26 +164,31 @@ compile_dtl(Source, Target, Config) ->
"===============================================~n~n", []), "===============================================~n~n", []),
?FAIL; ?FAIL;
_ -> _ ->
case needs_compile(Source, Target, Config) of case needs_compile(Source, Target, DtlOpts) of
true -> true ->
do_compile(Source, Target, Config); do_compile(Source, Target, DtlOpts);
false -> false ->
skipped skipped
end end
end. end.
do_compile(Source, Target, Config) -> do_compile(Source, Target, DtlOpts) ->
%% TODO: Check last mod on target and referenced DTLs here.. %% TODO: Check last mod on target and referenced DTLs here..
DtlOpts = erlydtl_opts(Config),
%% ensure that doc_root and out_dir are defined, %% ensure that doc_root and out_dir are defined,
%% using defaults if necessary %% using defaults if necessary
Opts = [{out_dir, option(out_dir, DtlOpts)}, Opts = lists:ukeymerge(1,
DtlOpts,
lists:sort(
[{out_dir, option(out_dir, DtlOpts)},
{doc_root, option(doc_root, DtlOpts)}, {doc_root, option(doc_root, DtlOpts)},
{custom_tags_dir, option(custom_tags_dir, DtlOpts)}, {custom_tags_dir, option(custom_tags_dir, DtlOpts)},
report, return], {compiler_options, option(compiler_options, DtlOpts)}])),
?INFO("Compiling \"~s\" -> \"~s\" with options:~n ~s~n",
[Source, Target, io_lib:format("~p", [Opts])]),
case erlydtl:compile(Source, case erlydtl:compile(Source,
module_name(Target), module_name(Target),
Opts++DtlOpts) of Opts) of
ok -> ok; ok -> ok;
Reason -> Reason ->
?ERROR("Compiling template ~s failed:~n ~p~n", ?ERROR("Compiling template ~s failed:~n ~p~n",
@ -153,19 +200,20 @@ module_name(Target) ->
F = filename:basename(Target), F = filename:basename(Target),
string:substr(F, 1, length(F)-length(".beam")). string:substr(F, 1, length(F)-length(".beam")).
needs_compile(Source, Target, Config) -> needs_compile(Source, Target, DtlOpts) ->
LM = filelib:last_modified(Target), LM = filelib:last_modified(Target),
LM < filelib:last_modified(Source) orelse LM < filelib:last_modified(Source) orelse
lists:any(fun(D) -> LM < filelib:last_modified(D) end, lists:any(fun(D) -> LM < filelib:last_modified(D) end,
referenced_dtls(Source, Config)). referenced_dtls(Source, DtlOpts)).
referenced_dtls(Source, Config) -> referenced_dtls(Source, DtlOpts) ->
Set = referenced_dtls1([Source], Config, DtlOpts1 = lists:keyreplace(doc_root, 1, DtlOpts,
{doc_root, filename:dirname(Source)}),
Set = referenced_dtls1([Source], DtlOpts1,
sets:add_element(Source, sets:new())), sets:add_element(Source, sets:new())),
sets:to_list(sets:del_element(Source, Set)). sets:to_list(sets:del_element(Source, Set)).
referenced_dtls1(Step, Config, Seen) -> referenced_dtls1(Step, DtlOpts, Seen) ->
DtlOpts = erlydtl_opts(Config),
ExtMatch = re:replace(option(source_ext, DtlOpts), "\.", "\\\\\\\\.", ExtMatch = re:replace(option(source_ext, DtlOpts), "\.", "\\\\\\\\.",
[{return, list}]), [{return, list}]),
@ -184,10 +232,11 @@ referenced_dtls1(Step, Config, Seen) ->
end || F <- Step]), end || F <- Step]),
DocRoot = option(doc_root, DtlOpts), DocRoot = option(doc_root, DtlOpts),
WithPaths = [ filename:join([DocRoot, F]) || F <- AllRefs ], WithPaths = [ filename:join([DocRoot, F]) || F <- AllRefs ],
?DEBUG("All deps: ~p\n", [WithPaths]),
Existing = [F || F <- WithPaths, filelib:is_regular(F)], Existing = [F || F <- WithPaths, filelib:is_regular(F)],
New = sets:subtract(sets:from_list(Existing), Seen), New = sets:subtract(sets:from_list(Existing), Seen),
case sets:size(New) of case sets:size(New) of
0 -> Seen; 0 -> Seen;
_ -> referenced_dtls1(sets:to_list(New), Config, _ -> referenced_dtls1(sets:to_list(New), DtlOpts,
sets:union(New, Seen)) sets:union(New, Seen))
end. end.