Skip to content

Commit

Permalink
Merge pull request #2559 from tsloughter/upgrade-vsn
Browse files Browse the repository at this point in the history
local upgrade: extract vsn of rebar3 from appfile of downloaded escript
  • Loading branch information
ferd authored May 22, 2021
2 parents 2593c48 + e0b26b6 commit e44c884
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
9 changes: 6 additions & 3 deletions src/rebar_prv_local_install.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
do/1,
format_error/1]).

-export([extract_escript/2]).
-export([extract_escript/2,
install_escript/3]).

-include("rebar.hrl").
-include_lib("providers/include/providers.hrl").
Expand Down Expand Up @@ -71,11 +72,13 @@ erl -pz ", (rebar_utils:to_binary(OutputDir))/binary,"/${VSN}/lib/*/ebin +sbtu +
extract_escript(State, ScriptPath) ->
{ok, Escript} = escript:extract(ScriptPath, []),
{archive, Archive} = lists:keyfind(archive, 1, Escript),
{ok, Vsn} = application:get_key(rebar, vsn),
install_escript(State, Vsn, Archive).

%% Extract contents of Archive to ~/.cache/rebar3/lib
install_escript(State, Vsn, Archive) ->
%% Extract contents of Archive to ~/.cache/rebar3/vsns/<VSN>/lib
%% And add a rebar3 bin script to ~/.cache/rebar3/bin
Opts = rebar_state:opts(State),
{ok, Vsn} = application:get_key(rebar, vsn),
VersionsDir = filename:join(rebar_dir:global_cache_dir(Opts), "vsns"),
OutputDir = filename:join([VersionsDir, Vsn, "lib"]),
case filelib:ensure_dir(filename:join([OutputDir, "empty"])) of
Expand Down
15 changes: 14 additions & 1 deletion src/rebar_prv_local_upgrade.erl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,18 @@ do(State) ->

case maybe_fetch_rebar3(Md5) of
{saved, TmpRebar3} ->
rebar_prv_local_install:extract_escript(State, TmpRebar3);
{Vsn, Archive} =
try
{ok, Escript} = escript:extract(TmpRebar3, []),
{comment, "Rebar3 " ++ Rebar3Vsn} = lists:keyfind(comment, 1, Escript),
{archive, FullArchive} = lists:keyfind(archive, 1, Escript),
{Rebar3Vsn, FullArchive}
catch
C:T:S ->
?DIAGNOSTIC("local upgrade version extraction exception: ~p:~p:~p", [C, T, S]),
error(?PRV_ERROR(failed_vsn_lookup))
end,
rebar_prv_local_install:install_escript(State, Vsn, Archive);
up_to_date ->
{ok, State};
Error ->
Expand All @@ -61,6 +72,8 @@ do(State) ->
end.

-spec format_error(any()) -> iolist().
format_error(failed_vsn_lookup) ->
"Failed to extract the version from the downloaded rebar3 escript.\n Try downloading https://s3.amazonaws.com/rebar3/rebar3 manually and running `chmod +x rebar3 && ./rebar3 local install`";
format_error(bad_checksum) ->
"Not updating rebar3, the checksum of download did not match the one provided by s3.";
format_error(Reason) ->
Expand Down

0 comments on commit e44c884

Please sign in to comment.