aboutsummaryrefslogtreecommitdiffstats
path: root/src/rlx_util.erl
diff options
context:
space:
mode:
authorTristan Sloughter <[email protected]>2015-06-21 10:22:02 -0500
committerTristan Sloughter <[email protected]>2015-06-21 10:22:02 -0500
commit4f38907d4967d672d3a2b13422cde2e13a42a604 (patch)
treefd6eb5fb93acd86c9dca01c9ffbdd029e4169b87 /src/rlx_util.erl
parent87e87154fa52f815404e25d1ef59179253f9c5bb (diff)
parent04a945375a63542811d3af7bd14bc7d5c5b303d9 (diff)
downloadrelx-4f38907d4967d672d3a2b13422cde2e13a42a604.tar.gz
relx-4f38907d4967d672d3a2b13422cde2e13a42a604.tar.bz2
relx-4f38907d4967d672d3a2b13422cde2e13a42a604.zip
Merge pull request #349 from Taure/windows-fix
Fixing test for windows, changing symlink to symlink_or_copy
Diffstat (limited to 'src/rlx_util.erl')
-rw-r--r--src/rlx_util.erl85
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
%%%===================================================================