basho / rebar

Erlang Build Tools -- Please file bug and feature requests at http://issues.basho.com.

Clone this repository (size: 432.4 KB): HTTPS / SSH
$ hg clone http://hg.basho.com/rebar
commit 185: 2bce82b41e54
parent 184: efd76a2b9786
branch: default
Add forward-compatible escript_foldl function escript:foldl/3 was undocumented and has been replaced with better APIs post-R13B04. The new exported funs are officially documented.
Tuncer Ayaz / tuncer
5 months ago

Changed (Δ1.1 KB):

raw changeset »

src/rebar_templater.erl (5 lines added, 2 lines removed)

src/rebar_utils.erl (28 lines added, 1 lines removed)

Up to file-list src/rebar_templater.erl:

@@ -110,8 +110,11 @@ create(_Config, _) ->
110
110
%% Scan the current escript for available files and cache in pdict.
111
111
%%
112
112
cache_escript_files() ->
113
    {ok, Files} = escript:foldl(fun(Name, _, GetBin, Acc) -> [{Name, GetBin()} | Acc] end,
114
                                [], rebar_config:get_global(escript, undefined)),
113
    {ok, Files} = rebar_utils:escript_foldl(
114
                      fun(Name, _, GetBin, Acc) ->
115
                              [{Name, GetBin()} | Acc]
116
                      end,
117
                      [], rebar_config:get_global(escript, undefined)),
115
118
    erlang:put(escript_files, Files).
116
119
117
120

Up to file-list src/rebar_utils.erl:

36
36
         now_str/0,
37
37
         ensure_dir/1,
38
38
         beam_to_mod/2, beams/1,
39
         abort/2]).
39
         abort/2,
40
         escript_foldl/3]).
40
41
41
42
-include("rebar.hrl").
42
43
@@ -111,6 +112,14 @@ abort(String, Args) ->
111
112
    ?ERROR(String, Args),
112
113
    halt(1).
113
114
115
escript_foldl(Fun, Acc, File) ->
116
    case erlang:function_exported(zip, foldl, 3) of
117
        true ->
118
            emulate_escript_foldl(Fun, Acc, File);
119
        false ->
120
            escript:foldl(Fun, Acc, File)
121
    end.
122
114
123
%% ====================================================================
115
124
%% Internal functions
116
125
%% ====================================================================
@@ -144,3 +153,21 @@ beams(Dir) ->
144
153
    filelib:fold_files(Dir, ".*\.beam\$", true,
145
154
                       fun(F, Acc) -> [F | Acc] end, []).
146
155
156
emulate_escript_foldl(Fun, Acc, File) ->
157
    case escript:extract(File, [compile_source]) of
158
        {ok, [_Shebang, _Comment, _EmuArgs, Body]} ->
159
            case Body of
160
                {source, BeamCode} ->
161
                    GetInfo = fun() -> file:read_file_info(File) end,
162
                    GetBin = fun() -> BeamCode end,
163
                    {ok, Fun(".", GetInfo, GetBin, Acc)};
164
                {beam, BeamCode} ->
165
                    GetInfo = fun() -> file:read_file_info(File) end,
166
                    GetBin = fun() -> BeamCode end,
167
                    {ok, Fun(".", GetInfo, GetBin, Acc)};
168
                {archive, ArchiveBin} ->
169
                    zip:foldl(Fun, Acc, {File, ArchiveBin})
170
            end;
171
        {error, Reason} ->
172
            {error, Reason}
173
    end.