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.

241 lines
8.6 KiB

15 years ago
15 years ago
12 years ago
15 years ago
15 years ago
12 years ago
15 years ago
15 years ago
12 years ago
14 years ago
14 years ago
14 years ago
  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_file_utils).
  28. -export([symlink_or_copy/2,
  29. rm_rf/1,
  30. cp_r/2,
  31. mv/2,
  32. delete_each/1,
  33. write_file_if_contents_differ/2,
  34. system_tmpdir/0,
  35. system_tmpdir/1,
  36. reset_dir/1]).
  37. -include("rebar.hrl").
  38. %% ===================================================================
  39. %% Public API
  40. %% ===================================================================
  41. symlink_or_copy(Source, Target) ->
  42. Link = case os:type() of
  43. {win32, _} ->
  44. Source;
  45. _ ->
  46. rebar_dir:make_relative_path(Source, Target)
  47. end,
  48. case file:make_symlink(Link, Target) of
  49. ok ->
  50. ok;
  51. {error, eexist} ->
  52. ok;
  53. {error, _} ->
  54. cp_r([Source], Target)
  55. end.
  56. %% @doc Remove files and directories.
  57. %% Target is a single filename, directoryname or wildcard expression.
  58. -spec rm_rf(string()) -> 'ok'.
  59. rm_rf(Target) ->
  60. case os:type() of
  61. {unix, _} ->
  62. EscTarget = escape_path(Target),
  63. {ok, []} = rebar_utils:sh(?FMT("rm -rf ~s", [EscTarget]),
  64. [{use_stdout, false}, abort_on_error]),
  65. ok;
  66. {win32, _} ->
  67. Filelist = filelib:wildcard(Target),
  68. Dirs = [F || F <- Filelist, filelib:is_dir(F)],
  69. Files = Filelist -- Dirs,
  70. ok = delete_each(Files),
  71. ok = delete_each_dir_win32(Dirs),
  72. ok
  73. end.
  74. -spec cp_r(list(string()), file:filename()) -> 'ok'.
  75. cp_r([], _Dest) ->
  76. ok;
  77. cp_r(Sources, Dest) ->
  78. case os:type() of
  79. {unix, _} ->
  80. EscSources = [escape_path(Src) || Src <- Sources],
  81. SourceStr = string:join(EscSources, " "),
  82. {ok, []} = rebar_utils:sh(?FMT("cp -R ~s \"~s\"",
  83. [SourceStr, Dest]),
  84. [{use_stdout, false}, abort_on_error]),
  85. ok;
  86. {win32, _} ->
  87. lists:foreach(fun(Src) -> ok = cp_r_win32(Src,Dest) end, Sources),
  88. ok
  89. end.
  90. -spec mv(string(), file:filename()) -> 'ok'.
  91. mv(Source, Dest) ->
  92. case os:type() of
  93. {unix, _} ->
  94. EscSource = escape_path(Source),
  95. EscDest = escape_path(Dest),
  96. {ok, []} = rebar_utils:sh(?FMT("mv ~s ~s", [EscSource, EscDest]),
  97. [{use_stdout, false}, abort_on_error]),
  98. ok;
  99. {win32, _} ->
  100. {ok, R} = rebar_utils:sh(
  101. ?FMT("move /y \"~s\" \"~s\" 1> nul",
  102. [filename:nativename(Source),
  103. filename:nativename(Dest)]),
  104. [{use_stdout, false}, return_on_error]),
  105. case R of
  106. [] ->
  107. ok;
  108. _ ->
  109. {error, lists:flatten(
  110. io_lib:format("Failed to move ~s to ~s~n",
  111. [Source, Dest]))}
  112. end
  113. end.
  114. delete_each([]) ->
  115. ok;
  116. delete_each([File | Rest]) ->
  117. case file:delete(File) of
  118. ok ->
  119. delete_each(Rest);
  120. {error, enoent} ->
  121. delete_each(Rest);
  122. {error, Reason} ->
  123. ?ERROR("Failed to delete file ~s: ~p\n", [File, Reason]),
  124. ?FAIL
  125. end.
  126. write_file_if_contents_differ(Filename, Bytes) ->
  127. ToWrite = iolist_to_binary(Bytes),
  128. case file:read_file(Filename) of
  129. {ok, ToWrite} ->
  130. ok;
  131. {ok, _} ->
  132. file:write_file(Filename, ToWrite);
  133. {error, _} ->
  134. file:write_file(Filename, ToWrite)
  135. end.
  136. %% returns an os appropriate tmpdir given a path
  137. -spec system_tmpdir() -> file:filename().
  138. -spec system_tmpdir(PathComponents) -> file:filename() when
  139. PathComponents :: [file:name()].
  140. system_tmpdir() -> system_tmpdir([]).
  141. system_tmpdir(PathComponents) ->
  142. Tmp = case erlang:system_info(system_architecture) of
  143. "win32" ->
  144. "./tmp";
  145. _SysArch ->
  146. "/tmp"
  147. end,
  148. filename:join([Tmp|PathComponents]).
  149. %% recursively removes a directory and then recreates the same
  150. %% directory but empty
  151. -spec reset_dir(Path) -> ok | {error, Reason} when
  152. Path :: file:name(),
  153. Reason :: file:posix().
  154. reset_dir(Path) ->
  155. %% delete the directory if it exists
  156. _ = ec_file:remove(Path, [recursive]),
  157. %% recreate the directory
  158. filelib:ensure_dir(filename:join([Path, "dummy.beam"])).
  159. %% ===================================================================
  160. %% Internal functions
  161. %% ===================================================================
  162. delete_each_dir_win32([]) -> ok;
  163. delete_each_dir_win32([Dir | Rest]) ->
  164. {ok, []} = rebar_utils:sh(?FMT("rd /q /s \"~s\"",
  165. [filename:nativename(Dir)]),
  166. [{use_stdout, false}, return_on_error]),
  167. delete_each_dir_win32(Rest).
  168. xcopy_win32(Source,Dest)->
  169. {ok, R} = rebar_utils:sh(
  170. ?FMT("xcopy \"~s\" \"~s\" /q /y /e 2> nul",
  171. [filename:nativename(Source), filename:nativename(Dest)]),
  172. [{use_stdout, false}, return_on_error]),
  173. case length(R) > 0 of
  174. %% when xcopy fails, stdout is empty and and error message is printed
  175. %% to stderr (which is redirected to nul)
  176. true -> ok;
  177. false ->
  178. {error, lists:flatten(
  179. io_lib:format("Failed to xcopy from ~s to ~s~n",
  180. [Source, Dest]))}
  181. end.
  182. cp_r_win32({true, SourceDir}, {true, DestDir}) ->
  183. %% from directory to directory
  184. SourceBase = filename:basename(SourceDir),
  185. ok = case file:make_dir(filename:join(DestDir, SourceBase)) of
  186. {error, eexist} -> ok;
  187. Other -> Other
  188. end,
  189. ok = xcopy_win32(SourceDir, filename:join(DestDir, SourceBase));
  190. cp_r_win32({false, Source} = S,{true, DestDir}) ->
  191. %% from file to directory
  192. cp_r_win32(S, {false, filename:join(DestDir, filename:basename(Source))});
  193. cp_r_win32({false, Source},{false, Dest}) ->
  194. %% from file to file
  195. {ok,_} = file:copy(Source, Dest),
  196. ok;
  197. cp_r_win32({true, SourceDir}, {false, DestDir}) ->
  198. case filelib:is_regular(DestDir) of
  199. true ->
  200. %% From directory to file? This shouldn't happen
  201. {error, lists:flatten(
  202. io_lib:format("Cannot copy dir (~p) to file (~p)\n",
  203. [SourceDir, DestDir]))};
  204. false ->
  205. %% Specifying a target directory that doesn't currently exist.
  206. %% So let's attempt to create this directory
  207. case filelib:ensure_dir(filename:join(DestDir, "dummy")) of
  208. ok ->
  209. ok = xcopy_win32(SourceDir, DestDir);
  210. {error, Reason} ->
  211. {error, lists:flatten(
  212. io_lib:format("Unable to create dir ~p: ~p\n",
  213. [DestDir, Reason]))}
  214. end
  215. end;
  216. cp_r_win32(Source,Dest) ->
  217. Dst = {filelib:is_dir(Dest), Dest},
  218. lists:foreach(fun(Src) ->
  219. ok = cp_r_win32({filelib:is_dir(Src), Src}, Dst)
  220. end, filelib:wildcard(Source)),
  221. ok.
  222. escape_path(Str) ->
  223. re:replace(Str, "([ ()?])", "\\\\&", [global, {return, list}]).