Skip to content
This repository
Browse code

Merge branch 'ates-diameter'

  • Loading branch information...
commit bf35ec939214faef0f33c5f7a04c94bde943546a 2 parents 7dad2b3 + 473843d
Dave Smith dizzyd authored
2  ebin/rebar.app
@@ -9,6 +9,7 @@
9 9 rebar_app_utils,
10 10 rebar_appups,
11 11 rebar_asn1_compiler,
  12 + rebar_dia_compiler,
12 13 rebar_base_compiler,
13 14 rebar_cleaner,
14 15 rebar_config,
@@ -67,6 +68,7 @@
67 68 rebar_protobuffs_compiler,
68 69 rebar_neotoma_compiler,
69 70 rebar_asn1_compiler,
  71 + rebar_dia_compiler,
70 72 rebar_erlc_compiler,
71 73 rebar_lfe_compiler,
72 74 rebar_erlydtl_compiler,
5 rebar.config
@@ -15,5 +15,8 @@
15 15 - (\"erlydtl\":\"compile\"/\"3\")
16 16 - (\"lfe_comp\":\"file\"/\"2\")
17 17 - (\"neotoma\":\"file\"/\"2\")
18   - - (\"protobuffs_compile\":\"scan_file\"/\"2\"))",
  18 + - (\"protobuffs_compile\":\"scan_file\"/\"2\")
  19 + - (\"diameter_codegen\":\"from_dict\"/\"4\")
  20 + - (\"diameter_dict_util\":\"format_error\"/\"1\")
  21 + - (\"diameter_dict_util\":\"parse\"/\"2\"))",
19 22 []}]}.
86 src/rebar_dia_compiler.erl
... ... @@ -0,0 +1,86 @@
  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 +
  29 +-export([compile/2, clean/2]).
  30 +
  31 +-include("rebar.hrl").
  32 +
  33 +%% ===================================================================
  34 +%% Public API
  35 +%% ===================================================================
  36 +
  37 +-spec compile(rebar_config:config(), file:filename()) -> 'ok'.
  38 +compile(Config, _AppFile) ->
  39 + rebar_base_compiler:run(Config, filelib:wildcard("dia/*.dia"),
  40 + "dia", ".dia", "src", ".erl",
  41 + fun compile_dia/3).
  42 +
  43 +-spec clean(rebar_config:config(), file:filename()) -> 'ok'.
  44 +clean(_Config, _AppFile) ->
  45 + GeneratedFiles = dia_generated_files("dia", "src", "include"),
  46 + ok = rebar_file_utils:delete_each(GeneratedFiles),
  47 + ok.
  48 +
  49 +-spec compile_dia(file:filename(), file:filename(),
  50 + rebar_config:config()) -> ok.
  51 +compile_dia(Source, Target, Config) ->
  52 + ok = filelib:ensure_dir(Target),
  53 + ok = filelib:ensure_dir(filename:join("include", "dummy.hrl")),
  54 + Opts = [{outdir, "src"}] ++ rebar_config:get(Config, dia_opts, []),
  55 + case diameter_dict_util:parse({path, Source}, []) of
  56 + {ok, Spec} ->
  57 + FileName = dia_filename(Source, Spec),
  58 + diameter_codegen:from_dict(FileName, Spec, Opts, erl),
  59 + diameter_codegen:from_dict(FileName, Spec, Opts, hrl),
  60 + HrlFile = filename:join("src", FileName ++ ".hrl"),
  61 + case filelib:is_regular(HrlFile) of
  62 + true ->
  63 + ok = rebar_file_utils:mv(HrlFile, "include");
  64 + false ->
  65 + ok
  66 + end;
  67 + {error, Reason} ->
  68 + ?ERROR("~s~n", [diameter_dict_util:format_error(Reason)])
  69 + end.
  70 +
  71 +dia_generated_files(DiaDir, SrcDir, IncDir) ->
  72 + F = fun(File, Acc) ->
  73 + {ok, Spec} = diameter_dict_util:parse({path, File}, []),
  74 + FileName = dia_filename(File, Spec),
  75 + [filename:join([IncDir, FileName ++ ".hrl"]) |
  76 + filelib:wildcard(filename:join([SrcDir, FileName ++ ".*"]))] ++ Acc
  77 + end,
  78 + lists:foldl(F, [], filelib:wildcard(filename:join([DiaDir, "*.dia"]))).
  79 +
  80 +dia_filename(File, Spec) ->
  81 + case proplists:get_value(name, Spec) of
  82 + undefined ->
  83 + filename:rootname(filename:basename(File));
  84 + Name ->
  85 + Name
  86 + end.

0 comments on commit bf35ec9

Please sign in to comment.
Something went wrong with that request. Please try again.