diff --git a/src/rebar_templater.erl b/src/rebar_templater.erl index c4217cd..fd1d813 100644 --- a/src/rebar_templater.erl +++ b/src/rebar_templater.erl @@ -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). diff --git a/src/rebar_utils.erl b/src/rebar_utils.erl index bfe7bce..4bf089e 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,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.