diff options
author | Daniel Widgren <[email protected]> | 2015-06-11 14:44:44 +0200 |
---|---|---|
committer | Daniel Widgren <[email protected]> | 2015-06-17 21:37:57 +0200 |
commit | 04a945375a63542811d3af7bd14bc7d5c5b303d9 (patch) | |
tree | 9af1c78a7fbcf6b549192a850d52be2c3c9bf19a /src/rlx_util.erl | |
parent | d7d18d2f88ea9a6597f6d45bc110e2f247b4ef57 (diff) | |
download | relx-04a945375a63542811d3af7bd14bc7d5c5b303d9.tar.gz relx-04a945375a63542811d3af7bd14bc7d5c5b303d9.tar.bz2 relx-04a945375a63542811d3af7bd14bc7d5c5b303d9.zip |
Fixing test for windows, changing symlink to symlink_or_copy
Diffstat (limited to 'src/rlx_util.erl')
-rw-r--r-- | src/rlx_util.erl | 85 |
1 files changed, 84 insertions, 1 deletions
diff --git a/src/rlx_util.erl b/src/rlx_util.erl index 8a54ae6..db448e8 100644 --- a/src/rlx_util.erl +++ b/src/rlx_util.erl @@ -37,7 +37,8 @@ render/2, load_file/3, template_files/0, - escript_foldl/3]). + escript_foldl/3, + symlink_or_copy/2]). -define(ONE_LEVEL_INDENT, " "). %%============================================================================ @@ -212,6 +213,88 @@ escript_foldl(Fun, Acc, File) -> Error end. +symlink_or_copy(Source, Target) -> + case file:make_symlink(Source, Target) of + ok -> + ok; + {error, eexist} -> + ok; + {error, _} -> + case os:type() of + {win32, _} -> + win32_symlink(Source, Target); + _ -> + case filelib:is_dir(Target) of + true -> ok; + false -> + cp_r([Source], Target) + end + end + end. + + +win32_symlink(Source, Target) -> + os:cmd("cmd /c mklink /j " ++ Target ++ " " ++ Source), + ok. + +-spec cp_r(list(string()), file:filename()) -> 'ok'. +cp_r([], _Dest) -> + ok; +cp_r(Sources, Dest) -> + case os:type() of + {unix, _} -> + ok; + {win32, _} -> + lists:foreach(fun(Src) -> ok = cp_r_win32(Src,Dest) end, Sources), + ok + end. + +xcopy_win32(Source,Dest)-> + %% "xcopy \"~s\" \"~s\" /q /y /e 2> nul", Chanegd to robocopy to + %% handle long names. May have issues with older windows. + os:cmd("robocopy " ++ Source ++ " " ++ Dest ++ " /e /is"), + ok. + +cp_r_win32({true, SourceDir}, {true, DestDir}) -> + %% from directory to directory + ok = case file:make_dir(DestDir) of + {error, eexist} -> ok; + Other -> Other + end, + ok = xcopy_win32(SourceDir, DestDir); +cp_r_win32({false, Source} = S,{true, DestDir}) -> + %% from file to directory + cp_r_win32(S, {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({true, SourceDir}, {false, DestDir}) -> + case filelib:is_regular(DestDir) of + true -> + %% From directory to file? This shouldn't happen + {error, lists:flatten( + io_lib:format("Cannot copy dir (~p) to file (~p)\n", + [SourceDir, DestDir]))}; + false -> + %% Specifying a target directory that doesn't currently exist. + %% So let's attempt to create this directory + case filelib:ensure_dir(filename:join(DestDir, "dummy")) of + ok -> + ok = xcopy_win32(SourceDir, DestDir); + {error, Reason} -> + {error, lists:flatten( + io_lib:format("Unable to create dir ~p: ~p\n", + [DestDir, Reason]))} + end + end; +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. + %%%=================================================================== %%% Test Functions %%%=================================================================== |