summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTristan Sloughter <t@crashfast.com>2015-09-07 20:03:08 -0500
committerTristan Sloughter <t@crashfast.com>2015-09-07 22:05:53 -0500
commitda3d8a69cc6467dcb63348dfa02bc4b98f528ca4 (patch)
treee64a7014f58b75335824753247cf69a5230b4a20 /src
parentd7f2226d31f4708518c1ddb41f7666f9c1d0f151 (diff)
add providers 'local upgrade' and 'local install' for installing/upgrading
Diffstat (limited to 'src')
-rw-r--r--src/rebar.app.src4
-rw-r--r--src/rebar3.erl18
-rw-r--r--src/rebar_pkg_resource.erl3
-rw-r--r--src/rebar_prv_local_install.erl90
-rw-r--r--src/rebar_prv_local_upgrade.erl76
5 files changed, 187 insertions, 4 deletions
diff --git a/src/rebar.app.src b/src/rebar.app.src
index 655506e..4d1dfd7 100644
--- a/src/rebar.app.src
+++ b/src/rebar.app.src
@@ -23,7 +23,7 @@
erlware_commons,
providers,
bbmustache,
- ssl_verify_hostname,
+ ssl_verify_hostname,
relx,
inets]},
{env, [
@@ -49,6 +49,8 @@
rebar_prv_eunit,
rebar_prv_help,
rebar_prv_install_deps,
+ rebar_prv_local_install,
+ rebar_prv_local_upgrade,
rebar_prv_lock,
rebar_prv_new,
rebar_prv_packages,
diff --git a/src/rebar3.erl b/src/rebar3.erl
index 5233f8e..aae9ec0 100644
--- a/src/rebar3.erl
+++ b/src/rebar3.erl
@@ -26,7 +26,8 @@
%% -------------------------------------------------------------------
-module(rebar3).
--export([main/1,
+-export([main/0,
+ main/1,
run/1,
run/2,
global_option_spec_list/0,
@@ -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) ->
@@ -146,7 +153,14 @@ init_config() ->
%% resources out of the escript
State1 = try
ScriptName = filename:absname(escript:script_name()),
- rebar_state:escript_path(State, ScriptName)
+ %% Running with 'erl -s rebar3 main' still sets a name for some reason
+ %% so verify it is a real file
+ case filelib:is_regular(ScriptName) of
+ true ->
+ rebar_state:escript_path(State, ScriptName);
+ false ->
+ State
+ end
catch
_:_ ->
State
diff --git a/src/rebar_pkg_resource.erl b/src/rebar_pkg_resource.erl
index 4ac0a1d..bff7bc6 100644
--- a/src/rebar_pkg_resource.erl
+++ b/src/rebar_pkg_resource.erl
@@ -9,7 +9,8 @@
,needs_update/2
,make_vsn/1]).
--export([ssl_opts/1]).
+-export([request/2
+ ,ssl_opts/1]).
-include("rebar.hrl").
-include_lib("public_key/include/OTP-PUB-KEY.hrl").
diff --git a/src/rebar_prv_local_install.erl b/src/rebar_prv_local_install.erl
new file mode 100644
index 0000000..e9dcdb9
--- /dev/null
+++ b/src/rebar_prv_local_install.erl
@@ -0,0 +1,90 @@
+%% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
+%% ex: ts=4 sw=4 et
+
+-module(rebar_prv_local_install).
+
+-behaviour(provider).
+
+-export([init/1,
+ do/1,
+ format_error/1]).
+
+-export([extract_escript/2]).
+
+-include("rebar.hrl").
+-include_lib("kernel/include/file.hrl").
+
+-define(PROVIDER, install).
+-define(NAMESPACE, local).
+-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},
+ {namespace, ?NAMESPACE},
+ {deps, ?DEPS},
+ {example, "rebar3 local install"},
+ {short_desc, "Extract libs from rebar3 escript along with a run script."},
+ {desc, ""},
+ {opts, []}])),
+ {ok, State1}.
+
+-spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}.
+do(State) ->
+ case rebar_state:escript_path(State) of
+ undefined ->
+ ?INFO("Already running from an unpacked rebar3. Nothing to do...", []),
+ {ok, State};
+ ScriptPath ->
+ extract_escript(State, ScriptPath)
+ end.
+
+-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 -boot start_clean -s rebar3 main -extra \"$@\"
+">>.
+
+extract_escript(State, ScriptPath) ->
+ {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 = _,
+ 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=33277,
+ uid=Uid,
+ gid=Gid}),
+
+ ?INFO("Add to $PATH for use: export PATH=$PATH:~s", [BinDir]),
+
+ {ok, State}.
diff --git a/src/rebar_prv_local_upgrade.erl b/src/rebar_prv_local_upgrade.erl
new file mode 100644
index 0000000..248be97
--- /dev/null
+++ b/src/rebar_prv_local_upgrade.erl
@@ -0,0 +1,76 @@
+%% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
+%% ex: ts=4 sw=4 et
+
+-module(rebar_prv_local_upgrade).
+
+-behaviour(provider).
+
+-export([init/1,
+ do/1,
+ format_error/1]).
+
+-include("rebar.hrl").
+-include_lib("kernel/include/file.hrl").
+
+-define(PROVIDER, upgrade).
+-define(NAMESPACE, local).
+-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},
+ {namespace, ?NAMESPACE},
+ {deps, ?DEPS},
+ {example, "rebar3 local upgrade"},
+ {short_desc, "Download latest rebar3 escript and extract."},
+ {desc, ""},
+ {opts, []}])),
+ {ok, State1}.
+
+-spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}.
+do(State) ->
+ Md5 = case rebar_state:escript_path(State) of
+ undefined ->
+ false;
+ ScriptPath ->
+ get_md5(ScriptPath)
+ end,
+
+ case maybe_fetch_rebar3(Md5) of
+ up_to_date ->
+ ?CONSOLE("No upgrade available", []),
+ {ok, State};
+ {saved, TmpRebar3} ->
+ rebar_prv_local_install:extract_escript(State, TmpRebar3)
+ end.
+
+-spec format_error(any()) -> iolist().
+format_error(Reason) ->
+ io_lib:format("~p", [Reason]).
+
+%% Internal
+
+get_md5(Rebar3Path) ->
+ {ok, Rebar3File} = file:read_file(Rebar3Path),
+ Digest = crypto:hash(md5, Rebar3File),
+ DigestHex = lists:flatten([io_lib:format("~2.16.0B", [X]) || X <- binary_to_list(Digest)]),
+ string:to_lower(DigestHex).
+
+maybe_fetch_rebar3(Rebar3Md5) ->
+ TmpDir = ec_file:insecure_mkdtemp(),
+ TmpFile = filename:join(TmpDir, "rebar3"),
+ case rebar_pkg_resource:request("https://s3.amazonaws.com/rebar3/rebar3", Rebar3Md5) of
+ {ok, Binary, _ETag} ->
+ file:write_file(TmpFile, Binary),
+ {saved, TmpFile};
+ _ ->
+ up_to_date
+ end.