You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

323 lines
15 KiB

преди 14 години
преди 15 години
преди 10 години
преди 15 години
преди 14 години
преди 13 години
преди 14 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 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_app_utils).
  28. -export([find/2,
  29. find/3,
  30. is_app_src/1,
  31. app_src_to_app/2,
  32. validate_application_info/1,
  33. validate_application_info/2,
  34. parse_deps/5,
  35. parse_deps/6,
  36. dep_to_app/7,
  37. format_error/1]).
  38. -include("rebar.hrl").
  39. -include_lib("providers/include/providers.hrl").
  40. %% ===================================================================
  41. %% Public API
  42. %% ===================================================================
  43. %% @doc finds the proper app info record for a given app name in a list of
  44. %% such records.
  45. -spec find(binary(), [rebar_app_info:t()]) -> {ok, rebar_app_info:t()} | error.
  46. find(Name, Apps) ->
  47. ec_lists:find(fun(App) -> rebar_app_info:name(App) =:= Name end, Apps).
  48. %% @doc finds the proper app info record for a given app name at a given version
  49. %% in a list of such records.
  50. -spec find(binary(), binary(), [rebar_app_info:t()]) -> {ok, rebar_app_info:t()} | error.
  51. find(Name, Vsn, Apps) ->
  52. ec_lists:find(fun(App) ->
  53. rebar_app_info:name(App) =:= Name
  54. andalso rebar_app_info:original_vsn(App) =:= Vsn
  55. end, Apps).
  56. %% @doc checks if a given file is .app.src file
  57. is_app_src(Filename) ->
  58. %% If removing the extension .app.src yields a shorter name,
  59. %% this is an .app.src file.
  60. Filename =/= filename:rootname(Filename, ".app.src").
  61. %% @doc translates the name of the .app.src[.script] file to where
  62. %% its .app counterpart should be stored.
  63. -spec app_src_to_app(OutDir, SrcFilename) -> OutFilename when
  64. OutDir :: file:filename(),
  65. SrcFilename :: file:filename(),
  66. OutFilename :: file:filename().
  67. app_src_to_app(OutDir, Filename) ->
  68. AppFile =
  69. case lists:suffix(".app.src", Filename) of
  70. true ->
  71. filename:join([OutDir, "ebin", filename:basename(Filename, ".app.src") ++ ".app"]);
  72. false ->
  73. filename:join([OutDir, "ebin", filename:basename(Filename,
  74. ".app.src.script") ++ ".app"])
  75. end,
  76. filelib:ensure_dir(AppFile),
  77. AppFile.
  78. %% @doc checks whether the .app file has all the required data to be valid,
  79. %% and cross-references it with compiled modules on disk
  80. -spec validate_application_info(rebar_app_info:t()) -> boolean().
  81. validate_application_info(AppInfo) ->
  82. validate_application_info(AppInfo, rebar_app_info:app_details(AppInfo)).
  83. %% @doc checks whether the .app file has all the required data to be valid
  84. %% and cross-references it with compiled modules on disk.
  85. %% The app info is passed explicitly as a second argument.
  86. -spec validate_application_info(rebar_app_info:t(), list()) -> boolean().
  87. validate_application_info(AppInfo, AppDetail) ->
  88. EbinDir = rebar_app_info:ebin_dir(AppInfo),
  89. case rebar_app_info:app_file(AppInfo) of
  90. undefined ->
  91. false;
  92. AppFile ->
  93. case proplists:get_value(modules, AppDetail) of
  94. undefined ->
  95. ?PRV_ERROR({module_list, AppFile});
  96. List ->
  97. has_all_beams(EbinDir, List)
  98. end
  99. end.
  100. %% @doc parses all dependencies from the root of the project
  101. -spec parse_deps(Dir, Deps, State, Locks, Level) -> [rebar_app_info:t()] when
  102. Dir :: file:filename(),
  103. Deps :: [tuple() | atom() | binary()], % TODO: meta to source() | lock()
  104. State :: rebar_state:t(),
  105. Locks :: [tuple()], % TODO: meta to [lock()]
  106. Level :: non_neg_integer().
  107. parse_deps(DepsDir, Deps, State, Locks, Level) ->
  108. parse_deps(root, DepsDir, Deps, State, Locks, Level).
  109. %% @doc runs `parse_dep/6' for a set of dependencies.
  110. -spec parse_deps(Parent, Dir, Deps, State, Locks, Level) -> [rebar_app_info:t()] when
  111. Parent :: root | binary(),
  112. Dir :: file:filename(),
  113. Deps :: [tuple() | atom() | binary()], % TODO: meta to source() | lock()
  114. State :: rebar_state:t(),
  115. Locks :: [tuple()], % TODO: meta to [lock()]
  116. Level :: non_neg_integer().
  117. parse_deps(Parent, DepsDir, Deps, State, Locks, Level) ->
  118. [parse_dep(Dep, Parent, DepsDir, State, Locks, Level) || Dep <- Deps].
  119. %% @doc for a given dep, return its app info record. The function
  120. %% also has to choose whether to define the dep from its immediate spec
  121. %% (if it is a newer thing) or from the locks specified in the lockfile.
  122. -spec parse_dep(Dep, Parent, Dir, State, Locks, Level) -> rebar_app_info:t() when
  123. Dep :: tuple() | atom() | binary(), % TODO: meta to source() | lock()
  124. Parent :: root | binary(),
  125. Dir :: file:filename(),
  126. State :: rebar_state:t(),
  127. Locks :: [tuple()], % TODO: meta to [lock()]
  128. Level :: non_neg_integer().
  129. parse_dep(Dep, Parent, DepsDir, State, Locks, Level) ->
  130. Name = case Dep of
  131. Dep when is_tuple(Dep) ->
  132. element(1, Dep);
  133. Dep ->
  134. Dep
  135. end,
  136. case lists:keyfind(rebar_utils:to_binary(Name), 1, Locks) of
  137. false ->
  138. parse_dep(Parent, Dep, DepsDir, false, State);
  139. LockedDep ->
  140. LockedLevel = element(3, LockedDep),
  141. case LockedLevel > Level of
  142. true ->
  143. parse_dep(Parent, Dep, DepsDir, false, State);
  144. false ->
  145. parse_dep(Parent, LockedDep, DepsDir, true, State)
  146. end
  147. end.
  148. %% @doc converts a dependency definition and a location for it on disk
  149. %% into an app info tuple representing it.
  150. -spec parse_dep(Parent, Dep, Dir, IsLock, State) -> rebar_app_info:t() when
  151. Parent :: root | binary(),
  152. Dep :: tuple() | atom() | binary(), % TODO: meta to source() | lock()
  153. Dir :: file:filename(),
  154. IsLock :: boolean(),
  155. State :: rebar_state:t().
  156. parse_dep(Parent, {Name, Vsn, {pkg, PkgName}}, DepsDir, IsLock, State) ->
  157. {PkgName1, PkgVsn} = {rebar_utils:to_binary(PkgName),
  158. rebar_utils:to_binary(Vsn)},
  159. dep_to_app(Parent, DepsDir, Name, PkgVsn, {pkg, PkgName1, PkgVsn, undefined}, IsLock, State);
  160. parse_dep(Parent, {Name, {pkg, PkgName}}, DepsDir, IsLock, State) ->
  161. %% Package dependency with different package name from app name
  162. dep_to_app(Parent, DepsDir, Name, undefined, {pkg, rebar_utils:to_binary(PkgName), undefined, undefined}, IsLock, State);
  163. parse_dep(Parent, {Name, Vsn}, DepsDir, IsLock, State) when is_list(Vsn); is_binary(Vsn) ->
  164. %% Versioned Package dependency
  165. {PkgName, PkgVsn} = {rebar_utils:to_binary(Name),
  166. rebar_utils:to_binary(Vsn)},
  167. dep_to_app(Parent, DepsDir, PkgName, PkgVsn, {pkg, PkgName, PkgVsn, undefined}, IsLock, State);
  168. parse_dep(Parent, Name, DepsDir, IsLock, State) when is_atom(Name); is_binary(Name) ->
  169. %% Unversioned package dependency
  170. dep_to_app(Parent, DepsDir, rebar_utils:to_binary(Name), undefined, {pkg, rebar_utils:to_binary(Name), undefined, undefined}, IsLock, State);
  171. parse_dep(Parent, {Name, Source}, DepsDir, IsLock, State) when is_tuple(Source) ->
  172. dep_to_app(Parent, DepsDir, Name, [], Source, IsLock, State);
  173. parse_dep(Parent, {Name, _Vsn, Source}, DepsDir, IsLock, State) when is_tuple(Source) ->
  174. dep_to_app(Parent, DepsDir, Name, [], Source, IsLock, State);
  175. parse_dep(Parent, {Name, _Vsn, Source, Opts}, DepsDir, IsLock, State) when is_tuple(Source),
  176. is_list(Opts) ->
  177. ?WARN("Dependency option list ~p in ~p is not supported and will be ignored", [Opts, Name]),
  178. dep_to_app(Parent, DepsDir, Name, [], Source, IsLock, State);
  179. parse_dep(Parent, {Name, Source, Opts}, DepsDir, IsLock, State) when is_tuple(Source),
  180. is_list(Opts) ->
  181. ?WARN("Dependency option list ~p in ~p is not supported and will be ignored", [Opts, Name]),
  182. dep_to_app(Parent, DepsDir, Name, [], Source, IsLock, State);
  183. parse_dep(Parent, {Name, {pkg, PkgName, Vsn}, Level}, DepsDir, IsLock, State) when is_integer(Level) ->
  184. dep_to_app(Parent, DepsDir, Name, Vsn, {pkg, PkgName, Vsn, undefined}, IsLock, State);
  185. parse_dep(Parent, {Name, {pkg, PkgName, Vsn, Hash}, Level}, DepsDir, IsLock, State) when is_integer(Level) ->
  186. dep_to_app(Parent, DepsDir, Name, Vsn, {pkg, PkgName, Vsn, Hash}, IsLock, State);
  187. parse_dep(Parent, {Name, Source, Level}, DepsDir, IsLock, State) when is_tuple(Source)
  188. , is_integer(Level) ->
  189. dep_to_app(Parent, DepsDir, Name, [], Source, IsLock, State);
  190. parse_dep(_, Dep, _, _, _) ->
  191. throw(?PRV_ERROR({parse_dep, Dep})).
  192. %% @doc convert a dependency that has just been fetched into
  193. %% an app info record related to it
  194. -spec dep_to_app(Parent, Dir, Name, Vsn, Source, IsLock, State) -> rebar_app_info:t() when
  195. Parent :: root | binary(),
  196. Dir :: file:filename(),
  197. Name :: binary(),
  198. Vsn :: iodata() | undefined,
  199. Source :: tuple(),
  200. IsLock :: boolean(),
  201. State :: rebar_state:t().
  202. dep_to_app(Parent, DepsDir, Name, Vsn, Source, IsLock, State) ->
  203. CheckoutsDir = rebar_utils:to_list(rebar_dir:checkouts_dir(State, Name)),
  204. AppInfo = case rebar_app_info:discover(CheckoutsDir) of
  205. {ok, App} ->
  206. rebar_app_info:source(rebar_app_info:is_checkout(App, true), checkout);
  207. not_found ->
  208. Dir = rebar_utils:to_list(filename:join(DepsDir, Name)),
  209. {ok, AppInfo0} =
  210. case rebar_app_info:discover(Dir) of
  211. {ok, App} ->
  212. {ok, rebar_app_info:parent(App, Parent)};
  213. not_found ->
  214. rebar_app_info:new(Parent, Name, Vsn, Dir, [])
  215. end,
  216. update_source(AppInfo0, Source, State)
  217. end,
  218. C = rebar_config:consult(rebar_app_info:dir(AppInfo)),
  219. AppInfo1 = rebar_app_info:update_opts(AppInfo, rebar_app_info:opts(AppInfo), C),
  220. Overrides = rebar_state:get(State, overrides, []),
  221. AppInfo2 = rebar_app_info:set(AppInfo1, overrides, rebar_app_info:get(AppInfo, overrides, [])++Overrides),
  222. AppInfo3 = rebar_app_info:apply_overrides(rebar_app_info:get(AppInfo2, overrides, []), AppInfo2),
  223. AppInfo4 = rebar_app_info:apply_profiles(AppInfo3, [default, prod]),
  224. AppInfo5 = rebar_app_info:profiles(AppInfo4, [default]),
  225. rebar_app_info:is_lock(AppInfo5, IsLock).
  226. %% @doc sets the source for a given dependency or app along with metadata
  227. %% around version if required.
  228. -spec update_source(rebar_app_info:t(), Source, rebar_state:t()) ->
  229. rebar_app_info:t() when
  230. Source :: tuple() | atom() | binary(). % TODO: meta to source()
  231. update_source(AppInfo, {pkg, PkgName, PkgVsn, Hash}, State) ->
  232. {PkgName1, PkgVsn1} = case PkgVsn of
  233. undefined ->
  234. get_package(PkgName, "0", State);
  235. <<"~>", Vsn/binary>> ->
  236. [Vsn1] = [X || X <- binary:split(Vsn, [<<" ">>], [global]), X =/= <<>>],
  237. get_package(PkgName, Vsn1, State);
  238. _ ->
  239. {PkgName, PkgVsn}
  240. end,
  241. %% store the expected hash for the dependency
  242. Hash1 = case Hash of
  243. undefined -> % unknown, define the hash since we know the dep
  244. fetch_checksum(PkgName1, PkgVsn1, Hash, State);
  245. _ -> % keep as is
  246. Hash
  247. end,
  248. AppInfo1 = rebar_app_info:source(AppInfo, {pkg, PkgName1, PkgVsn1, Hash1}),
  249. Deps = rebar_packages:deps(PkgName1
  250. ,PkgVsn1
  251. ,State),
  252. AppInfo2 = rebar_app_info:resource_type(rebar_app_info:deps(AppInfo1, Deps), pkg),
  253. rebar_app_info:original_vsn(AppInfo2, PkgVsn1);
  254. update_source(AppInfo, Source, _State) ->
  255. rebar_app_info:source(AppInfo, Source).
  256. %% @doc grab the checksum for a given package
  257. -spec fetch_checksum(atom(), string(), iodata() | undefined, rebar_state:t()) ->
  258. iodata() | no_return().
  259. fetch_checksum(PkgName, PkgVsn, Hash, State) ->
  260. try
  261. rebar_packages:registry_checksum({pkg, PkgName, PkgVsn, Hash}, State)
  262. catch
  263. _:_ ->
  264. ?INFO("Package ~ts-~ts not found. Fetching registry updates and trying again...", [PkgName, PkgVsn]),
  265. {ok, _} = rebar_prv_update:do(State),
  266. rebar_packages:registry_checksum({pkg, PkgName, PkgVsn, Hash}, State)
  267. end.
  268. %% @doc convert a given exception's payload into an io description.
  269. -spec format_error(any()) -> iolist().
  270. format_error({missing_package, Package}) ->
  271. io_lib:format("Package not found in registry: ~ts", [Package]);
  272. format_error({parse_dep, Dep}) ->
  273. io_lib:format("Failed parsing dep ~p", [Dep]);
  274. format_error(Error) ->
  275. io_lib:format("~p", [Error]).
  276. %% ===================================================================
  277. %% Internal functions
  278. %% ===================================================================
  279. %% @private find the correct version of a package based on the version
  280. %% and name passed in.
  281. -spec get_package(binary(), binary() | string(), rebar_state:t()) ->
  282. term() | no_return().
  283. get_package(Dep, Vsn, State) ->
  284. case rebar_packages:find_highest_matching(Dep, Vsn, ?PACKAGE_TABLE, State) of
  285. {ok, HighestDepVsn} ->
  286. {Dep, HighestDepVsn};
  287. none ->
  288. throw(?PRV_ERROR({missing_package, rebar_utils:to_binary(Dep)}))
  289. end.
  290. %% @private checks that all the beam files have been properly
  291. %% created.
  292. -spec has_all_beams(file:filename_all(), [module()]) ->
  293. true | ?PRV_ERROR({missing_module, module()}).
  294. has_all_beams(EbinDir, [Module | ModuleList]) ->
  295. BeamFile = filename:join([EbinDir, rebar_utils:to_list(Module) ++ ".beam"]),
  296. case filelib:is_file(BeamFile) of
  297. true ->
  298. has_all_beams(EbinDir, ModuleList);
  299. false ->
  300. ?PRV_ERROR({missing_module, Module})
  301. end;
  302. has_all_beams(_, []) ->
  303. true.