Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

421 righe
19 KiB

10 anni fa
10 anni fa
10 anni fa
10 anni fa
10 anni fa
10 anni fa
10 anni fa
10 anni fa
10 anni fa
10 anni fa
10 anni fa
10 anni fa
10 anni fa
10 anni fa
10 anni fa
10 anni fa
10 anni fa
10 anni fa
10 anni fa
10 anni fa
10 anni fa
10 anni fa
10 anni fa
10 anni fa
10 anni fa
10 anni fa
10 anni fa
10 anni fa
10 anni fa
10 anni fa
10 anni fa
  1. %% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
  2. %% ex: ts=4 sw=4 et
  3. %% -------------------------------------------------------------------
  4. %%
  5. %% rebar: Erlang Build Tools
  6. %%
  7. %% Copyright (c) 2009 Dave Smith (dizzyd@dizzyd.com)
  8. %%
  9. %% Permission is hereby granted, free of charge, to any person obtaining a copy
  10. %% of this software and associated documentation files (the "Software"), to deal
  11. %% in the Software without restriction, including without limitation the rights
  12. %% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. %% copies of the Software, and to permit persons to whom the Software is
  14. %% furnished to do so, subject to the following conditions:
  15. %%
  16. %% The above copyright notice and this permission notice shall be included in
  17. %% all copies or substantial portions of the Software.
  18. %%
  19. %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. %% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. %% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. %% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. %% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. %% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. %% THE SOFTWARE.
  26. %% -------------------------------------------------------------------
  27. -module(rebar_prv_install_deps).
  28. -behaviour(provider).
  29. -export([init/1,
  30. do/1,
  31. format_error/1]).
  32. -include("rebar.hrl").
  33. -export([handle_deps/2,
  34. handle_deps/3]).
  35. -export_type([dep/0]).
  36. -define(PROVIDER, install_deps).
  37. -define(DEPS, [app_discovery]).
  38. -type src_dep() :: {atom(), string(), {atom(), string(), string()}}.
  39. -type pkg_dep() :: {atom(), binary()} | atom().
  40. -type dep() :: src_dep() | pkg_dep().
  41. %% ===================================================================
  42. %% Public API
  43. %% ===================================================================
  44. -spec init(rebar_state:t()) -> {ok, rebar_state:t()}.
  45. init(State) ->
  46. State1 = rebar_state:add_provider(State, providers:create([{name, ?PROVIDER},
  47. {module, ?MODULE},
  48. {bare, true},
  49. {deps, ?DEPS},
  50. {example, undefined},
  51. {short_desc, "Install dependencies"},
  52. {desc, info("Install dependencies")},
  53. {opts, []}])),
  54. {ok, State1}.
  55. -spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}.
  56. do(State) ->
  57. Profile = rebar_state:current_profile(State),
  58. ?INFO("Verifying ~p dependencies...", [Profile]),
  59. ProjectApps = rebar_state:project_apps(State),
  60. try
  61. {ok, SrcApps, State1} = case {Profile, rebar_state:get(State, locks, [])} of
  62. {default, []} ->
  63. handle_deps(State, rebar_state:get(State, {deps, Profile}, []));
  64. {default, Locks} ->
  65. handle_deps(State, Locks);
  66. _ ->
  67. %% If not the default profile, ignore locks file
  68. handle_deps(State, rebar_state:get(State, {deps, Profile}, []))
  69. end,
  70. Source = ProjectApps ++ SrcApps,
  71. case rebar_digraph:compile_order(Source) of
  72. {ok, Sort} ->
  73. {ok, rebar_state:deps_to_build(State1,
  74. lists:dropwhile(fun rebar_app_info:valid/1
  75. , Sort -- ProjectApps))};
  76. {error, {cycles, Cycles}} ->
  77. {error, {?MODULE, {cycles, Cycles}}};
  78. {error, Error} ->
  79. {error, Error}
  80. end
  81. catch
  82. %% maybe_fetch will maybe_throw an exception to break out of some loops
  83. _:Reason ->
  84. {error, Reason}
  85. end.
  86. -spec format_error(any()) -> iolist().
  87. format_error({cycles, Cycles}) ->
  88. Prints = [["applications: ",
  89. [io_lib:format("~s ", [Dep]) || Dep <- Cycle],
  90. "depend on each other~n"]
  91. || Cycle <- Cycles],
  92. ["Dependency cycle(s) detected:~n", Prints];
  93. format_error(Reason) ->
  94. io_lib:format("~p", [Reason]).
  95. -spec handle_deps(rebar_state:t(), list()) ->
  96. {ok, [rebar_app_info:t()], rebar_state:t()} | {error, string()}.
  97. handle_deps(State, Deps) ->
  98. handle_deps(State, Deps, false).
  99. -spec handle_deps(rebar_state:t(), list(), boolean() | {true, binary(), integer()})
  100. -> {ok, [rebar_app_info:t()], rebar_state:t()} | {error, string()}.
  101. handle_deps(State, [], _) ->
  102. {ok, [], State};
  103. handle_deps(State, Deps, Update) ->
  104. %% Read in package index and dep graph
  105. {Packages, Graph} = rebar_packages:get_packages(State),
  106. %% Split source deps from pkg deps, needed to keep backwards compatibility
  107. DepsDir = rebar_dir:deps_dir(State),
  108. {SrcDeps, PkgDeps} = parse_deps(State, DepsDir, Deps),
  109. %% Fetch transitive src deps
  110. {State1, SrcApps, PkgDeps1, Seen} =
  111. update_src_deps(0, SrcDeps, PkgDeps, [], State, Update, sets:new()),
  112. {Solved, State2} = case PkgDeps1 of
  113. [] -> %% No pkg deps
  114. {[], State1};
  115. PkgDeps2 ->
  116. %% Find pkg deps needed
  117. S = case rebar_digraph:cull_deps(Graph, PkgDeps2) of
  118. {ok, []} ->
  119. throw({rebar_digraph, no_solution});
  120. {ok, Solution} ->
  121. Solution
  122. end,
  123. update_pkg_deps(S, Packages, Update, Seen, State1)
  124. end,
  125. AllDeps = lists:ukeymerge(2
  126. ,lists:ukeysort(2, SrcApps)
  127. ,lists:ukeysort(2, Solved)),
  128. %% Sort all apps to build order
  129. State3 = rebar_state:all_deps(State2, AllDeps),
  130. {ok, SrcApps, State3}.
  131. %% ===================================================================
  132. %% Internal functions
  133. %% ===================================================================
  134. update_pkg_deps(Pkgs, Packages, Update, Seen, State) ->
  135. %% Create app_info record for each pkg dep
  136. DepsDir = rebar_dir:deps_dir(State),
  137. {Solved, _, State1}
  138. = lists:foldl(fun(Pkg, {Acc, SeenAcc, StateAcc}) ->
  139. AppInfo = package_to_app(DepsDir
  140. ,Packages
  141. ,Pkg),
  142. {SeenAcc1, StateAcc1} = maybe_lock(AppInfo, SeenAcc, StateAcc),
  143. case maybe_fetch(StateAcc1, AppInfo, Update, SeenAcc1) of
  144. true ->
  145. {[AppInfo | Acc], SeenAcc1, StateAcc1};
  146. false ->
  147. {Acc, SeenAcc1, StateAcc1}
  148. end
  149. end, {[], Seen, State}, Pkgs),
  150. {Solved, State1}.
  151. maybe_lock(AppInfo, Seen, State) ->
  152. Name = rebar_app_info:name(AppInfo),
  153. case rebar_state:current_profile(State) of
  154. default ->
  155. case sets:is_element(Name, Seen) of
  156. false ->
  157. {sets:add_element(Name, Seen),
  158. rebar_state:lock(State, AppInfo)};
  159. true ->
  160. {Seen, State}
  161. end;
  162. _ ->
  163. {Seen, State}
  164. end.
  165. package_to_app(DepsDir, Packages, {Name, Vsn}) ->
  166. case dict:find({Name, Vsn}, Packages) of
  167. error ->
  168. {error, missing_package};
  169. {ok, P} ->
  170. PkgDeps = proplists:get_value(<<"deps">>, P, []),
  171. Link = proplists:get_value(<<"link">>, P, ""),
  172. {ok, AppInfo} = rebar_app_info:new(Name, Vsn),
  173. AppInfo1 = rebar_app_info:deps(AppInfo, PkgDeps),
  174. AppInfo2 = rebar_app_info:dir(AppInfo1, rebar_dir:deps_dir(DepsDir, Name)),
  175. rebar_app_info:source(AppInfo2, {pkg, Name, Vsn, Link})
  176. end.
  177. -spec update_src_deps(non_neg_integer(), list(), list(), list(), rebar_state:t(), boolean(), sets:set(binary())) -> {rebar_state:t(), list(), list(), sets:set(binary())}.
  178. update_src_deps(Level, SrcDeps, PkgDeps, SrcApps, State, Update, Seen) ->
  179. case lists:foldl(fun(AppInfo, {SrcDepsAcc, PkgDepsAcc, SrcAppsAcc, StateAcc, SeenAcc}) ->
  180. %% If not seen, add to list of locks to write out
  181. case sets:is_element(rebar_app_info:name(AppInfo), SeenAcc) of
  182. true ->
  183. {SrcDepsAcc, PkgDepsAcc, SrcAppsAcc, StateAcc, SeenAcc};
  184. false ->
  185. {SeenAcc1, StateAcc1} = maybe_lock(AppInfo, SeenAcc, StateAcc),
  186. {SrcDepsAcc1, PkgDepsAcc1, SrcAppsAcc1, StateAcc2} =
  187. case Update of
  188. {true, UpdateName, UpdateLevel} ->
  189. handle_update(AppInfo
  190. ,UpdateName
  191. ,UpdateLevel
  192. ,SrcDepsAcc
  193. ,PkgDepsAcc
  194. ,SrcAppsAcc
  195. ,Level
  196. ,StateAcc1);
  197. _ ->
  198. maybe_fetch(StateAcc, AppInfo, false, SeenAcc1),
  199. handle_dep(AppInfo
  200. ,SrcDepsAcc
  201. ,PkgDepsAcc
  202. ,SrcAppsAcc
  203. ,Level
  204. ,StateAcc1)
  205. end,
  206. {SrcDepsAcc1, PkgDepsAcc1, SrcAppsAcc1, StateAcc2, SeenAcc1}
  207. end
  208. end, {[], PkgDeps, SrcApps, State, Seen}, SrcDeps) of
  209. {[], NewPkgDeps, NewSrcApps, State1, Seen1} ->
  210. {State1, NewSrcApps, NewPkgDeps, Seen1};
  211. {NewSrcDeps, NewPkgDeps, NewSrcApps, State1, Seen1} ->
  212. update_src_deps(Level+1, NewSrcDeps, NewPkgDeps, NewSrcApps, State1, Update, Seen1)
  213. end.
  214. handle_update(AppInfo, UpdateName, UpdateLevel, SrcDeps, PkgDeps, SrcApps, Level, State) ->
  215. Name = rebar_app_info:name(AppInfo),
  216. Locks = rebar_state:get(State, locks, []),
  217. {_, _, _, DepLevel} = lists:keyfind(Name, 1, Locks),
  218. case UpdateLevel < DepLevel
  219. orelse Name =:= UpdateName of
  220. true ->
  221. case maybe_fetch(State, AppInfo, true, []) of
  222. true ->
  223. handle_dep(AppInfo
  224. ,SrcDeps
  225. ,PkgDeps
  226. ,SrcApps
  227. ,Level
  228. ,State);
  229. false ->
  230. {SrcDeps, PkgDeps, SrcApps, State}
  231. end;
  232. false ->
  233. {SrcDeps, PkgDeps, SrcApps, State}
  234. end.
  235. handle_dep(AppInfo, SrcDeps, PkgDeps, SrcApps, Level, State) ->
  236. DepsDir = rebar_dir:deps_dir(State),
  237. {AppInfo1, NewSrcDeps, NewPkgDeps} =
  238. handle_dep(State, DepsDir, AppInfo),
  239. AppInfo2 = rebar_app_info:dep_level(AppInfo1, Level),
  240. {NewSrcDeps ++ SrcDeps
  241. ,NewPkgDeps++PkgDeps
  242. ,[AppInfo2 | SrcApps]
  243. ,State}.
  244. -spec handle_dep(rebar_state:t(), file:filename_all(), rebar_app_info:t()) ->
  245. {rebar_app_info:t(), [rebar_app_info:t()], [pkg_dep()]}.
  246. handle_dep(State, DepsDir, AppInfo) ->
  247. Profile = rebar_state:current_profile(State),
  248. C = rebar_config:consult(rebar_app_info:dir(AppInfo)),
  249. S = rebar_state:new(rebar_state:new(), C, rebar_app_info:dir(AppInfo)),
  250. S1 = rebar_state:apply_profile(S, Profile),
  251. Deps = case Profile of
  252. default ->
  253. rebar_state:get(S1, {deps, Profile}, []);
  254. _ ->
  255. rebar_state:get(S1, {deps, default}, []) ++
  256. rebar_state:get(S1, {deps, Profile}, [])
  257. end,
  258. AppInfo1 = rebar_app_info:deps(AppInfo, rebar_state:deps_names(Deps)),
  259. {SrcDeps, PkgDeps} = parse_deps(State, DepsDir, Deps),
  260. {AppInfo1, SrcDeps, PkgDeps}.
  261. -spec maybe_fetch(rebar_state:t(), rebar_app_info:t(), boolean() | {true, binary(), integer()},
  262. sets:set(binary())) -> boolean().
  263. maybe_fetch(State, AppInfo, Update, Seen) ->
  264. AppDir = ec_cnv:to_list(rebar_app_info:dir(AppInfo)),
  265. DefaultProfileDeps = rebar_dir:default_profile_deps(State),
  266. Apps = rebar_app_discover:find_apps(["_checkouts", DefaultProfileDeps], all),
  267. case rebar_app_utils:find(rebar_app_info:name(AppInfo), Apps) of
  268. {ok, _} ->
  269. %% Don't fetch dep if it exists in the _checkouts dir
  270. false;
  271. error ->
  272. Exists = case rebar_app_utils:is_app_dir(filename:absname(AppDir)++"-*") of
  273. {true, _} ->
  274. true;
  275. _ ->
  276. case rebar_app_utils:is_app_dir(filename:absname(AppDir)) of
  277. {true, _} ->
  278. true;
  279. _ ->
  280. false
  281. end
  282. end,
  283. case not Exists orelse Update of
  284. true ->
  285. ?INFO("Fetching ~s", [rebar_app_info:name(AppInfo)]),
  286. Source = rebar_app_info:source(AppInfo),
  287. case rebar_fetch:download_source(AppDir, Source) of
  288. {error, Reason} ->
  289. throw(Reason);
  290. Result ->
  291. Result
  292. end;
  293. _ ->
  294. case sets:is_element(rebar_app_info:name(AppInfo), Seen) of
  295. true ->
  296. false;
  297. false ->
  298. Source = rebar_app_info:source(AppInfo),
  299. case rebar_fetch:needs_update(AppDir, Source) of
  300. true ->
  301. ?INFO("Updating ~s", [rebar_app_info:name(AppInfo)]),
  302. case rebar_fetch:download_source(AppDir, Source) of
  303. {error, Reason} ->
  304. throw(Reason);
  305. Result ->
  306. Result
  307. end;
  308. false ->
  309. false
  310. end
  311. end
  312. end
  313. end.
  314. -spec parse_deps(rebar_state:t(), binary(), list()) -> {[rebar_app_info:t()], [pkg_dep()]}.
  315. parse_deps(State, DepsDir, Deps) ->
  316. lists:foldl(fun({Name, Vsn}, {SrcDepsAcc, PkgDepsAcc}) ->
  317. {SrcDepsAcc, [parse_goal(ec_cnv:to_binary(Name)
  318. ,ec_cnv:to_binary(Vsn)) | PkgDepsAcc]};
  319. (Name, {SrcDepsAcc, PkgDepsAcc}) when is_atom(Name) ->
  320. {SrcDepsAcc, [ec_cnv:to_binary(Name) | PkgDepsAcc]};
  321. ({Name, Source}, {SrcDepsAcc, PkgDepsAcc}) when is_tuple (Source) ->
  322. Dep = new_dep(State, DepsDir, Name, [], Source),
  323. {[Dep | SrcDepsAcc], PkgDepsAcc};
  324. ({Name, _Vsn, Source}, {SrcDepsAcc, PkgDepsAcc}) when is_tuple (Source) ->
  325. Dep = new_dep(State, DepsDir, Name, [], Source),
  326. {[Dep | SrcDepsAcc], PkgDepsAcc};
  327. ({Name, Source, Level}, {SrcDepsAcc, PkgDepsAcc}) when is_tuple (Source)
  328. , is_integer(Level) ->
  329. Dep = new_dep(State, DepsDir, Name, [], Source),
  330. {[Dep | SrcDepsAcc], PkgDepsAcc}
  331. end, {[], []}, Deps).
  332. new_dep(State, DepsDir, Name, Vsn, Source) ->
  333. Dirs = [ec_cnv:to_list(filename:join(rebar_dir:default_profile_deps(State), Name)),
  334. ec_cnv:to_list(filename:join(DepsDir, Name))],
  335. {ok, Dep} = case ec_lists:search(fun(Dir) ->
  336. rebar_app_info:discover(Dir)
  337. end, Dirs) of
  338. {ok, App, _} ->
  339. {ok, App};
  340. not_found ->
  341. rebar_app_info:new(Name, Vsn, ec_cnv:to_list(filename:join(DepsDir, Name)))
  342. end,
  343. rebar_app_info:source(Dep, Source).
  344. -spec parse_goal(binary(), binary()) -> pkg_dep().
  345. parse_goal(Name, Constraint) ->
  346. case re:run(Constraint, "([^\\d]*)(\\d.*)", [{capture, [1,2], binary}]) of
  347. {match, [<<>>, Vsn]} ->
  348. {Name, Vsn};
  349. {match, [Op, Vsn]} ->
  350. {Name, Vsn, binary_to_atom(Op, utf8)};
  351. nomatch ->
  352. fail
  353. end.
  354. info(Description) ->
  355. io_lib:format("~s.~n"
  356. "~n"
  357. "Valid rebar.config options:~n"
  358. " ~p~n"
  359. " ~p~n",
  360. [
  361. Description,
  362. {deps_dir, "deps"},
  363. {deps,
  364. [app_name,
  365. {rebar, "1.0.*"},
  366. {rebar, ".*",
  367. {git, "git://github.com/rebar/rebar.git"}},
  368. {rebar, ".*",
  369. {git, "git://github.com/rebar/rebar.git", "Rev"}},
  370. {rebar, "1.0.*",
  371. {git, "git://github.com/rebar/rebar.git", {branch, "master"}}},
  372. {rebar, "1.0.0",
  373. {git, "git://github.com/rebar/rebar.git", {tag, "1.0.0"}}},
  374. {rebar, "",
  375. {git, "git://github.com/rebar/rebar.git", {branch, "master"}},
  376. [raw]},
  377. {app_name, ".*", {hg, "https://www.example.org/url"}},
  378. {app_name, ".*", {rsync, "Url"}},
  379. {app_name, ".*", {svn, "https://www.example.org/url"}},
  380. {app_name, ".*", {svn, "svn://svn.example.org/url"}},
  381. {app_name, ".*", {bzr, "https://www.example.org/url", "Rev"}},
  382. {app_name, ".*", {fossil, "https://www.example.org/url"}},
  383. {app_name, ".*", {fossil, "https://www.example.org/url", "Vsn"}},
  384. {app_name, ".*", {p4, "//depot/subdir/app_dir"}}]}
  385. ]).