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

163 行
6.1 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) 2010 Cliff Moon (cliff@moonpolysoft.com)
  8. %%
  9. %% Permission is hereby granted, free of charge, to any person obtaining a copy
  10. %% of this software and associated documentation files (the "Software"), to deal
  11. %% in the Software without restriction, including without limitation the rights
  12. %% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. %% copies of the Software, and to permit persons to whom the Software is
  14. %% furnished to do so, subject to the following conditions:
  15. %%
  16. %% The above copyright notice and this permission notice shall be included in
  17. %% all copies or substantial portions of the Software.
  18. %%
  19. %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. %% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. %% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. %% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. %% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. %% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. %% THE SOFTWARE.
  26. %% -------------------------------------------------------------------
  27. %% The rebar_neotoma module is a plugin for rebar that compiles
  28. %% neotoma peg files. By default, it compiles all src/*.peg to src/*.erl
  29. %%
  30. %% Configuration options should be placed in rebar.config under
  31. %% neotoma_opts. Available options include:
  32. %%
  33. %% doc_root: where to find the peg files to compile.
  34. %% "src" by default
  35. %% out_dir: where to put the generated erl files.
  36. %% "src" by defualt
  37. %% module_ext: characters to append to the module's name.
  38. %% "" by default
  39. %% source_ext: extension of peg source files
  40. -module(rebar_neotoma_compiler).
  41. -export([compile/2]).
  42. %% for internal use only
  43. -export([info/2]).
  44. -include("rebar.hrl").
  45. %% ============================================================================
  46. %% Public API
  47. %% ============================================================================
  48. compile(Config, _AppFile) ->
  49. NeoOpts = neotoma_opts(Config),
  50. rebar_base_compiler:run(Config, [],
  51. option(doc_root, NeoOpts), ".peg",
  52. option(out_dir, NeoOpts),
  53. option(module_ext, NeoOpts) ++ ".erl",
  54. fun compile_neo/3, [{check_last_mod, true}]).
  55. %% ============================================================================
  56. %% Internal functions
  57. %% ============================================================================
  58. info(help, compile) ->
  59. ?CONSOLE(
  60. "Build Neotoma (*.peg) sources.~n"
  61. "~n"
  62. "Valid rebar.config options:~n"
  63. " ~p~n",
  64. [
  65. {neotoma_opts, [{doc_root, "src"},
  66. {out_dir, "src"},
  67. {source_ext, ".peg"},
  68. {module_ext, ""}]}
  69. ]).
  70. neotoma_opts(Config) ->
  71. rebar_config:get(Config, neotoma_opts, []).
  72. option(Opt, Options) ->
  73. proplists:get_value(Opt, Options, default(Opt)).
  74. default(doc_root) -> "src";
  75. default(out_dir) -> "src";
  76. default(module_ext) -> "";
  77. default(source_ext) -> ".peg".
  78. compile_neo(Source, Target, Config) ->
  79. case code:which(neotoma) of
  80. non_existing ->
  81. ?ERROR("~n===============================================~n"
  82. " You need to install neotoma to compile PEG grammars~n"
  83. " Download the latest tarball release from github~n"
  84. " https://github.com/seancribbs/neotoma~n"
  85. " and install it into your erlang library dir~n"
  86. "===============================================~n~n", []),
  87. ?FAIL;
  88. _ ->
  89. case needs_compile(Source, Target, Config) of
  90. true ->
  91. do_compile(Source, Target, Config);
  92. false ->
  93. skipped
  94. end
  95. end.
  96. do_compile(Source, _Target, Config) ->
  97. %% TODO: Check last mod on target and referenced DTLs here..
  98. NeoOpts = neotoma_opts(Config),
  99. %% ensure that doc_root and out_dir are defined,
  100. %% using defaults if necessary
  101. Opts = [{output, option(out_dir, NeoOpts)},
  102. {module, list_to_atom(filename:basename(Source, ".peg")
  103. ++ option(module_ext, NeoOpts))}],
  104. case neotoma:file(Source, Opts ++ NeoOpts) of
  105. ok ->
  106. ok;
  107. Reason ->
  108. ?ERROR("Compiling peg ~s failed:~n ~p~n",
  109. [Source, Reason]),
  110. ?FAIL
  111. end.
  112. needs_compile(Source, Target, Config) ->
  113. LM = filelib:last_modified(Target),
  114. LM < filelib:last_modified(Source) orelse
  115. lists:any(fun(D) -> LM < filelib:last_modified(D) end,
  116. referenced_pegs(Source, Config)).
  117. referenced_pegs(Source, Config) ->
  118. Set = referenced_pegs1([Source], Config,
  119. sets:add_element(Source, sets:new())),
  120. sets:to_list(sets:del_element(Source, Set)).
  121. referenced_pegs1(Step, Config, Seen) ->
  122. NeoOpts = neotoma_opts(Config),
  123. ExtMatch = re:replace(option(source_ext, NeoOpts), "\.", "\\\\\\\\.",
  124. [{return, list}]),
  125. ShOpts = [{use_stdout, false}, return_on_error],
  126. AllRefs =
  127. lists:append(
  128. [begin
  129. Cmd = lists:flatten(["grep -o [^\\\"]*",
  130. ExtMatch, " ", F]),
  131. case rebar_utils:sh(Cmd, ShOpts) of
  132. {ok, Res} ->
  133. string:tokens(Res, "\n");
  134. {error, _} ->
  135. ""
  136. end
  137. end || F <- Step]),
  138. DocRoot = option(doc_root, NeoOpts),
  139. WithPaths = [ filename:join([DocRoot, F]) || F <- AllRefs ],
  140. Existing = [F || F <- WithPaths, filelib:is_regular(F)],
  141. New = sets:subtract(sets:from_list(Existing), Seen),
  142. case sets:size(New) of
  143. 0 -> Seen;
  144. _ -> referenced_pegs1(sets:to_list(New), Config,
  145. sets:union(New, Seen))
  146. end.