Unify executable invocation

Add flags to rebar_utils:sh to control output and error handling.
Replace calls to os:cmd with calls to rebar_utils:sh.
This commit is contained in:
Juhani Rankimies 2010-12-05 02:07:12 +02:00 committed by Tuncer Ayaz
parent f7eb0a7158
commit 52ca7795fe
11 changed files with 199 additions and 96 deletions

View file

@ -71,7 +71,7 @@ run_test(TestDir, Config, _File) ->
Output = " 2>&1 | tee -a " ++ RawLog Output = " 2>&1 | tee -a " ++ RawLog
end, end,
rebar_utils:sh(Cmd ++ Output, [{"TESTDIR", TestDir}]), rebar_utils:sh(Cmd ++ Output, [{env,[{"TESTDIR", TestDir}]}]),
check_log(RawLog). check_log(RawLog).
@ -89,7 +89,9 @@ clear_log(RawLog) ->
%% calling ct with erl does not return non-zero on failure - have to check %% calling ct with erl does not return non-zero on failure - have to check
%% log results %% log results
check_log(RawLog) -> check_log(RawLog) ->
Msg = os:cmd("grep -e 'TEST COMPLETE' -e '{error,make_failed}' " ++ RawLog), {ok, Msg} =
rebar_utils:sh("grep -e 'TEST COMPLETE' -e '{error,make_failed}' "
++ RawLog, [{use_stdout, false}]),
MakeFailed = string:str(Msg, "{error,make_failed}") =/= 0, MakeFailed = string:str(Msg, "{error,make_failed}") =/= 0,
RunFailed = string:str(Msg, ", 0 failed") =:= 0, RunFailed = string:str(Msg, ", 0 failed") =:= 0,
if if

View file

