Parcourir la source

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.
pull/3/head
Jan Klötzke il y a 14 ans
committed by Tuncer Ayaz
Parent
révision
7ec9b48d50
2 fichiers modifiés avec 33 ajouts et 25 suppressions
  1. +7
    -19
      src/rebar_port_compiler.erl
  2. +26
    -6
      src/rebar_utils.erl

+ 7
- 19
src/rebar_port_compiler.erl Voir le fichier

@ -247,8 +247,8 @@ apply_defaults(Vars, Defaults) ->
dict:merge(fun(Key, VarValue, DefaultValue) -> dict:merge(fun(Key, VarValue, DefaultValue) ->
case is_expandable(DefaultValue) of case is_expandable(DefaultValue) of
true -> true ->
expand_env_variable(DefaultValue,
Key, VarValue);
rebar_utils:expand_env_variable(DefaultValue,
Key, VarValue);
false -> VarValue false -> VarValue
end end
end, end,
@ -267,10 +267,10 @@ merge_each_var([{Key, Value} | Rest], Vars) ->
error -> error ->
%% Nothing yet defined for this key/value. %% Nothing yet defined for this key/value.
%% Expand any self-references as blank. %% Expand any self-references as blank.
expand_env_variable(Value, Key, "");
rebar_utils:expand_env_variable(Value, Key, "");
{ok, Value0} -> {ok, Value0} ->
%% Use previous definition in expansion %% Use previous definition in expansion
expand_env_variable(Value, Key, Value0)
rebar_utils:expand_env_variable(Value, Key, Value0)
end, end,
merge_each_var(Rest, orddict:store(Key, Evalue, Vars)). merge_each_var(Rest, orddict:store(Key, Evalue, Vars)).
@ -305,7 +305,7 @@ expand_vars(Key, Value, Vars) ->
Key -> Key ->
AValue; AValue;
_ -> _ ->
expand_env_variable(AValue, Key, Value)
rebar_utils:expand_env_variable(AValue, Key, Value)
end, end,
[{AKey, NewValue} | Acc] [{AKey, NewValue} | Acc]
end, end,
@ -313,8 +313,8 @@ expand_vars(Key, Value, Vars) ->
expand_command(TmplName, Env, InFiles, OutFile) -> expand_command(TmplName, Env, InFiles, OutFile) ->
Cmd0 = proplists:get_value(TmplName, Env), 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}]). re:replace(Cmd2, "\\\$\\w+|\\\${\\w+}", "", [global, {return, list}]).
%% %%
@ -326,18 +326,6 @@ is_expandable(InStr) ->
nomatch -> false nomatch -> false
end. 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 %% Filter a list of env vars such that only those which match the provided
%% architecture regex (or do not have a regex) are returned. %% architecture regex (or do not have a regex) are returned.

+ 26
- 6
src/rebar_utils.erl Voir le fichier

@ -41,7 +41,8 @@
find_executable/1, find_executable/1,
prop_check/3, prop_check/3,
expand_code_path/0, expand_code_path/0,
deprecated/5]).
deprecated/5,
expand_env_variable/3]).
-include("rebar.hrl"). -include("rebar.hrl").
@ -94,7 +95,7 @@ sh(Command0, Options0) ->
ErrorHandler = proplists:get_value(error_handler, Options), ErrorHandler = proplists:get_value(error_handler, Options),
OutputHandler = proplists:get_value(output_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) ++ PortSettings = proplists:get_all_values(port_settings, Options) ++
[exit_status, {line, 16384}, use_stdio, stderr_to_stdout, hide], [exit_status, {line, 16384}, use_stdio, stderr_to_stdout, hide],
Port = open_port({spawn, Command}, PortSettings), Port = open_port({spawn, Command}, PortSettings),
@ -106,9 +107,11 @@ sh(Command0, Options0) ->
ErrorHandler(Command, Err) ErrorHandler(Command, Err)
end. 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 case os:type() of
{win32,nt} -> {win32,nt} ->
case find_executable("bash") of case find_executable("bash") of
@ -117,7 +120,10 @@ patch_on_windows(Cmd) ->
Bash ++ " -c \"" ++ Cmd ++ "; echo _port_cmd_status_ $?\" " Bash ++ " -c \"" ++ Cmd ++ "; echo _port_cmd_status_ $?\" "
end; end;
_ -> _ ->
Cmd
lists:foldl(fun({Key, Value}, Acc) ->
expand_env_variable(Acc, Key, Value)
end,
Cmd, Env)
end. end.
find_files(Dir, Regex) -> find_files(Dir, Regex) ->
@ -175,6 +181,20 @@ expand_code_path() ->
end, [], code:get_path()), end, [], code:get_path()),
code:set_path(lists:reverse(CodePath)). 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 %% Internal functions

Chargement…
Annuler
Enregistrer