Permalink
Find file
3391235 Nov 25, 2011
executable file 56 lines (47 sloc) 1.76 KB
#!/usr/bin/env escript
main(_Args) ->
%% Make sure file:consult can parse the .app file
case file:consult("ebin/edis.app") of
{ok, _} ->
ok;
{error, Reason} ->
io:format("Invalid syntax in ebin/edis.app: ~p\n", [Reason]),
halt(1)
end,
%% Add ebin paths to our path
true = code:add_path("ebin"),
ok = code:add_paths(filelib:wildcard("deps/*/ebin")),
%% Read the contents of the files in ebin(s)
Files = lists:flatmap(fun(Dir) -> load_files(Dir) end, ["ebin"|filelib:wildcard("deps/*/ebin")]),
case zip:create("mem", Files, [memory]) of
{ok, {"mem", ZipBin}} ->
%% Archive was successfully created. Prefix that with header and write to "edis" file
Script = <<"#!/usr/bin/env escript\n%%! +Bc +K true -name edis -smp enable -detached\n", ZipBin/binary>>,
case file:write_file("bin/edis", Script) of
ok -> ok;
{error, WriteError} ->
io:format("Failed to write bin/edis: ~p\n", [WriteError]),
halt(1)
end;
{error, ZipError} ->
io:format("Failed to construct bin/edis archive: ~p\n", [ZipError]),
halt(1)
end,
%% Finally, update executable perms for our script
case os:type() of
{unix,_} ->
[] = os:cmd("chmod u+x bin/edis"),
ok;
_ ->
ok
end,
%% Place the eleveldb.so file near our script
{ok, _Bytes} = file:copy("deps/eleveldb/priv/eleveldb.so", "bin/eleveldb.so"),
%% Add a helpful message
io:format("Congratulations! You now have a self-contained script called \"edis\" in\n"
"your bin directory.\n").
load_files(Dir) ->
[read_file(Filename, Dir) || Filename <- filelib:wildcard("*", Dir)].
read_file(Filename, Dir) ->
{ok, Bin} = file:read_file(filename:join(Dir, Filename)),
{Filename, Bin}.