Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 165 lines (145 sloc) 5.619 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) ->
411d703 @lrascao bootstrap now accepts --help usage flag
lrascao authored
6 case lists:member("--help", Args) of
7 true ->
8 usage(),
9 halt(0);
41095e7 @lrascao fix indentation issues
lrascao authored
10 false ->
411d703 @lrascao bootstrap now accepts --help usage flag
lrascao authored
11 ok
41095e7 @lrascao fix indentation issues
lrascao authored
12 end,
13
ffa0cda @dizzyd Updating bootstrap to embed the build time into a macro that we can p…
dizzyd authored
14 %% Get a string repr of build time
15 Built = build_time(),
16
27f638b @dizzyd Add support for embedding git version; make sure to always rebuild re…
dizzyd authored
17 %% Get a string repr of first matching VCS changeset
e34b702 @tuncer bootstrap: fix and enhance VCS_INFO handling
tuncer authored
18 VcsInfo = vcs_info([{hg, ".hg", "hg identify -i", "hg status"},
19 {git, ".git", "git describe --always --tags",
20 "git status -s"}]),
02bc52f @dizzyd Add support for embedding VCS info into the version data
dizzyd authored
21
ffa0cda @dizzyd Updating bootstrap to embed the build time into a macro that we can p…
dizzyd authored
22 %% Check for force=1 flag to force a rebuild
23 case lists:member("force=1", Args) of
24 true ->
8a5eea5 @juranki add bat scripts for bootstrap and rebat (windows doesn't understand s…
juranki authored
25 rm("ebin/*.beam");
ffa0cda @dizzyd Updating bootstrap to embed the build time into a macro that we can p…
dizzyd authored
26 false ->
e34b702 @tuncer bootstrap: fix and enhance VCS_INFO handling
tuncer authored
27 rm("ebin/rebar.beam")
ffa0cda @dizzyd Updating bootstrap to embed the build time into a macro that we can p…
dizzyd authored
28 end,
1ea7065 @dizzyd Rework how the escript is built so that we can inclue templates
dizzyd authored
29
465af36 @dizzyd Add debug flag to make it easier to get a debug version of rebar
dizzyd authored
30 %% Add check for debug flag
27019e2 @tuncer Fix code clarity in bootstrap
tuncer authored
31 DebugFlag = case lists:member("debug", Args) of
32 true -> debug_info;
33 false -> undefined
34 end,
465af36 @dizzyd Add debug flag to make it easier to get a debug version of rebar
dizzyd authored
35
0f24d93 @dizzyd Move to major.minor.patch release version; add OTP build version
dizzyd authored
36 %% Extract the system info of the version of OTP we use to compile rebar
37 OtpInfo = string:strip(erlang:system_info(otp_release), both, $\n),
38
5f995bf @tuncer Fix a few minor formatting inconsistencies
tuncer authored
39 %% Types dict:dict() and digraph:digraph() have been introduced in
40 %% Erlang 17.
41 %% At the same time, their counterparts dict() and digraph() are to be
42 %% deprecated in Erlang 18. namespaced_types option is used to select
15373a5 @tuncer Fix typo s/of/on/
tuncer authored
43 %% proper type name depending on the OTP version used.
4de9ea5 @matwey Add namespaced_types to bootstrap script.
matwey authored
44 NamespacedTypes = case is_otp(OtpInfo, "^[0-9]+") of
45 true -> {d, namespaced_types};
46 false -> undefined
47 end,
48
b491898 @dizzyd Refactor rebar to build a self-contained script
dizzyd authored
49 %% Compile all src/*.erl to ebin
3fb4a7c @tuncer Fix OS X resource fork handling (Reported-by: Richard O'Keefe)
tuncer authored
50 %% To not accidentally try to compile files like Mac OS X resource forks,
51 %% we only look for rebar source files that start with a letter.
52 case make:files(filelib:wildcard("src/[a-zA-Z]*.erl"),
461335a @tuncer Reformat bootstrap
tuncer authored
53 [{outdir, "ebin"}, {i, "include"},
54 DebugFlag,
4de9ea5 @matwey Add namespaced_types to bootstrap script.
matwey authored
55 NamespacedTypes,
461335a @tuncer Reformat bootstrap
tuncer authored
56 {d, 'BUILD_TIME', Built},
0f24d93 @dizzyd Move to major.minor.patch release version; add OTP build version
dizzyd authored
57 {d, 'VCS_INFO', VcsInfo},
58 {d, 'OTP_INFO', OtpInfo}]) of
b491898 @dizzyd Refactor rebar to build a self-contained script
dizzyd authored
59 up_to_date ->
60 ok;
61 error ->
62 io:format("Failed to compile rebar files!\n"),
63 halt(1)
64 end,
65
66 %% Make sure file:consult can parse the .app file
67 case file:consult("ebin/rebar.app") of
68 {ok, _} ->
69 ok;
70 {error, Reason} ->
71 io:format("Invalid syntax in ebin/rebar.app: ~p\n", [Reason]),
72 halt(1)
73 end,
74
75 %% Add ebin/ to our path
76 true = code:add_path("ebin"),
77
1e98085 @tuncer Extend escriptize and reuse in bootstrap
tuncer authored
78 %% Run rebar compile to do proper .app validation etc.
79 %% and rebar escriptize to create the rebar script
af305f2 @tuncer bootstrap: avoid trying to run 'debug' command
tuncer authored
80 RebarArgs = Args -- ["debug"], %% Avoid trying to run 'debug' command
81 rebar:main(["compile", "escriptize"] ++ RebarArgs),
b491898 @dizzyd Refactor rebar to build a self-contained script
dizzyd authored
82
8c01734 @xenolinguist bootstrap: generate windows scripts
xenolinguist authored
83 %% Finally, update executable perms for our script on *nix,
84 %% or write out script files on win32.
8a5eea5 @juranki add bat scripts for bootstrap and rebat (windows doesn't understand s…
juranki authored
85 case os:type() of
86 {unix,_} ->
87 [] = os:cmd("chmod u+x rebar"),
88 ok;
8c01734 @xenolinguist bootstrap: generate windows scripts
xenolinguist authored
89 {win32,_} ->
90 write_windows_scripts(),
91 ok;
8a5eea5 @juranki add bat scripts for bootstrap and rebat (windows doesn't understand s…
juranki authored
92 _ ->
93 ok
94 end,
2911d48 @tuncer Remove trailing whitespace
tuncer authored
95
01684ff @dizzyd Add a nice message
dizzyd authored
96 %% Add a helpful message
461335a @tuncer Reformat bootstrap
tuncer authored
97 io:format("Congratulations! You now have a self-contained script called"
98 " \"rebar\" in\n"
99 "your current working directory. "
100 "Place this script anywhere in your path\n"
01684ff @dizzyd Add a nice message
dizzyd authored
101 "and you can use rebar to build OTP-compliant apps.\n").
102
411d703 @lrascao bootstrap now accepts --help usage flag
lrascao authored
103 usage() ->
104 io:format("Usage: bootstrap [OPTION]...~n"),
41095e7 @lrascao fix indentation issues
lrascao authored
105 io:format(" force=1 unconditional build~n"),
106 io:format(" debug add debug information~n").
107
4de9ea5 @matwey Add namespaced_types to bootstrap script.
matwey authored
108 is_otp(OtpInfo, Regex) ->
109 case re:run(OtpInfo, Regex, [{capture, none}]) of
110 match -> true;
111 nomatch -> false
112 end.
113
8a5eea5 @juranki add bat scripts for bootstrap and rebat (windows doesn't understand s…
juranki authored
114 rm(Path) ->
2911d48 @tuncer Remove trailing whitespace
tuncer authored
115 NativePath = filename:nativename(Path),
8a5eea5 @juranki add bat scripts for bootstrap and rebat (windows doesn't understand s…
juranki authored
116 Cmd = case os:type() of
117 {unix,_} -> "rm -f ";
118 {win32,_} -> "del /q "
119 end,
2911d48 @tuncer Remove trailing whitespace
tuncer authored
120 [] = os:cmd(Cmd ++ NativePath),
8a5eea5 @juranki add bat scripts for bootstrap and rebat (windows doesn't understand s…
juranki authored
121 ok.
1ea7065 @dizzyd Rework how the escript is built so that we can inclue templates
dizzyd authored
122
ffa0cda @dizzyd Updating bootstrap to embed the build time into a macro that we can p…
dizzyd authored
123 build_time() ->
bad3a79 @tuncer Adapt to 18.x time api changes
tuncer authored
124 {{Y, M, D}, {H, Min, S}} = calendar:now_to_universal_time(rebar_now()),
461335a @tuncer Reformat bootstrap
tuncer authored
125 lists:flatten(io_lib:format("~4..0w~2..0w~2..0w_~2..0w~2..0w~2..0w",
126 [Y, M, D, H, Min, S])).
bad3a79 @tuncer Adapt to 18.x time api changes
tuncer authored
127 rebar_now() ->
128 case erlang:function_exported(erlang, timestamp, 0) of
129 true ->
130 erlang:timestamp();
131 false ->
5809ba1 @tuncer bootstrap: better warning fix (Thanks James Fish)
tuncer authored
132 %% erlang:now/0 was deprecated in 18.0, and as the escript has to
133 %% pass erl_lint:module/1 (even without -mode(compile)), we would
858fb4f @ferd Fix commit typo
ferd authored
134 %% see a deprecation warning for erlang:now/0. One solution is to
5809ba1 @tuncer bootstrap: better warning fix (Thanks James Fish)
tuncer authored
135 %% use -compile({nowarn_deprecated_function, [{erlang, now, 0}]}),
136 %% but that would raise a warning in versions older than 18.0.
137 %% Calling erlang:now/0 via apply/3 avoids that.
138 apply(erlang, now, [])
bad3a79 @tuncer Adapt to 18.x time api changes
tuncer authored
139 end.
140
27f638b @dizzyd Add support for embedding git version; make sure to always rebuild re…
dizzyd authored
141 vcs_info([]) ->
142 "No VCS info available.";
e34b702 @tuncer bootstrap: fix and enhance VCS_INFO handling
tuncer authored
143 vcs_info([{Id, Dir, VsnCmd, StatusCmd} | Rest]) ->
27f638b @dizzyd Add support for embedding git version; make sure to always rebuild re…
dizzyd authored
144 case filelib:is_dir(Dir) of
145 true ->
e34b702 @tuncer bootstrap: fix and enhance VCS_INFO handling
tuncer authored
146 Vsn = string:strip(os:cmd(VsnCmd), both, $\n),
147 Status = case string:strip(os:cmd(StatusCmd), both, $\n) of
148 [] ->
149 "";
150 _ ->
151 "-dirty"
152 end,
153 lists:concat([Id, " ", Vsn, Status]);
27f638b @dizzyd Add support for embedding git version; make sure to always rebuild re…
dizzyd authored
154 false ->
155 vcs_info(Rest)
156 end.
8c01734 @xenolinguist bootstrap: generate windows scripts
xenolinguist authored
157
158 write_windows_scripts() ->
159 CmdScript=
160 "@echo off\r\n"
161 "setlocal\r\n"
162 "set rebarscript=%~f0\r\n"
fa3dab3 Fix typos in generated cmd script in bootstrap
Mike Lazar authored
163 "escript.exe \"%rebarscript:.cmd=%\" %*\r\n",
cbfabd6 Remove generation of ps1 script (no longer needed)
Mike Lazar authored
164 ok = file:write_file("rebar.cmd", CmdScript).
Something went wrong with that request. Please try again.