Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

95 рядки
3.3 KiB

10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
  1. -module(rebar_prv_plugins_upgrade).
  2. -behaviour(provider).
  3. -export([init/1,
  4. do/1,
  5. format_error/1]).
  6. -include("rebar.hrl").
  7. -include_lib("providers/include/providers.hrl").
  8. -define(PROVIDER, upgrade).
  9. -define(NAMESPACE, plugins).
  10. -define(DEPS, []).
  11. -spec init(rebar_state:t()) -> {ok, rebar_state:t()}.
  12. init(State) ->
  13. State1 = rebar_state:add_provider(
  14. State,
  15. providers:create([
  16. {name, ?PROVIDER},
  17. {module, ?MODULE},
  18. {namespace, ?NAMESPACE},
  19. {bare, true},
  20. {deps, ?DEPS},
  21. {example, "rebar3 plugins upgrade <plugin>"},
  22. {short_desc, "Uprade plugins"},
  23. {desc, "List or upgrade plugins"},
  24. {opts, [{plugin, undefined, undefined, string,
  25. "Plugin to upgrade"}]}])),
  26. {ok, State1}.
  27. -spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}.
  28. do(State) ->
  29. {Args, _} = rebar_state:command_parsed_args(State),
  30. case proplists:get_value(plugin, Args, none) of
  31. none ->
  32. ?PRV_ERROR(no_plugin_arg);
  33. Plugin ->
  34. upgrade(Plugin, State)
  35. end.
  36. -spec format_error(any()) -> iolist().
  37. format_error(no_plugin_arg) ->
  38. io_lib:format("Must give an installed plugin to upgrade as an argument", []);
  39. format_error({not_found, Plugin}) ->
  40. io_lib:format("Plugin to upgrade not found: ~s", [Plugin]);
  41. format_error(Reason) ->
  42. io_lib:format("~p", [Reason]).
  43. upgrade(Plugin, State) ->
  44. Profiles = rebar_state:current_profiles(State),
  45. case find_plugin(Plugin, Profiles, State) of
  46. not_found ->
  47. Dep = find_plugin(Plugin, [global], State);
  48. Dep ->
  49. Dep
  50. end,
  51. case Dep of
  52. not_found ->
  53. ?PRV_ERROR({not_found, Plugin});
  54. {ok, P, Profile} ->
  55. State1 = rebar_state:set(State, deps_dir, ?DEFAULT_PLUGINS_DIR),
  56. {Apps, _State2} = rebar_prv_install_deps:handle_deps_as_profile(Profile, State1, [P], true),
  57. {no_cycle, Sorted} = rebar_prv_install_deps:find_cycles(Apps),
  58. ToBuild = rebar_prv_install_deps:cull_compile(Sorted, []),
  59. %% Add already built plugin deps to the code path
  60. CodePaths = [rebar_app_info:ebin_dir(A) || A <- Apps -- ToBuild],
  61. code:add_pathsa(CodePaths),
  62. %% Build plugin and its deps
  63. [build_plugin(AppInfo, Apps, State) || AppInfo <- ToBuild],
  64. {ok, State}
  65. end.
  66. find_plugin(Plugin, Profiles, State) ->
  67. ec_lists:search(fun(Profile) ->
  68. Plugins = rebar_state:get(State, {plugins, Profile}, []),
  69. case rebar_utils:tup_find(list_to_atom(Plugin), Plugins) of
  70. false ->
  71. not_found;
  72. P ->
  73. {ok, P}
  74. end
  75. end, Profiles).
  76. build_plugin(AppInfo, Apps, State) ->
  77. Providers = rebar_state:providers(State),
  78. AppDir = rebar_app_info:dir(AppInfo),
  79. C = rebar_config:consult(AppDir),
  80. S = rebar_state:new(rebar_state:all_deps(rebar_state:new(), Apps), C, AppInfo),
  81. rebar_prv_compile:compile(S, Providers, AppInfo).