2009-12-04 20:48:57 +00:00
|
|
|
#!/usr/bin/env escript
|
2010-02-25 20:43:23 +00:00
|
|
|
%% -*- mode:erlang;tab-width:4;erlang-indent-level:4;indent-tabs-mode:nil -*-
|
2010-02-10 23:16:34 +00:00
|
|
|
%% ex: ft=erlang ts=4 sw=4 et
|
2009-12-04 20:48:57 +00:00
|
|
|
|
|
|
|
main(Args) ->
|
2009-12-07 23:03:56 +00:00
|
|
|
%% Get a string repr of build time
|
|
|
|
Built = build_time(),
|
|
|
|
|
|
|
|
%% Check for force=1 flag to force a rebuild
|
|
|
|
case lists:member("force=1", Args) of
|
|
|
|
true ->
|
|
|
|
[] = os:cmd("rm -rf ebin/*.beam"),
|
|
|
|
ok;
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end,
|
2010-01-08 17:54:26 +00:00
|
|
|
|
2009-12-04 20:48:57 +00:00
|
|
|
%% Compile all src/*.erl to ebin
|
2009-12-07 23:03:56 +00:00
|
|
|
case make:files(filelib:wildcard("src/*.erl"), [{outdir, "ebin"}, {i, "include"},
|
2010-04-09 15:24:45 +00:00
|
|
|
debug_info,
|
2009-12-07 23:03:56 +00:00
|
|
|
{d, 'BUILD_TIME', Built}]) of
|
2009-12-04 20:48:57 +00:00
|
|
|
up_to_date ->
|
|
|
|
ok;
|
|
|
|
error ->
|
|
|
|
io:format("Failed to compile rebar files!\n"),
|
|
|
|
halt(1)
|
|
|
|
end,
|
|
|
|
|
|
|
|
%% Make sure file:consult can parse the .app file
|
|
|
|
case file:consult("ebin/rebar.app") of
|
|
|
|
{ok, _} ->
|
|
|
|
ok;
|
|
|
|
{error, Reason} ->
|
|
|
|
io:format("Invalid syntax in ebin/rebar.app: ~p\n", [Reason]),
|
|
|
|
halt(1)
|
|
|
|
end,
|
|
|
|
|
|
|
|
%% Add ebin/ to our path
|
|
|
|
true = code:add_path("ebin"),
|
|
|
|
|
|
|
|
%% Run rebar to do proper .app validation and such
|
|
|
|
rebar:main(["compile"] ++ Args),
|
|
|
|
|
2010-01-08 17:54:26 +00:00
|
|
|
%% 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("priv/templates/*", "."),
|
|
|
|
|
|
|
|
case zip:create("mem", Files, [memory]) of
|
2009-12-04 20:48:57 +00:00
|
|
|
{ok, {"mem", ZipBin}} ->
|
|
|
|
%% Archive was successfully created. Prefix that binary with our
|
|
|
|
%% header and write to "rebar" file
|
|
|
|
Script = <<"#!/usr/bin/env escript\n", ZipBin/binary>>,
|
|
|
|
case file:write_file("rebar", Script) of
|
|
|
|
ok ->
|
|
|
|
ok;
|
|
|
|
{error, WriteError} ->
|
|
|
|
io:format("Failed to write rebar script: ~p\n", [WriteError]),
|
|
|
|
halt(1)
|
|
|
|
end;
|
|
|
|
{error, ZipError} ->
|
|
|
|
io:format("Failed to construct rebar script archive: ~p\n", [ZipError]),
|
|
|
|
halt(1)
|
|
|
|
end,
|
|
|
|
|
|
|
|
%% Finally, update executable perms for our script
|
2009-12-04 20:54:44 +00:00
|
|
|
[] = os:cmd("chmod u+x rebar"),
|
|
|
|
|
|
|
|
%% Add a helpful message
|
|
|
|
io:format("Congratulations! You now have a self-contained script called \"rebar\" in\n"
|
|
|
|
"your current working directory. Place this script anywhere in your path\n"
|
|
|
|
"and you can use rebar to build OTP-compliant apps.\n").
|
|
|
|
|
2010-01-08 17:54:26 +00:00
|
|
|
|
2009-12-07 23:03:56 +00:00
|
|
|
build_time() ->
|
|
|
|
{{Y, M, D}, {H, Min, S}} = calendar:now_to_universal_time(now()),
|
|
|
|
lists:flatten(io_lib:format("~4..0w~2..0w~2..0w_~2..0w~2..0w~2..0w", [Y, M, D, H, Min, S])).
|
2009-12-04 20:48:57 +00:00
|
|
|
|
2010-01-08 17:54:26 +00:00
|
|
|
|
|
|
|
load_files(Wildcard, Dir) ->
|
|
|
|
[read_file(Filename, Dir) || Filename <- filelib:wildcard(Wildcard, Dir)].
|
|
|
|
|
|
|
|
read_file(Filename, Dir) ->
|
|
|
|
{ok, Bin} = file:read_file(filename:join(Dir, Filename)),
|
|
|
|
{Filename, Bin}.
|