No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

302 líneas
11 KiB

hace 15 años
hace 15 años
  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_config).
  28. -export([consult/1
  29. ,consult_app_file/1
  30. ,consult_file/1
  31. ,consult_lock_file/1
  32. ,write_lock_file/2
  33. ,verify_config_format/1
  34. ,format_error/1
  35. ,merge_locks/2]).
  36. -include("rebar.hrl").
  37. -include_lib("providers/include/providers.hrl").
  38. %% ===================================================================
  39. %% Public API
  40. %% ===================================================================
  41. -spec consult(file:name()) -> [any()].
  42. consult(Dir) ->
  43. consult_file(filename:join(Dir, ?DEFAULT_CONFIG_FILE)).
  44. consult_app_file(File) ->
  45. consult_file_(File).
  46. consult_lock_file(File) ->
  47. Terms = consult_file_(File),
  48. case Terms of
  49. [] ->
  50. [];
  51. [Locks] when is_list(Locks) -> % beta lock file
  52. read_attrs(beta, Locks, []);
  53. [{Vsn, Locks}|Attrs] when is_list(Locks) -> % versioned lock file
  54. %% Because this is the first version of rebar3 to introduce a lock
  55. %% file, all versionned lock files with a different versions have
  56. %% to be newer.
  57. case Vsn of
  58. ?CONFIG_VERSION ->
  59. ok;
  60. _ ->
  61. %% Make sure the warning below is to be shown whenever a version
  62. %% newer than the current one is being used, as we can't parse
  63. %% all the contents of the lock file properly.
  64. warn_vsn_once()
  65. end,
  66. read_attrs(Vsn, Locks, Attrs)
  67. end.
  68. warn_vsn_once() ->
  69. Warn = application:get_env(rebar, warn_config_vsn) =/= {ok, false},
  70. application:set_env(rebar, warn_config_vsn, false),
  71. case Warn of
  72. false -> ok;
  73. true ->
  74. ?WARN("Rebar3 detected a lock file from a newer version. "
  75. "It will be loaded in compatibility mode, but important "
  76. "information may be missing or lost. It is recommended to "
  77. "upgrade Rebar3.", [])
  78. end.
  79. write_lock_file(LockFile, Locks) ->
  80. {NewLocks, Attrs} = write_attrs(Locks),
  81. %% Write locks in the beta format, at least until it's been long
  82. %% enough we can start modifying the lock format.
  83. case Attrs of
  84. [] -> % write the old beta copy to avoid changes
  85. file:write_file(LockFile, io_lib:format("~p.~n", [NewLocks]));
  86. _ ->
  87. file:write_file(LockFile,
  88. io_lib:format("{~p,~n~p}.~n[~n~s~n].~n",
  89. [?CONFIG_VERSION, NewLocks,
  90. format_attrs(Attrs)]))
  91. end.
  92. %% Attributes have a special formatting to ensure there's only one per
  93. %% line in terms of pkg_hash, so we disturb source diffing as little
  94. %% as possible.
  95. format_attrs([]) -> [];
  96. format_attrs([{pkg_hash, Vals}|T]) ->
  97. [io_lib:format("{pkg_hash,[~n",[]), format_hashes(Vals), "]}",
  98. maybe_comma(T) | format_attrs(T)];
  99. format_attrs([H|T]) ->
  100. [io_lib:format("~p~s", [H, maybe_comma(T)]) | format_attrs(T)].
  101. format_hashes([]) -> [];
  102. format_hashes([{Pkg,Hash}|T]) ->
  103. [" {", io_lib:format("~p",[Pkg]), ", ", io_lib:format("~p", [Hash]), "}",
  104. maybe_comma(T) | format_hashes(T)].
  105. maybe_comma([]) -> "";
  106. maybe_comma([_|_]) -> io_lib:format(",~n", []).
  107. read_attrs(_Vsn, Locks, Attrs) ->
  108. %% Beta copy does not know how to expand attributes, but
  109. %% is ready to support it.
  110. expand_locks(Locks, extract_pkg_hashes(Attrs)).
  111. extract_pkg_hashes(Attrs) ->
  112. Props = case Attrs of
  113. [First|_] -> First;
  114. [] -> []
  115. end,
  116. proplists:get_value(pkg_hash, Props, []).
  117. expand_locks([], _Hashes) ->
  118. [];
  119. expand_locks([{Name, {pkg,PkgName,Vsn}, Lvl} | Locks], Hashes) ->
  120. Hash = proplists:get_value(Name, Hashes),
  121. [{Name, {pkg,PkgName,Vsn,Hash}, Lvl} | expand_locks(Locks, Hashes)];
  122. expand_locks([Lock|Locks], Hashes) ->
  123. [Lock | expand_locks(Locks, Hashes)].
  124. write_attrs(Locks) ->
  125. %% No attribute known that needs to be taken out of the structure,
  126. %% just return terms as is.
  127. {NewLocks, Hashes} = split_locks(Locks, [], []),
  128. case Hashes of
  129. [] -> {NewLocks, []};
  130. _ -> {NewLocks, [{pkg_hash, lists:sort(Hashes)}]}
  131. end.
  132. split_locks([], Locks, Hashes) ->
  133. {lists:reverse(Locks), Hashes};
  134. split_locks([{Name, {pkg,PkgName,Vsn,undefined}, Lvl} | Locks], LAcc, HAcc) ->
  135. split_locks(Locks, [{Name,{pkg,PkgName,Vsn},Lvl}|LAcc], HAcc);
  136. split_locks([{Name, {pkg,PkgName,Vsn,Hash}, Lvl} | Locks], LAcc, HAcc) ->
  137. split_locks(Locks, [{Name,{pkg,PkgName,Vsn},Lvl}|LAcc], [{Name, Hash}|HAcc]);
  138. split_locks([Lock|Locks], LAcc, HAcc) ->
  139. split_locks(Locks, [Lock|LAcc], HAcc).
  140. consult_file(File) ->
  141. Terms = consult_file_(File),
  142. true = verify_config_format(Terms),
  143. Terms.
  144. -spec consult_file_(file:name()) -> [any()].
  145. consult_file_(File) when is_binary(File) ->
  146. consult_file_(binary_to_list(File));
  147. consult_file_(File) ->
  148. case filename:extension(File) of
  149. ".script" ->
  150. {ok, Terms} = consult_and_eval(remove_script_ext(File), File),
  151. Terms;
  152. _ ->
  153. Script = File ++ ".script",
  154. case filelib:is_regular(Script) of
  155. true ->
  156. {ok, Terms} = consult_and_eval(File, Script),
  157. Terms;
  158. false ->
  159. rebar_file_utils:try_consult(File)
  160. end
  161. end.
  162. verify_config_format([]) ->
  163. true;
  164. verify_config_format([{_Key, _Value} | T]) ->
  165. verify_config_format(T);
  166. verify_config_format([Term | _]) ->
  167. throw(?PRV_ERROR({bad_config_format, Term})).
  168. %% no lockfile
  169. merge_locks(Config, []) ->
  170. Config;
  171. %% lockfile with entries
  172. merge_locks(Config, Locks) ->
  173. ConfigDeps = proplists:get_value(deps, Config, []),
  174. %% We want the top level deps only from the lock file.
  175. %% This ensures deterministic overrides for configs.
  176. %% Then check if any new deps have been added to the config
  177. %% since it was locked.
  178. Deps = [X || X <- Locks, element(3, X) =:= 0],
  179. NewDeps = find_newly_added(ConfigDeps, Locks),
  180. [{{locks, default}, Locks}, {{deps, default}, NewDeps++Deps} | Config].
  181. format_error({bad_config_format, Term}) ->
  182. io_lib:format("Unable to parse config. Term is not in {Key, Value} format:~n~p", [Term]);
  183. format_error({bad_dep_name, Dep}) ->
  184. io_lib:format("Dependency name must be an atom, instead found: ~p", [Dep]).
  185. %% ===================================================================
  186. %% Internal functions
  187. %% ===================================================================
  188. -spec consult_and_eval(File::file:name_all(), Script::file:name_all()) ->
  189. {ok, Terms::[term()]} |
  190. {error, Reason::term()}.
  191. consult_and_eval(File, Script) ->
  192. ?DEBUG("Evaluating config script ~p", [Script]),
  193. StateData = rebar_file_utils:try_consult(File),
  194. %% file:consult/1 always returns the terms as a list, however file:script
  195. %% can (and will) return any kind of term(), to make consult_and_eval
  196. %% work the same way as eval we ensure that when no list is returned we
  197. %% convert it in a list.
  198. case file:script(Script, bs([{'CONFIG', StateData}, {'SCRIPT', Script}])) of
  199. {ok, Terms} when is_list(Terms) ->
  200. {ok, Terms};
  201. {ok, Term} ->
  202. {ok, [Term]};
  203. Error ->
  204. Error
  205. end.
  206. remove_script_ext(F) ->
  207. filename:rootname(F, ".script").
  208. bs(Vars) ->
  209. lists:foldl(fun({K,V}, Bs) ->
  210. erl_eval:add_binding(K, V, Bs)
  211. end, erl_eval:new_bindings(), Vars).
  212. %% Find deps that have been added to the config after the lock was created
  213. find_newly_added(ConfigDeps, LockedDeps) ->
  214. [D || {true, D} <- [check_newly_added(Dep, LockedDeps) || Dep <- ConfigDeps]].
  215. check_newly_added({_, _}=Dep, LockedDeps) ->
  216. check_newly_added_(Dep, LockedDeps);
  217. check_newly_added({_, _, {pkg, _}}=Dep, LockedDeps) ->
  218. check_newly_added_(Dep, LockedDeps);
  219. check_newly_added({Name, _, Source}, LockedDeps) ->
  220. check_newly_added_({Name, Source}, LockedDeps);
  221. check_newly_added(Dep, LockedDeps) ->
  222. check_newly_added_(Dep, LockedDeps).
  223. %% get [raw] deps out of the way
  224. check_newly_added_({Name, Source, Opts}, LockedDeps) when is_tuple(Source),
  225. is_list(Opts) ->
  226. case check_newly_added_(Name, LockedDeps) of
  227. {true, Name1} ->
  228. {true, {Name1, Source}};
  229. false ->
  230. false
  231. end;
  232. check_newly_added_({Name,_Vsn,Source,Opts}, LockedDeps) when is_tuple(Source),
  233. is_list(Opts) ->
  234. case check_newly_added_(Name, LockedDeps) of
  235. {true, Name1} ->
  236. {true, {Name1, Source}};
  237. false ->
  238. false
  239. end;
  240. %% and on to regular deps
  241. check_newly_added_({Name, Vsn, Source}, LockedDeps) ->
  242. case check_newly_added_(Name, LockedDeps) of
  243. {true, Name1} ->
  244. {true, {Name1, Vsn, Source}};
  245. false ->
  246. false
  247. end;
  248. check_newly_added_({Name, Source}, LockedDeps) ->
  249. case check_newly_added_(Name, LockedDeps) of
  250. {true, Name1} ->
  251. {true, {Name1, Source}};
  252. false ->
  253. false
  254. end;
  255. check_newly_added_(Dep, LockedDeps) when is_atom(Dep) ->
  256. Name = ec_cnv:to_binary(Dep),
  257. case lists:keyfind(Name, 1, LockedDeps) of
  258. false ->
  259. {true, Name};
  260. Match ->
  261. case element(3, Match) of
  262. 0 ->
  263. {true, Name};
  264. _ ->
  265. ?WARN("Newly added dep ~s is locked at a lower level. "
  266. "If you really want to unlock it, use 'rebar3 upgrade ~s'",
  267. [Name, Name]),
  268. false
  269. end
  270. end;
  271. check_newly_added_(Dep, _) ->
  272. throw(?PRV_ERROR({bad_dep_name, Dep})).