My PowerShell scripting skills are pretty poor, but I've managed to hobble together got this script:
function Create-Process() {
param([string] $fileName, [string] $arguments)
$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.RedirectStandardError = $true
$pinfo.RedirectStandardOutput = $true
$pinfo.UseShellExecute = $false
$pinfo.FileName = $fileName
$pinfo.Arguments = $arguments
$p = New-Object System.Diagnostics.Process
$p.StartInfo = $pinfo
return $p
}
function Pack {
param(
[string] $nuspec,
[string] $OutputDirectory,
[string] $BasePath,
[ValidateSet("normal", "quiet", "detailed", IgnoreCase = $true)]
[string] $Verbosity,
[switch] $Symbols
)
# format parameters for NuGet.exe
If ($BasePath) {
$basePathParam = "-BasePath ""$BasePath"""
}
If ($OutputDirectory) {
$outputDirParam = "-OutputDirectory ""$BasePath"""
}
If ($Verbosity) {
$verbosityParam = "-Verbosity $Verbosity"
}
If ($Symbols) {
$symbolsParam = "-Symbols"
}
Write-Log "Creating package..." -ForegroundColor Green
$packageTask = Create-Process .\NuGet.exe ("pack $nuspec $basePathParam $outputDirParam $verbosityParam $symbolsParam")
$packageTask.Start() | Out-Null
$packageTask.WaitForExit()
$output = ($packageTask.StandardOutput.ReadToEnd() -Split '[\r\n]') |? {$_}
$error = (($packageTask.StandardError.ReadToEnd() -Split '[\r\n]') |? {$_})
Write-Log $output
Write-Log $error Error
$global:ExitCode = $packageTask.ExitCode
}
So that I can call it elsewhere like this:
Pack .\Package.nuspec -BasePath $TargetDir -OutputDirectory $TargetDir -Verbosity Detailed
How can I clean up the process for formatting the arguments string? It seems pretty ugly at the moment, and if I have to add any more parameters, it's just going to get uglier. Is there an easy way to convert a list of parameters to a string that's suitable for invoking another function?
Note: Simply passing in the parameters as a single string (e.g. Pack .\Package.nuspec "-Verbosity Detailed"
) doesn't really solve my issue, it just moves it back one step. I'd like to create the string within Pack
itself.