Split out run methods with Powershell functions

This commit is contained in:
Brian Cain 2018-04-06 10:58:39 -07:00
parent 12b1a3dfe4
commit 188fd5d6a6
No known key found for this signature in database
GPG Key ID: 43D51080D357A001
2 changed files with 35 additions and 15 deletions

View File

@ -152,33 +152,32 @@ module Vagrant
# #
# @param [Provisioners::Shell::Config] config A Shell provisioner config # @param [Provisioners::Shell::Config] config A Shell provisioner config
def run(config, on_error) def run(config, on_error)
if !config.inline.nil? if config.inline
cmd = Shellwords.split(config.inline) cmd = Shellwords.split(config.inline)
if Vagrant::Util::Platform.windows?
powershell_exe = Vagrant::Util::PowerShell.executable
cmd = Shellwords.split("#{powershell_exe} #{config.powershell_args} '#{cmd.join(' ')}'")
end
@machine.ui.detail(I18n.t("vagrant.trigger.run.inline", command: config.inline)) @machine.ui.detail(I18n.t("vagrant.trigger.run.inline", command: config.inline))
else else
cmd = File.expand_path(config.path, @env.root_path) cmd = File.expand_path(config.path, @env.root_path)
cmd << " #{config.args.join(' ' )}" if config.args cmd << " #{config.args.join(' ' )}" if config.args
if Vagrant::Util::Platform.windows?
powershell_exe = Vagrant::Util::PowerShell.executable
cmd = Shellwords.split("#{powershell_exe} #{config.powershell_args} #{cmd}")
else
cmd = Shellwords.split(cmd) cmd = Shellwords.split(cmd)
end
@machine.ui.detail(I18n.t("vagrant.trigger.run.script", path: config.path)) @machine.ui.detail(I18n.t("vagrant.trigger.run.script", path: config.path))
end end
# Pick an execution method to run the script or inline string with
# Default to Subprocess::Execute
exec_method = Vagrant::Util::Subprocess.method(:execute)
if Vagrant::Util::Platform.windows?
if config.inline
exec_method = Vagrant::Util::PowerShell.method(:execute_inline)
else
exec_method = Vagrant::Util::PowerShell.method(:execute)
end
end
begin begin
result = Vagrant::Util::Subprocess.execute(*cmd, :notify => [:stdout, :stderr]) do |type,data| result = exec_method.call(*cmd, :notify => [:stdout, :stderr]) do |type,data|
options = {} options = {}
case type case type
when :stdout when :stdout

View File

@ -87,6 +87,27 @@ module Vagrant
return r.stdout.chomp return r.stdout.chomp
end end
# Execute a powershell command and return a result
#
# @param [String] command PowerShell command to execute.
# @param [Hash] opts A collection of options for subprocess::execute
# @param [Block] block Ruby block
def self.execute_inline(*command, **opts, &block)
validate_install!
c = [
executable,
"-NoLogo",
"-NoProfile",
"-NonInteractive",
"-ExecutionPolicy", "Bypass",
"-Command",
command
].flatten.compact
c << opts
Subprocess.execute(*c, &block)
end
# Returns the version of PowerShell that is installed. # Returns the version of PowerShell that is installed.
# #
# @return [String] # @return [String]