Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

147 rader
5.7 KiB

14 år sedan
14 år sedan
14 år sedan
14 år sedan
14 år sedan
14 år sedan
14 år sedan
14 år sedan
  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. ?ERROR("~n===============================================~n"
  68. " You need to install neotoma to compile PEG grammars~n"
  69. " Download the latest tarball release from github~n"
  70. " https://github.com/seancribbs/neotoma~n"
  71. " and install it into your erlang library dir~n"
  72. "===============================================~n~n", []),
  73. ?FAIL;
  74. _ ->
  75. case needs_compile(Source, Target, Config) of
  76. true ->
  77. do_compile(Source, Target, Config);
  78. false ->
  79. skipped
  80. end
  81. end.
  82. do_compile(Source, _Target, Config) ->
  83. %% TODO: Check last mod on target and referenced DTLs here..
  84. NeoOpts = neotoma_opts(Config),
  85. %% ensure that doc_root and out_dir are defined,
  86. %% using defaults if necessary
  87. Opts = [{output, option(out_dir, NeoOpts)},
  88. {module, list_to_atom(filename:basename(Source, ".peg")
  89. ++ option(module_ext, NeoOpts))}],
  90. case neotoma:file(Source, Opts ++ NeoOpts) of
  91. ok ->
  92. ok;
  93. Reason ->
  94. ?ERROR("Compiling peg ~s failed:~n ~p~n",
  95. [Source, Reason]),
  96. ?FAIL
  97. end.
  98. needs_compile(Source, Target, Config) ->
  99. LM = filelib:last_modified(Target),
  100. LM < filelib:last_modified(Source) orelse
  101. lists:any(fun(D) -> LM < filelib:last_modified(D) end,
  102. referenced_pegs(Source, Config)).
  103. referenced_pegs(Source, Config) ->
  104. Set = referenced_pegs1([Source], Config,
  105. sets:add_element(Source, sets:new())),
  106. sets:to_list(sets:del_element(Source, Set)).
  107. referenced_pegs1(Step, Config, Seen) ->
  108. NeoOpts = neotoma_opts(Config),
  109. ExtMatch = re:replace(option(source_ext, NeoOpts), "\.", "\\\\\\\\.",
  110. [{return, list}]),
  111. ShOpts = [{use_stdout, false}, return_on_error],
  112. AllRefs =
  113. lists:append(
  114. [begin
  115. Cmd = lists:flatten(["grep -o [^\\\"]*",
  116. ExtMatch, " ", F]),
  117. case rebar_utils:sh(Cmd, ShOpts) of
  118. {ok, Res} ->
  119. string:tokens(Res, "\n");
  120. {error, _} ->
  121. ""
  122. end
  123. end || F <- Step]),
  124. DocRoot = option(doc_root, NeoOpts),
  125. WithPaths = [ filename:join([DocRoot, F]) || F <- AllRefs ],
  126. Existing = [F || F <- WithPaths, filelib:is_regular(F)],
  127. New = sets:subtract(sets:from_list(Existing), Seen),
  128. case sets:size(New) of
  129. 0 -> Seen;
  130. _ -> referenced_pegs1(sets:to_list(New), Config,
  131. sets:union(New, Seen))
  132. end.