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.

299 lines
9.9 KiB

пре 15 година
пре 15 година
пре 14 година
пре 15 година
пре 13 година
пре 14 година
пре 14 година
пре 13 година
пре 14 година
пре 14 година
пре 14 година
пре 14 година
пре 13 година
пре 14 година
пре 14 година
пре 13 година
пре 13 година
пре 14 година
пре 14 година
  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, 2010 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_utils).
  28. -export([get_cwd/0,
  29. is_arch/1,
  30. get_arch/0,
  31. wordsize/0,
  32. sh/2,
  33. find_files/2,
  34. find_files/3,
  35. now_str/0,
  36. ensure_dir/1,
  37. beam_to_mod/2, beams/1,
  38. erl_to_mod/1,
  39. abort/2,
  40. escript_foldl/3,
  41. find_executable/1,
  42. prop_check/3,
  43. expand_code_path/0,
  44. deprecated/5,
  45. expand_env_variable/3]).
  46. -include("rebar.hrl").
  47. %% ====================================================================
  48. %% Public API
  49. %% ====================================================================
  50. get_cwd() ->
  51. {ok, Dir} = file:get_cwd(),
  52. Dir.
  53. is_arch(ArchRegex) ->
  54. case re:run(get_arch(), ArchRegex, [{capture, none}]) of
  55. match ->
  56. true;
  57. nomatch ->
  58. false
  59. end.
  60. get_arch() ->
  61. Words = wordsize(),
  62. erlang:system_info(otp_release) ++ "-"
  63. ++ erlang:system_info(system_architecture) ++ "-" ++ Words.
  64. wordsize() ->
  65. try erlang:system_info({wordsize, external}) of
  66. Val ->
  67. integer_to_list(8 * Val)
  68. catch
  69. error:badarg ->
  70. integer_to_list(8 * erlang:system_info(wordsize))
  71. end.
  72. %%
  73. %% Options = [Option] -- defaults to [use_stdout, abort_on_error]
  74. %% Option = ErrorOption | OutputOption | {cd, string()} | {env, Env}
  75. %% ErrorOption = return_on_error | abort_on_error | {abort_on_error, string()}
  76. %% OutputOption = use_stdout | {use_stdout, bool()}
  77. %% Env = [{string(), Val}]
  78. %% Val = string() | false
  79. %%
  80. sh(Command0, Options0) ->
  81. ?INFO("sh info:\n\tcwd: ~p\n\tcmd: ~s\n\topts: ~p\n",
  82. [get_cwd(), Command0, Options0]),
  83. DefaultOptions = [use_stdout, abort_on_error],
  84. Options = [expand_sh_flag(V)
  85. || V <- proplists:compact(Options0 ++ DefaultOptions)],
  86. ErrorHandler = proplists:get_value(error_handler, Options),
  87. OutputHandler = proplists:get_value(output_handler, Options),
  88. Command = patch_on_windows(Command0, proplists:get_value(env, Options, [])),
  89. PortSettings = proplists:get_all_values(port_settings, Options) ++
  90. [exit_status, {line, 16384}, use_stdio, stderr_to_stdout, hide],
  91. Port = open_port({spawn, Command}, PortSettings),
  92. case sh_loop(Port, OutputHandler, []) of
  93. {ok, _Output} = Ok ->
  94. Ok;
  95. {error, {_Rc, _Output}=Err} ->
  96. ErrorHandler(Command, Err)
  97. end.
  98. %% We do the shell variable substitution ourselves on Windows and hope that the
  99. %% command doesn't use any other shell magic.
  100. patch_on_windows(Cmd, Env) ->
  101. case os:type() of
  102. {win32,nt} ->
  103. "cmd /q /c " ++ lists:foldl(fun({Key, Value}, Acc) ->
  104. expand_env_variable(Acc, Key, Value)
  105. end, Cmd, Env);
  106. _ ->
  107. Cmd
  108. end.
  109. find_files(Dir, Regex) ->
  110. find_files(Dir, Regex, true).
  111. find_files(Dir, Regex, Recursive) ->
  112. filelib:fold_files(Dir, Regex, Recursive,
  113. fun(F, Acc) -> [F | Acc] end, []).
  114. now_str() ->
  115. {{Year, Month, Day}, {Hour, Minute, Second}} = calendar:local_time(),
  116. lists:flatten(io_lib:format("~4b/~2..0b/~2..0b ~2..0b:~2..0b:~2..0b",
  117. [Year, Month, Day, Hour, Minute, Second])).
  118. %% TODO: filelib:ensure_dir/1 corrected in R13B04. Remove when we drop
  119. %% support for OTP releases older than R13B04.
  120. ensure_dir(Path) ->
  121. case filelib:ensure_dir(Path) of
  122. ok ->
  123. ok;
  124. {error,eexist} ->
  125. ok;
  126. Error ->
  127. Error
  128. end.
  129. -spec abort(string(), [term()]) -> no_return().
  130. abort(String, Args) ->
  131. ?ERROR(String, Args),
  132. halt(1).
  133. %% TODO: Rename emulate_escript_foldl to escript_foldl and remove
  134. %% this function when the time is right. escript:foldl/3 was an
  135. %% undocumented exported fun and has been removed in R14.
  136. escript_foldl(Fun, Acc, File) ->
  137. {module, zip} = code:ensure_loaded(zip),
  138. case erlang:function_exported(zip, foldl, 3) of
  139. true ->
  140. emulate_escript_foldl(Fun, Acc, File);
  141. false ->
  142. escript:foldl(Fun, Acc, File)
  143. end.
  144. find_executable(Name) ->
  145. case os:find_executable(Name) of
  146. false -> false;
  147. Path ->
  148. "\"" ++ filename:nativename(Path) ++ "\""
  149. end.
  150. %% Helper function for checking values and aborting when needed
  151. prop_check(true, _, _) -> true;
  152. prop_check(false, Msg, Args) -> ?ABORT(Msg, Args).
  153. %% Convert all the entries in the code path to absolute paths.
  154. expand_code_path() ->
  155. CodePath = lists:foldl(fun (Path, Acc) ->
  156. [filename:absname(Path) | Acc]
  157. end, [], code:get_path()),
  158. code:set_path(lists:reverse(CodePath)).
  159. %%
  160. %% Given env. variable FOO we want to expand all references to
  161. %% it in InStr. References can have two forms: $FOO and ${FOO}
  162. %% The end of form $FOO is delimited with whitespace or eol
  163. %%
  164. expand_env_variable(InStr, VarName, RawVarValue) ->
  165. case string:chr(InStr, $$) of
  166. 0 ->
  167. %% No variables to expand
  168. InStr;
  169. _ ->
  170. ReOpts = [global, {return, list}],
  171. VarValue = re:replace(RawVarValue, "\\\\", "\\\\\\\\", [global]),
  172. %% Use a regex to match/replace:
  173. %% $FOO\s | ${FOO} | $FOO eol
  174. RegEx = io_lib:format("\\\$(~s(\\s|$)|{~s})", [VarName, VarName]),
  175. re:replace(InStr, RegEx, VarValue ++ "\\2", ReOpts)
  176. end.
  177. %% ====================================================================
  178. %% Internal functions
  179. %% ====================================================================
  180. expand_sh_flag(return_on_error) ->
  181. {error_handler,
  182. fun(_Command, Err) ->
  183. {error, Err}
  184. end};
  185. expand_sh_flag({abort_on_error, Message}) ->
  186. {error_handler,
  187. log_msg_and_abort(Message)};
  188. expand_sh_flag(abort_on_error) ->
  189. {error_handler,
  190. fun log_and_abort/2};
  191. expand_sh_flag(use_stdout) ->
  192. {output_handler,
  193. fun(Line, Acc) ->
  194. ?CONSOLE("~s", [Line]),
  195. [Line | Acc]
  196. end};
  197. expand_sh_flag({use_stdout, false}) ->
  198. {output_handler,
  199. fun(Line, Acc) ->
  200. [Line | Acc]
  201. end};
  202. expand_sh_flag({cd, _CdArg} = Cd) ->
  203. {port_settings, Cd};
  204. expand_sh_flag({env, _EnvArg} = Env) ->
  205. {port_settings, Env}.
  206. -type err_handler() :: fun((string(), {integer(), string()}) -> no_return()).
  207. -spec log_msg_and_abort(string()) -> err_handler().
  208. log_msg_and_abort(Message) ->
  209. fun(_Command, {_Rc, _Output}) ->
  210. ?ABORT(Message, [])
  211. end.
  212. -spec log_and_abort(string(), {integer(), string()}) -> no_return().
  213. log_and_abort(Command, {Rc, Output}) ->
  214. ?ABORT("~s failed with error: ~w and output:~n~s~n",
  215. [Command, Rc, Output]).
  216. sh_loop(Port, Fun, Acc) ->
  217. receive
  218. {Port, {data, {eol, Line}}} ->
  219. sh_loop(Port, Fun, Fun(Line ++ "\n", Acc));
  220. {Port, {data, {noeol, Line}}} ->
  221. sh_loop(Port, Fun, Fun(Line, Acc));
  222. {Port, {exit_status, 0}} ->
  223. {ok, lists:flatten(lists:reverse(Acc))};
  224. {Port, {exit_status, Rc}} ->
  225. {error, {Rc, lists:flatten(lists:reverse(Acc))}}
  226. end.
  227. beam_to_mod(Dir, Filename) ->
  228. [Dir | Rest] = filename:split(Filename),
  229. list_to_atom(filename:basename(string:join(Rest, "."), ".beam")).
  230. erl_to_mod(Filename) ->
  231. list_to_atom(filename:rootname(filename:basename(Filename))).
  232. beams(Dir) ->
  233. filelib:fold_files(Dir, ".*\.beam\$", true,
  234. fun(F, Acc) -> [F | Acc] end, []).
  235. emulate_escript_foldl(Fun, Acc, File) ->
  236. case escript:extract(File, [compile_source]) of
  237. {ok, [_Shebang, _Comment, _EmuArgs, Body]} ->
  238. case Body of
  239. {source, BeamCode} ->
  240. GetInfo = fun() -> file:read_file_info(File) end,
  241. GetBin = fun() -> BeamCode end,
  242. {ok, Fun(".", GetInfo, GetBin, Acc)};
  243. {beam, BeamCode} ->
  244. GetInfo = fun() -> file:read_file_info(File) end,
  245. GetBin = fun() -> BeamCode end,
  246. {ok, Fun(".", GetInfo, GetBin, Acc)};
  247. {archive, ArchiveBin} ->
  248. zip:foldl(Fun, Acc, {File, ArchiveBin})
  249. end;
  250. {error, _} = Error ->
  251. Error
  252. end.
  253. deprecated(Key, Old, New, Opts, When) ->
  254. case lists:member(Old, Opts) of
  255. true ->
  256. io:format(
  257. <<"WARNING: deprecated ~p option used~n"
  258. "Option '~p' has been deprecated~n"
  259. "in favor of '~p'.~n"
  260. "'~p' will be removed ~s.~n~n">>,
  261. [Key, Old, New, Old, When]);
  262. false ->
  263. ok
  264. end.