Skip to content

Commit

Permalink
Merge pull request #9 from grisp/sylane/fix-semver
Browse files Browse the repository at this point in the history
Fix issue with dependencies using server
  • Loading branch information
sylane authored Oct 8, 2024
2 parents 02a2b73 + 3220bc7 commit 4e9827e
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 169 deletions.
4 changes: 4 additions & 0 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
]}
]}.

{plugins, [
{rebar3_grisp, {git, "[email protected]:grisp/rebar3_grisp", {branch, "master"}}}
]}.

{profiles, [
{test, [
{deps, [
Expand Down
32 changes: 17 additions & 15 deletions src/rebar3_grisp_io_api.erl
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,31 @@ auth(RState, Username, Password) ->

%% @doc Performs a PUT request tp /grisp-manager/api/update-package/PackageName
%% @param Token is the clear token of the user
%% @param PackageName must have the following format "platform.appname.x.y.z.tar
%% @param PackageBin is the content of the package
%% @param PackageName must have the following format
%% platform.appname.x.y.z.[profilename[+profilename]].tar
%% @param PackagePath is the path to the package file to upload
%% @param Force indicates if the PUT request should overwrite
%% the remote files if they exist
-spec update_package(RState, Token, PackageName, PackageBin, Force) -> Res when
-spec update_package(RState, Token, PackageName, PackagePath, Force) -> Res
when
RState :: rebar_state:t(),
Token :: rebar3_grisp_io_config:clear_token(),
PackageName :: string(),
PackageBin :: binary(),
PackageName :: binary(),
PackagePath :: binary(),
Force :: boolean(),
Res :: ok | no_return().
update_package(RState, Token, PackageName, PackageBin, Force) ->
update_package(RState, Token, PackageName, PackagePath, Force) ->
BaseUrl = base_url(RState),
URI = list_to_binary("/grisp-manager/api/update-package/" ++ PackageName),
URI = <<"/grisp-manager/api/update-package/", PackageName/binary>>,
Url = <<BaseUrl/binary, URI/binary>>,
BinSize = byte_size(PackageBin),
BinSize = filelib:file_size(PackagePath),
Etag = <<"\"", PackageName/binary, "\"">>,
Headers = [{<<"authorization">>, bearer_token(Token)},
{<<"content-type">>, <<"application/octet-stream">>},
{<<"content-length">>, integer_to_binary(BinSize)}]
++ if_none_match(Force, "\"" ++ PackageName ++ "\""),
++ if_none_match(Force, Etag),
Options = insecure_option(RState),

case hackney:request(put, Url, Headers, PackageBin, Options) of
case hackney:request(put, Url, Headers, {file, PackagePath}, Options) of
{ok, 201, _, _} ->
ok;
{ok, 204, _, _} ->
Expand Down Expand Up @@ -97,7 +99,7 @@ update_package(RState, Token, PackageName, PackageBin, Force) ->
Res :: ok | no_return().
deploy_update(RState, Token, PackageName, Device) ->
BaseUrl = base_url(RState),
URI = list_to_binary( "/grisp-manager/api/deploy-update/" ++ PackageName),
URI = list_to_binary("/grisp-manager/api/deploy-update/" ++ PackageName),
QS = <<"device=", (integer_to_binary(Device))/binary>>,
Url = hackney_url:make_url(BaseUrl, URI, QS),
Headers = [{<<"authorization">>, bearer_token(Token)},
Expand All @@ -114,7 +116,7 @@ deploy_update(RState, Token, PackageName, Device) ->
{ok, 401, _, _} ->
throw(wrong_credentials);
{ok, 404, _, _} ->
throw(package_does_not_exist);
throw({package_does_not_exist, PackageName});
Other ->
error({error, Other})
end.
Expand Down Expand Up @@ -181,9 +183,9 @@ insecure_option(RState) ->
%% The Etag format must be: <<"\"...\"">>
-spec if_none_match(Force, Etag) -> Result when
Force :: boolean(),
Etag :: string(),
Etag :: binary(),
Result :: [{binary(), binary()}].
if_none_match(true, _) ->
[];
if_none_match(false, Etag) ->
[{<<"if-none-match">>, list_to_binary(Etag)}].
[{<<"if-none-match">>, Etag}].
9 changes: 1 addition & 8 deletions src/rebar3_grisp_io_auth.erl
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,7 @@ do(RState) ->
throw:forbidden ->
abort("Error: No permission to perform this operation");
throw:not_matching ->
abort("Error: The 2 local password entries don't match");
error:E:S ->
case lists:member(test, rebar_state:current_profiles(RState)) of
true ->
abort("Unexpected error: ~p -> ~p ~n", [E, S]);
false ->
abort("Unexpected error: ~p ~n", [E])
end
abort("Error: The 2 local password entries don't match")
end.

-spec format_error(any()) -> iolist().
Expand Down
26 changes: 15 additions & 11 deletions src/rebar3_grisp_io_deploy.erl
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,14 @@ do(RState) ->
{ok, _} = application:ensure_all_started(rebar3_grisp_io),
try
{Args, _} = rebar_state:command_parsed_args(RState),
RelNameArg = proplists:get_value(relname, Args, undefined),
RelVsnArg = proplists:get_value(relvsn, Args, undefined),
Device = try_get_device_serial(Args),
CurrentRelease = rebar3_grisp_io_utils:expected_package_name(RState),

{RelName, RelVsn}
= rebar3_grisp_util:select_release(RState, RelNameArg, RelVsnArg),
CurrentRelease = binary_to_list(
rebar3_grisp_util:update_file_name(RState, RelName, RelVsn)),
PackageName = proplists:get_value(package, Args, CurrentRelease),

Config = rebar3_grisp_io_config:read_config(RState),
Expand All @@ -68,16 +74,10 @@ do(RState) ->
abort("Error: Wrong credentials");
throw:forbidden ->
abort("Error: No permission to perform this operation");
throw:package_does_not_exist ->
abort("Error: The package doesn't exists. Use the upload command" ++
" first to upload an update package to grisp.io");
error:E:S ->
case lists:member(test, rebar_state:current_profiles(RState)) of
true ->
abort("Unexpected error: ~p -> ~p ~n", [E, S]);
false ->
abort("Unexpected error: ~p ~n", [E])
end
throw:{package_does_not_exist, Name} ->
abort("Error: The package ~s doesn't exists. Use the upload " ++
"command first to upload an update package to grisp.io",
[Name])
end.

-spec format_error(any()) -> iolist().
Expand All @@ -87,6 +87,10 @@ format_error(Reason) ->
%--- Internals -----------------------------------------------------------------

options() -> [
{relname, $n, "relname", string,
"Specify the name for the release to deploy"},
{relvsn, $v, "relvsn", string,
"Specify the version of the release to deploy"},
{device, $d, "device", integer, "The serial number of the GRiSP board"},
{package, $p, "package", string,
"The name of the package that will be deployed"}
Expand Down
2 changes: 1 addition & 1 deletion src/rebar3_grisp_io_io.erl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ success(Msg) ->
success(Msg, []).
success(Msg, Args) ->
Text = io_lib:format(Msg, Args),
rebar_api:console(color(40, Text), Args).
rebar_api:console(color(40, Text), []).

%--- Internals -----------------------------------------------------------------

Expand Down
123 changes: 64 additions & 59 deletions src/rebar3_grisp_io_upload.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
success/1,
success/2]).


%--- MACROS --------------------------------------------------------------------

-define(MAX_DDOT, 2).


%--- API -----------------------------------------------------------------------

-spec init(rebar_state:t()) -> {ok, rebar_state:t()}.
Expand All @@ -39,37 +45,31 @@ init(State) ->
do(RState) ->
{ok, _} = application:ensure_all_started(rebar3_grisp_io),
try
{Args, _} = rebar_state:command_parsed_args(RState),
{Args, ExtraArgs} = rebar_state:command_parsed_args(RState),
RelNameArg = proplists:get_value(relname, Args, undefined),
RelVsnArg = proplists:get_value(relvsn, Args, undefined),
{RelName, RelVsn}
= rebar3_grisp_util:select_release(RState, RelNameArg, RelVsnArg),

Force = proplists:get_value(force, Args),
NoPack = proplists:get_value(no_pack, Args),
Refresh = proplists:get_value(refresh, Args),

Config = rebar3_grisp_io_config:read_config(RState),
EncryptedToken = maps:get(encrypted_token, Config),
Password = ask("Local password", password),
Token = rebar3_grisp_io_config:try_decrypt_token(Password,
EncryptedToken),

RState1 = case NoPack of
false ->
try_pack_command(RState, Force);
true ->
RState
end,

ProjectDir = rebar_state:dir(RState),
PackageName = rebar3_grisp_io_utils:expected_package_name(RState),
{ok, PackageBin} = try_get_package_bin(ProjectDir, PackageName),
case get_package(RState, Refresh, RelName, RelVsn, ExtraArgs) of
{error, _Reason} = Error -> Error;
{ok, PackageName, PackagePath, RState2} ->
rebar3_grisp_io_api:update_package(RState2, Token, PackageName,
PackagePath, Force),
success("Package ~s succesfully uploaded to grisp.io",
[PackageName]),
{ok, RState2}
end

rebar3_grisp_io_api:update_package(RState1,
Token,
PackageName,
PackageBin,
Force),

success("Package " ++ PackageName ++
" succesfully uploaded to grisp.io"),

{ok, RState1}
catch
throw:enoent ->
abort("No configuration available." ++
Expand All @@ -86,54 +86,59 @@ do(RState) ->
throw:package_already_exists ->
abort("Error: A package has already been uploaded for " ++
"the same release. Use -f or --force to force the upload");
throw:{package_not_found, Name} ->
abort("Error: Package file ~s not found",
[grisp_tools_util:maybe_relative(Name, ?MAX_DDOT)]);
throw:package_too_big ->
abort("Package size is too big");
error:E:S ->
case lists:member(test, rebar_state:current_profiles(RState)) of
true ->
abort("Unexpected error: ~p -> ~p ~n", [E, S]);
false ->
abort("Unexpected error: ~p ~n", [E])
end
abort("Package size is too big")
end.

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

%--- Internals -----------------------------------------------------------------

options() -> [
{relname, $n, "relname", string,
"Specify the name for the release that will be uploaded"},
{relvsn, $v, "relvsn", string,
"Specify the version of the release"},
{force, $f, "force", {boolean, false},
"Force overwriting of the files both locally and remotely"},
{no_pack, $n, "no-pack", {boolean, false},
"Do not run the pack command on " ++
"the current project before uploading"}
"Force overwriting of the package remotely"},
{refresh, $r, "refresh", {boolean, false},
"Force software package building even if it already exists"}
].

-spec try_pack_command(rebar_state:t(), boolean()) -> rebar_state:t().
try_pack_command(RState, Force) ->
Args = case Force of
true ->
["--force"];
false ->
[]
end,
case rebar3_grisp_io_utils:grisp_pack(RState, Args) of
{error, Reason} ->
error(Reason);
{ok, NewRState} ->
NewRState
-spec get_package(rebar_state:t(), boolean(), atom(), binary(), [binary()])
-> {ok, binary(), binary(), rebar_state:t()} | {error, term()}.
get_package(RState, Refresh, RelName, RelVsn, ExtraRelArgs) ->
PackageName = rebar3_grisp_util:update_file_name(RState, RelName, RelVsn),
PackagePath = rebar3_grisp_util:update_file_path(RState, RelName, RelVsn),
case filelib:is_file(PackagePath) of
true when Refresh =:= false ->
RelPath = grisp_tools_util:maybe_relative(PackagePath, ?MAX_DDOT),
console("* Using existing package: ~s", [RelPath]),
{ok, PackageName, PackagePath, RState};
_ ->
console("* Building software package...", []),
case build_package(RState, Refresh, RelName,
RelVsn, ExtraRelArgs) of
{ok, RState2} -> {ok, PackageName, PackagePath, RState2};
{error, _Reason} = Error -> Error
end
end.

-spec try_get_package_bin(ProjectDir, PackageName) -> Result when
ProjectDir :: string(),
PackageName :: string(),
Result :: {ok, binary()} | {error, term()}.
try_get_package_bin(ProjectDir, PackageName) ->
Path = filename:join([ProjectDir, "_grisp/update/", PackageName]),
case filelib:is_file(Path) of
false ->
error(package_not_found);
true ->
file:read_file(Path)
end.
-spec build_package(rebar_state:t(), boolean(), atom(), binary(), [binary()])
-> {ok, rebar_state:t()} | {error, term()}.
build_package(RState, Refresh, RelName, RelVsn, ExtraRelArgs) ->
Args = [
"--force",
"--quiet",
"--relname", atom_to_list(RelName),
"--relvsn", RelVsn
] ++ case Refresh of
true -> ["--refresh"];
false -> []
end,
rebar3_grisp_io_utils:grisp_pack(RState, Args, ExtraRelArgs).
Loading

0 comments on commit 4e9827e

Please sign in to comment.