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.

320 righe
12 KiB

  1. %% -*- tab-width: 4;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_deps).
  28. -include("rebar.hrl").
  29. -export([preprocess/2,
  30. postprocess/2,
  31. compile/2,
  32. 'check-deps'/2,
  33. 'get-deps'/2,
  34. 'delete-deps'/2]).
  35. -record(dep, { dir,
  36. app,
  37. vsn_regex,
  38. source }).
  39. %% ===================================================================
  40. %% Public API
  41. %% ===================================================================
  42. preprocess(Config, _) ->
  43. %% Side effect to set deps_dir globally for all dependencies from top level down.
  44. %% Means the root deps_dir is honoured or the default used globally
  45. %% since it will be set on the first time through here
  46. set_global_deps_dir(Config, rebar_config:get_global(deps_dir, [])),
  47. %% Get the list of deps for the current working directory and identify those
  48. %% deps that are available/present.
  49. Deps = rebar_config:get_local(Config, deps, []),
  50. {AvailableDeps, MissingDeps} = find_deps(Deps),
  51. ?DEBUG("Available deps: ~p\n", [AvailableDeps]),
  52. ?DEBUG("Missing deps : ~p\n", [MissingDeps]),
  53. %% Add available deps to code path
  54. update_deps_code_path(AvailableDeps),
  55. %% If skip_deps=true, don't worry about processing deps at all.
  56. case rebar_config:get_global(skip_deps, false) of
  57. false ->
  58. %% Return all the available dep directories for process
  59. {ok, [D#dep.dir || D <- AvailableDeps]};
  60. _ ->
  61. {ok, []}
  62. end.
  63. postprocess(_Config, _) ->
  64. case erlang:get(?MODULE) of
  65. undefined ->
  66. {ok, []};
  67. Dirs ->
  68. erlang:erase(?MODULE),
  69. {ok, Dirs}
  70. end.
  71. compile(Config, AppFile) ->
  72. 'check-deps'(Config, AppFile).
  73. 'check-deps'(Config, _) ->
  74. %% Get the list of immediate (i.e. non-transitive) deps that are missing
  75. Deps = rebar_config:get_local(Config, deps, []),
  76. case find_deps(Deps) of
  77. {_, []} ->
  78. %% No missing deps
  79. ok;
  80. {_, MissingDeps} ->
  81. [?CONSOLE("Dependency not available: ~p-~s (~p)\n",
  82. [D#dep.app, D#dep.vsn_regex, D#dep.source]) ||
  83. D <- MissingDeps],
  84. ?FAIL
  85. end.
  86. 'get-deps'(Config, _) ->
  87. %% Determine what deps are available and missing
  88. Deps = rebar_config:get_local(Config, deps, []),
  89. {_AvailableDeps, MissingDeps} = find_deps(Deps),
  90. %% For each missing dep with a specified source, try to pull it.
  91. PulledDeps = [use_source(D) || D <- MissingDeps, D#dep.source /= undefined],
  92. %% Add each pulled dep to our list of dirs for post-processing. This yields
  93. %% the necessary transitivity of the deps
  94. erlang:put(?MODULE, [D#dep.dir || D <- PulledDeps]),
  95. ok.
  96. 'delete-deps'(Config, _) ->
  97. %% Delete all the available deps, if any
  98. Deps = rebar_config:get_local(Config, deps, []),
  99. {AvailableDeps, _} = find_deps(Deps),
  100. [delete_dep(D) || D <- AvailableDeps],
  101. ok.
  102. %% ===================================================================
  103. %% Internal functions
  104. %% ===================================================================
  105. %% Added because of trans deps,
  106. %% need all deps in same dir and should be the one set by the root rebar.config
  107. %% Sets a default if root config has no deps_dir set
  108. set_global_deps_dir(Config, []) ->
  109. rebar_config:set_global(deps_dir, rebar_config:get_local(Config, deps_dir, "deps"));
  110. set_global_deps_dir(_Config, _DepsDir) ->
  111. ok.
  112. get_deps_dir() ->
  113. BaseDir = rebar_config:get_global(base_dir, []),
  114. DepsDir = rebar_config:get_global(deps_dir, "deps"),
  115. filename:join(BaseDir, DepsDir).
  116. update_deps_code_path([]) ->
  117. ok;
  118. update_deps_code_path([Dep | Rest]) ->
  119. case is_app_available(Dep#dep.app, Dep#dep.vsn_regex, Dep#dep.dir) of
  120. {true, _} ->
  121. code:add_patha(filename:join(Dep#dep.dir, ebin));
  122. false ->
  123. ok
  124. end,
  125. update_deps_code_path(Rest).
  126. find_deps(Deps) ->
  127. find_deps(Deps, {[], []}).
  128. find_deps([], {Avail, Missing}) ->
  129. {lists:reverse(Avail), lists:reverse(Missing)};
  130. find_deps([App | Rest], Acc) when is_atom(App) ->
  131. find_deps([{App, ".*", undefined} | Rest], Acc);
  132. find_deps([{App, VsnRegex} | Rest], Acc) when is_atom(App) ->
  133. find_deps([{App, VsnRegex, undefined} | Rest], Acc);
  134. find_deps([{App, VsnRegex, Source} | Rest], {Avail, Missing}) ->
  135. Dep = #dep { app = App,
  136. vsn_regex = VsnRegex,
  137. source = Source },
  138. case is_app_available(App, VsnRegex) of
  139. {true, AppDir} ->
  140. find_deps(Rest, {[Dep#dep { dir = AppDir } | Avail], Missing});
  141. false ->
  142. AppDir = filename:join(get_deps_dir(), Dep#dep.app),
  143. case is_app_available(App, VsnRegex, AppDir) of
  144. {true, AppDir} ->
  145. find_deps(Rest, {[Dep#dep { dir = AppDir } | Avail], Missing});
  146. false ->
  147. find_deps(Rest, {Avail, [Dep#dep { dir = AppDir } | Missing]})
  148. end
  149. end;
  150. find_deps([Other | _Rest], _Acc) ->
  151. ?ABORT("Invalid dependency specification ~p in ~s\n",
  152. [Other, rebar_utils:get_cwd()]).
  153. delete_dep(D) ->
  154. case filelib:is_dir(D#dep.dir) of
  155. true ->
  156. ?INFO("Deleting dependency: ~s\n", [D#dep.dir]),
  157. rebar_file_utils:rm_rf(D#dep.dir);
  158. false ->
  159. ok
  160. end.
  161. require_source_engine(Source) ->
  162. case source_engine_avail(Source) of
  163. true ->
  164. ok;
  165. false ->
  166. ?ABORT("No command line interface available to process ~p\n", [Source])
  167. end.
  168. is_app_available(App, VsnRegex) ->
  169. case code:lib_dir(App) of
  170. {error, bad_name} ->
  171. false;
  172. Path ->
  173. is_app_available(App, VsnRegex, Path)
  174. end.
  175. is_app_available(App, VsnRegex, Path) ->
  176. case rebar_app_utils:is_app_dir(Path) of
  177. {true, AppFile} ->
  178. case rebar_app_utils:app_name(AppFile) of
  179. App ->
  180. Vsn = rebar_app_utils:app_vsn(AppFile),
  181. ?INFO("Looking for ~s-~s ; found ~s-~s at ~s\n",
  182. [App, VsnRegex, App, Vsn, Path]),
  183. case re:run(Vsn, VsnRegex, [{capture, none}]) of
  184. match ->
  185. {true, Path};
  186. nomatch ->
  187. ?WARN("~s has version ~p; requested regex was ~s\n",
  188. [AppFile, Vsn, VsnRegex]),
  189. false
  190. end;
  191. OtherApp ->
  192. ?WARN("~s has application id ~p; expected ~p\n", [AppFile, OtherApp, App]),
  193. false
  194. end;
  195. false ->
  196. ?WARN("Expected ~s to be an app dir (containing ebin/*.app), but no .app found.\n",
  197. [Path]),
  198. false
  199. end.
  200. use_source(Dep) ->
  201. use_source(Dep, 3).
  202. use_source(Dep, 0) ->
  203. ?ABORT("Failed to acquire source from ~p after 3 tries.\n", [Dep#dep.source]);
  204. use_source(Dep, Count) ->
  205. case filelib:is_dir(Dep#dep.dir) of
  206. true ->
  207. %% Already downloaded -- verify the versioning matches up with our regex
  208. case is_app_available(Dep#dep.app, Dep#dep.vsn_regex, Dep#dep.dir) of
  209. {true, _} ->
  210. %% Available version matches up -- we're good to go; add the
  211. %% app dir to our code path
  212. code:add_patha(filename:join(Dep#dep.dir, ebin)),
  213. Dep;
  214. false ->
  215. %% The app that was downloaded doesn't match up (or had
  216. %% errors or something). For the time being, abort.
  217. ?ABORT("Dependency dir ~s does not satisfy version regex ~s.\n",
  218. [Dep#dep.dir, Dep#dep.vsn_regex])
  219. end;
  220. false ->
  221. ?CONSOLE("Pulling ~p from ~p\n", [Dep#dep.app, Dep#dep.source]),
  222. require_source_engine(Dep#dep.source),
  223. TargetDir = filename:join(get_deps_dir(), Dep#dep.app),
  224. download_source(TargetDir, Dep#dep.source),
  225. use_source(Dep#dep { dir = TargetDir }, Count-1)
  226. end.
  227. download_source(AppDir, {hg, Url, Rev}) ->
  228. ok = filelib:ensure_dir(AppDir),
  229. rebar_utils:sh(?FMT("hg clone -U ~s ~s", [Url, filename:basename(AppDir)]), [], filename:dirname(AppDir)),
  230. rebar_utils:sh(?FMT("hg update ~s", [Rev]), [], AppDir);
  231. download_source(AppDir, {git, Url, Rev}) ->
  232. ok = filelib:ensure_dir(AppDir),
  233. rebar_utils:sh(?FMT("git clone -n ~s ~s", [Url, filename:basename(AppDir)]), [], filename:dirname(AppDir)),
  234. rebar_utils:sh(?FMT("git checkout ~s", [Rev]), [], AppDir);
  235. download_source(AppDir, {bzr, Url, Rev}) ->
  236. ok = filelib:ensure_dir(AppDir),
  237. rebar_utils:sh(?FMT("bzr branch -r ~s ~s ~s",
  238. [Rev, Url, filename:basename(AppDir)]), [],
  239. filename:dirname(AppDir));
  240. download_source(AppDir, {svn, Url, Rev}) ->
  241. ok = filelib:ensure_dir(AppDir),
  242. rebar_utils:sh(?FMT("svn checkout -r ~s ~s ~s",
  243. [Rev, Url, filename:basename(AppDir)]), [],
  244. filename:dirname(AppDir)).
  245. %% ===================================================================
  246. %% Source helper functions
  247. %% ===================================================================
  248. source_engine_avail({Name, _, _})
  249. when Name == hg; Name == git; Name == svn; Name == bzr ->
  250. case scm_client_vsn(Name) >= required_scm_client_vsn(Name) of
  251. true ->
  252. true;
  253. false ->
  254. ?ABORT("Rebar requires version ~p or higher of ~s\n",
  255. [required_scm_client_vsn(Name), Name])
  256. end.
  257. scm_client_vsn(false, _VsnArg, _VsnRegex) ->
  258. false;
  259. scm_client_vsn(Path, VsnArg, VsnRegex) ->
  260. Info = os:cmd(Path ++ VsnArg),
  261. case re:run(Info, VsnRegex, [{capture, all_but_first, list}]) of
  262. {match, Match} ->
  263. list_to_tuple([list_to_integer(S) || S <- Match]);
  264. _ ->
  265. false
  266. end.
  267. required_scm_client_vsn(hg) -> {1, 1};
  268. required_scm_client_vsn(git) -> {1, 5};
  269. required_scm_client_vsn(bzr) -> {2, 0};
  270. required_scm_client_vsn(svn) -> {1, 6}.
  271. scm_client_vsn(hg) ->
  272. scm_client_vsn(os:find_executable(hg), " --version", "version (\\d+).(\\d+)");
  273. scm_client_vsn(git) ->
  274. scm_client_vsn(os:find_executable(git), " --version", "git version (\\d+).(\\d+)");
  275. scm_client_vsn(bzr) ->
  276. scm_client_vsn(os:find_executable(bzr), " --version", "Bazaar \\(bzr\\) (\\d+).(\\d+)");
  277. scm_client_vsn(svn) ->
  278. scm_client_vsn(os:find_executable(svn), " --version", "svn, version (\\d+).(\\d+)");
  279. scm_client_vsn(_) ->
  280. undefined.