Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

395 řádky
18 KiB

před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
  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. -define(PROVIDER, install_deps).
  36. -define(DEPS, [app_discovery]).
  37. -type src_dep() :: {atom(), string(), {atom(), string(), string()}}.
  38. -type pkg_dep() :: {atom(), binary()} | atom().
  39. -type dep() :: src_dep() | pkg_dep().
  40. %% ===================================================================
  41. %% Public API
  42. %% ===================================================================
  43. -spec init(rebar_state:t()) -> {ok, rebar_state:t()}.
  44. init(State) ->
  45. State1 = rebar_state:add_provider(State, providers:create([{name, ?PROVIDER},
  46. {module, ?MODULE},
  47. {bare, true},
  48. {deps, ?DEPS},
  49. {example, undefined},
  50. {short_desc, "Install dependencies"},
  51. {desc, info("Install dependencies")},
  52. {opts, []}])),
  53. {ok, State1}.
  54. -spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}.
  55. do(State) ->
  56. Profile = rebar_state:current_profile(State),
  57. ProjectApps = rebar_state:project_apps(State),
  58. try
  59. {ok, SrcApps, State1} = case rebar_state:get(State, locks, []) of
  60. [] ->
  61. handle_deps(State, rebar_state:get(State, {deps, Profile}, []));
  62. Locks ->
  63. handle_deps(State, Locks)
  64. end,
  65. Source = ProjectApps ++ SrcApps,
  66. case rebar_digraph:compile_order(Source) of
  67. {ok, Sort} ->
  68. {ok, rebar_state:set(State1, deps_to_build,
  69. lists:dropwhile(fun rebar_app_info:valid/1, Sort -- ProjectApps))};
  70. {error, Error} ->
  71. {error, Error}
  72. end
  73. catch
  74. %% maybe_fetch will maybe_throw an exception to break out of some loops
  75. _:Reason ->
  76. {error, Reason}
  77. end.
  78. -spec format_error(any()) -> iolist().
  79. format_error(Reason) ->
  80. io_lib:format("~p", [Reason]).
  81. -spec handle_deps(rebar_state:t(), [dep()]) -> {ok, rebar_state:t()}.
  82. handle_deps(State, Deps) ->
  83. handle_deps(State, Deps, false).
  84. -spec handle_deps(rebar_state:t(), [dep()], boolean() | {true, binary(), integer()})
  85. -> {ok, rebar_state:t()} | {error, string()}.
  86. handle_deps(State, [], _) ->
  87. {ok, State};
  88. handle_deps(State, Deps, Update) ->
  89. %% Read in package index and dep graph
  90. {Packages, Graph} = rebar_packages:get_packages(State),
  91. %% Split source deps from pkg deps, needed to keep backwards compatibility
  92. DepsDir = rebar_utils:deps_dir(State),
  93. {SrcDeps, PkgDeps} = parse_deps(State, DepsDir, Deps),
  94. %% Fetch transitive src deps
  95. {State1, SrcApps, PkgDeps1, Seen} =
  96. update_src_deps(0, SrcDeps, PkgDeps, [], State, Update, sets:new()),
  97. {Solved, State2} = case PkgDeps1 of
  98. [] -> %% No pkg deps
  99. {[], State1};
  100. PkgDeps2 ->
  101. %% Find pkg deps needed
  102. S = case rebar_digraph:cull_deps(Graph, PkgDeps2) of
  103. {ok, []} ->
  104. throw({rebar_digraph, no_solution});
  105. {ok, Solution} ->
  106. Solution;
  107. [] ->
  108. throw({rebar_digraph, no_solution})
  109. end,
  110. update_pkg_deps(S, Packages, Update, Seen, State1)
  111. end,
  112. AllDeps = lists:ukeymerge(2
  113. ,lists:ukeysort(2, SrcApps)
  114. ,lists:ukeysort(2, Solved)),
  115. %% Sort all apps to build order
  116. State3 = rebar_state:all_deps(State2, AllDeps),
  117. {ok, SrcApps, State3}.
  118. %% ===================================================================
  119. %% Internal functions
  120. %% ===================================================================
  121. update_pkg_deps(Pkgs, Packages, Update, Seen, State) ->
  122. %% Create app_info record for each pkg dep
  123. DepsDir = rebar_utils:deps_dir(State),
  124. {Solved, _, State1}
  125. = lists:foldl(fun(Pkg, {Acc, SeenAcc, StateAcc}) ->
  126. AppInfo = package_to_app(DepsDir
  127. ,Packages
  128. ,Pkg),
  129. {SeenAcc1, StateAcc1} = maybe_lock(AppInfo, SeenAcc, StateAcc),
  130. case maybe_fetch(StateAcc1, AppInfo, Update, SeenAcc) of
  131. true ->
  132. {[AppInfo | Acc], SeenAcc1, StateAcc1};
  133. false ->
  134. {Acc, SeenAcc1, StateAcc1}
  135. end
  136. end, {[], Seen, State}, Pkgs),
  137. {Solved, State1}.
  138. maybe_lock(AppInfo, Seen, State) ->
  139. Name = rebar_app_info:name(AppInfo),
  140. case sets:is_element(Name, Seen) of
  141. false ->
  142. {sets:add_element(Name, Seen),
  143. rebar_state:lock(State, AppInfo)};
  144. true ->
  145. {sets:add_element(Name, Seen), State}
  146. end.
  147. package_to_app(DepsDir, Packages, {Name, Vsn}) ->
  148. case dict:find({Name, Vsn}, Packages) of
  149. error ->
  150. {error, missing_package};
  151. {ok, P} ->
  152. PkgDeps = proplists:get_value(<<"deps">>, P, []),
  153. Link = proplists:get_value(<<"link">>, P, ""),
  154. {ok, AppInfo} = rebar_app_info:new(Name, Vsn),
  155. AppInfo1 = rebar_app_info:deps(AppInfo, PkgDeps),
  156. AppInfo2 = rebar_app_info:dir(AppInfo1, rebar_utils:deps_dir(DepsDir, Name)),
  157. rebar_app_info:source(AppInfo2, {pkg, Name, Vsn, Link})
  158. end.
  159. -spec update_src_deps(integer(), list(), list(), list(), rebar_state:t(), boolean(), sets:set(binary())) ->
  160. {rebar_state:t(), [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_utils: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_utils: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(), [dep()]) -> {[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_utils: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. ]).