Merge pull request #10390 from chrisroberts/e-powerup-handle-spaces

Update powerup to handle spaces properly in arguments
This commit is contained in:
Chris Roberts 2018-11-12 15:41:20 -08:00 committed by GitHub
commit 90dee00809
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 23 additions and 25 deletions

View File

@ -1,3 +1,4 @@
require "base64"
require "tmpdir"
require_relative "subprocess"
require_relative "which"
@ -208,18 +209,26 @@ module Vagrant
# @return [Array<String>]
def self.powerup_command(path, args, opts)
Dir.mktmpdir("vagrant") do |dpath|
all_args = ["-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", path] + args
arg_list = "@('" + all_args.join("', '") + "')"
all_args = [path] + args.flatten.map{ |a|
a.gsub(/^['"](.+)['"]$/, "\\1")
}
arg_list = "\"" + all_args.join("\" \"") + "\""
stdout = File.join(dpath, "stdout.txt")
stderr = File.join(dpath, "stderr.txt")
exitcode = File.join(dpath, "exitcode.txt")
script = "$sp = Start-Process -FilePath powershell -ArgumentList #{arg_list} " \
"-PassThru -Wait -RedirectStandardOutput '#{stdout}' -RedirectStandardError '#{stderr}' -WindowStyle Hidden; " \
"if($sp){ Set-Content -Path '#{exitcode}' -Value $sp.ExitCode;exit $sp.ExitCode; }else{ exit 1 }"
script = "& #{arg_list} ; exit $LASTEXITCODE;"
script_content = Base64.urlsafe_encode64(script.encode("UTF-16LE", "UTF-8"))
# escape quotes so we can nest our script within a start-process
script.gsub!("'", "''")
# Wrap so we can redirect output to read later
wrapper = "$p = Start-Process -FilePath powershell -ArgumentList @('-NoLogo', '-NoProfile', " \
"'-NonInteractive', '-ExecutionPolicy', 'Bypass', '-EncodedCommand', '#{script_content}') " \
"-PassThru -WindowStyle Hidden -Wait -RedirectStandardOutput '#{stdout}' -RedirectStandardError '#{stderr}'; " \
"if($p){ exit $p.ExitCode; }else{ exit 1 }"
wrapper_content = Base64.urlsafe_encode64(wrapper.encode("UTF-16LE", "UTF-8"))
powerup = "$p = Start-Process -FilePath powershell -ArgumentList @('-NoLogo', '-NoProfile', " \
"'-NonInteractive', '-ExecutionPolicy', 'Bypass', '-EncodedCommand', '#{wrapper_content}') " \
"-PassThru -WindowStyle Hidden -Wait -Verb RunAs; if($p){ exit $p.ExitCode; }else{ exit 1 }"
cmd = [
"powershell",
@ -227,31 +236,20 @@ module Vagrant
"-NoProfile",
"-NonInteractive",
"-ExecutionPolicy", "Bypass",
"-Command", "$p = Start-Process -FilePath powershell -ArgumentList " \
"@('-NoLogo', '-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass', '-Command', '#{script}') " \
"-PassThru -Wait -WindowStyle Hidden -Verb RunAs; if($p){ exit $p.ExitCode; }else{ exit 1 }"
"-Command", powerup
]
result = Subprocess.execute(*cmd.push(opts))
r_stdout = result.stdout
if File.exist?(stdout)
r_stdout = File.read(stdout)
else
r_stdout = result.stdout
r_stdout += File.read(stdout)
end
r_stderr = result.stderr
if File.exist?(stderr)
r_stderr = File.read(stderr)
else
r_stderr = result.stderr
r_stderr += File.read(stderr)
end
code = 1
if File.exist?(exitcode)
code_txt = File.read(exitcode).strip
if code_txt.match(/^\d+$/)
code = code_txt.to_i
end
end
Subprocess::Result.new(code, r_stdout, r_stderr)
Subprocess::Result.new(result.exit_code, r_stdout, r_stderr)
end
end