diff options
Diffstat (limited to 'src/rebar_utils.erl')
-rw-r--r-- | src/rebar_utils.erl | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/src/rebar_utils.erl b/src/rebar_utils.erl index bfe7bce..e7d22fa 100644 --- a/src/rebar_utils.erl +++ b/src/rebar_utils.erl @@ -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,18 @@ abort(String, Args) -> ?ERROR(String, Args), halt(1). +%% TODO: Rename emulate_escript_foldl to escript_foldl and remove +%% this function when the time is right. escript:foldl/3 was an +%% undocumented exported fun and is going to be removed post-R13B04. +escript_foldl(Fun, Acc, File) -> + {module, zip} = code:ensure_loaded(zip), + 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 +157,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. |