diff options
author | Tuncer Ayaz <tuncer.ayaz@gmail.com> | 2010-03-05 22:56:31 +0100 |
---|---|---|
committer | Tuncer Ayaz <tuncer.ayaz@gmail.com> | 2010-03-05 22:56:31 +0100 |
commit | dfb5af404978fad2cef4c1e6a4c6545217e55cc8 (patch) | |
tree | c3b9c86c8e27420ae317c8c74e352dc11c0aedd5 | |
parent | a49d257412015b9cedff18103ba52d6deef6a808 (diff) |
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.
-rw-r--r-- | src/rebar_templater.erl | 7 | ||||
-rw-r--r-- | src/rebar_utils.erl | 29 |
2 files changed, 33 insertions, 3 deletions
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. |