diff options
author | Dave Smith <dizzyd@dizzyd.com> | 2009-12-04 13:48:57 -0700 |
---|---|---|
committer | Dave Smith <dizzyd@dizzyd.com> | 2009-12-04 13:48:57 -0700 |
commit | b49189869067401c9da92a22f0a2343b7a2a2456 (patch) | |
tree | 71067ef3fc688b26c2f919e6234340a906add155 /bootstrap | |
parent | 63d4968e36275e4d8ab4698d002b0e5db6d0d7a1 (diff) |
Refactor rebar to build a self-contained script
Diffstat (limited to 'bootstrap')
-rwxr-xr-x | bootstrap | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/bootstrap b/bootstrap new file mode 100755 index 0000000..04f94dc --- /dev/null +++ b/bootstrap @@ -0,0 +1,53 @@ +#!/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"). + + + |