@ -292,28 +292,31 @@ use_source(Dep, Count) ->
download_source(AppDir, {hg, Url, Rev}) -> download_source(AppDir, {hg, Url, Rev}) ->
ok = filelib:ensure_dir(AppDir), ok = filelib:ensure_dir(AppDir),
rebar_utils:sh(?FMT("hg clone -U ~s ~s", [Url, filename:basename(AppDir)]), [], filename:dirname(AppDir)), rebar_utils:sh(?FMT("hg clone -U ~s ~s", [Url, filename:basename(AppDir)]),
rebar_utils:sh(?FMT("hg update ~s", [Rev]), [], AppDir); [{cd, filename:dirname(AppDir)}]),
rebar_utils:sh(?FMT("hg update ~s", [Rev]), [{cd, AppDir}]);
download_source(AppDir, {git, Url, {branch, Branch}}) -> download_source(AppDir, {git, Url, {branch, Branch}}) ->
ok = filelib:ensure_dir(AppDir), ok = filelib:ensure_dir(AppDir),
rebar_utils:sh(?FMT("git clone -n ~s ~s", [Url, filename:basename(AppDir)]), [], filename:dirname(AppDir)), rebar_utils:sh(?FMT("git clone -n ~s ~s", [Url, filename:basename(AppDir)]),
rebar_utils:sh(?FMT("git checkout -q origin/~s", [Branch]), [], AppDir); [{cd, filename:dirname(AppDir)}]),
rebar_utils:sh(?FMT("git checkout -q origin/~s", [Branch]), [{cd, AppDir}]);
download_source(AppDir, {git, Url, {tag, Tag}}) -> download_source(AppDir, {git, Url, {tag, Tag}}) ->
ok = filelib:ensure_dir(AppDir), ok = filelib:ensure_dir(AppDir),
rebar_utils:sh(?FMT("git clone -n ~s ~s", [Url, filename:basename(AppDir)]), [], filename:dirname(AppDir)), rebar_utils:sh(?FMT("git clone -n ~s ~s", [Url, filename:basename(AppDir)]),
rebar_utils:sh(?FMT("git checkout -q ~s", [Tag]), [], AppDir); [{cd, filename:dirname(AppDir)}]),
rebar_utils:sh(?FMT("git checkout -q ~s", [Tag]), [{cd, AppDir}]);
download_source(AppDir, {git, Url, Rev}) -> download_source(AppDir, {git, Url, Rev}) ->
download_source(AppDir, {git, Url, {branch, Rev}}); download_source(AppDir, {git, Url, {branch, Rev}});
download_source(AppDir, {bzr, Url, Rev}) -> download_source(AppDir, {bzr, Url, Rev}) ->
ok = filelib:ensure_dir(AppDir), ok = filelib:ensure_dir(AppDir),
rebar_utils:sh(?FMT("bzr branch -r ~s ~s ~s", rebar_utils:sh(?FMT("bzr branch -r ~s ~s ~s",
[Rev, Url, filename:basename(AppDir)]), [], [Rev, Url, filename:basename(AppDir)]),
filename:dirname(AppDir)); [{cd, filename:dirname(AppDir)}]);
download_source(AppDir, {svn, Url, Rev}) -> download_source(AppDir, {svn, Url, Rev}) ->
ok = filelib:ensure_dir(AppDir), ok = filelib:ensure_dir(AppDir),
rebar_utils:sh(?FMT("svn checkout -r ~s ~s ~s", rebar_utils:sh(?FMT("svn checkout -r ~s ~s ~s",
[Rev, Url, filename:basename(AppDir)]), [], [Rev, Url, filename:basename(AppDir)]),
filename:dirname(AppDir)). [{cd, filename:dirname(AppDir)}]).
update_source(Dep) -> update_source(Dep) ->
%% It's possible when updating a source, that a given dep does not have a %% It's possible when updating a source, that a given dep does not have a
@ -333,19 +336,19 @@ update_source(Dep) ->
end. end.
update_source(AppDir, {git, _Url, {branch, Branch}}) -> update_source(AppDir, {git, _Url, {branch, Branch}}) ->
rebar_utils:sh(?FMT("git fetch origin", []), [], AppDir), rebar_utils:sh("git fetch origin", [{cd, AppDir}]),
rebar_utils:sh(?FMT("git checkout -q origin/~s", [Branch]), [], AppDir); rebar_utils:sh(?FMT("git checkout -q origin/~s", [Branch]), [{cd, AppDir}]);
update_source(AppDir, {git, _Url, {tag, Tag}}) -> update_source(AppDir, {git, _Url, {tag, Tag}}) ->
rebar_utils:sh(?FMT("git fetch --tags origin", []), [], AppDir), rebar_utils:sh("git fetch --tags origin", [{cd, AppDir}]),
rebar_utils:sh(?FMT("git checkout -q ~s", [Tag]), [], AppDir); rebar_utils:sh(?FMT("git checkout -q ~s", [Tag]), [{cd, AppDir}]);
update_source(AppDir, {git, Url, Refspec}) -> update_source(AppDir, {git, Url, Refspec}) ->
update_source(AppDir, {git, Url, {branch, Refspec}}); update_source(AppDir, {git, Url, {branch, Refspec}});
update_source(AppDir, {svn, _Url, Rev}) -> update_source(AppDir, {svn, _Url, Rev}) ->
rebar_utils:sh(?FMT("svn up -r ~s", [Rev]), [], AppDir); rebar_utils:sh(?FMT("svn up -r ~s", [Rev]), [{cd, AppDir}]);
update_source(AppDir, {hg, _Url, Rev}) -> update_source(AppDir, {hg, _Url, Rev}) ->
rebar_utils:sh(?FMT("hg pull -u -r ~s", [Rev]), [], AppDir); rebar_utils:sh(?FMT("hg pull -u -r ~s", [Rev]), [{cd, AppDir}]);
update_source(AppDir, {bzr, _Url, Rev}) -> update_source(AppDir, {bzr, _Url, Rev}) ->
rebar_utils:sh(?FMT("bzr update -r ~s", [Rev]), [], AppDir). rebar_utils:sh(?FMT("bzr update -r ~s", [Rev]), [{cd, AppDir}]).
@ -366,7 +369,8 @@ source_engine_avail({Name, _, _}=Source)
scm_client_vsn(false, _VsnArg, _VsnRegex) -> scm_client_vsn(false, _VsnArg, _VsnRegex) ->
false; false;
scm_client_vsn(Path, VsnArg, VsnRegex) -> scm_client_vsn(Path, VsnArg, VsnRegex) ->
Info = os:cmd("LANG=C " ++ Path ++ VsnArg), {ok, Info} = rebar_utils:sh(Path ++ VsnArg, [{env, [{"LANG", "C"}]},
{use_stdout, false}]),
case re:run(Info, VsnRegex, [{capture, all_but_first, list}]) of case re:run(Info, VsnRegex, [{capture, all_but_first, list}]) of
{match, Match} -> {match, Match} ->
list_to_tuple([list_to_integer(S) || S <- Match]); list_to_tuple([list_to_integer(S) || S <- Match]);

