Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 88 lines (74 sloc) 3.22 KB
#!/usr/bin/env escript
%% -*- mode:erlang;tab-width:4;erlang-indent-level:4;indent-tabs-mode:nil -*-
%% ex: ft=erlang ts=4 sw=4 et
main(Args) ->
%% Add ebin/ to our path
true = code:add_path("ebin"),
true = code:add_path("deps/mochiweb/ebin"),
true = code:add_path("deps/ejson/ebin"),
true = code:add_path("deps/ibrowse/ebin"),
true = code:add_path("deps/oauth/ebin"),
true = code:add_path("deps/couchbeam/ebin"),
%% Run erica to do proper .app validation and such
erica:main(["compile"] ++ Args),
%% Read the contents of the files in ebin and templates; note that we place
%% all the beam files at the top level of the code archive so that code loading
%% works properly.
Files = load_files("*", "ebin")
++ load_files("*", "deps/mochiweb/ebin")
++ load_files("*", "deps/ejson/ebin")
++ load_files("*", "deps/ejson/priv")
++ load_files("*", "deps/ibrowse/ebin")
++ load_files("*", "deps/oauth/ebin")
++ load_files("*", "deps/couchbeam/ebin")
++ load_files("priv/templates/*", ".")
++ load_files("priv/web_static/*", ".")
++ load_files("priv/web_templates/*", "."),
case zip:create("mem", Files, [memory]) of
{ok, {"mem", ZipBin}} ->
%% Archive was successfully created. Prefix that binary with our
%% header and write to "rebar" file
Script = <<"#!/usr/bin/env escript\n%%! -pa . -noshell -noinput -sasl errlog_type error\n", ZipBin/binary>>,
case file:write_file("erica", Script) of
ok ->
{error, WriteError} ->
io:format("Failed to write erica script: ~p\n", [WriteError]),
{error, ZipError} ->
io:format("Failed to construct erica archive: ~p\n", [ZipError]),
%% Finally, update executable perms for our script
{ok, #file_info{mode = Mode}} = file:read_file_info("erica"),
ok = file:change_mode("erica", Mode bor 8#00100),
%% Add a helpful message
io:format("Congratulations! You now have a self-contained script called \"erica\" in\n"
"your current working directory. Place this script anywhere in your path\n"
"and you can use build and push your design docs and docs
like erica.\n").
load_files(Wildcard, Dir) ->
lists:foldl(fun(Filename, Acc) ->
case lists:member(Filename, [".", ".."]) of
true ->
_ ->
case read_file(Filename, Dir) of
{Filename, Bin} ->
[{Filename, Bin}|Acc];
Bins ->
Bins ++ Acc
end, [], filelib:wildcard(Wildcard, Dir)).
read_file(Filename, Dir) ->
Filename1 = filename:join(Dir, Filename),
case filelib:is_dir(Filename1) of
true ->
load_files(Filename ++ "/*", Dir);
false ->
{ok, Bin} = file:read_file(filename:join(Dir, Filename)),
{Filename, Bin}