Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for all argument in upgrade plugin #2641

Merged
merged 4 commits into from
Nov 26, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions src/rebar_prv_plugins_upgrade.erl
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,43 @@ init(State) ->
{bare, true},
{deps, ?DEPS},
{example, "rebar3 plugins upgrade <plugin>"},
{short_desc, "Upgrade plugins"},
{desc, "List or upgrade plugins"},
{short_desc, "Upgrade plugins."},
{desc, "List or upgrade plugins. Use the -a/--all option to upgrade"
" all plugins."},
{opts, [{plugin, undefined, undefined, string,
"Plugin to upgrade"}]}])),
"Plugin to upgrade"},
{all, $a, "all", undefined, "Upgrade all plugins."}]}])),
{ok, State1}.

-spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}.
do(State) ->
{Args, _} = rebar_state:command_parsed_args(State),
case proplists:get_value(plugin, Args, none) of
none ->
case handle_args(State) of
{false, undefined} -> throw(?PRV_ERROR(no_arg));
{true, _} ->
{_, LocalPluginsNames} = rebar_prv_plugins:list_local_plugins(State),
lists:foldl(
fun (LocalPluginName, {ok, StateAcc}) ->
upgrade(atom_to_list(LocalPluginName), StateAcc)
end,
{ok, State},
LocalPluginsNames);
Plugin ->
upgrade(Plugin, State)
{false, Plugin} -> upgrade(Plugin, State)
end.

-spec format_error(any()) -> iolist().
format_error({not_found, Plugin}) ->
io_lib:format("Plugin to upgrade not found: ~ts", [Plugin]);
format_error(no_arg) ->
"Specify a plugin to upgrade, or --all to upgrade them all";
format_error(Reason) ->
io_lib:format("~p", [Reason]).

handle_args(State) ->
{Args, _} = rebar_state:command_parsed_args(State),
All = proplists:get_value(all, Args, false),
Plugin = proplists:get_value(plugin, Args),
{All, Plugin}.

upgrade(Plugin, State) ->
Profiles = rebar_state:current_profiles(State),
case find_plugin(Plugin, Profiles, State) of
Expand Down
10 changes: 9 additions & 1 deletion test/rebar_plugins_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
list/1,
upgrade/1,
upgrade_project_plugin/1,
upgrade_no_args/1,
sub_app_plugins/1,
sub_app_plugin_overrides/1,
project_plugins/1,
Expand Down Expand Up @@ -220,7 +221,7 @@ upgrade(Config) ->
),

rebar_test_utils:run_and_check(
Config, RConf, ["plugins", "upgrade"],
Config, RConf, ["plugins", "upgrade", "--all"],
{ok, [{app, Name, valid}, {file, PluginBeam}, {plugin, PkgName}]}
).

Expand Down Expand Up @@ -263,6 +264,13 @@ upgrade_project_plugin(Config) ->
{ok, [{app, Name}, {plugin, PkgName, <<"0.1.3">>}]}
).

upgrade_no_args(Config) ->
try rebar_test_utils:run_and_check(Config, [], ["plugins", "upgrade"], return)
catch {error, {rebar_prv_plugins_upgrade, no_arg}} ->
ok
end,
ok.

sub_app_plugins(Config) ->
AppDir = ?config(apps, Config),

Expand Down