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.
This commit is contained in:
Tuncer Ayaz 2010-03-05 22:56:31 +01:00
parent a49d257412
commit dfb5af4049
2 changed files with 33 additions and 3 deletions

View file

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

View file

@ -36,7 +36,8 @@
now_str/0,
ensure_dir/1,
beam_to_mod/2, beams/1,
abort/2]).
abort/2,
escript_foldl/3]).
-include("rebar.hrl").
@ -111,6 +112,14 @@ abort(String, Args) ->
?ERROR(String, Args),
halt(1).
escript_foldl(Fun, Acc, File) ->
case erlang:function_exported(zip, foldl, 3) of
true ->
emulate_escript_foldl(Fun, Acc, File);
false ->
escript:foldl(Fun, Acc, File)
end.
%% ====================================================================
%% Internal functions
%% ====================================================================
@ -144,3 +153,21 @@ beams(Dir) ->
filelib:fold_files(Dir, ".*\.beam\$", true,
fun(F, Acc) -> [F | Acc] end, []).
emulate_escript_foldl(Fun, Acc, File) ->
case escript:extract(File, [compile_source]) of
{ok, [_Shebang, _Comment, _EmuArgs, Body]} ->
case Body of
{source, BeamCode} ->
GetInfo = fun() -> file:read_file_info(File) end,
GetBin = fun() -> BeamCode end,
{ok, Fun(".", GetInfo, GetBin, Acc)};
{beam, BeamCode} ->
GetInfo = fun() -> file:read_file_info(File) end,
GetBin = fun() -> BeamCode end,
{ok, Fun(".", GetInfo, GetBin, Acc)};
{archive, ArchiveBin} ->
zip:foldl(Fun, Acc, {File, ArchiveBin})
end;
{error, Reason} ->
{error, Reason}
end.