You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

261 lines
9.9 KiB

пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
  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'. It can be a list of name-value tuples or a list of
  34. %% lists of name-value tuples if you have multiple template directories
  35. %% that need to have different settings (see example below).
  36. %%
  37. %% Available options include:
  38. %%
  39. %% doc_root: where to find templates to compile
  40. %% "templates" by default
  41. %%
  42. %% out_dir: where to put compiled template beam files
  43. %% "ebin" by default
  44. %%
  45. %% source_ext: the file extension the template sources have
  46. %% ".dtl" by default
  47. %%
  48. %% module_ext: characters to append to the template's module name
  49. %% "_dtl" by default
  50. %%
  51. %% recursive: boolean that determines if doc_root(s) need to be
  52. %% scanned recursively for matching template file names
  53. %% (default: true).
  54. %% For example, if you had:
  55. %% /t_src/
  56. %% base.html
  57. %% foo.html
  58. %%
  59. %% And you wanted them compiled to:
  60. %% /priv/
  61. %% base.beam
  62. %% foo.beam
  63. %%
  64. %% You would add to your rebar.config:
  65. %% {erlydtl_opts, [
  66. %% {doc_root, "t_src"},
  67. %% {out_dir, "priv"},
  68. %% {source_ext, ".html"},
  69. %% {module_ext, ""}
  70. %% ]}.
  71. %%
  72. %% The default settings are the equivalent of:
  73. %% {erlydtl_opts, [
  74. %% {doc_root, "templates"},
  75. %% {out_dir, "ebin"},
  76. %% {source_ext, ".dtl"},
  77. %% {module_ext, "_dtl"}
  78. %% ]}.
  79. %%
  80. %% The following example will compile the following templates:
  81. %% "src/*.dtl" files into "ebin/*_dtl.beam" and
  82. %% "templates/*.html" into "ebin/*.beam". Note that any tuple option
  83. %% (such as 'out_dir') in the outer list is added to each inner list:
  84. %% {erlydtl_opts, [
  85. %% {out_dir, "ebin"},
  86. %% {recursive, false},
  87. %% [
  88. %% {doc_root, "src"}, {module_ext, "_dtl"}
  89. %% ],
  90. %% [
  91. %% {doc_root, "templates"}, {module_ext, ""}, {source_ext, ".html"}
  92. %% ]
  93. %% ]}.
  94. -module(rebar_prv_erlydtl_compiler).
  95. -behaviour(provider).
  96. -export([init/1,
  97. do/1,
  98. format_error/1]).
  99. -include("rebar.hrl").
  100. -include_lib("providers/include/providers.hrl").
  101. -define(PROVIDER, compile).
  102. -define(DEPS, [{default, compile}]).
  103. %% ===================================================================
  104. %% Public API
  105. %% ===================================================================
  106. -spec init(rebar_state:t()) -> {ok, rebar_state:t()}.
  107. init(State) ->
  108. State1 = rebar_state:add_provider(State, providers:create([{name, ?PROVIDER},
  109. {module, ?MODULE},
  110. {namespace, erlydtl},
  111. {bare, false},
  112. {deps, ?DEPS},
  113. {example, "rebar3 erlydtl compile"},
  114. {short_desc, "Compile erlydtl templates."},
  115. {desc, ""},
  116. {opts, []}])),
  117. {ok, State1}.
  118. do(State) ->
  119. ?INFO("Running erlydtl...", []),
  120. DtlOpts = proplists:unfold(rebar_state:get(State, erlydtl_opts, [])),
  121. %% We need a project app to store the results under in _build
  122. %% If there is more than 1 project app, check for an app config
  123. %% if that doesn't exist, error out.
  124. App1 = case rebar_state:project_apps(State) of
  125. [App] ->
  126. App;
  127. Apps ->
  128. case option(app, DtlOpts) of
  129. undefined ->
  130. ?PRV_ERROR(no_main_app);
  131. Name ->
  132. rebar_app_utils:find(Name, Apps)
  133. end
  134. end,
  135. Dir = rebar_app_info:dir(App1),
  136. OutDir = rebar_app_info:ebin_dir(App1),
  137. rebar_base_compiler:run(State,
  138. [],
  139. filename:join(Dir, option(doc_root, DtlOpts)),
  140. option(source_ext, DtlOpts),
  141. OutDir,
  142. option(module_ext, DtlOpts) ++ ".beam",
  143. fun(S, T, C) ->
  144. compile_dtl(C, S, T, DtlOpts, Dir, OutDir)
  145. end,
  146. [{check_last_mod, false},
  147. {recursive, option(recursive, DtlOpts)}]),
  148. {ok, State}.
  149. -spec format_error(any()) -> iolist().
  150. format_error(no_main_app) ->
  151. "Erlydtl Error: Multiple project apps found and no {app, atom()} option found in erlydtl_opts.";
  152. format_error(Reason) ->
  153. io_lib:format("~p", [Reason]).
  154. %% ===================================================================
  155. %% Internal functions
  156. %% ===================================================================
  157. option(Opt, DtlOpts) ->
  158. proplists:get_value(Opt, DtlOpts, default(Opt)).
  159. default(app) -> undefined;
  160. default(doc_root) -> "priv/templates";
  161. default(out_dir) -> "ebin";
  162. default(source_ext) -> ".dtl";
  163. default(module_ext) -> "_dtl";
  164. default(custom_tags_dir) -> "";
  165. default(compiler_options) -> [return];
  166. default(recursive) -> true.
  167. compile_dtl(State, Source, Target, DtlOpts, Dir, OutDir) ->
  168. case needs_compile(Source, Target, DtlOpts) of
  169. true ->
  170. do_compile(State, Source, Target, DtlOpts, Dir, OutDir);
  171. false ->
  172. skipped
  173. end.
  174. do_compile(State, Source, Target, DtlOpts, Dir, OutDir) ->
  175. CompilerOptions = option(compiler_options, DtlOpts),
  176. Sorted = proplists:unfold(
  177. lists:sort(
  178. [{out_dir, OutDir},
  179. {doc_root, filename:join(Dir, option(doc_root, DtlOpts))},
  180. {custom_tags_dir, option(custom_tags_dir, DtlOpts)},
  181. {compiler_options, CompilerOptions}])),
  182. %% ensure that doc_root and out_dir are defined,
  183. %% using defaults if necessary
  184. Opts = lists:ukeymerge(1, DtlOpts, Sorted),
  185. ?DEBUG("Compiling \"~s\" -> \"~s\" with options:~n ~s",
  186. [Source, Target, io_lib:format("~p", [Opts])]),
  187. case erlydtl:compile_file(ec_cnv:to_list(Source),
  188. list_to_atom(module_name(Target)),
  189. Opts) of
  190. {ok, _Mod} ->
  191. ok;
  192. {ok, _Mod, Ws} ->
  193. rebar_base_compiler:ok_tuple(State, Source, Ws);
  194. error ->
  195. rebar_base_compiler:error_tuple(State, Source, [], [], Opts);
  196. {error, Es, Ws} ->
  197. rebar_base_compiler:error_tuple(State, Source, Es, Ws, Opts)
  198. end.
  199. module_name(Target) ->
  200. filename:rootname(filename:basename(Target), ".beam").
  201. needs_compile(Source, Target, DtlOpts) ->
  202. LM = filelib:last_modified(Target),
  203. LM < filelib:last_modified(Source) orelse
  204. lists:any(fun(D) -> LM < filelib:last_modified(D) end,
  205. referenced_dtls(Source, DtlOpts)).
  206. referenced_dtls(Source, DtlOpts) ->
  207. DtlOpts1 = lists:keyreplace(doc_root, 1, DtlOpts,
  208. {doc_root, filename:dirname(Source)}),
  209. Set = referenced_dtls1([Source], DtlOpts1,
  210. sets:add_element(Source, sets:new())),
  211. sets:to_list(sets:del_element(Source, Set)).
  212. referenced_dtls1(Step, DtlOpts, Seen) ->
  213. ExtMatch = re:replace(option(source_ext, DtlOpts), "\.", "\\\\\\\\.",
  214. [{return, list}]),
  215. ShOpts = [{use_stdout, false}, return_on_error],
  216. AllRefs =
  217. lists:append(
  218. [begin
  219. Cmd = lists:flatten(["grep -o [^\\\"]*\\",
  220. ExtMatch, "[^\\\"]* ", F]),
  221. case rebar_utils:sh(Cmd, ShOpts) of
  222. {ok, Res} ->
  223. string:tokens(Res, "\n");
  224. {error, _} ->
  225. ""
  226. end
  227. end || F <- Step]),
  228. DocRoot = option(doc_root, DtlOpts),
  229. WithPaths = [ filename:join([DocRoot, F]) || F <- AllRefs ],
  230. ?DEBUG("All deps: ~p\n", [WithPaths]),
  231. Existing = [F || F <- WithPaths, filelib:is_regular(F)],
  232. New = sets:subtract(sets:from_list(Existing), Seen),
  233. case sets:size(New) of
  234. 0 -> Seen;
  235. _ -> referenced_dtls1(sets:to_list(New), DtlOpts,
  236. sets:union(New, Seen))
  237. end.