diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/rebar_escripter.erl | 55 |
1 files changed, 46 insertions, 9 deletions
diff --git a/src/rebar_escripter.erl b/src/rebar_escripter.erl index e8aa5dc..aa27399 100644 --- a/src/rebar_escripter.erl +++ b/src/rebar_escripter.erl @@ -40,6 +40,7 @@ escriptize(Config0, AppFile) -> %% Extract the application name from the archive -- this is the default %% name of the generated script {Config, AppName} = rebar_app_utils:app_name(Config0, AppFile), + AppNameStr = atom_to_list(AppName), %% Get the output filename for the escript -- this may include dirs Filename = rebar_config:get_local(Config, escript_name, AppName), @@ -57,7 +58,10 @@ escriptize(Config0, AppFile) -> %% 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 = load_files("*", "ebin") ++ InclBeams ++ InclExtra, + EbinPrefix = filename:join(AppNameStr, "ebin"), + EbinFiles = usort(load_files(EbinPrefix, "*", "ebin")), + ExtraFiles = usort(InclBeams ++ InclExtra), + Files = EbinFiles ++ ExtraFiles, case zip:create("mem", Files, [memory]) of {ok, {"mem", ZipBin}} -> @@ -66,7 +70,10 @@ escriptize(Config0, AppFile) -> Shebang = rebar_config:get(Config, escript_shebang, "#!/usr/bin/env escript\n"), Comment = rebar_config:get(Config, escript_comment, "%%\n"), - EmuArgs = rebar_config:get(Config, escript_emu_args, "%%!\n"), + DefaultEmuArgs = ?FMT("%%! -pa ~s/~s/ebin -noshell -noinput\n", + [AppNameStr, AppNameStr]), + EmuArgs = rebar_config:get(Config, escript_emu_args, + DefaultEmuArgs), Script = iolist_to_binary([Shebang, Comment, EmuArgs, ZipBin]), case file:write_file(Filename, Script) of ok -> @@ -109,9 +116,8 @@ get_app_beams([App | Rest], Acc) -> ?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)], + Prefix = filename:join(atom_to_list(App), "ebin"), + Acc2 = load_files(Prefix, "*", Path), get_app_beams(Rest, Acc2 ++ Acc) end. @@ -122,11 +128,42 @@ get_extra(Config) -> end, [], Extra). load_files(Wildcard, Dir) -> - [read_file(Filename, Dir) || Filename <- filelib:wildcard(Wildcard, Dir)]. - -read_file(Filename, Dir) -> - {Filename, file_contents(filename:join(Dir, Filename))}. + load_files("", Wildcard, Dir). + +load_files(Prefix, Wildcard, Dir) -> + [read_file(Prefix, Filename, Dir) + || Filename <- filelib:wildcard(Wildcard, Dir)]. + +read_file(Prefix, Filename, Dir) -> + Filename1 = case Prefix of + "" -> + Filename; + _ -> + filename:join([Prefix, Filename]) + end, + [dir_entries(filename:dirname(Filename1)), + {Filename1, file_contents(filename:join(Dir, Filename))}]. file_contents(Filename) -> {ok, Bin} = file:read_file(Filename), Bin. + +%% given a filename return zip archive dir entries for each sub-dir +dir_entries(File) -> + Dirs = dirs(File), + [{Dir ++ "/", <<>>} || Dir <- Dirs]. + +%% given "foo/bar/baz" return ["foo", "foo/bar", "foo/bar/baz"] +dirs(Dir) -> + dirs1(filename:split(Dir), "", []). + +dirs1([], _, Acc) -> + lists:reverse(Acc); +dirs1([H|T], "", []) -> + dirs1(T, H, [H]); +dirs1([H|T], Last, Acc) -> + Dir = filename:join(Last, H), + dirs1(T, Dir, [Dir|Acc]). + +usort(List) -> + lists:ukeysort(1, lists:flatten(List)). |