Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

109 rindas
3.4 KiB

pirms 15 gadiem
pirms 15 gadiem
  1. %% -------------------------------------------------------------------
  2. %%
  3. %% rebar: Erlang Build Tools
  4. %%
  5. %% Copyright (c) 2009 Dave Smith (dizzyd@dizzyd.com)
  6. %%
  7. %% Permission is hereby granted, free of charge, to any person obtaining a copy
  8. %% of this software and associated documentation files (the "Software"), to deal
  9. %% in the Software without restriction, including without limitation the rights
  10. %% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. %% copies of the Software, and to permit persons to whom the Software is
  12. %% furnished to do so, subject to the following conditions:
  13. %%
  14. %% The above copyright notice and this permission notice shall be included in
  15. %% all copies or substantial portions of the Software.
  16. %%
  17. %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. %% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. %% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. %% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. %% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. %% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. %% THE SOFTWARE.
  24. %% -------------------------------------------------------------------
  25. -module(rebar_utils).
  26. -export([get_cwd/0,
  27. is_arch/1,
  28. get_os/0,
  29. sh/2, sh/3,
  30. sh_failfast/2,
  31. now_str/0]).
  32. -include("rebar.hrl").
  33. %% ====================================================================
  34. %% Public API
  35. %% ====================================================================
  36. get_cwd() ->
  37. {ok, Dir} = file:get_cwd(),
  38. Dir.
  39. is_arch(ArchRegex) ->
  40. Arch = erlang:system_info(system_architecture),
  41. case re:run(Arch, ArchRegex, [{capture, none}]) of
  42. match ->
  43. true;
  44. nomatch ->
  45. false
  46. end.
  47. get_os() ->
  48. Arch = erlang:system_info(system_architecture),
  49. case match_first([{"linux", linux}, {"darwin", darwin}], Arch) of
  50. nomatch ->
  51. {unknown, Arch};
  52. ArchAtom ->
  53. ArchAtom
  54. end.
  55. sh(Command, Env) ->
  56. sh(Command, Env, get_cwd()).
  57. sh(Command, Env, Dir) ->
  58. ?INFO("sh: ~s\n~p\n", [Command, Env]),
  59. Port = open_port({spawn, Command}, [{cd, Dir}, {env, Env}, exit_status, {line, 16384},
  60. use_stdio, stderr_to_stdout]),
  61. case sh_loop(Port) of
  62. ok ->
  63. ok;
  64. {error, Rc} ->
  65. ?ABORT("~s failed with error: ~w\n", [Command, Rc])
  66. end.
  67. sh_failfast(Command, Env) ->
  68. sh(Command, Env).
  69. now_str() ->
  70. {{Year, Month, Day}, {Hour, Minute, Second}} = calendar:local_time(),
  71. lists:flatten(io_lib:format("~4b/~2..0b/~2..0b ~2..0b:~2..0b:~2..0b",
  72. [Year, Month, Day, Hour, Minute, Second])).
  73. %% ====================================================================
  74. %% Internal functions
  75. %% ====================================================================
  76. match_first([], _Val) ->
  77. nomatch;
  78. match_first([{Regex, MatchValue} | Rest], Val) ->
  79. case re:run(Val, Regex, [{capture, none}]) of
  80. match ->
  81. MatchValue;
  82. nomatch ->
  83. match_first(Rest, Val)
  84. end.
  85. sh_loop(Port) ->
  86. receive
  87. {Port, {data, {_, Line}}} ->
  88. ?CONSOLE("~s\n", [Line]),
  89. sh_loop(Port);
  90. {Port, {exit_status, 0}} ->
  91. ok;
  92. {Port, {exit_status, Rc}} ->
  93. {error, Rc}
  94. end.