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.

252 lines
9.3 KiB

15 years ago
15 years ago
  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. case Vsn of
  55. ?CONFIG_VERSION ->
  56. ok;
  57. _ ->
  58. %% Make sure the warning below is to be shown whenever a version
  59. %% newer than the current one is being used, as we can't parse
  60. %% all the contents of the lock file properly.
  61. ?WARN("Rebar3 detected a lock file from a newer version. "
  62. "It will be loaded in compatibility mode, but important "
  63. "information may be missing or lost. It is recommended to "
  64. "upgrade Rebar3.", [])
  65. end,
  66. read_attrs(Vsn, Locks, Attrs)
  67. end.
  68. write_lock_file(LockFile, Locks) ->
  69. {NewLocks, Attrs} = write_attrs(Locks),
  70. %% Write locks in the beta format, at least until it's been long
  71. %% enough we can start modifying the lock format.
  72. case Attrs of
  73. [] -> % write the old beta copy to avoid changes
  74. file:write_file(LockFile, io_lib:format("~p.~n", [NewLocks]));
  75. _ ->
  76. file:write_file(LockFile,
  77. io_lib:format("{~p,~p}.~n~p.~n",
  78. [?CONFIG_VERSION, NewLocks, Attrs]))
  79. end.
  80. read_attrs(_Vsn, Locks, Attrs) ->
  81. %% Beta copy does not know how to expand attributes, but
  82. %% is ready to support it.
  83. expand_locks(Locks, extract_pkg_hashes(Attrs)).
  84. extract_pkg_hashes(Attrs) ->
  85. Props = case Attrs of
  86. [First|_] -> First;
  87. [] -> []
  88. end,
  89. proplists:get_value(pkg_hash, Props, []).
  90. expand_locks([], _Hashes) ->
  91. [];
  92. expand_locks([{Name, {pkg,PkgName,Vsn}, Lvl} | Locks], Hashes) ->
  93. Hash = proplists:get_value(Name, Hashes),
  94. [{Name, {pkg,PkgName,Vsn,Hash}, Lvl} | expand_locks(Locks, Hashes)];
  95. expand_locks([Lock|Locks], Hashes) ->
  96. [Lock | expand_locks(Locks, Hashes)].
  97. write_attrs(Locks) ->
  98. %% No attribute known that needs to be taken out of the structure,
  99. %% just return terms as is.
  100. {NewLocks, Hashes} = split_locks(Locks, [], []),
  101. case Hashes of
  102. [] -> {NewLocks, []};
  103. _ -> {NewLocks, [{pkg_hash, lists:sort(Hashes)}]}
  104. end.
  105. split_locks([], Locks, Hashes) ->
  106. {lists:reverse(Locks), Hashes};
  107. split_locks([{Name, {pkg,PkgName,Vsn,undefined}, Lvl} | Locks], LAcc, HAcc) ->
  108. split_locks(Locks, [{Name,{pkg,PkgName,Vsn},Lvl}|LAcc], HAcc);
  109. split_locks([{Name, {pkg,PkgName,Vsn,Hash}, Lvl} | Locks], LAcc, HAcc) ->
  110. split_locks(Locks, [{Name,{pkg,PkgName,Vsn},Lvl}|LAcc], [{Name, Hash}|HAcc]);
  111. split_locks([Lock|Locks], LAcc, HAcc) ->
  112. split_locks(Locks, [Lock|LAcc], HAcc).
  113. consult_file(File) ->
  114. Terms = consult_file_(File),
  115. true = verify_config_format(Terms),
  116. Terms.
  117. -spec consult_file_(file:name()) -> [any()].
  118. consult_file_(File) when is_binary(File) ->
  119. consult_file_(binary_to_list(File));
  120. consult_file_(File) ->
  121. case filename:extension(File) of
  122. ".script" ->
  123. {ok, Terms} = consult_and_eval(remove_script_ext(File), File),
  124. Terms;
  125. _ ->
  126. Script = File ++ ".script",
  127. case filelib:is_regular(Script) of
  128. true ->
  129. {ok, Terms} = consult_and_eval(File, Script),
  130. Terms;
  131. false ->
  132. rebar_file_utils:try_consult(File)
  133. end
  134. end.
  135. verify_config_format([]) ->
  136. true;
  137. verify_config_format([{_Key, _Value} | T]) ->
  138. verify_config_format(T);
  139. verify_config_format([Term | _]) ->
  140. throw(?PRV_ERROR({bad_config_format, Term})).
  141. %% no lockfile
  142. merge_locks(Config, []) ->
  143. Config;
  144. %% lockfile with entries
  145. merge_locks(Config, Locks) ->
  146. ConfigDeps = proplists:get_value(deps, Config, []),
  147. %% We want the top level deps only from the lock file.
  148. %% This ensures deterministic overrides for configs.
  149. %% Then check if any new deps have been added to the config
  150. %% since it was locked.
  151. Deps = [X || X <- Locks, element(3, X) =:= 0],
  152. NewDeps = find_newly_added(ConfigDeps, Locks),
  153. [{{locks, default}, Locks}, {{deps, default}, NewDeps++Deps} | Config].
  154. format_error({bad_config_format, Term}) ->
  155. io_lib:format("Unable to parse config. Term is not in {Key, Value} format:~n~p", [Term]);
  156. format_error({bad_dep_name, Dep}) ->
  157. io_lib:format("Dependency name must be an atom, instead found: ~p", [Dep]).
  158. %% ===================================================================
  159. %% Internal functions
  160. %% ===================================================================
  161. -spec consult_and_eval(File::file:name_all(), Script::file:name_all()) ->
  162. {ok, Terms::[term()]} |
  163. {error, Reason::term()}.
  164. consult_and_eval(File, Script) ->
  165. ?DEBUG("Evaluating config script ~p", [Script]),
  166. StateData = rebar_file_utils:try_consult(File),
  167. %% file:consult/1 always returns the terms as a list, however file:script
  168. %% can (and will) return any kind of term(), to make consult_and_eval
  169. %% work the same way as eval we ensure that when no list is returned we
  170. %% convert it in a list.
  171. case file:script(Script, bs([{'CONFIG', StateData}, {'SCRIPT', Script}])) of
  172. {ok, Terms} when is_list(Terms) ->
  173. {ok, Terms};
  174. {ok, Term} ->
  175. {ok, [Term]};
  176. Error ->
  177. Error
  178. end.
  179. remove_script_ext(F) ->
  180. filename:rootname(F, ".script").
  181. bs(Vars) ->
  182. lists:foldl(fun({K,V}, Bs) ->
  183. erl_eval:add_binding(K, V, Bs)
  184. end, erl_eval:new_bindings(), Vars).
  185. %% Find deps that have been added to the config after the lock was created
  186. find_newly_added(ConfigDeps, LockedDeps) ->
  187. [D || {true, D} <- [check_newly_added(Dep, LockedDeps) || Dep <- ConfigDeps]].
  188. check_newly_added({_, _}=Dep, LockedDeps) ->
  189. check_newly_added_(Dep, LockedDeps);
  190. check_newly_added({_, _, {pkg, _}}=Dep, LockedDeps) ->
  191. check_newly_added_(Dep, LockedDeps);
  192. check_newly_added({Name, _, Source}, LockedDeps) ->
  193. check_newly_added_({Name, Source}, LockedDeps);
  194. check_newly_added(Dep, LockedDeps) ->
  195. check_newly_added_(Dep, LockedDeps).
  196. check_newly_added_({Name, Vsn, Source}, LockedDeps) ->
  197. case check_newly_added_(Name, LockedDeps) of
  198. {true, Name1} ->
  199. {true, {Name1, Vsn, Source}};
  200. false ->
  201. false
  202. end;
  203. check_newly_added_({Name, Source}, LockedDeps) ->
  204. case check_newly_added_(Name, LockedDeps) of
  205. {true, Name1} ->
  206. {true, {Name1, Source}};
  207. false ->
  208. false
  209. end;
  210. check_newly_added_(Dep, LockedDeps) when is_atom(Dep) ->
  211. Name = ec_cnv:to_binary(Dep),
  212. case lists:keyfind(Name, 1, LockedDeps) of
  213. false ->
  214. {true, Name};
  215. Match ->
  216. case element(3, Match) of
  217. 0 ->
  218. {true, Name};
  219. _ ->
  220. ?WARN("Newly added dep ~s is locked at a lower level. "
  221. "If you really want to unlock it, use 'rebar3 upgrade ~s'",
  222. [Name, Name]),
  223. false
  224. end
  225. end;
  226. check_newly_added_(Dep, _) ->
  227. throw(?PRV_ERROR({bad_dep_name, Dep})).