Public/Add-NuspecDependency.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
function Add-NuspecDependency
{
    <#
        .SYNOPSIS
            Adds a dependency in a Nuspec manifest.
        .DESCRIPTION
            Adds a dependency in a Nuspec manifest.
            This CmdLet supports PowerShell module manifest file (psd1) 'RequiredModules' property as an input object.
        .PARAMETER Name
            A string containing the name of the dependency to be added.
        .PARAMETER Version
            A string containing the version of the dependency to be added.
        .PARAMETER InputObject
            An object containing the dependencies Name(s) and Version(s) to be added.
        .PARAMETER Nuspec
            An XmlDocument containing the Nuspec manifest.
        .INPUTS
            System.Object
            Accepts an object containing the Name/Id and Version as an input from the pipeline.
        .OUTPUTS
            System.Xml.XmlDocument
            Returns an XmlDocument containing the manifest.
        .EXAMPLE
            Add-NuspecDependency -Name "MyPackage" -Version "1.0.0" -Nuspec $NuspecManifest
 
            Description
            -----------
            This example will add a dependency on "MyPackage" minimum version "1.0.0" to the manifest, and return the XmlDocument.
        .EXAMPLE
            Add-NuspecDependency -Name "MyPackage" -Version "[1.0.0]" -Nuspec $NuspecManifest
 
            Description
            -----------
            This example will add a dependency on "MyPackage" exact version "1.0.0" to the manifest, and return the XmlDocument.
        .EXAMPLE
            Add-NuspecDependency -InputObject @{ id = "MyPackage" ; version = "1.0.0" } -Nuspec $NuspecManifest
 
            Description
            -----------
            This example will add a dependency on "MyPackage" version "1.0.0" to the manifest, and return the XmlDocument.
        .EXAMPLE
            Add-NuspecDependency -InputObject @( @{ id = "MyPackage" ; version = "1.0.0" } , @{ id = "MyPackage2" ; version = "2.0.0" } ) -Nuspec $NuspecManifest
 
            Description
            -----------
            This example will add a dependency on "MyPackage" version "1.0.0" and a dependency on "MyPackage2" version "2.0.0" to the manifest, and return the XmlDocument.
        .EXAMPLE
            Add-NuspecDependency -InputObject @( "MyModule1", @{ ModuleName = "MyModule2" ; ModuleVersion = "1.0.0" }, @{ ModuleName = "MyModule3" ; RequiredVersion = "1.0.0" } ) -Nuspec $NuspecManifest
 
            Description
            -----------
            This example will add a dependency on "MyModule1" implicitely on minimum version "0.0.0", a dependency on "MyModule2" version "1.0.0", and a dependency on "MyModule3" precise version "1.0.0" to the manifest, and return the XmlDocument.
        .EXAMPLE
            Add-NuspecDependency -InputObject (Import-PowerShellDataFile -Path .\MyModule.psd1).RequiredModules -Nuspec $NuspecManifest
 
            Description
            -----------
            This example will add dependencies contained in the property RequiredModules of the file MyModule.psd1, and return the XmlDocument.
        .NOTES
        .LINK
            https://docs.microsoft.com/en-us/nuget/consume-packages/dependency-resolution
        .LINK
            https://docs.microsoft.com/en-us/nuget/reference/package-versioning#version-ranges-and-wildcards
    #>

    [CmdLetBinding()]
    param(
        [Parameter(ParameterSetName = "FromValue", Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [Alias("Id")]
        [string] $Name,
        [Parameter(ParameterSetName = "FromValue", Mandatory = $false)]
        [string] $Version = "0.0.0",
        [Parameter(ParameterSetName = "FromObject", Mandatory = $true, ValueFromPipeline = $true)]
        $InputObject,
        [Parameter(ParameterSetName = "FromValue", Mandatory = $true)]
        [Parameter(ParameterSetName = "FromObject", Mandatory = $true)]
        [xml] $Nuspec,
        [Parameter(ParameterSetName = "FromObject", Mandatory = $false)]
        [Parameter(ParameterSetName = "FromValue", Mandatory = $false)]
        $Match
    )

    $Dependencies = $Nuspec.GetElementsByTagName("dependencies")
    if (!$Dependencies.Name)
    {
        $FilesNode = $Nuspec.CreateElement("dependencies", $Nuspec.package.xmlns)
        $Nuspec.GetElementsByTagName("metadata").AppendChild($FilesNode) | Out-Null
    }

    switch ($PSCmdlet.ParameterSetName)
    {
        "FromValue"
        {
            if ($Name -match $Match)
            {
                $CurrentDependency = $Nuspec.package.metadata.dependencies.dependency | Where-Object { $_.id -eq $Name }
                if ($CurrentDependency) { Write-Warning "Dependency on '$Name' already exists with version '$($CurrentDependency.version)'." }
                else
                {
                    $Dependency = $Nuspec.CreateElement("dependency", $Nuspec.package.xmlns)
                    $Dependency.SetAttribute("id", $Name)
                    $Dependency.SetAttribute("version", $Version)
                    Write-Verbose "Adding a dependency on '$($Dependency.id)' (version '$($Dependency.version)')."
                    $Nuspec.GetElementsByTagName("dependencies").AppendChild($Dependency) | Out-Null
                }
            }
            else { Write-Warning "Dependency name '$Name' does not match the pattern '$Match', it will not be added." }
        }
        "FromObject"
        {
            $InputObject = $InputObject | ConvertTo-Json -Depth 100 | ConvertFrom-Json
            $InputObject | ForEach-Object {
                if ($_.GetType().Name -eq "PSCustomObject")
                {
                    if ($_.ModuleName -or $_.Name)
                    {
                        $Version = "0.0.0"
                        if ($_.ModuleName) { $Id = $_.ModuleName }
                        if ($_.Name) { $Id = $_.Name }
                        if ($_.Version) { $Version = $_.Version }
                        if ($_.ModuleVersion) { $Version = $_.ModuleVersion }
                        if ($_.RequiredVersion) { $Version = "[$($_.RequiredVersion)]" }
                        if ($_.MaximumVersion) { $Version = "(,$($_.MaximumVersion)]" }
                    }
                    elseif ($_.id)
                    {
                        $Id = $_.id
                        if (!$_.version) { $Version = "0.0.0" }
                        else { $Version = $_.version }
                    }
                    elseif ($_.value)
                    {
                        Add-NuspecDependency -InputObject $_.Value -Match $Match -Nuspec $Nuspec
                        break
                    }
                    else
                    {
                        Write-Error -Message "Unsupported object format for value for dependency '$_' ($($_.GetType().Name))." -Category InvalidData -CategoryActivity $MyInvocation.MyCommand -TargetName $Name -TargetType "Nuspec Property" -Exception InvalidDataException
                    }
                }
                elseif ($_.GetType().Name -eq "Object[]")
                {
                    $_ | ForEach-Object { Add-NuspecDependency -Name $_ -Version "0.0.0" -Match $Match -Nuspec $Nuspec }
                }
                elseif ($_.GetType().Name -eq "string")
                {
                    $_.Replace(" ", "").Split(",") | ForEach-Object { Add-NuspecDependency -Name $_ -Version "0.0.0" -Match $Match -Nuspec $Nuspec }
                }
                else
                {
                    Write-Verbose $_
                    $Id = $_
                    $Version = "0.0.0"
                }
                if ($Id) { Add-NuspecDependency -Name $Id -Version $Version -Match $Match -Nuspec $Nuspec }
            }
        }
    }
    $Nuspec
}