Changed relative path
[makerl.git] / bootstrap
blob7705b60dede0ff9922ecdccfe41bfd551a4b66ff
1 #!/usr/bin/env escript
2 %%! -noshell -noinput -pa .\ebin
3 %% -*- mode:erlang;tab-width:4;erlang-indent-level:4;indent-tabs-mode:nil -*-
4 %% ex: ft=erlang ts=4 sw=4 et
6 -define(SCRIPT, "mkrl").
8 compile_dir(DebugFlag, Dir) ->
9     Files = filelib:wildcard(filename:join(Dir, "*.erl")),
10     io:format("~p~n", [Files]),
11     case make:files(Files, 
12                     [{outdir, "ebin"},
13                      {i, "include"},
14                      DebugFlag]) of
15         up_to_date ->
16             ok;
17         error ->
18             io:format("Compilation failed!~n"),
19             halt(1)
20     end.
22 main(Args) ->
23     %% Add check for debug flag
24     case lists:member("debug", Args) of
25         true ->
26             DebugFlag = debug_info;
27         false ->
28             DebugFlag = undefined
29     end,    
31     %% Compile all src/*.erl to ebin
32     %%XXX: This is not enough for bootstraping... at least hooks/core
33     lists:foreach(fun(F) -> 
34                           compile_dir(DebugFlag, F)
35                   end, 
36                   ["src",
37                    "src/hooks/otp",
38                    "src/hooks/core"]),
40     true = code:add_path("ebin"),
42     Files = load_files("*", "ebin") ++ load_files("*.config", "etc"),
44     case zip:create("mem", Files, [memory]) of
45         {ok, {"mem", ZipBin}} ->
46             %% Archive was successfully created. Prefix that binary with our
47             %% header and write to ?SCRIPT file
48             Script = <<"#!/usr/bin/env escript\n%%! -noshell -noinput\n", ZipBin/binary>>,
49             case file:write_file(?SCRIPT, Script) of
50                 ok ->
51                     ok;
52                 {error, WriteError} ->
53                     io:format("Failed to write ~p script: ~p\n", [?SCRIPT, WriteError]),
54                     halt(1)
55             end;
56         {error, ZipError} ->
57             io:format("Failed to construct ~p script archive: ~p\n", [?SCRIPT, ZipError]),
58             halt(1)
59     end,
60     
61     %% Finally, update executable perms for our script
62     case os:type() of
63         {unix,_} ->
64             [] = os:cmd("chmod u+x " ++ ?SCRIPT),
65             ok;
66         _ ->
67             ok
68     end,    
70     
71     %% Add a helpful message
72     io:format("CONGRATULATIONS! You now have a self-contained script called \"~s\" in~n"
73               "your current working directory. Place this script anywhere in your path.~n", [?SCRIPT]).
75 load_files(Wildcard, Dir) ->
76     [read_file(Filename, Dir) || Filename <- filelib:wildcard(Wildcard, Dir)].
78 read_file(Filename, Dir) ->
79     {ok, Bin} = file:read_file(filename:join(Dir, Filename)),
80     {Filename, Bin}.