6
\$\begingroup\$

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.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Here's what I've been able to come up with so far. It could probably be improved further.

function Pack {
    param(
        [Parameter(Mandatory=$true)]
        [string] $nuspec,

        [string] $OutputDirectory,

        [string] $BasePath,

        [ValidateSet("normal", "quiet", "detailed", IgnoreCase = $true)]
        [string] $Verbosity,

        [switch] $Symbols 
    )

    # these parameters are passed as name - value pairs
    $named = @('OutputDirectory', 'BasePath', 'Verbosity') 

    # these parameters are passed as switches
    $keyed = @('Symbols')

    $p = $PSBoundParameters
    Write-Log "Creating package..." -ForegroundColor Green
    $output = Invoke-Command {
            .\NuGet.exe "pack" $nuspec ($p.Keys | foreach { 
                If ($named -contains $_) { "-$_", $p[$_] } 
                ElseIf ($keyed -contains $_) { "-$_" } 
            })
        } -ErrorVariable error

    Write-Log $output
    Write-Log $error Error
    Write-Log " "

    $global:ExitCode = $LastExitCode
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.