View file

@ -162,11 +162,16 @@ referenced_dtls1(Step, Config, Seen) ->
DtlOpts = erlydtl_opts(Config), DtlOpts = erlydtl_opts(Config),
ExtMatch = re:replace(option(source_ext, DtlOpts), "\.", "\\\\\\\\.", ExtMatch = re:replace(option(source_ext, DtlOpts), "\.", "\\\\\\\\.",
[{return, list}]), [{return, list}]),
AllRefs = lists:append( AllRefs =
[string:tokens( lists:append(
os:cmd(["grep -o [^\\\"]*",ExtMatch," ",F]), lists:map(
"\n") fun(F) ->
|| F <- Step]), {ok, Res} = rebar_utils:sh(
lists:flatten(["grep -o [^\\\"]*",
ExtMatch," ",F]),
[{use_stdout, false}]),
string:tokens(Res, "\n")
end, Step)),
DocRoot = option(doc_root, DtlOpts), DocRoot = option(doc_root, DtlOpts),
WithPaths = [ filename:join([DocRoot, F]) || F <- AllRefs ], WithPaths = [ filename:join([DocRoot, F]) || F <- AllRefs ],
Existing = [F || F <- WithPaths, filelib:is_file(F)], Existing = [F || F <- WithPaths, filelib:is_file(F)],
@ -176,4 +181,3 @@ referenced_dtls1(Step, Config, Seen) ->
_ -> referenced_dtls1(sets:to_list(New), Config, _ -> referenced_dtls1(sets:to_list(New), Config,
sets:union(New, Seen)) sets:union(New, Seen))
end. end.

View file

