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.

223 lines
10 KiB

преди 14 години
преди 15 години
преди 10 години
преди 15 години
преди 14 години
преди 13 години
преди 14 години
преди 9 години
преди 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. -spec find(binary(), [rebar_app_info:t()]) -> {ok, rebar_app_info:t()} | error.
  44. find(Name, Apps) ->
  45. ec_lists:find(fun(App) -> rebar_app_info:name(App) =:= Name end, Apps).
  46. -spec find(binary(), binary(), [rebar_app_info:t()]) -> {ok, rebar_app_info:t()} | error.
  47. find(Name, Vsn, Apps) ->
  48. ec_lists:find(fun(App) ->
  49. rebar_app_info:name(App) =:= Name
  50. andalso rebar_app_info:original_vsn(App) =:= Vsn
  51. end, Apps).
  52. is_app_src(Filename) ->
  53. %% If removing the extension .app.src yields a shorter name,
  54. %% this is an .app.src file.
  55. Filename =/= filename:rootname(Filename, ".app.src").
  56. app_src_to_app(OutDir, Filename) ->
  57. AppFile =
  58. case lists:suffix(".app.src", Filename) of
  59. true ->
  60. filename:join([OutDir, "ebin", filename:basename(Filename, ".app.src") ++ ".app"]);
  61. false ->
  62. filename:join([OutDir, "ebin", filename:basename(Filename,
  63. ".app.src.script") ++ ".app"])
  64. end,
  65. filelib:ensure_dir(AppFile),
  66. AppFile.
  67. -spec validate_application_info(rebar_app_info:t()) -> boolean().
  68. validate_application_info(AppInfo) ->
  69. validate_application_info(AppInfo, rebar_app_info:app_details(AppInfo)).
  70. validate_application_info(AppInfo, AppDetail) ->
  71. EbinDir = rebar_app_info:ebin_dir(AppInfo),
  72. case rebar_app_info:app_file(AppInfo) of
  73. undefined ->
  74. false;
  75. AppFile ->
  76. case proplists:get_value(modules, AppDetail) of
  77. undefined ->
  78. ?PRV_ERROR({module_list, AppFile});
  79. List ->
  80. has_all_beams(EbinDir, List)
  81. end
  82. end.
  83. -spec parse_deps(binary(), list(), rebar_state:t(), list(), integer()) -> [rebar_app_info:t()].
  84. parse_deps(DepsDir, Deps, State, Locks, Level) ->
  85. parse_deps(root, DepsDir, Deps, State, Locks, Level).
  86. parse_deps(Parent, DepsDir, Deps, State, Locks, Level) ->
  87. [parse_dep(Dep, Parent, DepsDir, State, Locks, Level) || Dep <- Deps].
  88. parse_dep(Dep, Parent, DepsDir, State, Locks, Level) ->
  89. Name = case Dep of
  90. Dep when is_tuple(Dep) ->
  91. element(1, Dep);
  92. Dep ->
  93. Dep
  94. end,
  95. case lists:keyfind(ec_cnv:to_binary(Name), 1, Locks) of
  96. false ->
  97. parse_dep(Parent, Dep, DepsDir, false, State);
  98. LockedDep ->
  99. LockedLevel = element(3, LockedDep),
  100. case LockedLevel > Level of
  101. true ->
  102. parse_dep(Parent, Dep, DepsDir, false, State);
  103. false ->
  104. parse_dep(Parent, LockedDep, DepsDir, true, State)
  105. end
  106. end.
  107. parse_dep(Parent, {Name, Vsn, {pkg, PkgName}}, DepsDir, IsLock, State) ->
  108. {PkgName1, PkgVsn} = {ec_cnv:to_binary(PkgName), ec_cnv:to_binary(Vsn)},
  109. dep_to_app(Parent, DepsDir, Name, PkgVsn, {pkg, PkgName1, PkgVsn}, IsLock, State);
  110. parse_dep(Parent, {Name, {pkg, PkgName}}, DepsDir, IsLock, State) ->
  111. %% Package dependency with different package name from app name
  112. dep_to_app(Parent, DepsDir, Name, undefined, {pkg, ec_cnv:to_binary(PkgName), undefined}, IsLock, State);
  113. parse_dep(Parent, {Name, Vsn}, DepsDir, IsLock, State) when is_list(Vsn); is_binary(Vsn) ->
  114. %% Versioned Package dependency
  115. {PkgName, PkgVsn} = {ec_cnv:to_binary(Name), ec_cnv:to_binary(Vsn)},
  116. dep_to_app(Parent, DepsDir, PkgName, PkgVsn, {pkg, PkgName, PkgVsn}, IsLock, State);
  117. parse_dep(Parent, Name, DepsDir, IsLock, State) when is_atom(Name); is_binary(Name) ->
  118. %% Unversioned package dependency
  119. dep_to_app(Parent, DepsDir, ec_cnv:to_binary(Name), undefined, {pkg, ec_cnv:to_binary(Name), undefined}, IsLock, State);
  120. parse_dep(Parent, {Name, Source}, DepsDir, IsLock, State) when is_tuple(Source) ->
  121. dep_to_app(Parent, DepsDir, Name, [], Source, IsLock, State);
  122. parse_dep(Parent, {Name, _Vsn, Source}, DepsDir, IsLock, State) when is_tuple(Source) ->
  123. dep_to_app(Parent, DepsDir, Name, [], Source, IsLock, State);
  124. parse_dep(Parent, {Name, _Vsn, Source, Opts}, DepsDir, IsLock, State) when is_tuple(Source) ->
  125. ?WARN("Dependency option list ~p in ~p is not supported and will be ignored", [Opts, Name]),
  126. dep_to_app(Parent, DepsDir, Name, [], Source, IsLock, State);
  127. parse_dep(Parent, {Name, {pkg, PkgName, Vsn}, Level}, DepsDir, IsLock, State) when is_integer(Level) ->
  128. dep_to_app(Parent, DepsDir, Name, Vsn, {pkg, PkgName, Vsn}, IsLock, State);
  129. parse_dep(Parent, {Name, Source, Level}, DepsDir, IsLock, State) when is_tuple(Source)
  130. , is_integer(Level) ->
  131. dep_to_app(Parent, DepsDir, Name, [], Source, IsLock, State);
  132. parse_dep(_, Dep, _, _, _) ->
  133. throw(?PRV_ERROR({parse_dep, Dep})).
  134. dep_to_app(Parent, DepsDir, Name, Vsn, Source, IsLock, State) ->
  135. CheckoutsDir = ec_cnv:to_list(rebar_dir:checkouts_dir(State, Name)),
  136. AppInfo = case rebar_app_info:discover(CheckoutsDir) of
  137. {ok, App} ->
  138. rebar_app_info:source(rebar_app_info:is_checkout(App, true), checkout);
  139. not_found ->
  140. Dir = ec_cnv:to_list(filename:join(DepsDir, Name)),
  141. {ok, AppInfo0} =
  142. case rebar_app_info:discover(Dir) of
  143. {ok, App} ->
  144. {ok, rebar_app_info:parent(App, Parent)};
  145. not_found ->
  146. rebar_app_info:new(Parent, Name, Vsn, Dir, [])
  147. end,
  148. update_source(AppInfo0, Source, State)
  149. end,
  150. C = rebar_config:consult(rebar_app_info:dir(AppInfo)),
  151. AppInfo1 = rebar_app_info:update_opts(AppInfo, rebar_app_info:opts(AppInfo), C),
  152. Overrides = rebar_state:get(State, overrides, []),
  153. AppInfo2 = rebar_app_info:set(AppInfo1, overrides, rebar_app_info:get(AppInfo, overrides, [])++Overrides),
  154. AppInfo3 = rebar_app_info:apply_overrides(rebar_app_info:get(AppInfo2, overrides, []), AppInfo2),
  155. AppInfo4 = rebar_app_info:apply_profiles(AppInfo3, [default, prod]),
  156. AppInfo5 = rebar_app_info:profiles(AppInfo4, [default]),
  157. rebar_app_info:is_lock(AppInfo5, IsLock).
  158. update_source(AppInfo, {pkg, PkgName, PkgVsn}, State) ->
  159. {PkgName1, PkgVsn1} = case PkgVsn of
  160. undefined ->
  161. get_package(PkgName, "0", State);
  162. <<"~>", Vsn/binary>> ->
  163. [Vsn1] = binary:split(Vsn, [<<" ">>], [trim_all, global]),
  164. get_package(PkgName, Vsn1, State);
  165. _ ->
  166. {PkgName, PkgVsn}
  167. end,
  168. AppInfo1 = rebar_app_info:source(AppInfo, {pkg, PkgName1, PkgVsn1}),
  169. Deps = rebar_packages:deps(PkgName1
  170. ,PkgVsn1
  171. ,State),
  172. AppInfo2 = rebar_app_info:resource_type(rebar_app_info:deps(AppInfo1, Deps), pkg),
  173. rebar_app_info:original_vsn(AppInfo2, PkgVsn1);
  174. update_source(AppInfo, Source, _State) ->
  175. rebar_app_info:source(AppInfo, Source).
  176. format_error({missing_package, Package}) ->
  177. io_lib:format("Package not found in registry: ~s", [Package]);
  178. format_error({parse_dep, Dep}) ->
  179. io_lib:format("Failed parsing dep ~p", [Dep]);
  180. format_error(Error) ->
  181. io_lib:format("~p", [Error]).
  182. %% ===================================================================
  183. %% Internal functions
  184. %% ===================================================================
  185. get_package(Dep, Vsn, State) ->
  186. case rebar_packages:find_highest_matching(Dep, Vsn, ?PACKAGE_TABLE, State) of
  187. {ok, HighestDepVsn} ->
  188. {Dep, HighestDepVsn};
  189. none ->
  190. throw(?PRV_ERROR({missing_package, ec_cnv:to_binary(Dep)}))
  191. end.
  192. -spec has_all_beams(file:filename_all(), [module()]) ->
  193. true | ?PRV_ERROR({missing_module, module()}).
  194. has_all_beams(EbinDir, [Module | ModuleList]) ->
  195. BeamFile = filename:join([EbinDir, ec_cnv:to_list(Module) ++ ".beam"]),
  196. case filelib:is_file(BeamFile) of
  197. true ->
  198. has_all_beams(EbinDir, ModuleList);
  199. false ->
  200. ?PRV_ERROR({missing_module, Module})
  201. end;
  202. has_all_beams(_, []) ->
  203. true.