From da3d8a69cc6467dcb63348dfa02bc4b98f528ca4 Mon Sep 17 00:00:00 2001 From: Tristan Sloughter Date: Mon, 7 Sep 2015 20:03:08 -0500 Subject: add providers 'local upgrade' and 'local install' for installing/upgrading --- src/rebar_prv_local_install.erl | 90 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 src/rebar_prv_local_install.erl (limited to 'src/rebar_prv_local_install.erl') 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}. -- cgit v1.1 From 286e832a29ee502f5debb5a7393e7209e1848cf4 Mon Sep 17 00:00:00 2001 From: Tristan Sloughter Date: Thu, 10 Sep 2015 15:12:22 -0500 Subject: move local install/upgrade to 'unstable install/upgrade' and print error if windows --- src/rebar_prv_local_install.erl | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'src/rebar_prv_local_install.erl') diff --git a/src/rebar_prv_local_install.erl b/src/rebar_prv_local_install.erl index e9dcdb9..6d83fc3 100644 --- a/src/rebar_prv_local_install.erl +++ b/src/rebar_prv_local_install.erl @@ -15,7 +15,7 @@ -include_lib("kernel/include/file.hrl"). -define(PROVIDER, install). --define(NAMESPACE, local). +-define(NAMESPACE, unstable). -define(DEPS, []). %% =================================================================== @@ -39,12 +39,18 @@ init(State) -> -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...", []), + case os:type() of + {win32, _} -> + ?ERROR("Sorry, this feature is not yet available on Windows.", []), {ok, State}; - ScriptPath -> - extract_escript(State, ScriptPath) + _ -> + 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 end. -spec format_error(any()) -> iolist(). -- cgit v1.1 From 9fb81989267b01a954108c4b9528e52a595aa199 Mon Sep 17 00:00:00 2001 From: Tristan Sloughter Date: Thu, 10 Sep 2015 16:41:48 -0500 Subject: update completions and docs for command unstable install and upgrade --- src/rebar_prv_local_install.erl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/rebar_prv_local_install.erl') diff --git a/src/rebar_prv_local_install.erl b/src/rebar_prv_local_install.erl index 6d83fc3..4422c2d 100644 --- a/src/rebar_prv_local_install.erl +++ b/src/rebar_prv_local_install.erl @@ -31,7 +31,7 @@ init(State) -> {bare, true}, {namespace, ?NAMESPACE}, {deps, ?DEPS}, - {example, "rebar3 local install"}, + {example, "rebar3 unstable install"}, {short_desc, "Extract libs from rebar3 escript along with a run script."}, {desc, ""}, {opts, []}])), -- cgit v1.1