Skip to content

Commit

Permalink
Merge pull request #2521 from paulo-ferraz-oliveira/feature/rebar3_pl…
Browse files Browse the repository at this point in the history
…ugins_upgrade

Have "rebar3 plugins upgrade" work without specifying plugin name
  • Loading branch information
ferd authored Apr 2, 2021
2 parents ab5c342 + 10404ab commit c102379
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
22 changes: 18 additions & 4 deletions src/rebar_prv_plugins.erl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

-export([init/1,
do/1,
format_error/1]).
format_error/1,
list_local_plugins/1]).

-include("rebar.hrl").
-include_lib("providers/include/providers.hrl").
Expand Down Expand Up @@ -41,22 +42,35 @@ do(State) ->

RebarOpts = rebar_state:opts(State),
SrcDirs = rebar_dir:src_dirs(RebarOpts, ["src"]),
Plugins = rebar_state:get(State, plugins, []),
ProjectPlugins = rebar_state:get(State, project_plugins, []),
{LocalPluginsDefs, _} = list_local_plugins(State),
PluginsDirs = filelib:wildcard(filename:join(rebar_dir:plugins_dir(State), "*")),

%% use `checkouts_dir' and not `checkouts_out_dir'. Since we use `all' in `find_apps'
%% so it doesn't need to be built and the apps in `checkouts_dir' could be old
%% because the user removing from `_checkouts/' doesn't cause removal of the output
CheckoutsDirs = filelib:wildcard(filename:join(rebar_dir:checkouts_dir(State), "*")),
Apps = rebar_app_discover:find_apps(CheckoutsDirs++PluginsDirs, SrcDirs, all, State),
display_plugins("Local plugins", Apps, Plugins ++ ProjectPlugins),
display_plugins("Local plugins", Apps, LocalPluginsDefs),
{ok, State}.

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

list_local_plugins(State) ->
LocalPluginsDefs = rebar_state:get(State, plugins, [])
++ rebar_state:get(State, project_plugins, []),
LocalPluginsNames = lists:map(
fun (LocalPluginDef) ->
rebar_utils:to_atom(
if is_tuple(LocalPluginDef) -> element(1, LocalPluginDef);
LocalPluginDef -> LocalPluginDef
end
)
end,
LocalPluginsDefs),
{LocalPluginsDefs, LocalPluginsNames}.

display_plugins(_Header, _Apps, []) ->
ok;
display_plugins(Header, Apps, Plugins) ->
Expand Down
10 changes: 7 additions & 3 deletions src/rebar_prv_plugins_upgrade.erl
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,18 @@ do(State) ->
{Args, _} = rebar_state:command_parsed_args(State),
case proplists:get_value(plugin, Args, none) of
none ->
?PRV_ERROR(no_plugin_arg);
{_, 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)
end.

-spec format_error(any()) -> iolist().
format_error(no_plugin_arg) ->
io_lib:format("Must give an installed plugin to upgrade as an argument", []);
format_error({not_found, Plugin}) ->
io_lib:format("Plugin to upgrade not found: ~ts", [Plugin]);
format_error(Reason) ->
Expand Down
5 changes: 5 additions & 0 deletions src/rebar_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
deps_to_binary/1,
to_binary/1,
to_list/1,
to_atom/1,
tup_dedup/1,
tup_umerge/2,
tup_sort/1,
Expand Down Expand Up @@ -270,6 +271,10 @@ to_list(B) when is_binary(B) -> unicode:characters_to_list(B);
to_list(I) when is_integer(I) -> integer_to_list(I);
to_list(Str) -> unicode:characters_to_list(Str).

to_atom(B) when is_binary(B) -> binary_to_atom(B, utf8);
to_atom(Str) when is_list(Str) -> list_to_atom(Str);
to_atom(A) when is_atom(A) -> A.

tup_dedup(List) ->
tup_dedup_(tup_sort(List)).

Expand Down

0 comments on commit c102379

Please sign in to comment.