-
Notifications
You must be signed in to change notification settings - Fork 518
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add extract provider for extracting escript archive
- Loading branch information
1 parent
d7f2226
commit 1190eb3
Showing
3 changed files
with
88 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 \"$@\" | ||
">>. |