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.

181 lines
5.7 KiB

15 years ago
15 years ago
15 years ago
  1. %% -*- tab-width: 4;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. get_os/0,
  32. sh/2, sh/3,
  33. sh_failfast/2,
  34. find_files/2,
  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. -include("rebar.hrl").
  42. %% ====================================================================
  43. %% Public API
  44. %% ====================================================================
  45. get_cwd() ->
  46. {ok, Dir} = file:get_cwd(),
  47. Dir.
  48. is_arch(ArchRegex) ->
  49. case re:run(get_arch(), ArchRegex, [{capture, none}]) of
  50. match ->
  51. true;
  52. nomatch ->
  53. false
  54. end.
  55. get_arch() ->
  56. Words = integer_to_list(8 * erlang:system_info(wordsize)),
  57. erlang:system_info(system_architecture) ++ "-" ++ Words.
  58. get_os() ->
  59. Arch = erlang:system_info(system_architecture),
  60. case match_first([{"linux", linux}, {"darwin", darwin}], Arch) of
  61. nomatch ->
  62. {unknown, Arch};
  63. ArchAtom ->
  64. ArchAtom
  65. end.
  66. sh(Command, Env) ->
  67. sh(Command, Env, get_cwd()).
  68. sh(Command, Env, Dir) ->
  69. ?INFO("sh: ~s\n~p\n", [Command, Env]),
  70. Port = open_port({spawn, Command}, [{cd, Dir}, {env, Env}, exit_status, {line, 16384},
  71. use_stdio, stderr_to_stdout]),
  72. case sh_loop(Port) of
  73. ok ->
  74. ok;
  75. {error, Rc} ->
  76. ?ABORT("~s failed with error: ~w\n", [Command, Rc])
  77. end.
  78. sh_failfast(Command, Env) ->
  79. sh(Command, Env).
  80. find_files(Dir, Regex) ->
  81. filelib:fold_files(Dir, Regex, true, fun(F, Acc) -> [F | Acc] end, []).
  82. now_str() ->
  83. {{Year, Month, Day}, {Hour, Minute, Second}} = calendar:local_time(),
  84. lists:flatten(io_lib:format("~4b/~2..0b/~2..0b ~2..0b:~2..0b:~2..0b",
  85. [Year, Month, Day, Hour, Minute, Second])).
  86. %% TODO: filelib:ensure_dir/1 corrected in R13B04. Can be removed.
  87. ensure_dir(Path) ->
  88. case filelib:ensure_dir(Path) of
  89. ok ->
  90. ok;
  91. {error,eexist} ->
  92. ok;
  93. Error ->
  94. Error
  95. end.
  96. abort(String, Args) ->
  97. ?ERROR(String, Args),
  98. halt(1).
  99. %% TODO: Rename emulate_escript_foldl to escript_foldl and remove
  100. %% this function when the time is right. escript:foldl/3 was an
  101. %% undocumented exported fun and is going to be removed post-R13B04.
  102. escript_foldl(Fun, Acc, File) ->
  103. {module, zip} = code:ensure_loaded(zip),
  104. case erlang:function_exported(zip, foldl, 3) of
  105. true ->
  106. emulate_escript_foldl(Fun, Acc, File);
  107. false ->
  108. escript:foldl(Fun, Acc, File)
  109. end.
  110. %% ====================================================================
  111. %% Internal functions
  112. %% ====================================================================
  113. match_first([], _Val) ->
  114. nomatch;
  115. match_first([{Regex, MatchValue} | Rest], Val) ->
  116. case re:run(Val, Regex, [{capture, none}]) of
  117. match ->
  118. MatchValue;
  119. nomatch ->
  120. match_first(Rest, Val)
  121. end.
  122. sh_loop(Port) ->
  123. receive
  124. {Port, {data, {_, Line}}} ->
  125. ?CONSOLE("~s\n", [Line]),
  126. sh_loop(Port);
  127. {Port, {exit_status, 0}} ->
  128. ok;
  129. {Port, {exit_status, Rc}} ->
  130. {error, Rc}
  131. end.
  132. beam_to_mod(Dir, Filename) ->
  133. [Dir | Rest] = filename:split(Filename),
  134. list_to_atom(filename:basename(string:join(Rest, "."), ".beam")).
  135. erl_to_mod(Filename) ->
  136. list_to_atom(filename:rootname(filename:basename(Filename))).
  137. beams(Dir) ->
  138. filelib:fold_files(Dir, ".*\.beam\$", true,
  139. fun(F, Acc) -> [F | Acc] end, []).
  140. emulate_escript_foldl(Fun, Acc, File) ->
  141. case escript:extract(File, [compile_source]) of
  142. {ok, [_Shebang, _Comment, _EmuArgs, Body]} ->
  143. case Body of
  144. {source, BeamCode} ->
  145. GetInfo = fun() -> file:read_file_info(File) end,
  146. GetBin = fun() -> BeamCode end,
  147. {ok, Fun(".", GetInfo, GetBin, Acc)};
  148. {beam, BeamCode} ->
  149. GetInfo = fun() -> file:read_file_info(File) end,
  150. GetBin = fun() -> BeamCode end,
  151. {ok, Fun(".", GetInfo, GetBin, Acc)};
  152. {archive, ArchiveBin} ->
  153. zip:foldl(Fun, Acc, {File, ArchiveBin})
  154. end;
  155. {error, Reason} ->
  156. {error, Reason}
  157. end.