Public/New-NuGetPackage.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
function New-NuGetPackage
{
    <#
        .SYNOPSIS
            Builds a NuGet package.
        .DESCRIPTION
            Builds a NuGet package from Nuspec manifest.
        .PARAMETER Manifest
            A string containing the path to the Nuspec manifest.
        .PARAMETER NuGetPath
            A string containing the path to the NuGet executable.
        .PARAMETER OutputPath
            A string containing the output path for the NuGet package.
            If 'OutputDirectory' is specified in 'Parameters' the value of 'OutputPath' - if specified - will override 'OutputDirectory'.
        .PARAMETER Parameters
            An hashtable containing the additionnal parameters to specify to NuGet.
            If 'OutputDirectory' is specified in 'Parameters' the value of 'OutputPath' - if specified - will override 'OutputDirectory'.
        .INPUTS
        .OUTPUTS
            PsObject
            Returns the output that is generated by the invoked command (nuget pack).
        .EXAMPLE
            New-NuGetPackage -Manifest .\package.nuspec
 
            Description
            -----------
            This example will build the package from the manifest.
        .EXAMPLE
            New-NuGetPackage -Manifest .\package.nuspec -OutputPath D:\packages
 
            Description
            -----------
            This example will build the package from the manifest and output it to D:\packages.
        .EXAMPLE
            New-NuGetPackage -Manifest .\package.nuspec -Parameters @{ "NoDefaultExcludes" = $true ; "OutputDirectory" = "D:\packages" }
 
            Description
            -----------
            This example will build the package from the manifest, not excluding default content, and output it to D:\packages.
        .NOTES
            - This CmdLet does not stop on nuget pack error, output should be parsed.
            - This function is Windows compatible only
        .LINK
            Invoke-NuGetCommand
        .LINK
            https://docs.microsoft.com/en-us/nuget/tools/cli-ref-pack
    #>

    [CmdLetBinding()]
    param(
        [Parameter(Mandatory = $true)]
        [string] $Manifest,
        [Parameter(Mandatory = $false)]
        [ValidateScript( { Test-Path $_ } )]
        [string] $NuGetPath = (Get-NuGetPath),
        [Parameter(Mandatory = $false)]
        [ValidateScript( { Test-Path $_ } )]
        [alias("OutputDirectory")]
        [string] $OutputPath = "",
        [Parameter(Mandatory = $false)]
        [alias("Options")]
        [hashtable] $Parameters = @{}
    )
    
    try 
    {
        if ($OutputPath) { $OutputPath = (Get-Item $OutputPath).FullName }
        if ($Parameters.Keys.Count -gt 0)
        {
            if ($OutputPath)
            {
                if ($Parameters.Keys -notcontains "OutputDirectory") { $Parameters.Add("OutputDirectory", $OutputPath) }
                else { $Parameters["OutputDirectory"] = $OutputPath }
            }
        }
        elseif (($Parameters.Keys.Count -eq 0) -and $OutputPath) { $Parameters.Add("OutputDirectory", $OutputPath) }

        Invoke-NuGetCommand -NuGetPath $NuGetPath -Command "pack" -Target "'$($Manifest)'" -Parameters $Parameters
    }
    catch
    {
        Write-Error $_
    }
}