From 98131261761b9272bff890ae283469c96d74b36b Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Sun, 29 Aug 2010 14:33:17 -0600 Subject: Adding support for embedding other apps via escript_incl_apps --HG-- extra : rebase_source : 9eccc596d8fe55b2e0fe3ff2c9c0a9f9a8c92e11 --- src/rebar_escripter.erl | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/src/rebar_escripter.erl b/src/rebar_escripter.erl index 84747c1..ddf0fce 100644 --- a/src/rebar_escripter.erl +++ b/src/rebar_escripter.erl @@ -34,15 +34,20 @@ %% Public API %% =================================================================== -escriptize(_Config, AppFile) -> +escriptize(Config, AppFile) -> %% Extract the application name from the archive -- this will be be what %% we call the output script AppName = rebar_app_utils:app_name(AppFile), + %% Look for a list of other applications (dependencies) to include + %% in the output file. We then use the .app files for each of these + %% to pull in all the .beam files. + InclBeams = get_app_beams(rebar_config:get_local(Config, escript_incl_apps, []), []), + %% Construct the archive of everything in ebin/ dir -- put it on the %% top-level of the zip file so that code loading works properly. - Files = filelib:wildcard("*", "ebin"), - case zip:create("mem", Files, [{cwd, "ebin"}, memory]) of + Files = load_files("*", "ebin") ++ InclBeams, + case zip:create("mem", Files, [memory]) of {ok, {"mem", ZipBin}} -> %% Archive was successfully created. Prefix that binary with our %% header and write to our escript file @@ -63,3 +68,30 @@ escriptize(_Config, AppFile) -> [] = os:cmd(?FMT("chmod u+x ~p", [AppName])), ok. + + +%% =================================================================== +%% Internal functions +%% =================================================================== + +get_app_beams([], Acc) -> + Acc; +get_app_beams([App | Rest], Acc) -> + case code:lib_dir(App, ebin) of + {error, bad_name} -> + ?ABORT("Failed to get ebin/ directory for ~p escript_incl_apps.", [App]); + Path -> + Acc2 = [{filename:join([App, ebin, F]), file_contents(filename:join(Path, F))} || + F <- filelib:wildcard("*", Path)], + get_app_beams(Rest, Acc2 ++ Acc) + end. + +load_files(Wildcard, Dir) -> + [read_file(Filename, Dir) || Filename <- filelib:wildcard(Wildcard, Dir)]. + +read_file(Filename, Dir) -> + {Filename, file_contents(filename:join(Dir, Filename))}. + +file_contents(Filename) -> + {ok, Bin} = file:read_file(Filename), + Bin. -- cgit v1.1