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

187 行
6.7 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), option(source_ext, DtlOpts),
  82. option(out_dir, DtlOpts), option(module_ext, DtlOpts) ++ ".beam",
  83. fun compile_dtl/3, [{check_last_mod, false}]).
  84. %% ===================================================================
  85. %% Internal functions
  86. %% ===================================================================
  87. erlydtl_opts(Config) ->
  88. rebar_config:get(Config, erlydtl_opts, []).
  89. option(Opt, DtlOpts) ->
  90. proplists:get_value(Opt, DtlOpts, default(Opt)).
  91. default(doc_root) -> "templates";
  92. default(out_dir) -> "ebin";
  93. default(source_ext) -> ".dtl";
  94. default(module_ext) -> "_dtl";
  95. default(custom_tags_dir) -> "".
  96. compile_dtl(Source, Target, Config) ->
  97. case code:which(erlydtl) of
  98. non_existing ->
  99. ?CONSOLE(
  100. "~n===============================================~n"
  101. " You need to install erlydtl to compile DTL templates~n"
  102. " Download the latest tarball release from github~n"
  103. " http://code.google.com/p/erlydtl/~n"
  104. " and install it into your erlang library dir~n"
  105. "===============================================~n~n", []),
  106. ?FAIL;
  107. _ ->
  108. case needs_compile(Source, Target, Config) of
  109. true ->
  110. do_compile(Source, Target, Config);
  111. false ->
  112. skipped
  113. end
  114. end.
  115. do_compile(Source, Target, Config) ->
  116. %% TODO: Check last mod on target and referenced DTLs here..
  117. DtlOpts = erlydtl_opts(Config),
  118. %% ensure that doc_root and out_dir are defined,
  119. %% using defaults if necessary
  120. Opts = [{out_dir, option(out_dir, DtlOpts)},
  121. {doc_root, option(doc_root, DtlOpts)},
  122. {custom_tags_dir, option(custom_tags_dir, DtlOpts)},
  123. report, return],
  124. case erlydtl:compile(Source,
  125. module_name(Target),
  126. Opts++DtlOpts) of
  127. ok -> ok;
  128. Reason ->
  129. ?CONSOLE("Compiling template ~s failed:~n ~p~n",
  130. [Source, Reason]),
  131. ?FAIL
  132. end.
  133. module_name(Target) ->
  134. F = filename:basename(Target),
  135. string:substr(F, 1, length(F)-length(".beam")).
  136. needs_compile(Source, Target, Config) ->
  137. LM = filelib:last_modified(Target),
  138. LM < filelib:last_modified(Source) orelse
  139. lists:any(fun(D) -> LM < filelib:last_modified(D) end,
  140. referenced_dtls(Source, Config)).
  141. referenced_dtls(Source, Config) ->
  142. Set = referenced_dtls1([Source], Config,
  143. sets:add_element(Source, sets:new())),
  144. sets:to_list(sets:del_element(Source, Set)).
  145. referenced_dtls1(Step, Config, Seen) ->
  146. DtlOpts = erlydtl_opts(Config),
  147. ExtMatch = re:replace(option(source_ext, DtlOpts), "\.", "\\\\\\\\.",
  148. [{return, list}]),
  149. ShOpts = [{use_stdout, false}, return_on_error],
  150. AllRefs =
  151. lists:append(
  152. [begin
  153. Cmd = lists:flatten(["grep -o [^\\\"]*",
  154. ExtMatch, " ", F]),
  155. case rebar_utils:sh(Cmd, ShOpts) of
  156. {ok, Res} ->
  157. string:tokens(Res, "\n");
  158. {error, _} ->
  159. ""
  160. end
  161. end || F <- Step]),
  162. DocRoot = option(doc_root, DtlOpts),
  163. WithPaths = [ filename:join([DocRoot, F]) || F <- AllRefs ],
  164. Existing = [F || F <- WithPaths, filelib:is_regular(F)],
  165. New = sets:subtract(sets:from_list(Existing), Seen),
  166. case sets:size(New) of
  167. 0 -> Seen;
  168. _ -> referenced_dtls1(sets:to_list(New), Config,
  169. sets:union(New, Seen))
  170. end.