Skip to content

Commit

Permalink
add extract provider for extracting escript archive
Browse files Browse the repository at this point in the history
  • Loading branch information
tsloughter committed Sep 8, 2015
1 parent d7f2226 commit 1190eb3
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/rebar.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
rebar_prv_edoc,
rebar_prv_escriptize,
rebar_prv_eunit,
rebar_prv_extract,
rebar_prv_help,
rebar_prv_install_deps,
rebar_prv_lock,
Expand Down
9 changes: 8 additions & 1 deletion src/rebar3.erl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
%% -------------------------------------------------------------------
-module(rebar3).

-export([main/1,
-export([main/0,
main/1,
run/1,
run/2,
global_option_spec_list/0,
Expand All @@ -42,6 +43,12 @@
%% Public API
%% ====================================================================

%% For running with:
%% erl +sbtu +A0 -noinput -mode minimal -boot start_clean -s rebar3 main -extra "$@"
main() ->
List = init:get_plain_arguments(),
main(List).

%% escript Entry point
-spec main(list()) -> no_return().
main(Args) ->
Expand Down
79 changes: 79 additions & 0 deletions src/rebar_prv_extract.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
%% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
%% ex: ts=4 sw=4 et

-module(rebar_prv_extract).

-behaviour(provider).

-export([init/1,
do/1,
format_error/1]).

-include("rebar.hrl").
-include_lib("kernel/include/file.hrl").

-define(PROVIDER, extract).
-define(DEPS, []).

%% ===================================================================
%% Public API
%% ===================================================================

-spec init(rebar_state:t()) -> {ok, rebar_state:t()}.
init(State) ->
State1 =
rebar_state:add_provider(State,
providers:create([{name, ?PROVIDER},
{module, ?MODULE},
{bare, true},
{deps, ?DEPS},
{example, "rebar3 extract"},
{short_desc, "Extract libs from rebar3 escript."},
{desc, ""},
{opts, []}])),
{ok, State1}.

-spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}.
do(State) ->
ScriptPath = rebar_state:escript_path(State),
{ok, Escript} = escript:extract(ScriptPath, []),
{archive, Archive} = lists:keyfind(archive, 1, Escript),

%% Extract contents of Archive to ~/.cache/rebar3/lib
%% And add a rebar3 bin script to ~/.cache/rebar3/bin
Opts = rebar_state:opts(State),
OutputDir = filename:join(rebar_dir:global_cache_dir(Opts), "lib"),
filelib:ensure_dir(filename:join(OutputDir, "empty")),

?INFO("Extracting rebar3 libs to ~s...", [OutputDir]),
zip:extract(Archive, [{cwd, OutputDir}]),

BinDir = filename:join(rebar_dir:global_cache_dir(Opts), "bin"),
BinFile = filename:join(BinDir, "rebar3"),
filelib:ensure_dir(BinFile),

{ok, #file_info{mode = Mode,
uid = Uid,
gid = Gid}} = file:read_file_info(ScriptPath, [mode, uid, gid]),

?INFO("Writing rebar3 run script ~s...", [BinFile]),
file:write_file(BinFile, bin_contents(OutputDir)),
ok = file:write_file_info(BinFile, #file_info{mode=Mode,
uid=Uid,
gid=Gid}),

?INFO("Add to $PATH for use: export PATH=$PATH:~s", [BinDir]),

{ok, State}.


-spec format_error(any()) -> iolist().
format_error(Reason) ->
io_lib:format("~p", [Reason]).

bin_contents(OutputDir) ->
<<"
#!/usr/bin/env sh
erl -pa ", (ec_cnv:to_binary(OutputDir))/binary,"/*/ebin +sbtu +A0 -noinput -smp disable -mode minimal -boot start_clean -s rebar3 main -extra \"$@\"
">>.

0 comments on commit 1190eb3

Please sign in to comment.