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

396 行
18 KiB

10 年前
10 年前
10 年前
10 年前
10 年前
10 年前
10 年前
10 年前
10 年前
10 年前
10 年前
10 年前
10 年前
  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. ProjectApps = rebar_state:project_apps(State),
  59. try
  60. {ok, SrcApps, State1} = case rebar_state:get(State, locks, []) of
  61. [] ->
  62. handle_deps(State, rebar_state:get(State, {deps, Profile}, []));
  63. Locks ->
  64. handle_deps(State, Locks)
  65. end,
  66. Source = ProjectApps ++ SrcApps,
  67. case rebar_digraph:compile_order(Source) of
  68. {ok, Sort} ->
  69. {ok, rebar_state:deps_to_build(State1,
  70. lists:dropwhile(fun rebar_app_info:valid/1
  71. , Sort -- ProjectApps))};
  72. {error, Error} ->
  73. {error, Error}
  74. end
  75. catch
  76. %% maybe_fetch will maybe_throw an exception to break out of some loops
  77. _:Reason ->
  78. {error, Reason}
  79. end.
  80. -spec format_error(any()) -> iolist().
  81. format_error(Reason) ->
  82. io_lib:format("~p", [Reason]).
  83. -spec handle_deps(rebar_state:t(), list()) ->
  84. {ok, [rebar_app_info:t()], rebar_state:t()} | {error, string()}.
  85. handle_deps(State, Deps) ->
  86. handle_deps(State, Deps, false).
  87. -spec handle_deps(rebar_state:t(), list(), boolean() | {true, binary(), integer()})
  88. -> {ok, [rebar_app_info:t()], rebar_state:t()} | {error, string()}.
  89. handle_deps(State, [], _) ->
  90. {ok, [], State};
  91. handle_deps(State, Deps, Update) ->
  92. %% Read in package index and dep graph
  93. {Packages, Graph} = rebar_packages:get_packages(State),
  94. %% Split source deps from pkg deps, needed to keep backwards compatibility
  95. DepsDir = rebar_dir:deps_dir(State),
  96. {SrcDeps, PkgDeps} = parse_deps(State, DepsDir, Deps),
  97. %% Fetch transitive src deps
  98. {State1, SrcApps, PkgDeps1, Seen} =
  99. update_src_deps(0, SrcDeps, PkgDeps, [], State, Update, sets:new()),
  100. {Solved, State2} = case PkgDeps1 of
  101. [] -> %% No pkg deps
  102. {[], State1};
  103. PkgDeps2 ->
  104. %% Find pkg deps needed
  105. S = case rebar_digraph:cull_deps(Graph, PkgDeps2) of
  106. {ok, []} ->
  107. throw({rebar_digraph, no_solution});
  108. {ok, Solution} ->
  109. Solution
  110. end,
  111. update_pkg_deps(S, Packages, Update, Seen, State1)
  112. end,
  113. AllDeps = lists:ukeymerge(2
  114. ,lists:ukeysort(2, SrcApps)
  115. ,lists:ukeysort(2, Solved)),
  116. %% Sort all apps to build order
  117. State3 = rebar_state:all_deps(State2, AllDeps),
  118. {ok, SrcApps, State3}.
  119. %% ===================================================================
  120. %% Internal functions
  121. %% ===================================================================
  122. update_pkg_deps(Pkgs, Packages, Update, Seen, State) ->
  123. %% Create app_info record for each pkg dep
  124. DepsDir = rebar_dir:deps_dir(State),
  125. {Solved, _, State1}
  126. = lists:foldl(fun(Pkg, {Acc, SeenAcc, StateAcc}) ->
  127. AppInfo = package_to_app(DepsDir
  128. ,Packages
  129. ,Pkg),
  130. {SeenAcc1, StateAcc1} = maybe_lock(AppInfo, SeenAcc, StateAcc),
  131. case maybe_fetch(StateAcc1, AppInfo, Update, SeenAcc) of
  132. true ->
  133. {[AppInfo | Acc], SeenAcc1, StateAcc1};
  134. false ->
  135. {Acc, SeenAcc1, StateAcc1}
  136. end
  137. end, {[], Seen, State}, Pkgs),
  138. {Solved, State1}.
  139. maybe_lock(AppInfo, Seen, State) ->
  140. Name = rebar_app_info:name(AppInfo),
  141. case sets:is_element(Name, Seen) of
  142. false ->
  143. {sets:add_element(Name, Seen),
  144. rebar_state:lock(State, AppInfo)};
  145. true ->
  146. {sets:add_element(Name, Seen), State}
  147. end.
  148. package_to_app(DepsDir, Packages, {Name, Vsn}) ->
  149. case dict:find({Name, Vsn}, Packages) of
  150. error ->
  151. {error, missing_package};
  152. {ok, P} ->
  153. PkgDeps = proplists:get_value(<<"deps">>, P, []),
  154. Link = proplists:get_value(<<"link">>, P, ""),
  155. {ok, AppInfo} = rebar_app_info:new(Name, Vsn),
  156. AppInfo1 = rebar_app_info:deps(AppInfo, PkgDeps),
  157. AppInfo2 = rebar_app_info:dir(AppInfo1, rebar_dir:deps_dir(DepsDir, Name)),
  158. rebar_app_info:source(AppInfo2, {pkg, Name, Vsn, Link})
  159. end.
  160. -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())}.
  161. update_src_deps(Level, SrcDeps, PkgDeps, SrcApps, State, Update, Seen) ->
  162. case lists:foldl(fun(AppInfo, {SrcDepsAcc, PkgDepsAcc, SrcAppsAcc, StateAcc, SeenAcc}) ->
  163. %% If not seen, add to list of locks to write out
  164. {SeenAcc1, StateAcc1} = maybe_lock(AppInfo, SeenAcc, StateAcc),
  165. {SrcDepsAcc1, PkgDepsAcc1, SrcAppsAcc1, StateAcc2} =
  166. case Update of
  167. {true, UpdateName, UpdateLevel} ->
  168. handle_update(AppInfo
  169. ,UpdateName
  170. ,UpdateLevel
  171. ,SrcDepsAcc
  172. ,PkgDepsAcc
  173. ,SrcAppsAcc
  174. ,Level
  175. ,StateAcc1);
  176. _ ->
  177. maybe_fetch(StateAcc, AppInfo, false, SeenAcc),
  178. handle_dep(AppInfo
  179. ,SrcDepsAcc
  180. ,PkgDepsAcc
  181. ,SrcAppsAcc
  182. ,Level
  183. ,StateAcc1)
  184. end,
  185. {SrcDepsAcc1, PkgDepsAcc1, SrcAppsAcc1, StateAcc2, SeenAcc1}
  186. end, {[], PkgDeps, SrcApps, State, Seen}, SrcDeps) of
  187. {[], NewPkgDeps, NewSrcApps, State1, Seen1} ->
  188. {State1, NewSrcApps, NewPkgDeps, Seen1};
  189. {NewSrcDeps, NewPkgDeps, NewSrcApps, State1, Seen1} ->
  190. update_src_deps(Level+1, NewSrcDeps, NewPkgDeps, NewSrcApps, State1, Update, Seen1)
  191. end.
  192. handle_update(AppInfo, UpdateName, UpdateLevel, SrcDeps, PkgDeps, SrcApps, Level, State) ->
  193. Name = rebar_app_info:name(AppInfo),
  194. Locks = rebar_state:get(State, locks, []),
  195. {_, _, _, DepLevel} = lists:keyfind(Name, 1, Locks),
  196. case UpdateLevel < DepLevel
  197. orelse Name =:= UpdateName of
  198. true ->
  199. case maybe_fetch(State, AppInfo, true, []) of
  200. true ->
  201. handle_dep(AppInfo
  202. ,SrcDeps
  203. ,PkgDeps
  204. ,SrcApps
  205. ,Level
  206. ,State);
  207. false ->
  208. {SrcDeps, PkgDeps, SrcApps, State}
  209. end;
  210. false ->
  211. {SrcDeps, PkgDeps, SrcApps, State}
  212. end.
  213. handle_dep(AppInfo, SrcDeps, PkgDeps, SrcApps, Level, State) ->
  214. DepsDir = rebar_dir:deps_dir(State),
  215. {AppInfo1, NewSrcDeps, NewPkgDeps} =
  216. handle_dep(State, DepsDir, AppInfo),
  217. AppInfo2 = rebar_app_info:dep_level(AppInfo1, Level),
  218. {NewSrcDeps ++ SrcDeps
  219. ,NewPkgDeps++PkgDeps
  220. ,[AppInfo2 | SrcApps]
  221. ,State}.
  222. -spec handle_dep(rebar_state:t(), file:filename_all(), rebar_app_info:t()) ->
  223. {rebar_app_info:t(), [rebar_app_info:t()], [pkg_dep()]}.
  224. handle_dep(State, DepsDir, AppInfo) ->
  225. Profile = rebar_state:current_profile(State),
  226. C = rebar_config:consult(rebar_app_info:dir(AppInfo)),
  227. S = rebar_state:new(rebar_state:new(), C, rebar_app_info:dir(AppInfo)),
  228. S1 = rebar_state:apply_profile(S, Profile),
  229. Deps = case Profile of
  230. default ->
  231. rebar_state:get(S1, {deps, Profile}, []);
  232. _ ->
  233. rebar_state:get(S1, {deps, default}, []) ++
  234. rebar_state:get(S1, {deps, Profile}, [])
  235. end,
  236. AppInfo1 = rebar_app_info:deps(AppInfo, rebar_state:deps_names(Deps)),
  237. {SrcDeps, PkgDeps} = parse_deps(State, DepsDir, Deps),
  238. {AppInfo1, SrcDeps, PkgDeps}.
  239. -spec maybe_fetch(rebar_state:t(), rebar_app_info:t(), boolean() | {true, binary(), integer()},
  240. sets:set(binary())) -> boolean().
  241. maybe_fetch(State, AppInfo, Update, Seen) ->
  242. AppDir = ec_cnv:to_list(rebar_app_info:dir(AppInfo)),
  243. DefaultProfileDeps = rebar_dir:default_profile_deps(State),
  244. Apps = rebar_app_discover:find_apps(["_checkouts", DefaultProfileDeps], all),
  245. case rebar_app_utils:find(rebar_app_info:name(AppInfo), Apps) of
  246. {ok, _} ->
  247. %% Don't fetch dep if it exists in the _checkouts dir
  248. false;
  249. error ->
  250. Exists = case rebar_app_utils:is_app_dir(filename:absname(AppDir)++"-*") of
  251. {true, _} ->
  252. true;
  253. _ ->
  254. case rebar_app_utils:is_app_dir(filename:absname(AppDir)) of
  255. {true, _} ->
  256. true;
  257. _ ->
  258. false
  259. end
  260. end,
  261. case not Exists orelse Update of
  262. true ->
  263. ?INFO("Fetching ~s", [rebar_app_info:name(AppInfo)]),
  264. Source = rebar_app_info:source(AppInfo),
  265. case rebar_fetch:download_source(AppDir, Source) of
  266. {error, Reason} ->
  267. throw(Reason);
  268. Result ->
  269. Result
  270. end;
  271. _ ->
  272. case sets:is_element(rebar_app_info:name(AppInfo), Seen) of
  273. true ->
  274. false;
  275. false ->
  276. Source = rebar_app_info:source(AppInfo),
  277. case rebar_fetch:needs_update(AppDir, Source) of
  278. true ->
  279. ?INFO("Updating ~s", [rebar_app_info:name(AppInfo)]),
  280. case rebar_fetch:download_source(AppDir, Source) of
  281. {error, Reason} ->
  282. throw(Reason);
  283. Result ->
  284. Result
  285. end;
  286. false ->
  287. false
  288. end
  289. end
  290. end
  291. end.
  292. -spec parse_deps(rebar_state:t(), binary(), list()) -> {[rebar_app_info:t()], [pkg_dep()]}.
  293. parse_deps(State, DepsDir, Deps) ->
  294. lists:foldl(fun({Name, Vsn}, {SrcDepsAcc, PkgDepsAcc}) ->
  295. {SrcDepsAcc, [parse_goal(ec_cnv:to_binary(Name)
  296. ,ec_cnv:to_binary(Vsn)) | PkgDepsAcc]};
  297. (Name, {SrcDepsAcc, PkgDepsAcc}) when is_atom(Name) ->
  298. {SrcDepsAcc, [ec_cnv:to_binary(Name) | PkgDepsAcc]};
  299. ({Name, Vsn, Source}, {SrcDepsAcc, PkgDepsAcc}) when is_tuple (Source) ->
  300. Dep = new_dep(State, DepsDir, Name, Vsn, Source),
  301. {[Dep | SrcDepsAcc], PkgDepsAcc};
  302. ({Name, Vsn, Source, _Level}, {SrcDepsAcc, PkgDepsAcc}) when is_tuple (Source) ->
  303. Dep = new_dep(State, DepsDir, Name, Vsn, Source),
  304. {[Dep | SrcDepsAcc], PkgDepsAcc}
  305. end, {[], []}, Deps).
  306. new_dep(State, DepsDir, Name, Vsn, Source) ->
  307. Dirs = [ec_cnv:to_list(filename:join(rebar_dir:default_profile_deps(State), Name)),
  308. ec_cnv:to_list(filename:join(DepsDir, Name))],
  309. {ok, Dep} = case ec_lists:search(fun(Dir) ->
  310. rebar_app_info:discover(Dir)
  311. end, Dirs) of
  312. {ok, App, _} ->
  313. {ok, App};
  314. not_found ->
  315. rebar_app_info:new(Name, Vsn, ec_cnv:to_list(filename:join(DepsDir, Name)))
  316. end,
  317. rebar_app_info:source(Dep, Source).
  318. -spec parse_goal(binary(), binary()) -> pkg_dep().
  319. parse_goal(Name, Constraint) ->
  320. case re:run(Constraint, "([^\\d]*)(\\d.*)", [{capture, [1,2], binary}]) of
  321. {match, [<<>>, Vsn]} ->
  322. {Name, Vsn};
  323. {match, [Op, Vsn]} ->
  324. {Name, Vsn, binary_to_atom(Op, utf8)};
  325. nomatch ->
  326. fail
  327. end.
  328. info(Description) ->
  329. io_lib:format("~s.~n"
  330. "~n"
  331. "Valid rebar.config options:~n"
  332. " ~p~n"
  333. " ~p~n",
  334. [
  335. Description,
  336. {deps_dir, "deps"},
  337. {deps,
  338. [app_name,
  339. {rebar, "1.0.*"},
  340. {rebar, ".*",
  341. {git, "git://github.com/rebar/rebar.git"}},
  342. {rebar, ".*",
  343. {git, "git://github.com/rebar/rebar.git", "Rev"}},
  344. {rebar, "1.0.*",
  345. {git, "git://github.com/rebar/rebar.git", {branch, "master"}}},
  346. {rebar, "1.0.0",
  347. {git, "git://github.com/rebar/rebar.git", {tag, "1.0.0"}}},
  348. {rebar, "",
  349. {git, "git://github.com/rebar/rebar.git", {branch, "master"}},
  350. [raw]},
  351. {app_name, ".*", {hg, "https://www.example.org/url"}},
  352. {app_name, ".*", {rsync, "Url"}},
  353. {app_name, ".*", {svn, "https://www.example.org/url"}},
  354. {app_name, ".*", {svn, "svn://svn.example.org/url"}},
  355. {app_name, ".*", {bzr, "https://www.example.org/url", "Rev"}},
  356. {app_name, ".*", {fossil, "https://www.example.org/url"}},
  357. {app_name, ".*", {fossil, "https://www.example.org/url", "Vsn"}},
  358. {app_name, ".*", {p4, "//depot/subdir/app_dir"}}]}
  359. ]).