From 7ec9b48d5036515b6d93b0999f6fc1911072309c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Kl=C3=B6tzke?= Date: Mon, 8 Aug 2011 20:12:04 +0200 Subject: 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. --- src/rebar_port_compiler.erl | 26 +++++++------------------- src/rebar_utils.erl | 32 ++++++++++++++++++++++++++------ 2 files changed, 33 insertions(+), 25 deletions(-) (limited to 'src') diff --git a/src/rebar_port_compiler.erl b/src/rebar_port_compiler.erl index c2430e7..f036bdf 100644 --- a/src/rebar_port_compiler.erl +++ b/src/rebar_port_compiler.erl @@ -247,8 +247,8 @@ apply_defaults(Vars, Defaults) -> dict:merge(fun(Key, VarValue, DefaultValue) -> case is_expandable(DefaultValue) of true -> - expand_env_variable(DefaultValue, - Key, VarValue); + rebar_utils:expand_env_variable(DefaultValue, + Key, VarValue); false -> VarValue end 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}]). %% @@ -327,18 +327,6 @@ is_expandable(InStr) -> 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. %% diff --git a/src/rebar_utils.erl b/src/rebar_utils.erl index 4571d17..885049c 100644 --- a/src/rebar_utils.erl +++ b/src/rebar_utils.erl @@ -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 -- cgit v1.1