diff options
Diffstat (limited to 'src/rebar_file_utils.erl')
-rw-r--r-- | src/rebar_file_utils.erl | 68 |
1 files changed, 47 insertions, 21 deletions
diff --git a/src/rebar_file_utils.erl b/src/rebar_file_utils.erl index 1538fa8..0a7cd4c 100644 --- a/src/rebar_file_utils.erl +++ b/src/rebar_file_utils.erl @@ -28,6 +28,7 @@ -export([rm_rf/1, cp_r/2, + mv/2, delete_each/1]). -include("rebar.hrl"). @@ -42,25 +43,54 @@ -spec rm_rf(Target::string()) -> ok. rm_rf(Target) -> case os:type() of - {unix,_} -> - [] = os:cmd(?FMT("rm -rf ~s", [Target])), + {unix, _} -> + {ok, []} = rebar_utils:sh(?FMT("rm -rf ~s", [Target]), + [{use_stdout, false}, return_on_error]), ok; - {win32,_} -> - ok = rm_rf_win32(Target) + {win32, _} -> + Filelist = filelib:wildcard(Target), + Dirs = lists:filter(fun filelib:is_dir/1,Filelist), + Files = lists:subtract(Filelist,Dirs), + ok = delete_each(Files), + ok = delete_each_dir_win32(Dirs), + ok end. -spec cp_r(Sources::list(string()), Dest::string()) -> ok. cp_r(Sources, Dest) -> case os:type() of - {unix,_} -> + {unix, _} -> SourceStr = string:join(Sources, " "), - [] = os:cmd(?FMT("cp -R ~s ~s", [SourceStr, Dest])), + {ok, []} = rebar_utils:sh(?FMT("cp -R ~s ~s", [SourceStr, Dest]), + [{use_stdout, false}, return_on_error]), ok; - {win32,_} -> + {win32, _} -> lists:foreach(fun(Src) -> ok = cp_r_win32(Src,Dest) end, Sources), ok end. +-spec mv(Source::string(), Dest::string()) -> ok. +mv(Source, Dest) -> + case os:type() of + {unix, _} -> + {ok, []} = rebar_utils:sh(?FMT("mv ~s ~s", [Source, Dest]), + [{use_stdout, false}, return_on_error]), + ok; + {win32, _} -> + {ok, R} = rebar_utils:sh( + ?FMT("cmd " "/c move /y ~s ~s 1> nul", + [filename:nativename(Source), + filename:nativename(Dest)]), + [{use_stdout, false}, return_on_error]), + case length(R) == 0 of + true -> ok; + false -> + {error, lists:flatten( + io_lib:format("Failed to move ~s to ~s~n", + [Source, Dest]))} + end + end. + delete_each([]) -> ok; delete_each([File | Rest]) -> @@ -78,30 +108,26 @@ delete_each([File | Rest]) -> %% Internal functions %% =================================================================== -rm_rf_win32(Target) -> - Filelist = filelib:wildcard(Target), - Dirs = lists:filter(fun filelib:is_dir/1,Filelist), - Files = lists:subtract(Filelist,Dirs), - ok = delete_each(Files), - ok = delete_each_dir_win32(Dirs), - ok. - delete_each_dir_win32([]) -> ok; delete_each_dir_win32([Dir | Rest]) -> - [] = os:cmd(?FMT("rd /q /s ~s", [filename:nativename(Dir)])), + {ok, []} = rebar_utils:sh(?FMT("cmd /c rd /q /s ~s", + [filename:nativename(Dir)]), + [{use_stdout, false}, return_on_error]), delete_each_dir_win32(Rest). xcopy_win32(Source,Dest)-> - R = os:cmd(?FMT("xcopy ~s ~s /q /y /e 2> nul", - [filename:nativename(Source), filename:nativename(Dest)])), - case string:str(R,"\r\n") > 0 of + {ok, R} = rebar_utils:sh( + ?FMT("cmd /c xcopy ~s ~s /q /y /e 2> nul", + [filename:nativename(Source), filename:nativename(Dest)]), + [{use_stdout, false}, return_on_error]), + case length(R) > 0 of %% when xcopy fails, stdout is empty and and error message is printed %% to stderr (which is redirected to nul) true -> ok; false -> {error, lists:flatten( - io_lib:format("Failed to xcopy from ~s to ~s\n", - [Source, Dest]))} + io_lib:format("Failed to xcopy from ~s to ~s~n", + [Source, Dest]))} end. cp_r_win32({true,SourceDir},{true,DestDir}) -> |