Sha256: 8233e955655db8d261580eb4d60672b7b3d75883c8330ade7fab5a51f6ccb32d

Contents?: true

Size: 1.72 KB

Versions: 16

Compression:

Stored size: 1.72 KB

Contents

require 'base64'

module Serverspec
  module Backend
    module PowerShell
      module ScriptHelper
        def build_command(cmd)
          path = Serverspec.configuration.path || RSpec.configuration.path
          if path
            cmd.strip!
            cmd = 
<<-EOF
$env:path = "#{path};$env:path"
#{cmd}
EOF
          end
          cmd
        end

        def add_pre_command(cmd)
          path = Serverspec.configuration.path || RSpec.configuration.path
          if Serverspec.configuration.pre_command
            cmd.strip!
            cmd = 
<<-EOF
if (#{Serverspec.configuration.pre_command})
{
#{cmd}
}
EOF
            cmd = "$env:path = \"#{path};$env:path\"\n#{cmd}" if path
          end
          cmd
        end

        def encode_script script
          script_text = script.chars.to_a.join("\x00").chomp
          script_text << "\x00" unless script_text[-1].eql? "\x00"
          if script_text.respond_to?(:encode)
            script_text = script_text.encode('ASCII-8BIT')
          end
          if Base64.respond_to?(:strict_encode64)
            Base64.strict_encode64(script_text)
          else
            [ script_text ].pack("m").strip
          end
        end

        def create_script command
          script = build_command(command.script)
          script = add_pre_command(script)
          ps_functions = command.import_functions.map { |f| File.read(File.join(File.dirname(__FILE__), 'support', f)) }
        <<-EOF
$exitCode = 1
try {
  #{ps_functions.join("\n")}
  $success = (#{script})
  if ($success -is [Boolean] -and $success) { $exitCode = 0 }
} catch {
  Write-Output $_.Exception.Message
}
Write-Output "Exiting with code: $exitCode"
exit $exitCode
          EOF
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
serverspec-0.10.6 lib/serverspec/backend/powershell/script_helper.rb
serverspec-0.10.5 lib/serverspec/backend/powershell/script_helper.rb
serverspec-0.10.4 lib/serverspec/backend/powershell/script_helper.rb
serverspec-0.10.3 lib/serverspec/backend/powershell/script_helper.rb
serverspec-0.10.2 lib/serverspec/backend/powershell/script_helper.rb
serverspec-0.10.1 lib/serverspec/backend/powershell/script_helper.rb
serverspec-0.10.0 lib/serverspec/backend/powershell/script_helper.rb
serverspec-0.9.8 lib/serverspec/backend/powershell/script_helper.rb
serverspec-0.9.7 lib/serverspec/backend/powershell/script_helper.rb
serverspec-0.9.6 lib/serverspec/backend/powershell/script_helper.rb
serverspec-0.9.5 lib/serverspec/backend/powershell/script_helper.rb
serverspec-0.9.4 lib/serverspec/backend/powershell/script_helper.rb
serverspec-0.9.3 lib/serverspec/backend/powershell/script_helper.rb
serverspec-0.9.2 lib/serverspec/backend/powershell/script_helper.rb
serverspec-0.9.1 lib/serverspec/backend/powershell/script_helper.rb
serverspec-0.9.0 lib/serverspec/backend/powershell/script_helper.rb