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) 2015 Tomas Abrahamsson (tomas.abrahamsson@gmail.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(gpb_compile).
28-export([file/2]).
29
30%% Simulate gpb compiling some proto files,
31%% but generate only enough of what's needed for testing -- dummy stuff only.
32%% if a bad.proto file is supplied then gpb fails
33file(Proto, Opts) ->
34    ok = case filename:basename(Proto) of
35            "bad.proto" -> error;
36            _ -> ok
37         end,
38    Prefix = proplists:get_value(module_name_prefix, Opts, ""),
39    Suffix = proplists:get_value(module_name_suffix, Opts, ""),
40    ProtoBase = filename:basename(Proto, ".proto"),
41    ModBase = Prefix ++ ProtoBase ++ Suffix,
42    ErlDest = filename:join(get_erl_outdir(Opts), ModBase ++ ".erl"),
43    HrlDest = filename:join(get_hrl_outdir(Opts), ModBase ++ ".hrl"),
44    ok = file:write_file(ErlDest, erl_text(ModBase)),
45    ok = file:write_file(HrlDest, hrl_text(ModBase)).
46
47erl_text(ModBase) ->
48    io_lib:format(
49      lines(["-module(~p).",
50             "-export([encode_msg/1]).",
51             "-export([decode_msg/2]).",
52             "",
53             "encode_msg(some_dummy_msg) -> <<1,2,3>>.",
54             "",
55             "decode_msg(<<1,2,3>>, _) -> some_dummy_msg."]),
56      [list_to_atom(ModBase)]).
57
58hrl_text(ModBase) ->
59    io_lib:format(
60      lines(["-ifndef(~s_hrl).",
61             "-define(~s_hrl, true).",
62             "",
63             "%% some record definitions would normally go here...",
64             ""
65             "-endif. %% ~s_hrl"]),
66      [ModBase, ModBase, ModBase]).
67
68get_erl_outdir(Opts) ->
69    proplists:get_value(o_erl, Opts, get_outdir(Opts)).
70
71get_hrl_outdir(Opts) ->
72    proplists:get_value(o_hrl, Opts, get_outdir(Opts)).
73
74get_outdir(Opts) ->
75    proplists:get_value(o, Opts, ".").
76
77lines(Lines) ->
78    lists:flatten([[L, $\n] || L <- Lines]).
79