summaryrefslogtreecommitdiff
path: root/bootstrap
blob: c68bcab5ae55669d2d3a3c6fdceee03fd1e21d51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env escript
%% -*- mode: erlang;erlang-indent-level: 4;indent-tabs-mode: nil -*-
%% ex: ft=erlang ts=4 sw=4 et

main(Args) ->
    %% Get a string repr of build time
    Built = build_time(),

    %% Get a string repr of first matching VCS changeset
    VcsInfo = vcs_info([{hg, ".hg", "hg identify -i"},
                        {git, ".git", "git describe --always --tags"}]),

    %% Check for force=1 flag to force a rebuild
    case lists:member("force=1", Args) of
        true ->
            rm("ebin/*.beam");
        false ->
            rm("ebin/rebar_core.beam")
    end,

    %% Add check for debug flag
    DebugFlag = case lists:member("debug", Args) of
                    true -> debug_info;
                    false -> undefined
                end,

    %% Extract the system info of the version of OTP we use to compile rebar
    OtpInfo = string:strip(erlang:system_info(otp_release), both, $\n),

    %% Compile all src/*.erl to ebin
    case make:files(filelib:wildcard("src/*.erl"),
                    [{outdir, "ebin"}, {i, "include"},
                     DebugFlag,
                     {d, 'BUILD_TIME', Built},
                     {d, 'VCS_INFO', VcsInfo},
                     {d, 'OTP_INFO', OtpInfo}]) of
        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),

    TempDir = make_temp_dir(),
    ok = copy_files(TempDir), %% Copy the ebin and priv/templates
    {ok, Dirs} = file:list_dir(TempDir),

    case zip:create("mem", Dirs, [memory, {cwd, TempDir}]) of
        {ok, {"mem", ZipBin}} ->
            ok = rebar_file_utils:rm_rf(TempDir),
            %% Archive was successfully created. Prefix that binary with our
            %% header and write to "rebar" file.
            %% Without -noshell -noinput escript consumes all input that would
            %% otherwise go to the shell for the next command.
            Script = <<"#!/usr/bin/env escript\n%%! -noshell -noinput\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} ->
            ok = rebar_file_utils:rm_rf(TempDir),
            io:format("Failed to construct rebar script archive: ~p\n",
                      [ZipError]),
            halt(1)
    end,

    %% Finally, update executable perms for our script on *nix,
    %%  or write out script files on win32.
    case os:type() of
        {unix,_} ->
            [] = os:cmd("chmod u+x rebar"),
            ok;
        {win32,_} ->
            write_windows_scripts(),
            ok;
        _ ->
            ok
    end,

    %% 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").

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.

rm(Path) ->
    NativePath = filename:nativename(Path),
    Cmd = case os:type() of
              {unix,_} -> "rm -f ";
              {win32,_} -> "del /q "
          end,
    [] = os:cmd(Cmd ++ NativePath),
    ok.

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])).

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).

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.

write_windows_scripts() ->
    CmdScript=
        "@echo off\r\n"
        "setlocal\r\n"
        "set rebarscript=%~f0\r\n"
        "escript.exe \"%rebarscript:.cmd=%\" %*\r\n",
    ok = file:write_file("rebar.cmd", CmdScript).