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.

238 line
8.4 KiB

15 年之前
15 年之前
15 年之前
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_rel_utils).
  28. -export([is_rel_dir/0,
  29. is_rel_dir/1,
  30. get_reltool_release_info/1,
  31. get_rel_release_info/1,
  32. get_rel_release_info/2,
  33. get_rel_apps/1,
  34. get_rel_apps/2,
  35. get_previous_release_path/1,
  36. get_rel_file_path/2,
  37. load_config/2,
  38. get_sys_tuple/1,
  39. get_target_dir/2,
  40. get_root_dir/2,
  41. get_target_parent_dir/2]).
  42. -include("rebar.hrl").
  43. is_rel_dir() ->
  44. is_rel_dir(rebar_utils:get_cwd()).
  45. is_rel_dir(Dir) ->
  46. Fname = filename:join([Dir, "reltool.config"]),
  47. Scriptname = Fname ++ ".script",
  48. Res = case filelib:is_regular(Scriptname) of
  49. true ->
  50. {true, Scriptname};
  51. false ->
  52. case filelib:is_regular(Fname) of
  53. true ->
  54. {true, Fname};
  55. false ->
  56. false
  57. end
  58. end,
  59. ?DEBUG("is_rel_dir(~s) -> ~p~n", [Dir, Res]),
  60. Res.
  61. %% Get release name and version from a reltool.config
  62. get_reltool_release_info([{sys, Config}| _]) ->
  63. {rel, Name, Ver, _} = proplists:lookup(rel, Config),
  64. {Name, Ver};
  65. get_reltool_release_info(ReltoolFile) when is_list(ReltoolFile) ->
  66. case file:consult(ReltoolFile) of
  67. {ok, ReltoolConfig} ->
  68. get_reltool_release_info(ReltoolConfig);
  69. _ ->
  70. ?ABORT("Failed to parse ~s~n", [ReltoolFile])
  71. end.
  72. %% Get release name and version from a rel file
  73. get_rel_release_info(RelFile) ->
  74. case file:consult(RelFile) of
  75. {ok, [{release, {Name, Ver}, _, _}]} ->
  76. {Name, Ver};
  77. _ ->
  78. ?ABORT("Failed to parse ~s~n", [RelFile])
  79. end.
  80. %% Get release name and version from a name and a path
  81. get_rel_release_info(Name, Path) ->
  82. RelPath = get_rel_file_path(Name, Path),
  83. get_rel_release_info(RelPath).
  84. %% Get list of apps included in a release from a rel file
  85. get_rel_apps(RelFile) ->
  86. case file:consult(RelFile) of
  87. {ok, [{release, _, _, Apps}]} ->
  88. make_proplist(Apps, []);
  89. _ ->
  90. ?ABORT("Failed to parse ~s~n", [RelFile])
  91. end.
  92. %% Get list of apps included in a release from a name and a path
  93. get_rel_apps(Name, Path) ->
  94. RelPath = get_rel_file_path(Name, Path),
  95. get_rel_apps(RelPath).
  96. %% Get rel file path from name and path
  97. get_rel_file_path(Name, Path) ->
  98. [RelFile] = filelib:wildcard(filename:join([Path, "releases", "*",
  99. Name ++ ".rel"])),
  100. RelFile.
  101. %% Get the previous release path from a global variable
  102. get_previous_release_path(Config) ->
  103. case rebar_config:get_global(Config, previous_release, false) of
  104. false ->
  105. ?ABORT("previous_release=PATH is required to "
  106. "create upgrade package~n", []);
  107. OldVerPath ->
  108. OldVerPath
  109. end.
  110. %%
  111. %% Load terms from reltool.config
  112. %%
  113. load_config(Config, ReltoolFile) ->
  114. case rebar_config:consult_file(ReltoolFile) of
  115. {ok, Terms} ->
  116. expand_version(Config, Terms, filename:dirname(ReltoolFile));
  117. Other ->
  118. ?ABORT("Failed to load expected config from ~s: ~p\n",
  119. [ReltoolFile, Other])
  120. end.
  121. %%
  122. %% Look for the {sys, [...]} tuple in the reltool.config file.
  123. %% Without this present, we can't run reltool.
  124. %%
  125. get_sys_tuple(ReltoolConfig) ->
  126. case lists:keyfind(sys, 1, ReltoolConfig) of
  127. {sys, _} = SysTuple ->
  128. SysTuple;
  129. false ->
  130. ?ABORT("Failed to find {sys, [...]} tuple in reltool.config.", [])
  131. end.
  132. %%
  133. %% Look for {target_dir, TargetDir} in the reltool config file; if none is
  134. %% found, use the name of the release as the default target directory.
  135. %%
  136. get_target_dir(Config, ReltoolConfig) ->
  137. case rebar_config:get_global(Config, target_dir, undefined) of
  138. undefined ->
  139. case lists:keyfind(target_dir, 1, ReltoolConfig) of
  140. {target_dir, TargetDir} ->
  141. filename:absname(TargetDir);
  142. false ->
  143. {sys, SysInfo} = get_sys_tuple(ReltoolConfig),
  144. case lists:keyfind(rel, 1, SysInfo) of
  145. {rel, Name, _Vsn, _Apps} ->
  146. filename:absname(Name);
  147. false ->
  148. filename:absname("target")
  149. end
  150. end;
  151. TargetDir ->
  152. filename:absname(TargetDir)
  153. end.
  154. get_target_parent_dir(Config, ReltoolConfig) ->
  155. TargetDir = get_target_dir(Config, ReltoolConfig),
  156. case lists:reverse(tl(lists:reverse(filename:split(TargetDir)))) of
  157. [] -> ".";
  158. Components -> filename:join(Components)
  159. end.
  160. %%
  161. %% Look for root_dir in sys tuple and command line; fall back to
  162. %% code:root_dir().
  163. %%
  164. get_root_dir(Config, ReltoolConfig) ->
  165. {sys, SysInfo} = get_sys_tuple(ReltoolConfig),
  166. SysRootDirTuple = lists:keyfind(root_dir, 1, SysInfo),
  167. CmdRootDir = rebar_config:get_global(Config, root_dir, undefined),
  168. case {SysRootDirTuple, CmdRootDir} of
  169. %% root_dir in sys typle and no root_dir on cmd-line
  170. {{root_dir, SysRootDir}, undefined} ->
  171. SysRootDir;
  172. %% root_dir in sys typle and also root_dir on cmd-line
  173. {{root_dir, SysRootDir}, CmdRootDir} when CmdRootDir =/= undefined ->
  174. case string:equal(SysRootDir, CmdRootDir) of
  175. true ->
  176. ok;
  177. false ->
  178. ?WARN("overriding reltool.config root_dir with "
  179. "different command line root_dir~n", [])
  180. end,
  181. CmdRootDir;
  182. %% no root_dir in sys typle and no root_dir on cmd-line
  183. {false, undefined} ->
  184. code:root_dir();
  185. %% no root_dir in sys tuple but root_dir on cmd-line
  186. {false, CmdRootDir} when CmdRootDir =/= undefined ->
  187. CmdRootDir
  188. end.
  189. %% ===================================================================
  190. %% Internal functions
  191. %% ===================================================================
  192. make_proplist([{_,_}=H|T], Acc) ->
  193. make_proplist(T, [H|Acc]);
  194. make_proplist([H|T], Acc) ->
  195. App = element(1, H),
  196. Ver = element(2, H),
  197. make_proplist(T, [{App,Ver}|Acc]);
  198. make_proplist([], Acc) ->
  199. Acc.
  200. expand_version(Config, ReltoolConfig, Dir) ->
  201. case lists:keyfind(sys, 1, ReltoolConfig) of
  202. {sys, Sys} ->
  203. {Config1, Rels} =
  204. lists:foldl(
  205. fun(Term, {C, R}) ->
  206. {C1, Rel} = expand_rel_version(C, Term, Dir),
  207. {C1, [Rel|R]}
  208. end, {Config, []}, Sys),
  209. ExpandedSys = {sys, lists:reverse(Rels)},
  210. {Config1, lists:keyreplace(sys, 1, ReltoolConfig, ExpandedSys)};
  211. _ ->
  212. {Config, ReltoolConfig}
  213. end.
  214. expand_rel_version(Config, {rel, Name, Version, Apps}, Dir) ->
  215. {NewConfig, VsnString} = rebar_utils:vcs_vsn(Config, Version, Dir),
  216. {NewConfig, {rel, Name, VsnString, Apps}};
  217. expand_rel_version(Config, Other, _Dir) ->
  218. {Config, Other}.