您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

107 行
3.5 KiB

  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 find(list_to_atom(Plugin), Plugins) of
  70. false ->
  71. not_found;
  72. P ->
  73. {ok, P}
  74. end
  75. end, Profiles).
  76. find(_Plugin, []) ->
  77. false;
  78. find(Plugin, [Plugin | _Plugins]) ->
  79. Plugin;
  80. find(Plugin, [Plugin1 | Plugins]) when is_tuple(Plugin1) ->
  81. case element(1, Plugin1) =:= Plugin of
  82. true ->
  83. Plugin1;
  84. false ->
  85. find(Plugin, Plugins)
  86. end.
  87. build_plugin(AppInfo, Apps, State) ->
  88. Providers = rebar_state:providers(State),
  89. AppDir = rebar_app_info:dir(AppInfo),
  90. C = rebar_config:consult(AppDir),
  91. S = rebar_state:new(rebar_state:all_deps(rebar_state:new(), Apps), C, AppDir),
  92. rebar_prv_compile:compile(S, Providers, AppInfo).