summaryrefslogtreecommitdiff
path: root/bootstrap
blob: 237c5b2c05d058b9b9e20cc2a7153125799cc66c (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
#!/usr/bin/env escript
%% -*- erlang -*-

main(Args) ->
    %% Compile all src/*.erl to ebin
    case make:files(filelib:wildcard("src/*.erl"), [{outdir, "ebin"}, {i, "include"}]) 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),

    %% Construct the archive of everything in ebin/ dir -- put it on the
    %% top-level of the zip file so that code loading works properly.
    Files = filelib:wildcard("*", "ebin"),
    case zip:create("mem", Files, [{cwd, "ebin"}, 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", 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
    [] = 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").