#9298: Try to fallback to "powershell.exe" on WSL if "powershell" is not available

This commit is contained in:
Sergii Bondarenko 2018-02-13 20:06:42 +04:00 committed by Chris Roberts
parent c5fdd47a56
commit 47a7f0817c
1 changed files with 22 additions and 8 deletions

View File

@ -13,12 +13,27 @@ module Vagrant
MINIMUM_REQUIRED_VERSION = 3 MINIMUM_REQUIRED_VERSION = 3
LOGGER = Log4r::Logger.new("vagrant::util::powershell") LOGGER = Log4r::Logger.new("vagrant::util::powershell")
# @return [String|nil] a powershell executable, depending on environment
def self.executable
if !defined?(@_powershell_executable)
@_powershell_executable = "powershell"
# Try to use WSL interoperability if PowerShell is not symlinked to
# the container.
if Which.which(@_powershell_executable).nil? && Platform.wsl?
@_powershell_executable += ".exe"
if Which.which(@_powershell_executable).nil?
@_powershell_executable = nil
end
end
end
@_powershell_executable
end
# @return [Boolean] powershell executable available on PATH # @return [Boolean] powershell executable available on PATH
def self.available? def self.available?
if !defined?(@_powershell_available) !executable.nil?
@_powershell_available = !!Which.which("powershell")
end
@_powershell_available
end end
# Execute a powershell script. # Execute a powershell script.
@ -27,12 +42,11 @@ module Vagrant
# @return [Subprocess::Result] # @return [Subprocess::Result]
def self.execute(path, *args, **opts, &block) def self.execute(path, *args, **opts, &block)
validate_install! validate_install!
if opts.delete(:sudo) || opts.delete(:runas) if opts.delete(:sudo) || opts.delete(:runas)
powerup_command(path, args, opts) powerup_command(path, args, opts)
else else
command = [ command = [
"powershell", executable,
"-NoLogo", "-NoLogo",
"-NoProfile", "-NoProfile",
"-NonInteractive", "-NonInteractive",
@ -57,7 +71,7 @@ module Vagrant
def self.execute_cmd(command) def self.execute_cmd(command)
validate_install! validate_install!
c = [ c = [
"powershell", executable,
"-NoLogo", "-NoLogo",
"-NoProfile", "-NoProfile",
"-NonInteractive", "-NonInteractive",
@ -77,7 +91,7 @@ module Vagrant
def self.version def self.version
if !defined?(@_powershell_version) if !defined?(@_powershell_version)
command = [ command = [
"powershell", executable,
"-NoLogo", "-NoLogo",
"-NoProfile", "-NoProfile",
"-NonInteractive", "-NonInteractive",