您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

189 行
6.8 KiB

  1. %% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
  2. %% ex: ts=4 sw=4 et
  3. %% -------------------------------------------------------------------
  4. %%
  5. %% rebar: Erlang Build Tools
  6. %%
  7. %% Copyright (c) 2009 Dave Smith (dizzyd@dizzyd.com),
  8. %% Bryan Fink (bryan@basho.com)
  9. %%
  10. %% Permission is hereby granted, free of charge, to any person obtaining a copy
  11. %% of this software and associated documentation files (the "Software"), to deal
  12. %% in the Software without restriction, including without limitation the rights
  13. %% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. %% copies of the Software, and to permit persons to whom the Software is
  15. %% furnished to do so, subject to the following conditions:
  16. %%
  17. %% The above copyright notice and this permission notice shall be included in
  18. %% all copies or substantial portions of the Software.
  19. %%
  20. %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. %% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. %% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. %% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. %% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. %% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26. %% THE SOFTWARE.
  27. %% -------------------------------------------------------------------
  28. %% The rebar_erlydtl_compiler module is a plugin for rebar that compiles
  29. %% ErlyDTL templates. By default, it compiles all templates/*.dtl
  30. %% to ebin/*_dtl.beam.
  31. %%
  32. %% Configuration options should be placed in rebar.config under
  33. %% 'erlydtl_opts'. Available options include:
  34. %%
  35. %% doc_root: where to find templates to compile
  36. %% "templates" by default
  37. %%
  38. %% out_dir: where to put compiled template beam files
  39. %% "ebin" by default
  40. %%
  41. %% source_ext: the file extension the template sources have
  42. %% ".dtl" by default
  43. %%
  44. %% module_ext: characters to append to the template's module name
  45. %% "_dtl" by default
  46. %%
  47. %% For example, if you had:
  48. %% /t_src/
  49. %% base.html
  50. %% foo.html
  51. %%
  52. %% And you wanted them compiled to:
  53. %% /priv/
  54. %% base.beam
  55. %% foo.beam
  56. %%
  57. %% You would add to your rebar.config:
  58. %% {erlydtl_opts, [
  59. %% {doc_root, "t_src"},
  60. %% {out_dir, "priv"},
  61. %% {source_ext, ".html"},
  62. %% {module_ext, ""}
  63. %% ]}.
  64. %%
  65. %% The default settings are the equivalent of:
  66. %% {erlydtl_opts, [
  67. %% {doc_root, "templates"},
  68. %% {out_dir, "ebin"},
  69. %% {source_ext, ".dtl"},
  70. %% {module_ext, "_dtl"}
  71. %% ]}.
  72. -module(rebar_erlydtl_compiler).
  73. -export([compile/2]).
  74. -include("rebar.hrl").
  75. %% ===================================================================
  76. %% Public API
  77. %% ===================================================================
  78. compile(Config, _AppFile) ->
  79. DtlOpts = erlydtl_opts(Config),
  80. rebar_base_compiler:run(Config, [],
  81. option(doc_root, DtlOpts),
  82. option(source_ext, DtlOpts),
  83. option(out_dir, DtlOpts),
  84. option(module_ext, DtlOpts) ++ ".beam",
  85. fun compile_dtl/3, [{check_last_mod, false}]).
  86. %% ===================================================================
  87. %% Internal functions
  88. %% ===================================================================
  89. erlydtl_opts(Config) ->
  90. rebar_config:get(Config, erlydtl_opts, []).
  91. option(Opt, DtlOpts) ->
  92. proplists:get_value(Opt, DtlOpts, default(Opt)).
  93. default(doc_root) -> "templates";
  94. default(out_dir) -> "ebin";
  95. default(source_ext) -> ".dtl";
  96. default(module_ext) -> "_dtl";
  97. default(custom_tags_dir) -> "".
  98. compile_dtl(Source, Target, Config) ->
  99. case code:which(erlydtl) of
  100. non_existing ->
  101. ?CONSOLE(
  102. <<"~n===============================================~n"
  103. " You need to install erlydtl to compile DTL templates~n"
  104. " Download the latest tarball release from github~n"
  105. " http://code.google.com/p/erlydtl/~n"
  106. " and install it into your erlang library dir~n"
  107. "===============================================~n~n">>, []),
  108. ?FAIL;
  109. _ ->
  110. case needs_compile(Source, Target, Config) of
  111. true ->
  112. do_compile(Source, Target, Config);
  113. false ->
  114. skipped
  115. end
  116. end.
  117. do_compile(Source, Target, Config) ->
  118. %% TODO: Check last mod on target and referenced DTLs here..
  119. DtlOpts = erlydtl_opts(Config),
  120. %% ensure that doc_root and out_dir are defined,
  121. %% using defaults if necessary
  122. Opts = [{out_dir, option(out_dir, DtlOpts)},
  123. {doc_root, option(doc_root, DtlOpts)},
  124. {custom_tags_dir, option(custom_tags_dir, DtlOpts)},
  125. report, return],
  126. case erlydtl:compile(Source,
  127. module_name(Target),
  128. Opts++DtlOpts) of
  129. ok -> ok;
  130. Reason ->
  131. ?CONSOLE("Compiling template ~s failed:~n ~p~n",
  132. [Source, Reason]),
  133. ?FAIL
  134. end.
  135. module_name(Target) ->
  136. F = filename:basename(Target),
  137. string:substr(F, 1, length(F)-length(".beam")).
  138. needs_compile(Source, Target, Config) ->
  139. LM = filelib:last_modified(Target),
  140. LM < filelib:last_modified(Source) orelse
  141. lists:any(fun(D) -> LM < filelib:last_modified(D) end,
  142. referenced_dtls(Source, Config)).
  143. referenced_dtls(Source, Config) ->
  144. Set = referenced_dtls1([Source], Config,
  145. sets:add_element(Source, sets:new())),
  146. sets:to_list(sets:del_element(Source, Set)).
  147. referenced_dtls1(Step, Config, Seen) ->
  148. DtlOpts = erlydtl_opts(Config),
  149. ExtMatch = re:replace(option(source_ext, DtlOpts), "\.", "\\\\\\\\.",
  150. [{return, list}]),
  151. ShOpts = [{use_stdout, false}, return_on_error],
  152. AllRefs =
  153. lists:append(
  154. [begin
  155. Cmd = lists:flatten(["grep -o [^\\\"]*",
  156. ExtMatch, " ", F]),
  157. case rebar_utils:sh(Cmd, ShOpts) of
  158. {ok, Res} ->
  159. string:tokens(Res, "\n");
  160. {error, _} ->
  161. ""
  162. end
  163. end || F <- Step]),
  164. DocRoot = option(doc_root, DtlOpts),
  165. WithPaths = [ filename:join([DocRoot, F]) || F <- AllRefs ],
  166. Existing = [F || F <- WithPaths, filelib:is_regular(F)],
  167. New = sets:subtract(sets:from_list(Existing), Seen),
  168. case sets:size(New) of
  169. 0 -> Seen;
  170. _ -> referenced_dtls1(sets:to_list(New), Config,
  171. sets:union(New, Seen))
  172. end.