2009-12-04 20:48:57 +00:00
|
|
|
#!/usr/bin/env escript
|
2011-01-31 16:43:31 +00:00
|
|
|
%% -*- mode: erlang;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(),
|
|
|
|
|
2010-07-28 13:19:25 +00:00
|
|
|
%% Get a string repr of first matching VCS changeset
|
|
|
|
VcsInfo = vcs_info([{hg, ".hg", "hg identify -i"},
|
2012-06-11 18:01:07 +00:00
|
|
|
{git, ".git", "git describe --always --tags"}]),
|
2010-06-22 19:47:06 +00:00
|
|
|
|
2009-12-07 23:03:56 +00:00
|
|
|
%% Check for force=1 flag to force a rebuild
|
|
|
|
case lists:member("force=1", Args) of
|
|
|
|
true ->
|
2010-08-02 17:35:26 +00:00
|
|
|
rm("ebin/*.beam");
|
2009-12-07 23:03:56 +00:00
|
|
|
false ->
|
2010-08-02 17:35:26 +00:00
|
|
|
rm("ebin/rebar_core.beam")
|
2009-12-07 23:03:56 +00:00
|
|
|
end,
|
2010-01-08 17:54:26 +00:00
|
|
|
|
2010-10-08 01:02:37 +00:00
|
|
|
%% Add check for debug flag
|
2011-01-10 14:50:04 +00:00
|
|
|
DebugFlag = case lists:member("debug", Args) of
|
|
|
|
true -> debug_info;
|
|
|
|
false -> undefined
|
|
|
|
end,
|
2010-10-08 01:02:37 +00:00
|
|
|
|
2012-06-04 14:46:17 +00:00
|
|
|
%% Extract the system info of the version of OTP we use to compile rebar
|
|
|
|
OtpInfo = string:strip(erlang:system_info(otp_release), both, $\n),
|
|
|
|
|
2009-12-04 20:48:57 +00:00
|
|
|
%% Compile all src/*.erl to ebin
|
2011-08-16 13:26:02 +00:00
|
|
|
case make:files(filelib:wildcard("src/*.erl"),
|
|
|
|
[{outdir, "ebin"}, {i, "include"},
|
|
|
|
DebugFlag,
|
|
|
|
{d, 'BUILD_TIME', Built},
|
2012-06-04 14:46:17 +00:00
|
|
|
{d, 'VCS_INFO', VcsInfo},
|
|
|
|
{d, 'OTP_INFO', OtpInfo}]) 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),
|
|
|
|
|
2012-04-27 09:51:49 +00:00
|
|
|
TempDir = make_temp_dir(),
|
|
|
|
ok = copy_files(TempDir), %% Copy the ebin and priv/templates
|
|
|
|
{ok, Dirs} = file:list_dir(TempDir),
|
2010-01-08 17:54:26 +00:00
|
|
|
|
2012-04-27 09:51:49 +00:00
|
|
|
case zip:create("mem", Dirs, [memory, {cwd, TempDir}]) of
|
2009-12-04 20:48:57 +00:00
|
|
|
{ok, {"mem", ZipBin}} ->
|
2012-04-27 09:51:49 +00:00
|
|
|
ok = rebar_file_utils:rm_rf(TempDir),
|
2009-12-04 20:48:57 +00:00
|
|
|
%% Archive was successfully created. Prefix that binary with our
|
2011-08-16 13:11:56 +00:00
|
|
|
%% header and write to "rebar" file.
|
|
|
|
%% Without -noshell -noinput escript consumes all input that would
|
|
|
|
%% otherwise go to the shell for the next command.
|
2011-08-16 13:26:02 +00:00
|
|
|
Script = <<"#!/usr/bin/env escript\n%%! -noshell -noinput\n",
|
|
|
|
ZipBin/binary>>,
|
2009-12-04 20:48:57 +00:00
|
|
|
case file:write_file("rebar", Script) of
|
|
|
|
ok ->
|
|
|
|
ok;
|
|
|
|
{error, WriteError} ->
|
2011-08-16 13:26:02 +00:00
|
|
|
io:format("Failed to write rebar script: ~p\n",
|
|
|
|
[WriteError]),
|
2009-12-04 20:48:57 +00:00
|
|
|
halt(1)
|
|
|
|
end;
|
|
|
|
{error, ZipError} ->
|
2012-04-27 09:51:49 +00:00
|
|
|
ok = rebar_file_utils:rm_rf(TempDir),
|
2011-08-16 13:26:02 +00:00
|
|
|
io:format("Failed to construct rebar script archive: ~p\n",
|
|
|
|
[ZipError]),
|
2009-12-04 20:48:57 +00:00
|
|
|
halt(1)
|
|
|
|
end,
|
|
|
|
|
2011-12-22 05:27:42 +00:00
|
|
|
%% Finally, update executable perms for our script on *nix,
|
|
|
|
%% or write out script files on win32.
|
2010-08-02 17:35:26 +00:00
|
|
|
case os:type() of
|
|
|
|
{unix,_} ->
|
|
|
|
[] = os:cmd("chmod u+x rebar"),
|
|
|
|
ok;
|
2011-12-22 05:27:42 +00:00
|
|
|
{win32,_} ->
|
|
|
|
write_windows_scripts(),
|
|
|
|
ok;
|
2010-08-02 17:35:26 +00:00
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end,
|
2011-08-08 20:35:45 +00:00
|
|
|
|
2009-12-04 20:54:44 +00:00
|
|
|
%% Add a helpful message
|
2011-08-16 13:26:02 +00:00
|
|
|
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"
|
2009-12-04 20:54:44 +00:00
|
|
|
"and you can use rebar to build OTP-compliant apps.\n").
|
|
|
|
|
2012-04-27 09:51:49 +00:00
|
|
|
make_temp_dir() ->
|
|
|
|
case temp_name("rebar.") of
|
|
|
|
{ok, TempDir} ->
|
|
|
|
case file:make_dir(TempDir) of
|
|
|
|
ok ->
|
|
|
|
TempDir;
|
|
|
|
Error ->
|
|
|
|
io:format("Failed to create temporary directory: ~p~n",
|
|
|
|
[Error]),
|
|
|
|
halt(1),
|
|
|
|
Error
|
|
|
|
end;
|
|
|
|
Error ->
|
|
|
|
io:format("Failed to create temporary directory: ~p~n",
|
|
|
|
[Error]),
|
|
|
|
halt(1)
|
|
|
|
end.
|
|
|
|
|
|
|
|
temp_name(Prefix) ->
|
|
|
|
temp_name(Prefix, 5).
|
|
|
|
|
|
|
|
temp_name(_Prefix, 0) ->
|
|
|
|
{error, eexist};
|
|
|
|
temp_name(Prefix, N) ->
|
|
|
|
Hash = erlang:phash2(make_ref()),
|
|
|
|
Name = Prefix ++ integer_to_list(Hash),
|
|
|
|
case filelib:is_file(Name) of
|
|
|
|
false -> {ok, Name};
|
|
|
|
true -> temp_name(Prefix, N-1)
|
|
|
|
end.
|
|
|
|
|
2010-08-02 17:35:26 +00:00
|
|
|
rm(Path) ->
|
2011-08-08 20:35:45 +00:00
|
|
|
NativePath = filename:nativename(Path),
|
2010-08-02 17:35:26 +00:00
|
|
|
Cmd = case os:type() of
|
|
|
|
{unix,_} -> "rm -f ";
|
|
|
|
{win32,_} -> "del /q "
|
|
|
|
end,
|
2011-08-08 20:35:45 +00:00
|
|
|
[] = os:cmd(Cmd ++ NativePath),
|
2010-08-02 17:35:26 +00:00
|
|
|
ok.
|
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()),
|
2011-08-16 13:26:02 +00:00
|
|
|
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
|
|
|
|
2012-04-27 09:51:49 +00:00
|
|
|
copy_files(Temp) ->
|
|
|
|
BaseEbinDir = filename:join("rebar", "ebin"),
|
|
|
|
BaseTemplatesDir = filename:join("priv", "templates"),
|
|
|
|
EbinDir = filename:join(Temp, BaseEbinDir),
|
|
|
|
TemplatesDir = filename:join(Temp, BaseTemplatesDir),
|
|
|
|
|
|
|
|
%% prepare directory structure
|
|
|
|
lists:foreach(
|
|
|
|
fun(Dir) ->
|
|
|
|
ok = filelib:ensure_dir(filename:join(Dir, "dummy"))
|
|
|
|
end, [EbinDir, TemplatesDir]),
|
|
|
|
|
|
|
|
%% copy content of ebin
|
|
|
|
EbinSrc = filename:join(["ebin", "*"]),
|
|
|
|
ok = rebar_file_utils:cp_r([EbinSrc], EbinDir),
|
|
|
|
|
|
|
|
%% copy content of priv/templates
|
|
|
|
TemplatesSrc = filename:join(BaseTemplatesDir, "*"),
|
|
|
|
ok = rebar_file_utils:cp_r([TemplatesSrc], TemplatesDir).
|
2010-07-28 13:19:25 +00:00
|
|
|
|
|
|
|
vcs_info([]) ->
|
|
|
|
"No VCS info available.";
|
|
|
|
vcs_info([{Id, Dir, Cmd} | Rest]) ->
|
|
|
|
case filelib:is_dir(Dir) of
|
|
|
|
true ->
|
|
|
|
lists:concat([Id, " ", string:strip(os:cmd(Cmd), both, $\n)]);
|
|
|
|
false ->
|
|
|
|
vcs_info(Rest)
|
|
|
|
end.
|
2011-12-22 05:27:42 +00:00
|
|
|
|
|
|
|
write_windows_scripts() ->
|
|
|
|
CmdScript=
|
|
|
|
"@echo off\r\n"
|
|
|
|
"setlocal\r\n"
|
|
|
|
"set rebarscript=%~f0\r\n"
|
2012-01-10 17:00:25 +00:00
|
|
|
"escript.exe \"%rebarscript:.cmd=%\" %*\r\n",
|
2012-01-10 17:15:40 +00:00
|
|
|
ok = file:write_file("rebar.cmd", CmdScript).
|