@ -28,6 +28,7 @@
-export([rm_rf/1, -export([rm_rf/1,
cp_r/2, cp_r/2,
mv/2,
delete_each/1]). delete_each/1]).
-include("rebar.hrl"). -include("rebar.hrl").
@ -42,25 +43,54 @@
-spec rm_rf(Target::string()) -> ok. -spec rm_rf(Target::string()) -> ok.
rm_rf(Target) -> rm_rf(Target) ->
case os:type() of case os:type() of
{unix,_} -> {unix, _} ->
[] = os:cmd(?FMT("rm -rf ~s", [Target])), {ok, []} = rebar_utils:sh(?FMT("rm -rf ~s", [Target]),
[{use_stdout, false}, return_on_error]),
ok; ok;
{win32,_} -> {win32, _} ->
ok = 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
end. end.
-spec cp_r(Sources::list(string()), Dest::string()) -> ok. -spec cp_r(Sources::list(string()), Dest::string()) -> ok.
cp_r(Sources, Dest) -> cp_r(Sources, Dest) ->
case os:type() of case os:type() of
{unix,_} -> {unix, _} ->
SourceStr = string:join(Sources, " "), 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; ok;
{win32,_} -> {win32, _} ->
lists:foreach(fun(Src) -> ok = cp_r_win32(Src,Dest) end, Sources), lists:foreach(fun(Src) -> ok = cp_r_win32(Src,Dest) end, Sources),
ok ok
end. 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([]) -> delete_each([]) ->
ok; ok;
delete_each([File | Rest]) -> delete_each([File | Rest]) ->
@ -78,30 +108,26 @@ delete_each([File | Rest]) ->
%% Internal functions %% 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([]) -> ok;
delete_each_dir_win32([Dir | Rest]) -> 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). delete_each_dir_win32(Rest).
xcopy_win32(Source,Dest)-> xcopy_win32(Source,Dest)->
R = os:cmd(?FMT("xcopy ~s ~s /q /y /e 2> nul", {ok, R} = rebar_utils:sh(
[filename:nativename(Source), filename:nativename(Dest)])), ?FMT("cmd /c xcopy ~s ~s /q /y /e 2> nul",
case string:str(R,"\r\n") > 0 of [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 %% when xcopy fails, stdout is empty and and error message is printed
%% to stderr (which is redirected to nul) %% to stderr (which is redirected to nul)
true -> ok; true -> ok;
false -> false ->
{error, lists:flatten( {error, lists:flatten(
io_lib:format("Failed to xcopy from ~s to ~s\n", io_lib:format("Failed to xcopy from ~s to ~s~n",
[Source, Dest]))} [Source, Dest]))}
end. end.
cp_r_win32({true,SourceDir},{true,DestDir}) -> cp_r_win32({true,SourceDir},{true,DestDir}) ->

View file

@ -121,11 +121,16 @@ referenced_pegs1(Step, Config, Seen) ->
NeoOpts = neotoma_opts(Config), NeoOpts = neotoma_opts(Config),
ExtMatch = re:replace(option(source_ext, NeoOpts), "\.", "\\\\\\\\.", ExtMatch = re:replace(option(source_ext, NeoOpts), "\.", "\\\\\\\\.",
[{return, list}]), [{return, list}]),
AllRefs = lists:append( AllRefs =
[string:tokens( lists:append(
os:cmd(["grep -o [^\\\"]*",ExtMatch," ",F]), lists:map(
"\n") fun(F) ->
|| F <- Step]), {ok, Res} = rebar_utils:sh(
lists:flatten(["grep -o [^\\\"]*",
ExtMatch," ",F]),
[{use_stdout, false}]),
string:tokens(Res, "\n")
end, Step)),
DocRoot = option(doc_root, NeoOpts), DocRoot = option(doc_root, NeoOpts),
WithPaths = [ filename:join([DocRoot, F]) || F <- AllRefs ], WithPaths = [ filename:join([DocRoot, F]) || F <- AllRefs ],
Existing = [F || F <- WithPaths, filelib:is_file(F)], Existing = [F || F <- WithPaths, filelib:is_file(F)],

View file

@ -89,7 +89,7 @@ compile(Config, AppFile) ->
%% One or more files are available for building. Run the pre-compile hook, if %% One or more files are available for building. Run the pre-compile hook, if
%% necessary. %% necessary.
run_precompile_hook(Config, Env), ok = run_precompile_hook(Config, Env),
%% Compile each of the sources %% Compile each of the sources
{NewBins, ExistingBins} = compile_each(Sources, Config, Env, [], []), {NewBins, ExistingBins} = compile_each(Sources, Config, Env, [], []),
@ -103,8 +103,9 @@ compile(Config, AppFile) ->
lists:foreach(fun({SoName,Bins}) -> lists:foreach(fun({SoName,Bins}) ->
case needs_link(SoName, sets:to_list(sets:intersection([sets:from_list(Bins),sets:from_list(NewBins)]))) of case needs_link(SoName, sets:to_list(sets:intersection([sets:from_list(Bins),sets:from_list(NewBins)]))) of
true -> true ->
rebar_utils:sh_failfast(?FMT("$CC ~s $LDFLAGS $DRV_LDFLAGS -o ~s", rebar_utils:sh(?FMT("$CC ~s $LDFLAGS $DRV_LDFLAGS -o ~s",
[string:join(Bins, " "), SoName]), Env); [string:join(Bins, " "), SoName]),
[{env, Env}]);
false -> false ->
?INFO("Skipping relink of ~s\n", [SoName]), ?INFO("Skipping relink of ~s\n", [SoName]),
ok ok
@ -148,7 +149,8 @@ run_precompile_hook(Config, Env) ->
case filelib:is_regular(BypassFileName) of case filelib:is_regular(BypassFileName) of
false -> false ->
?CONSOLE("Running ~s\n", [Script]), ?CONSOLE("Running ~s\n", [Script]),
rebar_utils:sh_failfast(Script, Env); {ok, _} = rebar_utils:sh(Script, [{env, Env}]),
ok;
true -> true ->
?INFO("~s exists; not running ~s\n", [BypassFileName, Script]) ?INFO("~s exists; not running ~s\n", [BypassFileName, Script])
end end
@ -160,7 +162,8 @@ run_cleanup_hook(Config) ->
ok; ok;
Script -> Script ->
?CONSOLE("Running ~s\n", [Script]), ?CONSOLE("Running ~s\n", [Script]),
rebar_utils:sh_failfast(Script, []) {ok, _} = rebar_utils:sh(Script, []),
ok
end. end.
@ -174,11 +177,12 @@ compile_each([Source | Rest], Config, Env, NewBins, ExistingBins) ->
?CONSOLE("Compiling ~s\n", [Source]), ?CONSOLE("Compiling ~s\n", [Source]),
case compiler(Ext) of case compiler(Ext) of
"$CC" -> "$CC" ->
rebar_utils:sh_failfast(?FMT("$CC -c $CFLAGS $DRV_CFLAGS ~s -o ~s", rebar_utils:sh(?FMT("$CC -c $CFLAGS $DRV_CFLAGS ~s -o ~s",
[Source, Bin]), Env); [Source, Bin]), [{env, Env}]);
"$CXX" -> "$CXX" ->
rebar_utils:sh_failfast(?FMT("$CXX -c $CXXFLAGS $DRV_CFLAGS ~s -o ~s", rebar_utils:sh(
[Source, Bin]), Env) ?FMT("$CXX -c $CXXFLAGS $DRV_CFLAGS ~s -o ~s",
[Source, Bin]), [{env, Env}])
end, end,
compile_each(Rest, Config, Env, [Bin | NewBins], ExistingBins); compile_each(Rest, Config, Env, [Bin | NewBins], ExistingBins);

View file

@ -51,5 +51,6 @@ execute_post_script(Config, Key) ->
undefined -> undefined ->
ok; ok;
Script -> Script ->
rebar_utils:sh(Script, []) {ok, _} = rebar_utils:sh(Script, []),
ok
end. end.

View file

@ -51,5 +51,6 @@ execute_pre_script(Config, Key) ->
undefined -> undefined ->
ok; ok;
Script -> Script ->
rebar_utils:sh(Script, []) {ok, _} = rebar_utils:sh(Script, []),
ok
end. end.

View file

@ -104,9 +104,9 @@ compile_each([{Proto, Beam, Hrl} | Rest]) ->
%% into the ebin/ and include/ directories respectively %% into the ebin/ and include/ directories respectively
%% TODO: Protobuffs really needs to be better about this...sigh. %% TODO: Protobuffs really needs to be better about this...sigh.
ok = filelib:ensure_dir(filename:join("ebin","dummy")), ok = filelib:ensure_dir(filename:join("ebin","dummy")),
[] = os:cmd(?FMT("mv ~s ebin", [Beam])), ok = rebar_file_utils:mv(Beam, "ebin"),
ok = filelib:ensure_dir(filename:join("include", Hrl)), ok = filelib:ensure_dir(filename:join("include", Hrl)),
[] = os:cmd(?FMT("mv ~s include", [Hrl])), ok = rebar_file_utils:mv(Hrl, "include"),
ok; ok;
Other -> Other ->
?ERROR("Protobuff compile of ~s failed: ~p\n", [Proto, Other]), ?ERROR("Protobuff compile of ~s failed: ~p\n", [Proto, Other]),

View file

@ -30,8 +30,7 @@
is_arch/1, is_arch/1,
get_arch/0, get_arch/0,
get_os/0, get_os/0,
sh/2, sh/3, sh/2,
sh_failfast/2,
find_files/2, find_files/2,
now_str/0, now_str/0,
ensure_dir/1, ensure_dir/1,
@ -73,36 +72,49 @@ get_os() ->
ArchAtom ArchAtom
end. end.
%%
%% Options = [Option] -- defaults to [use_stdout, abort_on_error]
%% Option = ErrorOption | OutputOption | {cd, string()} | {env, Env}
%% ErrorOption = return_on_error | abort_on_error | {abort_on_error, string()}
%% OutputOption = use_stdout | {use_stdout, bool()}
%% Env = [{string(), Val}]
%% Val = string() | false
%%
sh(Command0, Options0) ->
?INFO("sh: ~s\n~p\n", [Command0, Options0]),
sh(Command, Env) -> DefaultOptions = [use_stdout, abort_on_error],
sh(Command, Env, get_cwd()). Options = lists:map(fun expand_sh_flag/1,
proplists:compact(Options0 ++ DefaultOptions)),
sh(Command0, Env, Dir) -> ErrorHandler = proplists:get_value(error_handler, Options),
?INFO("sh: ~s\n~p\n", [Command0, Env]), OutputHandler = proplists:get_value(output_handler, Options),
Command = patch_on_windows(Command0, os:type()),
Port = open_port({spawn, Command}, [{cd, Dir}, {env, Env}, exit_status, {line, 16384}, Command = patch_on_windows(Command0),
use_stdio, stderr_to_stdout]), PortSettings = proplists:get_all_values(port_settings, Options) ++
case sh_loop(Port) of [exit_status, {line, 16384}, use_stdio, stderr_to_stdout, hide],
ok -> Port = open_port({spawn, Command}, PortSettings),
ok;
case sh_loop(Port, OutputHandler, []) of
{ok, Output} ->
{ok, Output};
{error, Rc} -> {error, Rc} ->
?ABORT("~s failed with error: ~w\n", [Command, Rc]) ErrorHandler(Command, Rc)
end. end.
%% We need a bash shell to execute on windows %% We need a bash shell to execute on windows
%% also the port doesn't seem to close from time to time (mingw) %% also the port doesn't seem to close from time to time (mingw)
patch_on_windows(Cmd, {win32,nt}) -> patch_on_windows(Cmd) ->
case find_executable("bash") of case os:type() of
false -> Cmd; {win32,nt} ->
Bash -> case find_executable("bash") of
Bash ++ " -c \"" ++ Cmd ++ "; echo _port_cmd_status_ $?\" " false -> Cmd;
end; Bash ->
patch_on_windows(Command, _) -> Bash ++ " -c \"" ++ Cmd ++ "; echo _port_cmd_status_ $?\" "
Command. end;
_ ->
sh_failfast(Command, Env) -> Cmd
sh(Command, Env). end.
find_files(Dir, Regex) -> find_files(Dir, Regex) ->
filelib:fold_files(Dir, Regex, true, fun(F, Acc) -> [F | Acc] end, []). filelib:fold_files(Dir, Regex, true, fun(F, Acc) -> [F | Acc] end, []).
@ -162,19 +174,51 @@ match_first([{Regex, MatchValue} | Rest], Val) ->
match_first(Rest, Val) match_first(Rest, Val)
end. end.
sh_loop(Port) -> expand_sh_flag(return_on_error) ->
{error_handler,
fun(_Command, Rc) ->
{error, Rc}
end};
expand_sh_flag({abort_on_error, Message}) ->
{error_handler,
fun(_Command, _Rc) ->
?ABORT(Message, [])
end};
expand_sh_flag(abort_on_error) ->
{error_handler,
fun(Command, Rc) ->
?ABORT("~s failed with error: ~w\n", [Command, Rc])
end};
expand_sh_flag(use_stdout) ->
{output_handler,
fun(Line, Acc) ->
?CONSOLE("~s", [Line]),
[Acc | Line]
end};
expand_sh_flag({use_stdout, false}) ->
{output_handler,
fun(Line, Acc) ->
[Acc | Line]
end};
expand_sh_flag({cd, Dir}) ->
{port_settings, {cd, Dir}};
expand_sh_flag({env, Env}) ->
{port_settings, {env, Env}}.
sh_loop(Port, Fun, Acc) ->
receive receive
{Port, {data, {_, "_port_cmd_status_ " ++ Status}}} -> {Port, {data, {_, "_port_cmd_status_ " ++ Status}}} ->
(catch erlang:port_close(Port)), % sigh () for indentation (catch erlang:port_close(Port)), % sigh () for indentation
case list_to_integer(Status) of case list_to_integer(Status) of
0 -> ok; 0 -> {ok, lists:flatten(Acc)};
Rc -> {error, Rc} Rc -> {error, Rc}
end; end;
{Port, {data, {_, Line}}} -> {Port, {data, {eol, Line}}} ->
?CONSOLE("~s\n", [Line]), sh_loop(Port, Fun, Fun(Line ++ "\n", Acc));
sh_loop(Port); {Port, {data, {noeol, Line}}} ->
sh_loop(Port, Fun, Fun(Line, Acc));
{Port, {exit_status, 0}} -> {Port, {exit_status, 0}} ->
ok; {ok, lists:flatten(Acc)};
{Port, {exit_status, Rc}} -> {Port, {exit_status, Rc}} ->
{error, Rc} {error, Rc}
end. end.

View file

@ -215,6 +215,18 @@ cp_r_overwrite_dir_fail_test_() ->
[filename:join([?TMP_DIR,"source"])], [filename:join([?TMP_DIR,"source"])],
filename:join([?TMP_DIR,"dest"])))]}. filename:join([?TMP_DIR,"dest"])))]}.
mv_file_test_() ->
{"move a file to folder",
setup,
fun() ->
setup(),
rebar_file_utils:mv(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"]))),
?_assertNot(filelib:is_file(filename:join([?TMP_DIR,"source","file1"])))]}.
%% ==================================================================== %% ====================================================================
%% Utilities %% Utilities
%% ==================================================================== %% ====================================================================