diff options
author | Juhani Rankimies <juhani@juranki.com> | 2010-10-26 07:19:30 +0300 |
---|---|---|
committer | Dave Smith <dizzyd@dizzyd.com> | 2010-10-27 11:39:12 -0600 |
commit | fd5ebe69a4d4028908247f6d669eb38254613269 (patch) | |
tree | f963d76d95a46fda14d6d491ecdc14cabcdc06c1 | |
parent | 75fc2378bf006b5660ce2115561d36bea10e04c5 (diff) |
Port rebar_file_utils to Windows
Modify rm_rf and cp_r to work when {win32,_} = os:type().
Simplify rm_rf to only accept one filename, directoryname or wildcard.
Add unit tests to ensure a similar behaviour on windows and unix.
Thanks to tuncer for guidance and feedback.
-rw-r--r-- | src/rebar_erlc_compiler.erl | 6 | ||||
-rw-r--r-- | src/rebar_file_utils.erl | 78 | ||||
-rw-r--r-- | src/rebar_reltool.erl | 2 | ||||
-rw-r--r-- | test/rebar_file_utils_tests.erl | 265 |
4 files changed, 341 insertions, 10 deletions
diff --git a/src/rebar_erlc_compiler.erl b/src/rebar_erlc_compiler.erl index 50e890c..e872e2b 100644 --- a/src/rebar_erlc_compiler.erl +++ b/src/rebar_erlc_compiler.erl @@ -71,10 +71,8 @@ compile(Config, _AppFile) -> -spec clean(Config::#config{}, AppFile::string()) -> 'ok'. clean(_Config, _AppFile) -> - %% TODO: This would be more portable if it used Erlang to traverse - %% the dir structure and delete each file; however it would also - %% much slower. - ok = rebar_file_utils:rm_rf("ebin/*.beam priv/mibs/*.bin"), + lists:foreach(fun(F) -> ok = rebar_file_utils:rm_rf(F) end, + ["ebin/*.beam", "priv/mibs/*.bin"]), YrlFiles = rebar_utils:find_files("src", "^.*\\.[x|y]rl\$"), rebar_file_utils:delete_each( diff --git a/src/rebar_file_utils.erl b/src/rebar_file_utils.erl index 429f478..1538fa8 100644 --- a/src/rebar_file_utils.erl +++ b/src/rebar_file_utils.erl @@ -36,14 +36,30 @@ %% Public API %% =================================================================== +%% @doc Remove files and directories. +%% Target is a single filename, directoryname or wildcard expression. +%% @spec rm_rf(string()) -> ok +-spec rm_rf(Target::string()) -> ok. rm_rf(Target) -> - [] = os:cmd(?FMT("rm -rf ~s", [Target])), - ok. + case os:type() of + {unix,_} -> + [] = os:cmd(?FMT("rm -rf ~s", [Target])), + ok; + {win32,_} -> + ok = rm_rf_win32(Target) + end. +-spec cp_r(Sources::list(string()), Dest::string()) -> ok. cp_r(Sources, Dest) -> - SourceStr = string:join(Sources, " "), - [] = os:cmd(?FMT("cp -R ~s ~s", [SourceStr, Dest])), - ok. + case os:type() of + {unix,_} -> + SourceStr = string:join(Sources, " "), + [] = os:cmd(?FMT("cp -R ~s ~s", [SourceStr, Dest])), + ok; + {win32,_} -> + lists:foreach(fun(Src) -> ok = cp_r_win32(Src,Dest) end, Sources), + ok + end. delete_each([]) -> ok; @@ -58,3 +74,55 @@ delete_each([File | Rest]) -> ?FAIL end. +%% =================================================================== +%% 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)])), + 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 + %% 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]))} + end. + +cp_r_win32({true,SourceDir},{true,DestDir}) -> + % from directory to directory + SourceBase = filename:basename(SourceDir), + ok = case file:make_dir(filename:join(DestDir,SourceBase)) of + {error,eexist} -> ok; + Other -> Other + end, + ok = xcopy_win32(SourceDir,filename:join(DestDir,SourceBase)); +cp_r_win32({false,Source},{true,DestDir}) -> + % from file to directory + cp_r_win32({false,Source}, + {false,filename:join(DestDir,filename:basename(Source))}); +cp_r_win32({false,Source},{false,Dest}) -> + % from file to file + {ok,_} = file:copy(Source,Dest), + ok; +cp_r_win32(Source,Dest) -> + Dst = {filelib:is_dir(Dest),Dest}, + lists:foreach(fun(Src) -> + ok = cp_r_win32({filelib:is_dir(Src),Src},Dst) + end, filelib:wildcard(Source)), + ok. diff --git a/src/rebar_reltool.erl b/src/rebar_reltool.erl index 024870e..b8e1095 100644 --- a/src/rebar_reltool.erl +++ b/src/rebar_reltool.erl @@ -281,7 +281,7 @@ execute_overlay([{copy, In, Out} | Rest], Vars, BaseDir, TargetDir) -> false -> ok = filelib:ensure_dir(OutFile) end, - rebar_utils:sh(?FMT("cp -R ~p ~p", [InFile, OutFile]), []), + rebar_file_utils:cp_r([InFile], OutFile), execute_overlay(Rest, Vars, BaseDir, TargetDir); execute_overlay([{template, In, Out} | Rest], Vars, BaseDir, TargetDir) -> InFile = render(filename:join(BaseDir, In), Vars), diff --git a/test/rebar_file_utils_tests.erl b/test/rebar_file_utils_tests.erl new file mode 100644 index 0000000..6b87986 --- /dev/null +++ b/test/rebar_file_utils_tests.erl @@ -0,0 +1,265 @@ +%% -*- tab-width: 4;erlang-indent-level: 4;indent-tabs-mode: nil -*- +%% ex: ts=4 sw=4 et +%% ------------------------------------------------------------------- +%% +%% rebar: Erlang Build Tools +%% +%% Copyright (c) 2009, 2010 Dave Smith (dizzyd@dizzyd.com) +%% +%% Permission is hereby granted, free of charge, to any person obtaining a copy +%% of this software and associated documentation files (the "Software"), to deal +%% in the Software without restriction, including without limitation the rights +%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +%% copies of the Software, and to permit persons to whom the Software is +%% furnished to do so, subject to the following conditions: +%% +%% The above copyright notice and this permission notice shall be included in +%% all copies or substantial portions of the Software. +%% +%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +%% THE SOFTWARE. +%% ------------------------------------------------------------------- +%% @author Juhani Rankimies <juhani@juranki.com> +%% @doc Tests functionality of rebar_file_utils module. +%% @copyright 2009, 2010 Dave Smith +%% ------------------------------------------------------------------- +-module(rebar_file_utils_tests). + +-compile(export_all). + +-include_lib("eunit/include/eunit.hrl"). + +-define(TMP_DIR, "tmp_file_utils"). +-define(DIR_TREE, [{d,"source",[{f,"file1"}, + {f,"file2"}]}, + {d,"dest",[]}]). +-define(FILE_CONTENT, <<"1234567890">>). + +%% ==================================================================== +%% delete_each tests +%% ==================================================================== + +delete_bogus_test_() -> + {"delete_each survives nonexisting files", + [?_assertMatch(ok, rebar_file_utils:delete_each(["bogus"])), + ?_assertMatch(ok, rebar_file_utils:delete_each(["bogus1","bogus2"]))]}. + +delete_each_test_() -> + {"delete_each removes files", + setup, + fun() -> + setup(), + rebar_file_utils:delete_each(file_list()) + end, + fun teardown/1, + [assert_files_not_in("source", file_list())]}. + +%% ==================================================================== +%% rm_rf tests +%% ==================================================================== + +rm_rf_wildcard_test_() -> + {"rm_rf removes files based on wildcard spec", + setup, + fun() -> + setup(), + rebar_file_utils:rm_rf(filename:join([?TMP_DIR,"source","file*"])) + end, + fun teardown/1, + [assert_files_not_in("source", file_list())]}. + +rm_rf_dir_test_() -> + {"rm_rf removes directory tree", + setup, + fun() -> + setup(), + rebar_file_utils:rm_rf(filename:join([?TMP_DIR,"source"])) + end, + fun teardown/1, + [?_assertNot(filelib:is_dir(filename:join([?TMP_DIR,"source"])))]}. + +%% ==================================================================== +%% cp_r tests +%% ==================================================================== + +cp_r_file_to_file_test_() -> + {"cp_r copies a file to file", + setup, + fun() -> + setup(), + rebar_file_utils:cp_r([filename:join([?TMP_DIR,"source","file1"])], + filename:join([?TMP_DIR,"dest","new_file"])) + end, + fun teardown/1, + [?_assert(filelib:is_file(filename:join([?TMP_DIR,"dest","new_file"])))]}. + +cp_r_file_to_dir_test_() -> + {"cp_r copies a file to directory", + setup, + fun() -> + setup(), + rebar_file_utils:cp_r([filename:join([?TMP_DIR,"source","file1"])], + filename:join([?TMP_DIR,"dest"])) + end, + fun teardown/1, + [?_assert(filelib:is_file(filename:join([?TMP_DIR,"dest","file1"])))]}. + +cp_r_dir_to_dir_test_() -> + {"cp_r copies a directory to directory", + setup, + fun() -> + setup(), + rebar_file_utils:cp_r([filename:join([?TMP_DIR,"source"])], + filename:join([?TMP_DIR,"dest"])) + end, + fun teardown/1, + [?_assert(filelib:is_dir(filename:join([?TMP_DIR,"dest","source"]))), + assert_files_in("dest/source", + [filename:join([?TMP_DIR,"dest","source",F]) || + F <- ["file1","file2"]])]}. + +cp_r_wildcard_file_to_dir_test_() -> + {"cp_r copies wildcard files to directory", + setup, + fun() -> + setup(), + rebar_file_utils:cp_r([filename:join([?TMP_DIR,"source","*1"])], + filename:join([?TMP_DIR,"dest"])) + end, + fun teardown/1, + [?_assert(filelib:is_file(filename:join([?TMP_DIR,"dest","file1"])))]}. + +cp_r_wildcard_dir_to_dir_test_() -> + {"cp_r copies wildcard directory to directory", + setup, + fun() -> + setup(), + rebar_file_utils:cp_r([filename:join([?TMP_DIR,"sour*"])], + filename:join([?TMP_DIR,"dest"])) + end, + fun teardown/1, + [?_assert(filelib:is_dir(filename:join([?TMP_DIR,"dest","source"]))), + assert_files_in("dest/source", + [filename:join([?TMP_DIR,"dest","source",F]) || + F <- ["file1","file2"]])]}. + +cp_r_overwrite_file_test_() -> + {"cp_r overwrites destination file", + setup, + fun() -> + setup(), + ok = file:write_file(filename:join([?TMP_DIR,"dest","file1"]), + <<"test">>), + rebar_file_utils:cp_r([filename:join([?TMP_DIR,"source","file1"])], + filename:join([?TMP_DIR,"dest"])) + end, + fun teardown/1, + [?_assertMatch({ok,?FILE_CONTENT}, + file:read_file( + filename:join([?TMP_DIR,"dest","file1"])))]}. + +cp_r_overwrite_dir_test_() -> + {"cp_r overwrites destination file (xcopy case on win32)", + setup, + fun() -> + setup(), + ok = file:make_dir(filename:join([?TMP_DIR,"dest","source"])), + ok = file:write_file( + filename:join([?TMP_DIR,"dest","source","file1"]), + <<"test">>), + rebar_file_utils:cp_r([filename:join([?TMP_DIR,"source"])], + filename:join([?TMP_DIR,"dest"])) + end, + fun teardown/1, + [?_assertMatch({ok,?FILE_CONTENT}, + file:read_file( + filename:join([?TMP_DIR,"dest","source","file1"])))]}. + +cp_r_overwrite_file_fail_test_() -> + {"cp_r fails to fs permission errors (file:copy/2 case on win32)", + setup, + fun() -> + setup(), + ok = file:write_file( + filename:join([?TMP_DIR,"dest","file1"]),<<"test">>), + ok = file:change_mode( + filename:join([?TMP_DIR,"dest","file1"]),0) + end, + fun teardown/1, + [?_assertError({badmatch,_}, + rebar_file_utils:cp_r( + [filename:join([?TMP_DIR,"source","file1"])], + filename:join([?TMP_DIR,"dest"])))]}. + +cp_r_overwrite_dir_fail_test_() -> + {"cp_r fails to fs permission error (xcopy case on win32)", + setup, + fun() -> + setup(), + ok = file:make_dir( + filename:join([?TMP_DIR,"dest","source"])), + ok = file:write_file( + filename:join([?TMP_DIR,"dest","source","file1"]), + <<"test">>), + ok = file:change_mode( + filename:join([?TMP_DIR,"dest","source","file1"]),0) + end, + fun teardown/1, + [?_assertError({badmatch,_}, + rebar_file_utils:cp_r( + [filename:join([?TMP_DIR,"source"])], + filename:join([?TMP_DIR,"dest"])))]}. + +%% ==================================================================== +%% Utilities +%% ==================================================================== + +file_list() -> + [filename:join([?TMP_DIR,"source",F]) || F <- ["file1","file2"]]. + +%% ==================================================================== +%% Setup and Teardown +%% ==================================================================== + +setup() -> + file:make_dir(?TMP_DIR), + make_dir_tree(?TMP_DIR,?DIR_TREE). + +make_dir_tree(Parent, [{d,Dir,Contents} | Rest]) -> + NewDir = filename:join(Parent,Dir), + ok = file:make_dir(NewDir), + ok = make_dir_tree(NewDir,Contents), + ok = make_dir_tree(Parent,Rest); +make_dir_tree(Parent, [{f,File} | Rest]) -> + ok = file:write_file(filename:join(Parent,File),?FILE_CONTENT), + ok = make_dir_tree(Parent,Rest); +make_dir_tree(_,[]) -> + ok. + +teardown(_) -> + case os:type() of + {unix, _} -> + os:cmd("rm -rf " ++ ?TMP_DIR ++ " 2>/dev/null"); + {win32, _} -> + os:cmd("rmdir /S /Q " ++ filename:nativename(?TMP_DIR)) + end. + +%% ==================================================================== +%% Assert helpers +%% ==================================================================== + +assert_files_in(Name, [File|T]) -> + [{Name ++ " has file: " ++ File, ?_assert(filelib:is_file(File))} | + assert_files_in(Name, T)]; +assert_files_in(_, []) -> []. + +assert_files_not_in(Name, [File|T]) -> + [{Name ++ " does not have file: " ++ File, + ?_assertNot(filelib:is_file(File))} | + assert_files_not_in(Name, T)]; +assert_files_not_in(_, []) -> []. |