summaryrefslogtreecommitdiff
path: root/src/rebar_compiler_yrl.erl
blob: 765ea36445f3103bac544898738edbd50c2891f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
-module(rebar_compiler_yrl).

-behaviour(rebar_compiler).

-export([context/1,
         needed_files/3,
         dependencies/3,
         compile/4,
         clean/2]).

context(AppInfo) ->
    Dir = rebar_app_info:dir(AppInfo),
    Mappings = [{".erl", filename:join([Dir, "src"])}],
    #{src_dirs => ["src"],
      include_dirs => [],
      src_ext => ".yrl",
      out_mappings => Mappings}.

needed_files(_, FoundFiles, AppInfo) ->
    FirstFiles = [],

    %% Remove first files from found files
    RestFiles = [Source || Source <- FoundFiles, not lists:member(Source, FirstFiles)],

    Opts = rebar_opts:get(rebar_app_info:opts(AppInfo), yrl_opts, []),
    {{FirstFiles, Opts}, {RestFiles, Opts}}.

dependencies(_, _, _) ->
    [].

compile(Source, [{_, OutDir}], _, Opts) ->
    BaseName = filename:basename(Source),
    Target = filename:join([OutDir, BaseName]),
    AllOpts = [{parserfile, Target} | Opts],
    AllOpts1 = [{includefile, filename:join(OutDir, I)} || {includefile, I} <- AllOpts,
                                                           filename:pathtype(I) =:= relative],
    case yeec:file(Source, AllOpts1 ++ [{return, true}]) of
        {ok, _} ->
            ok;
        {ok, _Mod, Ws} ->
            rebar_compiler:ok_tuple(Source, Ws);
        {error, Es, Ws} ->
            rebar_compiler:error_tuple(Source, Es, Ws, AllOpts1)
    end.

clean(YrlFiles, _AppInfo) ->
    rebar_file_utils:delete_each(
      [rebar_utils:to_list(re:replace(F, "\\.yrl$", ".erl", [unicode]))
       || F <- YrlFiles]).