Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

117 строки
4.1 KiB

10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
  1. -module(rebar_prv_deps).
  2. -behaviour(provider).
  3. -export([init/1,
  4. do/1,
  5. format_error/1]).
  6. -include("rebar.hrl").
  7. -define(PROVIDER, deps).
  8. -define(DEPS, [app_discovery]).
  9. -spec init(rebar_state:t()) -> {ok, rebar_state:t()}.
  10. init(State) ->
  11. State1 = rebar_state:add_provider(
  12. State,
  13. providers:create([
  14. {name, ?PROVIDER},
  15. {module, ?MODULE},
  16. {bare, true},
  17. {deps, ?DEPS},
  18. {example, "rebar3 deps"},
  19. {short_desc, "List dependencies"},
  20. {desc, "List dependencies. Those not matching lock files "
  21. "are followed by an asterisk (*)."},
  22. {opts, []}])),
  23. {ok, State1}.
  24. -spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}.
  25. do(State) ->
  26. Profiles = rebar_state:current_profiles(State),
  27. List = [{Profile, rebar_state:get(State, {deps, Profile}, [])}
  28. || Profile <- Profiles],
  29. [display(State, Profile, Deps) || {Profile, Deps} <- List],
  30. {ok, State}.
  31. -spec format_error(any()) -> iolist().
  32. format_error(Reason) ->
  33. io_lib:format("~p", [Reason]).
  34. display(State, default, Deps) ->
  35. NewDeps = merge(Deps, rebar_state:get(State, deps, [])),
  36. display_deps(State, NewDeps),
  37. ?CONSOLE("", []);
  38. display(State, Profile, Deps) ->
  39. ?CONSOLE("-- ~p --", [Profile]),
  40. display_deps(State, Deps),
  41. ?CONSOLE("", []).
  42. merge(Deps, SourceDeps) ->
  43. merge1(dedup([normalize(Dep) || Dep <- Deps]),
  44. [normalize(Dep) || Dep <- SourceDeps]).
  45. normalize(Name) when is_binary(Name) ->
  46. Name;
  47. normalize(Name) when is_atom(Name) ->
  48. ec_cnv:to_binary(Name);
  49. normalize(Dep) when is_tuple(Dep) ->
  50. Name = element(1, Dep),
  51. setelement(1, Dep, normalize(Name)).
  52. merge1(Deps, SourceDeps) ->
  53. Names = [name(Dep) || Dep <- Deps],
  54. ToAdd = [Dep || Dep <- SourceDeps,
  55. not lists:member(name(Dep), Names)],
  56. Deps ++ ToAdd.
  57. %% Keep the latter one as locks come after regular deps in the list.
  58. %% This is totally not safe as an assumption, but it's what we got.
  59. %% We do this by comparing the current element and looking if a
  60. %% similar named one happens later. If so, drop the current one.
  61. dedup(Deps) -> dedup(Deps, [name(Dep) || Dep <- Deps]).
  62. dedup([], []) -> [];
  63. dedup([Dep|Deps], [Name|DepNames]) ->
  64. case lists:member(Name, DepNames) of
  65. true -> dedup(Deps, DepNames);
  66. false -> [Dep | dedup(Deps, DepNames)]
  67. end.
  68. name(T) when is_tuple(T) -> element(1, T);
  69. name(B) when is_binary(B) -> B.
  70. display_deps(State, Deps) ->
  71. lists:foreach(fun(Dep) -> display_dep(State, Dep) end, Deps).
  72. %% packages
  73. display_dep(_State, {Name, Vsn}) when is_list(Vsn) ->
  74. ?CONSOLE("~s* (package ~s)", [ec_cnv:to_binary(Name), ec_cnv:to_binary(Vsn)]);
  75. display_dep(_State, Name) when is_binary(Name) ->
  76. ?CONSOLE("~s* (package)", [Name]);
  77. display_dep(_State, {Name, Source}) when is_tuple(Source) ->
  78. ?CONSOLE("~s* (~s source)", [ec_cnv:to_binary(Name), type(Source)]);
  79. display_dep(_State, {Name, _Vsn, Source}) when is_tuple(Source) ->
  80. ?CONSOLE("~s* (~s source)", [ec_cnv:to_binary(Name), type(Source)]);
  81. display_dep(_State, {Name, _Vsn, Source, _Opts}) when is_tuple(Source) ->
  82. ?CONSOLE("~s* (~s source)", [ec_cnv:to_binary(Name), type(Source)]);
  83. %% Locked
  84. display_dep(State, {Name, Source={pkg, _, Vsn}, Level}) when is_integer(Level) ->
  85. DepsDir = rebar_dir:deps_dir(State),
  86. AppDir = filename:join([DepsDir, ec_cnv:to_binary(Name)]),
  87. NeedsUpdate = case rebar_fetch:needs_update(AppDir, Source, State) of
  88. true -> "*";
  89. false -> ""
  90. end,
  91. ?CONSOLE("~s~s (locked package ~s)", [Name, NeedsUpdate, Vsn]);
  92. display_dep(State, {Name, Source, Level}) when is_tuple(Source), is_integer(Level) ->
  93. DepsDir = rebar_dir:deps_dir(State),
  94. AppDir = filename:join([DepsDir, ec_cnv:to_binary(Name)]),
  95. NeedsUpdate = case rebar_fetch:needs_update(AppDir, Source, State) of
  96. true -> "*";
  97. false -> ""
  98. end,
  99. ?CONSOLE("~s~s (locked ~s source)", [Name, NeedsUpdate, type(Source)]).
  100. type(Source) when is_tuple(Source) -> element(1, Source).