diff options
author | Dave Smith <dizzyd@dizzyd.com> | 2010-01-04 09:53:50 -0700 |
---|---|---|
committer | Dave Smith <dizzyd@dizzyd.com> | 2010-01-04 09:53:50 -0700 |
commit | 93111bfcf784bc8b17dd70220a19f95ce51f88f4 (patch) | |
tree | db47429834faa397eeaa1fdc92c19302978a2e86 | |
parent | 73d2073ed8382da7e038926fe19863b62acdfae7 (diff) | |
parent | abd535d0815aad6cb8b57a6c5f629919007d15cc (diff) |
Merging w/ bitbucket
-rw-r--r-- | src/rebar_erlydtl_compiler.erl | 95 |
1 files changed, 55 insertions, 40 deletions
diff --git a/src/rebar_erlydtl_compiler.erl b/src/rebar_erlydtl_compiler.erl index 9a40f67..d635854 100644 --- a/src/rebar_erlydtl_compiler.erl +++ b/src/rebar_erlydtl_compiler.erl @@ -103,7 +103,58 @@ default(out_dir) -> "ebin"; default(source_ext) -> ".dtl"; default(module_ext) -> "_dtl". -referenced_dtls(Source, _Target, Config) -> +compile_dtl(Source, Target, Config) -> + case code:which(erlydtl) of + non_existing -> + ?CONSOLE( + "~n===============================================~n" + " You need to install erlydtl to comple DTL templates~n" + " Download the latest tarball release from github~n" + " http://code.google.com/p/erlydtl/~n" + " and install it into your erlang library dir~n" + "===============================================~n~n", []), + ?FAIL; + _ -> + case needs_compile(Source, Target, Config) of + true -> + do_compile(Source, Target, Config); + false -> + skipped + end + end. + +do_compile(Source, Target, Config) -> + %% TODO: Check last mod on target and referenced DTLs here.. + DtlOpts = erlydtl_opts(Config), + %% ensure that doc_root and out_dir are defined, + %% using defaults if necessary + Opts = [{out_dir, option(out_dir, DtlOpts)}, + {doc_root, option(doc_root, DtlOpts)}, + report, return], + case erlydtl:compile(Source, + module_name(Target), + Opts++DtlOpts) of + ok -> ok; + Reason -> + ?CONSOLE("Compiling template ~s failed:~n ~p~n", + [Source, Reason]), + ?FAIL + end. + +module_name(Target) -> + F = filename:basename(Target), + string:substr(F, 1, length(F)-length(".beam")). + +needs_compile(Source, Target, Config) -> + LM = filelib:last_modified(Target), + case LM < filelib:last_modified(Source) of + true -> true; + false -> + lists:any(fun(D) -> LM < filelib:last_modified(D) end, + referenced_dtls(Source, Config)) + end. + +referenced_dtls(Source, Config) -> Set = referenced_dtls1([Source], Config, sets:add_element(Source, sets:new())), Final = sets:to_list(sets:del_element(Source, Set)), @@ -111,7 +162,7 @@ referenced_dtls(Source, _Target, Config) -> referenced_dtls1(Step, Config, Seen) -> DtlOpts = erlydtl_opts(Config), - ExtMatch = re:replace(option(source_ext, DtlOpts), "\.", "\\\\.", + ExtMatch = re:replace(option(source_ext, DtlOpts), "\.", "\\\\\\\\.", [{return, list}]), AllRefs = lists:append( [ string:tokens( @@ -124,43 +175,7 @@ referenced_dtls1(Step, Config, Seen) -> New = sets:subtract(sets:from_list(Existing), Seen), case sets:size(New) of 0 -> Seen; - _ -> referenced_dtls(sets:to_list(New), Config, - sets:union(New, Seen)) - end. - -compile_dtl(Source, Target, Config) -> - case code:which(erlydtl) of - non_existing -> - ?CONSOLE( - "~n===============================================~n" - " You need to install erlydtl to comple DTL templates~n" - " Download the latest tarball release from github~n" - " http://code.google.com/p/erlydtl/~n" - " and install it into your erlang library dir~n" - "===============================================~n~n", []), - ?FAIL; - _ -> - %% TODO: Check last mod on target and referenced DTLs here.. - DtlOpts = erlydtl_opts(Config), - %% ensure that doc_root and out_dir are defined, - %% using defaults if necessary - Opts = [{out_dir, option(out_dir, DtlOpts)}, - {doc_root, option(doc_root, DtlOpts)}, - report, return], - case erlydtl:compile(Source, - module_name(Source,DtlOpts), - Opts++DtlOpts) of - ok -> ok; - Reason -> - ?CONSOLE("Compiling template ~s failed:~n ~p~n", - [Source, Reason]), - ?FAIL - end + _ -> referenced_dtls1(sets:to_list(New), Config, + sets:union(New, Seen)) end. -module_name(DtlPath, DtlOpts) -> - F = filename:basename(DtlPath), - SourceExt = option(source_ext, DtlOpts), - ModuleExt = option(module_ext, DtlOpts), - list_to_atom(lists:sublist(F, length(F)-length(SourceExt)) - ++ModuleExt). |