From 09df513a7f23b93abbb4c976146673b87e053ff0 Mon Sep 17 00:00:00 2001 From: Fred Hebert Date: Wed, 7 Oct 2020 21:22:15 +0000 Subject: [PATCH] Run pkg updates on plugin upgrades Upgrading dependencies already checked for package updates before running the upgrade task. Plugins didn't have that mechanism in place and that made upgrading plugins over hex trickier since users could be stuck a long time without running an update manually. This fix mostly readapts existing dep code to the context of plugins to look them up one at a time and run the update as part of the upgrade. --- src/rebar_pkg_resource.erl | 7 +------ src/rebar_prv_plugins_upgrade.erl | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/rebar_pkg_resource.erl b/src/rebar_pkg_resource.erl index f50ac622..b5eecbb4 100644 --- a/src/rebar_pkg_resource.erl +++ b/src/rebar_pkg_resource.erl @@ -60,12 +60,7 @@ lock(AppInfo, _) -> Res :: boolean(). needs_update(AppInfo, _) -> {pkg, _Name, Vsn, _OldHash, _Hash, _} = rebar_app_info:source(AppInfo), - case rebar_utils:to_binary(rebar_app_info:original_vsn(AppInfo)) =:= rebar_utils:to_binary(Vsn) of - true -> - false; - false -> - true - end. + rebar_utils:to_binary(rebar_app_info:original_vsn(AppInfo)) =/= rebar_utils:to_binary(Vsn). %%------------------------------------------------------------------------------ %% @doc diff --git a/src/rebar_prv_plugins_upgrade.erl b/src/rebar_prv_plugins_upgrade.erl index bf51f903..ce87ebd3 100644 --- a/src/rebar_prv_plugins_upgrade.erl +++ b/src/rebar_prv_plugins_upgrade.erl @@ -62,6 +62,7 @@ upgrade(Plugin, State) -> ?PRV_ERROR({not_found, Plugin}); {ok, P, Profile} -> State1 = rebar_state:set(State, deps_dir, ?DEFAULT_PLUGINS_DIR), + maybe_update_pkg(P, State1), {Apps, State2} = rebar_prv_install_deps:handle_deps_as_profile(Profile, State1, [P], true), {no_cycle, Sorted} = rebar_prv_install_deps:find_cycles(Apps), @@ -92,3 +93,29 @@ find_plugin(Plugin, Profiles, State) -> build_plugin(ToBuild, State) -> Providers = rebar_state:providers(State), rebar_prv_compile:compile(State, Providers, ToBuild, plugins). + +maybe_update_pkg(Name, State) -> + try rebar_app_utils:parse_dep(root, ?DEFAULT_PLUGINS_DIR, Name, State, [], 0) of + AppInfo -> + Source = rebar_app_info:source(AppInfo), + case element(1, Source) of + pkg -> + Resources = rebar_state:resources(State), + #{repos := RepoConfigs} = rebar_resource_v2:find_resource_state(pkg, Resources), + PkgName = element(2, Source), + [update_package(PkgName, RepoConfig, State) || RepoConfig <- RepoConfigs]; + _ -> + skip + end + catch + throw:?PRV_ERROR({parse_dep, _}) -> + skip + end. + +update_package(Name, RepoConfig, State) -> + case rebar_packages:update_package(Name, RepoConfig, State) of + fail -> + ?WARN("Failed to fetch updates for package ~ts from repo ~ts", [Name, maps:get(name, RepoConfig)]); + _ -> + ok + end.