Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

271 rader
12 KiB

support for hex v2, multiple repository fetching, private organizations (#1884) * update to hex_core for hex-v2 repo support (#1865) * update to hex_core for hex-v2 repo support This patch adds only single repo hex-v2 support through hex_core. Packages no longer filtered out by buildtool metadata and the package index is updated per-package instead of fetched as one large ets dump. * tell travis to also build hex_core branch * support list of repos for hex packages (#1866) * support list of repos for hex packages repos are defined under the hex key in rebar configs. They can be defined at the top level of a project or globally, but not in profiles and the repos configured in dependencies are also ignored. Searching for packages involves first checking for a match in the local repo index cache, in the order repos are defined. If not found each repo is checked through the hex api for any known versions of the package and the first repo with a version that fits the constraint is used. * add {repos, replace, []} for overriding the global & default repos * add hex auth handling for repos (#1874) auth token are kept in a hex.config file that is modified by the rebar3 hex plugin. Repo names that have a : separating a parent and child are considered organizations. The parent repo's auth will be included with the child. So an organization named hexpm:rebar3_test will include any hexpm auth tokens found in the rebar3_test organization's configuration. * move packages to top level of of hexpm cache dir (#1876) * move packages to top level of of hexpm cache dir * append organization name to parent's repo_url when parsing repos * only eval config scripts and apply overrides once per app (#1879) * only eval config scripts and apply overrides once per app * move new resource behaviour to rebar_resource_v2 and keep v1 * cleanup use of rebar_resource module and unused functions * cleanup error messages and unused code * when discovering apps support mix packages as unbuilt apps (#1882) * use hex_core tarball unpacking support in pkg resource (#1883) * use hex_core tarball unpacking support in pkg resource * ignore etag if package doesn't exist and delete if checksum fails * add back tests for bad package checksums * improve bad registry checksum error message
6 år sedan
support for hex v2, multiple repository fetching, private organizations (#1884) * update to hex_core for hex-v2 repo support (#1865) * update to hex_core for hex-v2 repo support This patch adds only single repo hex-v2 support through hex_core. Packages no longer filtered out by buildtool metadata and the package index is updated per-package instead of fetched as one large ets dump. * tell travis to also build hex_core branch * support list of repos for hex packages (#1866) * support list of repos for hex packages repos are defined under the hex key in rebar configs. They can be defined at the top level of a project or globally, but not in profiles and the repos configured in dependencies are also ignored. Searching for packages involves first checking for a match in the local repo index cache, in the order repos are defined. If not found each repo is checked through the hex api for any known versions of the package and the first repo with a version that fits the constraint is used. * add {repos, replace, []} for overriding the global & default repos * add hex auth handling for repos (#1874) auth token are kept in a hex.config file that is modified by the rebar3 hex plugin. Repo names that have a : separating a parent and child are considered organizations. The parent repo's auth will be included with the child. So an organization named hexpm:rebar3_test will include any hexpm auth tokens found in the rebar3_test organization's configuration. * move packages to top level of of hexpm cache dir (#1876) * move packages to top level of of hexpm cache dir * append organization name to parent's repo_url when parsing repos * only eval config scripts and apply overrides once per app (#1879) * only eval config scripts and apply overrides once per app * move new resource behaviour to rebar_resource_v2 and keep v1 * cleanup use of rebar_resource module and unused functions * cleanup error messages and unused code * when discovering apps support mix packages as unbuilt apps (#1882) * use hex_core tarball unpacking support in pkg resource (#1883) * use hex_core tarball unpacking support in pkg resource * ignore etag if package doesn't exist and delete if checksum fails * add back tests for bad package checksums * improve bad registry checksum error message
6 år sedan
support for hex v2, multiple repository fetching, private organizations (#1884) * update to hex_core for hex-v2 repo support (#1865) * update to hex_core for hex-v2 repo support This patch adds only single repo hex-v2 support through hex_core. Packages no longer filtered out by buildtool metadata and the package index is updated per-package instead of fetched as one large ets dump. * tell travis to also build hex_core branch * support list of repos for hex packages (#1866) * support list of repos for hex packages repos are defined under the hex key in rebar configs. They can be defined at the top level of a project or globally, but not in profiles and the repos configured in dependencies are also ignored. Searching for packages involves first checking for a match in the local repo index cache, in the order repos are defined. If not found each repo is checked through the hex api for any known versions of the package and the first repo with a version that fits the constraint is used. * add {repos, replace, []} for overriding the global & default repos * add hex auth handling for repos (#1874) auth token are kept in a hex.config file that is modified by the rebar3 hex plugin. Repo names that have a : separating a parent and child are considered organizations. The parent repo's auth will be included with the child. So an organization named hexpm:rebar3_test will include any hexpm auth tokens found in the rebar3_test organization's configuration. * move packages to top level of of hexpm cache dir (#1876) * move packages to top level of of hexpm cache dir * append organization name to parent's repo_url when parsing repos * only eval config scripts and apply overrides once per app (#1879) * only eval config scripts and apply overrides once per app * move new resource behaviour to rebar_resource_v2 and keep v1 * cleanup use of rebar_resource module and unused functions * cleanup error messages and unused code * when discovering apps support mix packages as unbuilt apps (#1882) * use hex_core tarball unpacking support in pkg resource (#1883) * use hex_core tarball unpacking support in pkg resource * ignore etag if package doesn't exist and delete if checksum fails * add back tests for bad package checksums * improve bad registry checksum error message
6 år sedan
support for hex v2, multiple repository fetching, private organizations (#1884) * update to hex_core for hex-v2 repo support (#1865) * update to hex_core for hex-v2 repo support This patch adds only single repo hex-v2 support through hex_core. Packages no longer filtered out by buildtool metadata and the package index is updated per-package instead of fetched as one large ets dump. * tell travis to also build hex_core branch * support list of repos for hex packages (#1866) * support list of repos for hex packages repos are defined under the hex key in rebar configs. They can be defined at the top level of a project or globally, but not in profiles and the repos configured in dependencies are also ignored. Searching for packages involves first checking for a match in the local repo index cache, in the order repos are defined. If not found each repo is checked through the hex api for any known versions of the package and the first repo with a version that fits the constraint is used. * add {repos, replace, []} for overriding the global & default repos * add hex auth handling for repos (#1874) auth token are kept in a hex.config file that is modified by the rebar3 hex plugin. Repo names that have a : separating a parent and child are considered organizations. The parent repo's auth will be included with the child. So an organization named hexpm:rebar3_test will include any hexpm auth tokens found in the rebar3_test organization's configuration. * move packages to top level of of hexpm cache dir (#1876) * move packages to top level of of hexpm cache dir * append organization name to parent's repo_url when parsing repos * only eval config scripts and apply overrides once per app (#1879) * only eval config scripts and apply overrides once per app * move new resource behaviour to rebar_resource_v2 and keep v1 * cleanup use of rebar_resource module and unused functions * cleanup error messages and unused code * when discovering apps support mix packages as unbuilt apps (#1882) * use hex_core tarball unpacking support in pkg resource (#1883) * use hex_core tarball unpacking support in pkg resource * ignore etag if package doesn't exist and delete if checksum fails * add back tests for bad package checksums * improve bad registry checksum error message
6 år sedan
support for hex v2, multiple repository fetching, private organizations (#1884) * update to hex_core for hex-v2 repo support (#1865) * update to hex_core for hex-v2 repo support This patch adds only single repo hex-v2 support through hex_core. Packages no longer filtered out by buildtool metadata and the package index is updated per-package instead of fetched as one large ets dump. * tell travis to also build hex_core branch * support list of repos for hex packages (#1866) * support list of repos for hex packages repos are defined under the hex key in rebar configs. They can be defined at the top level of a project or globally, but not in profiles and the repos configured in dependencies are also ignored. Searching for packages involves first checking for a match in the local repo index cache, in the order repos are defined. If not found each repo is checked through the hex api for any known versions of the package and the first repo with a version that fits the constraint is used. * add {repos, replace, []} for overriding the global & default repos * add hex auth handling for repos (#1874) auth token are kept in a hex.config file that is modified by the rebar3 hex plugin. Repo names that have a : separating a parent and child are considered organizations. The parent repo's auth will be included with the child. So an organization named hexpm:rebar3_test will include any hexpm auth tokens found in the rebar3_test organization's configuration. * move packages to top level of of hexpm cache dir (#1876) * move packages to top level of of hexpm cache dir * append organization name to parent's repo_url when parsing repos * only eval config scripts and apply overrides once per app (#1879) * only eval config scripts and apply overrides once per app * move new resource behaviour to rebar_resource_v2 and keep v1 * cleanup use of rebar_resource module and unused functions * cleanup error messages and unused code * when discovering apps support mix packages as unbuilt apps (#1882) * use hex_core tarball unpacking support in pkg resource (#1883) * use hex_core tarball unpacking support in pkg resource * ignore etag if package doesn't exist and delete if checksum fails * add back tests for bad package checksums * improve bad registry checksum error message
6 år sedan
support for hex v2, multiple repository fetching, private organizations (#1884) * update to hex_core for hex-v2 repo support (#1865) * update to hex_core for hex-v2 repo support This patch adds only single repo hex-v2 support through hex_core. Packages no longer filtered out by buildtool metadata and the package index is updated per-package instead of fetched as one large ets dump. * tell travis to also build hex_core branch * support list of repos for hex packages (#1866) * support list of repos for hex packages repos are defined under the hex key in rebar configs. They can be defined at the top level of a project or globally, but not in profiles and the repos configured in dependencies are also ignored. Searching for packages involves first checking for a match in the local repo index cache, in the order repos are defined. If not found each repo is checked through the hex api for any known versions of the package and the first repo with a version that fits the constraint is used. * add {repos, replace, []} for overriding the global & default repos * add hex auth handling for repos (#1874) auth token are kept in a hex.config file that is modified by the rebar3 hex plugin. Repo names that have a : separating a parent and child are considered organizations. The parent repo's auth will be included with the child. So an organization named hexpm:rebar3_test will include any hexpm auth tokens found in the rebar3_test organization's configuration. * move packages to top level of of hexpm cache dir (#1876) * move packages to top level of of hexpm cache dir * append organization name to parent's repo_url when parsing repos * only eval config scripts and apply overrides once per app (#1879) * only eval config scripts and apply overrides once per app * move new resource behaviour to rebar_resource_v2 and keep v1 * cleanup use of rebar_resource module and unused functions * cleanup error messages and unused code * when discovering apps support mix packages as unbuilt apps (#1882) * use hex_core tarball unpacking support in pkg resource (#1883) * use hex_core tarball unpacking support in pkg resource * ignore etag if package doesn't exist and delete if checksum fails * add back tests for bad package checksums * improve bad registry checksum error message
6 år sedan
  1. %% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
  2. %% ex: ts=4 sw=4 et
  3. -module(rebar_prv_upgrade).
  4. -behaviour(provider).
  5. -export([init/1,
  6. do/1,
  7. format_error/1]).
  8. -include("rebar.hrl").
  9. -include_lib("providers/include/providers.hrl").
  10. -define(PROVIDER, upgrade).
  11. -define(DEPS, [lock]).
  12. %% Also only upgrade top-level (0) deps. Transitive deps shouldn't be
  13. %% upgradable -- if the user wants this, they should declare it at the
  14. %% top level and then upgrade.
  15. %% ===================================================================
  16. %% Public API
  17. %% ===================================================================
  18. -spec init(rebar_state:t()) -> {ok, rebar_state:t()}.
  19. init(State) ->
  20. State1 =
  21. rebar_state:add_provider(State,
  22. providers:create([{name, ?PROVIDER},
  23. {module, ?MODULE},
  24. {bare, true},
  25. {deps, ?DEPS},
  26. {example, "rebar3 upgrade [cowboy[,ranch]]"},
  27. {short_desc, "Upgrade dependencies."},
  28. {desc, "Upgrade project dependencies. Mentioning no application "
  29. "will upgrade all dependencies. To upgrade specific dependencies, "
  30. "their names can be listed in the command."},
  31. {opts, [
  32. {package, undefined, undefined, string,
  33. "List of packages to upgrade. If not specified, all dependencies are upgraded."}
  34. ]}])),
  35. {ok, State1}.
  36. -spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}.
  37. do(State) ->
  38. Cwd = rebar_state:dir(State),
  39. Providers = rebar_state:providers(State),
  40. rebar_hooks:run_project_and_app_hooks(Cwd, pre, ?PROVIDER, Providers, State),
  41. case do_(State) of
  42. {ok, NewState} ->
  43. rebar_hooks:run_project_and_app_hooks(Cwd, post, ?PROVIDER, Providers, NewState),
  44. {ok, NewState};
  45. Other ->
  46. rebar_hooks:run_project_and_app_hooks(Cwd, post, ?PROVIDER, Providers, State),
  47. Other
  48. end.
  49. do_(State) ->
  50. {Args, _} = rebar_state:command_parsed_args(State),
  51. Locks = rebar_state:get(State, {locks, default}, []),
  52. %% We have 3 sources of dependencies to upgrade from:
  53. %% 1. the top-level rebar.config (in `deps', dep name is an atom)
  54. %% 2. the app-level rebar.config in umbrella apps (in `{deps, default}',
  55. %% where the dep name is an atom)
  56. %% 3. the formatted sources for all after app-parsing (in `{deps, default}',
  57. %% where the reprocessed app name is a binary)
  58. %%
  59. %% The gotcha with these is that the selection of dependencies with a
  60. %% binary name (those that are stable and usable internally) is done with
  61. %% in the profile deps only, but while accounting for locks.
  62. %% Because our job here is to unlock those that have changed, we must
  63. %% instead use the atom-based names, both in `deps' and `{deps, default}',
  64. %% as those are the dependencies that may have changed but have been
  65. %% disregarded by locks.
  66. %%
  67. %% As such, the working set of dependencies is the addition of
  68. %% `deps' and `{deps, default}' entries with an atom name, as those
  69. %% disregard locks and parsed values post-selection altogether.
  70. %% Packages without versions can of course be a single atom.
  71. TopDeps = rebar_state:get(State, deps, []),
  72. ProfileDeps = rebar_state:get(State, {deps, default}, []),
  73. Deps = [Dep || Dep <- TopDeps ++ ProfileDeps, % TopDeps > ProfileDeps
  74. is_atom(Dep) orelse is_atom(element(1, Dep))],
  75. Names = parse_names(rebar_utils:to_binary(proplists:get_value(package, Args, <<"">>)), Locks),
  76. DepsDict = deps_dict(rebar_state:all_deps(State)),
  77. AltDeps = find_non_default_deps(Deps, State),
  78. FilteredNames = cull_default_names_if_profiles(Names, Deps, State),
  79. case prepare_locks(FilteredNames, Deps, Locks, [], DepsDict, AltDeps) of
  80. {error, Reason} ->
  81. {error, Reason};
  82. {Locks0, Unlocks0} ->
  83. Deps0 = top_level_deps(Deps, Locks),
  84. State1 = rebar_state:set(State, {deps, default}, Deps0),
  85. DepsDir = rebar_prv_install_deps:profile_dep_dir(State, default),
  86. D = rebar_app_utils:parse_deps(root, DepsDir, Deps0, State1, Locks0, 0),
  87. %% first update the package index for the packages to be upgraded
  88. update_pkg_deps(Unlocks0, D, State1),
  89. State2 = rebar_state:set(State1, {parsed_deps, default}, D),
  90. State3 = rebar_state:set(State2, {locks, default}, Locks0),
  91. State4 = rebar_state:set(State3, upgrade, true),
  92. UpdatedLocks = [L || L <- rebar_state:lock(State4),
  93. lists:keymember(rebar_app_info:name(L), 1, Locks0)],
  94. Res = rebar_prv_install_deps:do_(rebar_state:lock(State4, UpdatedLocks)),
  95. case Res of
  96. {ok, State5} ->
  97. rebar_utils:info_useless(
  98. [element(1,Lock) || Lock <- Locks],
  99. [rebar_app_info:name(App) || App <- rebar_state:lock(State5)]
  100. ),
  101. rebar_prv_lock:do(State5);
  102. _ ->
  103. Res
  104. end
  105. end.
  106. -spec format_error(any()) -> iolist().
  107. format_error({unknown_dependency, Name}) ->
  108. io_lib:format("Dependency ~ts not found", [Name]);
  109. format_error({transitive_dependency, Name}) ->
  110. io_lib:format("Dependency ~ts is transitive and cannot be safely upgraded. "
  111. "Promote it to your top-level rebar.config file to upgrade it.",
  112. [Name]);
  113. format_error(Reason) ->
  114. io_lib:format("~p", [Reason]).
  115. %% fetch updates for package deps that have been unlocked for upgrade
  116. update_pkg_deps([], _, _) ->
  117. ok;
  118. update_pkg_deps([{Name, _, _} | Rest], AppInfos, State) ->
  119. case rebar_app_utils:find(Name, AppInfos) of
  120. {ok, AppInfo} ->
  121. Source = rebar_app_info:source(AppInfo),
  122. case element(1, Source) of
  123. pkg ->
  124. Resources = rebar_state:resources(State),
  125. #{repos := RepoConfigs} = rebar_resource_v2:find_resource_state(pkg, Resources),
  126. PkgName = element(2, Source),
  127. [update_package(PkgName, RepoConfig, State) || RepoConfig <- RepoConfigs];
  128. _ ->
  129. skip
  130. end;
  131. _ ->
  132. %% this should be impossible...
  133. skip
  134. end,
  135. update_pkg_deps(Rest, AppInfos, State).
  136. update_package(Name, RepoConfig, State) ->
  137. case rebar_packages:update_package(Name, RepoConfig, State) of
  138. fail ->
  139. ?WARN("Failed to fetch updates for package ~ts from repo ~ts", [Name, maps:get(name, RepoConfig)]);
  140. _ ->
  141. ok
  142. end.
  143. parse_names(Bin, Locks) ->
  144. case lists:usort(re:split(Bin, <<" *, *">>, [trim, unicode])) of
  145. %% Nothing submitted, use *all* apps
  146. [<<"">>] -> [Name || {Name, _, 0} <- Locks];
  147. [] -> [Name || {Name, _, 0} <- Locks];
  148. %% Regular options
  149. Other -> Other
  150. end.
  151. %% Find alternative deps in non-default profiles since they may
  152. %% need to be passed through (they are never locked)
  153. find_non_default_deps(Deps, State) ->
  154. AltProfiles = rebar_state:current_profiles(State) -- [default],
  155. AltProfileDeps = lists:append([
  156. rebar_state:get(State, {deps, Profile}, []) || Profile <- AltProfiles]
  157. ),
  158. [Dep || Dep <- AltProfileDeps,
  159. is_atom(Dep) orelse is_atom(element(1, Dep))
  160. andalso not lists:member(Dep, Deps)].
  161. %% If any alt profiles are used, remove the default profiles from
  162. %% the upgrade list and warn about it.
  163. cull_default_names_if_profiles(Names, Deps, State) ->
  164. case rebar_state:current_profiles(State) of
  165. [default] ->
  166. Names;
  167. _ ->
  168. ?INFO("Dependencies in the default profile will not be upgraded", []),
  169. lists:filter(fun(Name) ->
  170. AtomName = binary_to_atom(Name, utf8),
  171. rebar_utils:tup_find(AtomName, Deps) == false
  172. end, Names)
  173. end.
  174. prepare_locks([], _, Locks, Unlocks, _Dict, _AltDeps) ->
  175. {Locks, Unlocks};
  176. prepare_locks([Name|Names], Deps, Locks, Unlocks, Dict, AltDeps) ->
  177. AtomName = binary_to_atom(Name, utf8),
  178. case lists:keyfind(Name, 1, Locks) of
  179. {_, _, 0} = Lock ->
  180. case rebar_utils:tup_find(AtomName, Deps) of
  181. false ->
  182. ?WARN("Dependency ~ts has been removed and will not be upgraded", [Name]),
  183. prepare_locks(Names, Deps, Locks, Unlocks, Dict, AltDeps);
  184. Dep ->
  185. {Source, NewLocks, NewUnlocks} = prepare_lock(Dep, Lock, Locks, Dict),
  186. prepare_locks(Names, Deps, NewLocks,
  187. [{Name, Source, 0} | NewUnlocks ++ Unlocks], Dict, AltDeps)
  188. end;
  189. {_, _, Level} = Lock when Level > 0 ->
  190. case rebar_utils:tup_find(AtomName, Deps) of
  191. false ->
  192. ?PRV_ERROR({transitive_dependency, Name});
  193. Dep -> % Dep has been promoted
  194. {Source, NewLocks, NewUnlocks} = prepare_lock(Dep, Lock, Locks, Dict),
  195. prepare_locks(Names, Deps, NewLocks,
  196. [{Name, Source, 0} | NewUnlocks ++ Unlocks], Dict, AltDeps)
  197. end;
  198. false ->
  199. case rebar_utils:tup_find(AtomName, AltDeps) of
  200. false ->
  201. ?PRV_ERROR({unknown_dependency, Name});
  202. _ -> % non-default profile dependency found, pass through
  203. prepare_locks(Names, Deps, Locks, Unlocks, Dict, AltDeps)
  204. end
  205. end.
  206. prepare_lock(Dep, Lock, Locks, Dict) ->
  207. {Name1, Source} = case Dep of
  208. {Name, SrcOrVsn} -> {Name, SrcOrVsn};
  209. {Name, _, Src} -> {Name, Src};
  210. _ when is_atom(Dep) ->
  211. %% version-free package. Must unlock whatever matches in locks
  212. {_, Vsn, _} = lists:keyfind(rebar_utils:to_binary(Dep), 1, Locks),
  213. {Dep, Vsn}
  214. end,
  215. Children = all_children(Name1, Dict),
  216. {NewLocks, NewUnlocks} = unlock_children(Children, Locks -- [Lock]),
  217. {Source, NewLocks, NewUnlocks}.
  218. top_level_deps(Deps, Locks) ->
  219. [Dep || Dep <- Deps, lists:keymember(0, 3, Locks)].
  220. unlock_children(Children, Locks) ->
  221. unlock_children(Children, Locks, [], []).
  222. unlock_children(_, [], Locks, Unlocks) ->
  223. {Locks, Unlocks};
  224. unlock_children(Children, [App = {Name,_,_} | Apps], Locks, Unlocks) ->
  225. case lists:member(rebar_utils:to_binary(Name), Children) of
  226. true ->
  227. unlock_children(Children, Apps, Locks, [App | Unlocks]);
  228. false ->
  229. unlock_children(Children, Apps, [App | Locks], Unlocks)
  230. end.
  231. deps_dict(Deps) ->
  232. lists:foldl(fun(App, Dict) ->
  233. Name = rebar_app_info:name(App),
  234. Parent = rebar_app_info:parent(App),
  235. dict:append_list(Parent, [Name], Dict)
  236. end, dict:new(), Deps).
  237. all_children(Name, Dict) ->
  238. lists:flatten(all_children_(Name, Dict)).
  239. all_children_(Name, Dict) ->
  240. case dict:find(rebar_utils:to_binary(Name), Dict) of
  241. {ok, Children} ->
  242. [Children | [all_children_(Child, Dict) || Child <- Children]];
  243. error ->
  244. []
  245. end.