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.

121 lines
4.5 KiB

преди 14 години
преди 15 години
преди 15 години
преди 10 години
преди 10 години
преди 13 години
  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_file/1
  30. ,merge_locks/2]).
  31. -include("rebar.hrl").
  32. %% ===================================================================
  33. %% Public API
  34. %% ===================================================================
  35. -spec consult(file:name()) -> [any()].
  36. consult(Dir) ->
  37. consult_file(filename:join(Dir, ?DEFAULT_CONFIG_FILE)).
  38. -spec consult_file(file:name()) -> [any()].
  39. consult_file(File) when is_binary(File) ->
  40. consult_file(binary_to_list(File));
  41. consult_file(File) ->
  42. case filename:extension(File) of
  43. ".script" ->
  44. consult_and_eval(remove_script_ext(File), File);
  45. _ ->
  46. Script = File ++ ".script",
  47. case filelib:is_regular(Script) of
  48. true ->
  49. {ok, Terms} = consult_and_eval(File, Script),
  50. Terms;
  51. false ->
  52. ?DEBUG("Consult config file ~p", [File]),
  53. try_consult(File)
  54. end
  55. end.
  56. merge_locks(Config, [[]]) ->
  57. Config;
  58. merge_locks(Config, [Locks]) ->
  59. {deps, ConfigDeps} = lists:keyfind(deps, 1, Config),
  60. %% We want the top level deps only from the lock file.
  61. %% This ensures deterministic overrides for configs.
  62. %% Then check if any new deps have been added to the config
  63. %% since it was locked.
  64. Deps = [X || X <- Locks, element(3, X) =:= 0],
  65. NewDeps = find_newly_added(ConfigDeps, Locks),
  66. [{{locks, default}, Locks}, {{deps, default}, NewDeps++Deps} | Config].
  67. %% ===================================================================
  68. %% Internal functions
  69. %% ===================================================================
  70. consult_and_eval(File, Script) ->
  71. ?DEBUG("Evaluating config script ~p", [Script]),
  72. StateData = try_consult(File),
  73. file:script(Script, bs([{'CONFIG', StateData}, {'SCRIPT', Script}])).
  74. remove_script_ext(F) ->
  75. filename:rootname(F, ".script").
  76. try_consult(File) ->
  77. case file:consult(File) of
  78. {ok, Terms} ->
  79. Terms;
  80. {error, enoent} ->
  81. [];
  82. {error, Reason} ->
  83. ?ABORT("Failed to read config file ~s:~n ~p", [File, Reason])
  84. end.
  85. bs(Vars) ->
  86. lists:foldl(fun({K,V}, Bs) ->
  87. erl_eval:add_binding(K, V, Bs)
  88. end, erl_eval:new_bindings(), Vars).
  89. %% Find deps that have been added to the config after the lock was created
  90. find_newly_added(ConfigDeps, LockedDeps) ->
  91. [Dep || Dep <- ConfigDeps,
  92. begin
  93. NewDep = ec_cnv:to_binary(element(1, Dep)),
  94. case lists:keyfind(NewDep, 1, LockedDeps) of
  95. false ->
  96. true;
  97. Match ->
  98. case element(3, Match) of
  99. 0 ->
  100. true;
  101. _ ->
  102. ?WARN("Newly added dep ~s is locked at a lower level. "
  103. "If you really want to unlock it, use 'rebar3 upgrade ~s'",
  104. [NewDep, NewDep]),
  105. false
  106. end
  107. end
  108. end].