Support command invocation on Windows without MSYS

If MSYS (with bash) is not installed on Windows then do the shell
variable substitution by ourselves. Otherwise just call bash to do the
job.
This commit is contained in:
Jan Klötzke 2011-08-08 20:12:04 +02:00 committed by Tuncer Ayaz
parent 5bb536f839
commit 7ec9b48d50
2 changed files with 33 additions and 25 deletions

View file

@ -247,7 +247,7 @@ apply_defaults(Vars, Defaults) ->
dict:merge(fun(Key, VarValue, DefaultValue) ->
case is_expandable(DefaultValue) of
true ->
expand_env_variable(DefaultValue,
rebar_utils:expand_env_variable(DefaultValue,
Key, VarValue);
false -> VarValue
end
@ -267,10 +267,10 @@ merge_each_var([{Key, Value} | Rest], Vars) ->
error ->
%% Nothing yet defined for this key/value.
%% Expand any self-references as blank.
expand_env_variable(Value, Key, "");
rebar_utils:expand_env_variable(Value, Key, "");
{ok, Value0} ->
%% Use previous definition in expansion
expand_env_variable(Value, Key, Value0)
rebar_utils:expand_env_variable(Value, Key, Value0)
end,
merge_each_var(Rest, orddict:store(Key, Evalue, Vars)).
@ -305,7 +305,7 @@ expand_vars(Key, Value, Vars) ->
Key ->
AValue;
_ ->
expand_env_variable(AValue, Key, Value)
rebar_utils:expand_env_variable(AValue, Key, Value)
end,
[{AKey, NewValue} | Acc]
end,
@ -313,8 +313,8 @@ expand_vars(Key, Value, Vars) ->
expand_command(TmplName, Env, InFiles, OutFile) ->
Cmd0 = proplists:get_value(TmplName, Env),
Cmd1 = expand_env_variable(Cmd0, "PORT_IN_FILES", InFiles),
Cmd2 = expand_env_variable(Cmd1, "PORT_OUT_FILE", OutFile),
Cmd1 = rebar_utils:expand_env_variable(Cmd0, "PORT_IN_FILES", InFiles),
Cmd2 = rebar_utils:expand_env_variable(Cmd1, "PORT_OUT_FILE", OutFile),
re:replace(Cmd2, "\\\$\\w+|\\\${\\w+}", "", [global, {return, list}]).
%%
@ -326,18 +326,6 @@ is_expandable(InStr) ->
nomatch -> false
end.
%%
%% Given env. variable FOO we want to expand all references to
%% it in InStr. References can have two forms: $FOO and ${FOO}
%% The end of form $FOO is delimited with whitespace or eol
%%
expand_env_variable(InStr, VarName, VarValue) ->
R1 = re:replace(InStr, "\\\$" ++ VarName ++ "\\s", VarValue ++ " ",
[global]),
R2 = re:replace(R1, "\\\$" ++ VarName ++ "\$", VarValue),
re:replace(R2, "\\\${" ++ VarName ++ "}", VarValue,
[global, {return, list}]).
%%
%% Filter a list of env vars such that only those which match the provided
%% architecture regex (or do not have a regex) are returned.

View file

@ -41,7 +41,8 @@
find_executable/1,
prop_check/3,
expand_code_path/0,
deprecated/5]).
deprecated/5,
expand_env_variable/3]).
-include("rebar.hrl").
@ -94,7 +95,7 @@ sh(Command0, Options0) ->
ErrorHandler = proplists:get_value(error_handler, Options),
OutputHandler = proplists:get_value(output_handler, Options),
Command = patch_on_windows(Command0),
Command = patch_on_windows(Command0, proplists:get_value(env, Options, [])),
PortSettings = proplists:get_all_values(port_settings, Options) ++
[exit_status, {line, 16384}, use_stdio, stderr_to_stdout, hide],
Port = open_port({spawn, Command}, PortSettings),
@ -106,9 +107,11 @@ sh(Command0, Options0) ->
ErrorHandler(Command, Err)
end.
%% We need a bash shell to execute on windows
%% also the port doesn't seem to close from time to time (mingw)
patch_on_windows(Cmd) ->
%% We use a bash shell to execute on windows if available. Otherwise we do the
%% shell variable substitution ourselves and hope that the command doesn't use
%% any shell magic. Also the port doesn't seem to close from time to time
%% (mingw).
patch_on_windows(Cmd, Env) ->
case os:type() of
{win32,nt} ->
case find_executable("bash") of
@ -117,7 +120,10 @@ patch_on_windows(Cmd) ->
Bash ++ " -c \"" ++ Cmd ++ "; echo _port_cmd_status_ $?\" "
end;
_ ->
Cmd
lists:foldl(fun({Key, Value}, Acc) ->
expand_env_variable(Acc, Key, Value)
end,
Cmd, Env)
end.
find_files(Dir, Regex) ->
@ -175,6 +181,20 @@ expand_code_path() ->
end, [], code:get_path()),
code:set_path(lists:reverse(CodePath)).
%%
%% Given env. variable FOO we want to expand all references to
%% it in InStr. References can have two forms: $FOO and ${FOO}
%% The end of form $FOO is delimited with whitespace or eol
%%
expand_env_variable(InStr, VarName, RawVarValue) ->
VarValue = re:replace(RawVarValue, "\\\\", "\\\\\\\\",
[global, {return, list}]),
R1 = re:replace(InStr, "\\\$" ++ VarName ++ "\\s", VarValue ++ " ",
[global]),
R2 = re:replace(R1, "\\\$" ++ VarName ++ "\$", VarValue),
re:replace(R2, "\\\${" ++ VarName ++ "}", VarValue,
[global, {return, list}]).
%% ====================================================================
%% Internal functions