Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 141 lines (121 sloc) 4.579 kb
b491898 @dizzyd Refactor rebar to build a self-contained script
dizzyd authored
1 #!/usr/bin/env escript
c466076 @tuncer Clean up emacs file local variables
tuncer authored
2 %% -*- mode: erlang;erlang-indent-level: 4;indent-tabs-mode: nil -*-
fa4b2ee @tuncer Add emacs and vim file local variables
tuncer authored
3 %% ex: ft=erlang ts=4 sw=4 et
b491898 @dizzyd Refactor rebar to build a self-contained script
dizzyd authored
4
5 main(Args) ->
ffa0cda @dizzyd Updating bootstrap to embed the build time into a macro that we can p…
dizzyd authored
6 %% Get a string repr of build time
7 Built = build_time(),
8
27f638b @dizzyd Add support for embedding git version; make sure to always rebuild re…
dizzyd authored
9 %% Get a string repr of first matching VCS changeset
10 VcsInfo = vcs_info([{hg, ".hg", "hg identify -i"},
11 {git, ".git", "git describe --always"}]),
02bc52f @dizzyd Add support for embedding VCS info into the version data
dizzyd authored
12
ffa0cda @dizzyd Updating bootstrap to embed the build time into a macro that we can p…
dizzyd authored
13 %% Check for force=1 flag to force a rebuild
14 case lists:member("force=1", Args) of
15 true ->
8a5eea5 @juranki add bat scripts for bootstrap and rebat (windows doesn't understand s…
juranki authored
16 rm("ebin/*.beam");
ffa0cda @dizzyd Updating bootstrap to embed the build time into a macro that we can p…
dizzyd authored
17 false ->
8a5eea5 @juranki add bat scripts for bootstrap and rebat (windows doesn't understand s…
juranki authored
18 rm("ebin/rebar_core.beam")
ffa0cda @dizzyd Updating bootstrap to embed the build time into a macro that we can p…
dizzyd authored
19 end,
1ea7065 @dizzyd Rework how the escript is built so that we can inclue templates
dizzyd authored
20
465af36 @dizzyd Add debug flag to make it easier to get a debug version of rebar
dizzyd authored
21 %% Add check for debug flag
27019e2 @tuncer Fix code clarity in bootstrap
tuncer authored
22 DebugFlag = case lists:member("debug", Args) of
23 true -> debug_info;
24 false -> undefined
25 end,
465af36 @dizzyd Add debug flag to make it easier to get a debug version of rebar
dizzyd authored
26
b491898 @dizzyd Refactor rebar to build a self-contained script
dizzyd authored
27 %% Compile all src/*.erl to ebin
461335a @tuncer Reformat bootstrap
tuncer authored
28 case make:files(filelib:wildcard("src/*.erl"),
29 [{outdir, "ebin"}, {i, "include"},
30 DebugFlag,
31 {d, 'BUILD_TIME', Built},
32 {d, 'VCS_INFO', VcsInfo}]) of
b491898 @dizzyd Refactor rebar to build a self-contained script
dizzyd authored
33 up_to_date ->
34 ok;
35 error ->
36 io:format("Failed to compile rebar files!\n"),
37 halt(1)
38 end,
39
40 %% Make sure file:consult can parse the .app file
41 case file:consult("ebin/rebar.app") of
42 {ok, _} ->
43 ok;
44 {error, Reason} ->
45 io:format("Invalid syntax in ebin/rebar.app: ~p\n", [Reason]),
46 halt(1)
47 end,
48
49 %% Add ebin/ to our path
50 true = code:add_path("ebin"),
51
52 %% Run rebar to do proper .app validation and such
53 rebar:main(["compile"] ++ Args),
54
461335a @tuncer Reformat bootstrap
tuncer authored
55 %% Read the contents of the files in ebin and templates; note that we
56 %% place all the beam files at the top level of the code archive so
57 %% that code loading works properly.
1ea7065 @dizzyd Rework how the escript is built so that we can inclue templates
dizzyd authored
58 Files = load_files("*", "ebin") ++ load_files("priv/templates/*", "."),
59
60 case zip:create("mem", Files, [memory]) of
b491898 @dizzyd Refactor rebar to build a self-contained script
dizzyd authored
61 {ok, {"mem", ZipBin}} ->
62 %% Archive was successfully created. Prefix that binary with our
92129d6 @slfritchie Document use of -noshell -noinput in emu_opts
slfritchie authored
63 %% header and write to "rebar" file.
64 %% Without -noshell -noinput escript consumes all input that would
65 %% otherwise go to the shell for the next command.
461335a @tuncer Reformat bootstrap
tuncer authored
66 Script = <<"#!/usr/bin/env escript\n%%! -noshell -noinput\n",
67 ZipBin/binary>>,
b491898 @dizzyd Refactor rebar to build a self-contained script
dizzyd authored
68 case file:write_file("rebar", Script) of
69 ok ->
70 ok;
71 {error, WriteError} ->
461335a @tuncer Reformat bootstrap
tuncer authored
72 io:format("Failed to write rebar script: ~p\n",
73 [WriteError]),
b491898 @dizzyd Refactor rebar to build a self-contained script
dizzyd authored
74 halt(1)
75 end;
76 {error, ZipError} ->
461335a @tuncer Reformat bootstrap
tuncer authored
77 io:format("Failed to construct rebar script archive: ~p\n",
78 [ZipError]),
b491898 @dizzyd Refactor rebar to build a self-contained script
dizzyd authored
79 halt(1)
80 end,
81
8c01734 @xenolinguist bootstrap: generate windows scripts
xenolinguist authored
82 %% Finally, update executable perms for our script on *nix,
83 %% or write out script files on win32.
8a5eea5 @juranki add bat scripts for bootstrap and rebat (windows doesn't understand s…
juranki authored
84 case os:type() of
85 {unix,_} ->
86 [] = os:cmd("chmod u+x rebar"),
87 ok;
8c01734 @xenolinguist bootstrap: generate windows scripts
xenolinguist authored
88 {win32,_} ->
89 write_windows_scripts(),
90 ok;
8a5eea5 @juranki add bat scripts for bootstrap and rebat (windows doesn't understand s…
juranki authored
91 _ ->
92 ok
93 end,
2911d48 @tuncer Remove trailing whitespace
tuncer authored
94
01684ff @dizzyd Add a nice message
dizzyd authored
95 %% Add a helpful message
461335a @tuncer Reformat bootstrap
tuncer authored
96 io:format("Congratulations! You now have a self-contained script called"
97 " \"rebar\" in\n"
98 "your current working directory. "
99 "Place this script anywhere in your path\n"
01684ff @dizzyd Add a nice message
dizzyd authored
100 "and you can use rebar to build OTP-compliant apps.\n").
101
8a5eea5 @juranki add bat scripts for bootstrap and rebat (windows doesn't understand s…
juranki authored
102 rm(Path) ->
2911d48 @tuncer Remove trailing whitespace
tuncer authored
103 NativePath = filename:nativename(Path),
8a5eea5 @juranki add bat scripts for bootstrap and rebat (windows doesn't understand s…
juranki authored
104 Cmd = case os:type() of
105 {unix,_} -> "rm -f ";
106 {win32,_} -> "del /q "
107 end,
2911d48 @tuncer Remove trailing whitespace
tuncer authored
108 [] = os:cmd(Cmd ++ NativePath),
8a5eea5 @juranki add bat scripts for bootstrap and rebat (windows doesn't understand s…
juranki authored
109 ok.
1ea7065 @dizzyd Rework how the escript is built so that we can inclue templates
dizzyd authored
110
ffa0cda @dizzyd Updating bootstrap to embed the build time into a macro that we can p…
dizzyd authored
111 build_time() ->
112 {{Y, M, D}, {H, Min, S}} = calendar:now_to_universal_time(now()),
461335a @tuncer Reformat bootstrap
tuncer authored
113 lists:flatten(io_lib:format("~4..0w~2..0w~2..0w_~2..0w~2..0w~2..0w",
114 [Y, M, D, H, Min, S])).
b491898 @dizzyd Refactor rebar to build a self-contained script
dizzyd authored
115
1ea7065 @dizzyd Rework how the escript is built so that we can inclue templates
dizzyd authored
116
117 load_files(Wildcard, Dir) ->
118 [read_file(Filename, Dir) || Filename <- filelib:wildcard(Wildcard, Dir)].
119
120 read_file(Filename, Dir) ->
121 {ok, Bin} = file:read_file(filename:join(Dir, Filename)),
122 {Filename, Bin}.
27f638b @dizzyd Add support for embedding git version; make sure to always rebuild re…
dizzyd authored
123
124 vcs_info([]) ->
125 "No VCS info available.";
126 vcs_info([{Id, Dir, Cmd} | Rest]) ->
127 case filelib:is_dir(Dir) of
128 true ->
129 lists:concat([Id, " ", string:strip(os:cmd(Cmd), both, $\n)]);
130 false ->
131 vcs_info(Rest)
132 end.
8c01734 @xenolinguist bootstrap: generate windows scripts
xenolinguist authored
133
134 write_windows_scripts() ->
135 CmdScript=
136 "@echo off\r\n"
137 "setlocal\r\n"
138 "set rebarscript=%~f0\r\n"
fa3dab3 Fix typos in generated cmd script in bootstrap
Mike Lazar authored
139 "escript.exe \"%rebarscript:.cmd=%\" %*\r\n",
cbfabd6 Remove generation of ps1 script (no longer needed)
Mike Lazar authored
140 ok = file:write_file("rebar.cmd", CmdScript).
Something went wrong with that request. Please try again.