Permalink
Browse files

Refactor rebar to build a self-contained script

  • Loading branch information...
1 parent 63d4968 commit b49189869067401c9da92a22f0a2343b7a2a2456 @dizzyd dizzyd committed Dec 4, 2009
Showing with 59 additions and 61 deletions.
  1. +1 −0 .hgignore
  2. +53 −0 bootstrap
  3. +2 −1 ebin/rebar.app
  4. +0 −20 install
  5. +0 −37 priv/rebar
  6. +2 −2 src/rebar_app_utils.erl
  7. +1 −1 src/rebar_core.erl
View
@@ -1 +1,2 @@
.beam
+rebar
View
@@ -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").
+
+
+
View
@@ -1,7 +1,8 @@
{application, rebar,
[{description, "Rebar: Erlang Build Tool"},
{vsn, "1"},
- {modules, [ rebar_app_utils,
+ {modules, [ rebar,
+ rebar_app_utils,
rebar_config,
rebar_core,
rebar_ct,
View
20 install
@@ -1,20 +0,0 @@
-#!/bin/bash
-
-## Check path for exiting rebar instance -- if it's around, use it
-## for compilation (NOT installation!)
-`which -s rebar`
-if [ $? == 0 ]; then
- rebar compile ${@}
-else
- ## Use raw erlc..
- erlc -I include -o ebin src/*.erl
-fi
-
-if [ $? != 0 ]; then
- exit $?
-fi
-
-## Use application installer to perform actual installation
-## into erlang distro
-export ERL_LIBS=`(cd .. && pwd)`
-priv/rebar install ${@}
View
@@ -1,37 +0,0 @@
-#!/usr/bin/env escript
-%% -*- erlang -*-
-
-%% -------------------------------------------------------------------
-%%
-%% rebar: Erlang Build Tools
-%%
-%% Copyright (c) 2009 Dave Smith (dizzyd@dizzyd.com)
-%%
-%% Permission is hereby granted, free of charge, to any person obtaining a copy
-%% of this software and associated documentation files (the "Software"), to deal
-%% in the Software without restriction, including without limitation the rights
-%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-%% copies of the Software, and to permit persons to whom the Software is
-%% furnished to do so, subject to the following conditions:
-%%
-%% The above copyright notice and this permission notice shall be included in
-%% all copies or substantial portions of the Software.
-%%
-%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-%% THE SOFTWARE.
-%% -------------------------------------------------------------------
-
--include_lib("rebar/include/rebar.hrl").
-
-main(Args) ->
- case rebar_core:run(Args) of
- ok ->
- ok;
- _ ->
- halt(1)
- end.
View
@@ -51,8 +51,8 @@ load_app_file(Filename) ->
{ok, AppName, AppData};
{error, Reason} ->
?ERROR("Failed to load app file from ~s: ~p\n", [Filename, Reason]),
- error;
+ ?FAIL;
Other ->
?ERROR("Unexpected terms from app file ~s: ~p\n", [Filename, Other]),
- error
+ ?FAIL
end.
View
@@ -38,7 +38,7 @@ run(Args) ->
Commands = filter_flags(Args, []),
%% Pre-load the rebar app so that we get default configuration
- application:load(rebar),
+ ok = application:load(rebar),
%% Initialize logging system
rebar_log:init(),

0 comments on commit b491898

Please sign in to comment.