Commits

tuncer committed 2bce82b

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.

Comments (0)

Files changed (2)

src/rebar_templater.erl

 %% 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).
 
 

src/rebar_utils.erl

          now_str/0,
          ensure_dir/1,
          beam_to_mod/2, beams/1,
-         abort/2]).
+         abort/2,
+         escript_foldl/3]).
 
 -include("rebar.hrl").
 
     ?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
 %% ====================================================================
     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.