25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
5.7 KiB

14 년 전
14 년 전
14 년 전
14 년 전
14 년 전
14 년 전
14 년 전
14 년 전
  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. -include("rebar.hrl").
  43. %% ============================================================================
  44. %% Public API
  45. %% ============================================================================
  46. compile(Config, _AppFile) ->
  47. NeoOpts = neotoma_opts(Config),
  48. rebar_base_compiler:run(Config, [],
  49. option(doc_root, NeoOpts), ".peg",
  50. option(out_dir, NeoOpts),
  51. option(module_ext, NeoOpts) ++ ".beam",
  52. fun compile_neo/3, [{check_last_mod,false}]).
  53. %% ============================================================================
  54. %% Public API
  55. %% ============================================================================
  56. neotoma_opts(Config) ->
  57. rebar_config:get(Config, neotoma_opts, []).
  58. option(Opt, Options) ->
  59. proplists:get_value(Opt, Options, default(Opt)).
  60. default(doc_root) -> "src";
  61. default(out_dir) -> "src";
  62. default(module_ext) -> "";
  63. default(source_ext) -> ".peg".
  64. compile_neo(Source, Target, Config) ->
  65. case code:which(neotoma) of
  66. non_existing ->
  67. ?CONSOLE(
  68. <<"~n===============================================~n"
  69. " You need to install neotoma to compile PEG grammars~n"
  70. " Download the latest tarball release from github~n"
  71. " https://github.com/seancribbs/neotoma~n"
  72. " and install it into your erlang library dir~n"
  73. "===============================================~n~n">>, []),
  74. ?FAIL;
  75. _ ->
  76. case needs_compile(Source, Target, Config) of
  77. true ->
  78. do_compile(Source, Target, Config);
  79. false ->
  80. skipped
  81. end
  82. end.
  83. do_compile(Source, _Target, Config) ->
  84. %% TODO: Check last mod on target and referenced DTLs here..
  85. NeoOpts = neotoma_opts(Config),
  86. %% ensure that doc_root and out_dir are defined,
  87. %% using defaults if necessary
  88. Opts = [{output, option(out_dir, NeoOpts)},
  89. {module, list_to_atom(filename:basename(Source, ".peg")
  90. ++ option(module_ext, NeoOpts))}],
  91. case neotoma:file(Source, Opts ++ NeoOpts) of
  92. ok ->
  93. ok;
  94. Reason ->
  95. ?CONSOLE("Compiling peg ~s failed:~n ~p~n",
  96. [Source, Reason]),
  97. ?FAIL
  98. end.
  99. needs_compile(Source, Target, Config) ->
  100. LM = filelib:last_modified(Target),
  101. LM < filelib:last_modified(Source) orelse
  102. lists:any(fun(D) -> LM < filelib:last_modified(D) end,
  103. referenced_pegs(Source, Config)).
  104. referenced_pegs(Source, Config) ->
  105. Set = referenced_pegs1([Source], Config,
  106. sets:add_element(Source, sets:new())),
  107. sets:to_list(sets:del_element(Source, Set)).
  108. referenced_pegs1(Step, Config, Seen) ->
  109. NeoOpts = neotoma_opts(Config),
  110. ExtMatch = re:replace(option(source_ext, NeoOpts), "\.", "\\\\\\\\.",
  111. [{return, list}]),
  112. ShOpts = [{use_stdout, false}, return_on_error],
  113. AllRefs =
  114. lists:append(
  115. [begin
  116. Cmd = lists:flatten(["grep -o [^\\\"]*",
  117. ExtMatch, " ", F]),
  118. case rebar_utils:sh(Cmd, ShOpts) of
  119. {ok, Res} ->
  120. string:tokens(Res, "\n");
  121. {error, _} ->
  122. ""
  123. end
  124. end || F <- Step]),
  125. DocRoot = option(doc_root, NeoOpts),
  126. WithPaths = [ filename:join([DocRoot, F]) || F <- AllRefs ],
  127. Existing = [F || F <- WithPaths, filelib:is_regular(F)],
  128. New = sets:subtract(sets:from_list(Existing), Seen),
  129. case sets:size(New) of
  130. 0 -> Seen;
  131. _ -> referenced_pegs1(sets:to_list(New), Config,
  132. sets:union(New, Seen))
  133. end.