Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

106 рядки
4.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) 2009, 2010 Dave Smith (dizzyd@dizzyd.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. -module(rebar_dia_compiler).
  28. -export([compile/2, clean/2]).
  29. %% for internal use only
  30. -export([info/2]).
  31. -include("rebar.hrl").
  32. %% ===================================================================
  33. %% Public API
  34. %% ===================================================================
  35. -spec compile(rebar_config:config(), file:filename()) -> 'ok'.
  36. compile(Config, _AppFile) ->
  37. rebar_base_compiler:run(Config, filelib:wildcard("dia/*.dia"),
  38. "dia", ".dia", "src", ".erl",
  39. fun compile_dia/3).
  40. -spec clean(rebar_config:config(), file:filename()) -> 'ok'.
  41. clean(_Config, _AppFile) ->
  42. GeneratedFiles = dia_generated_files("dia", "src", "include"),
  43. ok = rebar_file_utils:delete_each(GeneratedFiles),
  44. ok.
  45. %% ===================================================================
  46. %% Internal functions
  47. %% ===================================================================
  48. info(help, compile) ->
  49. info_help("Build Diameter (*.dia) sources");
  50. info(help, clean) ->
  51. info_help("Delete generated Diameter files").
  52. info_help(Description) ->
  53. ?CONSOLE(
  54. "~s.~n"
  55. "~n"
  56. "Valid rebar.config options:~n"
  57. " {dia_opts, []} (see diameter_codegen:from_dict/4 documentation)~n",
  58. [Description]).
  59. -spec compile_dia(file:filename(), file:filename(),
  60. rebar_config:config()) -> ok.
  61. compile_dia(Source, Target, Config) ->
  62. ok = filelib:ensure_dir(Target),
  63. ok = filelib:ensure_dir(filename:join("include", "dummy.hrl")),
  64. Opts = [{outdir, "src"}] ++ rebar_config:get(Config, dia_opts, []),
  65. case diameter_dict_util:parse({path, Source}, []) of
  66. {ok, Spec} ->
  67. FileName = dia_filename(Source, Spec),
  68. _ = diameter_codegen:from_dict(FileName, Spec, Opts, erl),
  69. _ = diameter_codegen:from_dict(FileName, Spec, Opts, hrl),
  70. HrlFile = filename:join("src", FileName ++ ".hrl"),
  71. case filelib:is_regular(HrlFile) of
  72. true ->
  73. ok = rebar_file_utils:mv(HrlFile, "include");
  74. false ->
  75. ok
  76. end;
  77. {error, Reason} ->
  78. ?ERROR("~s~n", [diameter_dict_util:format_error(Reason)])
  79. end.
  80. dia_generated_files(DiaDir, SrcDir, IncDir) ->
  81. F = fun(File, Acc) ->
  82. {ok, Spec} = diameter_dict_util:parse({path, File}, []),
  83. FileName = dia_filename(File, Spec),
  84. [filename:join([IncDir, FileName ++ ".hrl"]) |
  85. filelib:wildcard(filename:join([SrcDir, FileName ++ ".*"]))] ++ Acc
  86. end,
  87. lists:foldl(F, [], filelib:wildcard(filename:join([DiaDir, "*.dia"]))).
  88. dia_filename(File, Spec) ->
  89. case proplists:get_value(name, Spec) of
  90. undefined ->
  91. filename:rootname(filename:basename(File));
  92. Name ->
  93. Name
  94. end.