Public/ConvertTo-NuspecManifest.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
function ConvertTo-NuspecManifest
{
    <#
        .SYNOPSIS
            Converts an object to a NuspecManifest.
        .DESCRIPTION
            Converts an object to a NuspecManifest.
        .PARAMETER InputObject
            An object containing the specifications to be converted into a Nuspec manifest.
        .PARAMETER AcceptChocolateyProperties
            A switch specifying whether or not to accept Chocolatey-specific properties.
        .PARAMETER DependencyMatch
            A string containing a regular expression to match the dependencies against.
            If used and a dependency does not match, it will be excluded fro the resulting nuspec manifest.
        .INPUTS
            System.Object
            Accepts an object representing data that can be mapped to Nuspec properties and converted to a Nuspec manifest.
        .OUTPUTS
            System.Xml.XmlDocument
            Returns an XmlDocument containing the manifest.
        .EXAMPLE
            ConvertTo-NuspecManifest -InputObject ((Get-Content .\package.json) | Convert-From-Json)
 
            Description
            -----------
            This example will get the content of "package.json" and map properties to create a Nuspec manifest.
        .EXAMPLE
            Import-PowerShellDataFile -Path .\MyModule.psd1 | ConvertTo-NuspecManifest
 
            Description
            -----------
            This example will import the PowerShell module manifest "MyModule.psd1" and map properties to create a Nuspec manifest.
            Import-PowerShellDataFile only works with PowerShell v5+, for v4 use Import-LocalizedData.
        .EXAMPLE
            Get-Module MyModule | ConvertTo-NuspecManifest
 
            Description
            -----------
            This example will get the PowerShell module MyModule and map properties to create a Nuspec manifest.
        .EXAMPLE
            Test-ScriptFileInfo C:\MyScript.ps1 | ConvertTo-NuspecManifest
 
            Description
            -----------
            This example will get the script file info of the PowerShell script MyScript.ps1 and map properties to create a Nuspec manifest.
        .NOTES
            This version supports mostly PowerShell Module amd Scripts File Info properties.
        .LINK
            Get-NuspecProperty
        .LINK
            Set-NuspecProperty
        .LINK
            Resolve-NuspecProperty
        .LINK
            https://docs.microsoft.com/en-us/nuget/reference/nuspec
    #>

    [CmdLetBinding()]  
    param(
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [ValidateScript( {
                try
                {
                    $_ | ConvertTo-Json | ConvertFrom-Json
                    $true
                }
                catch { throw "Could not convert input object type '$($InputObject.GetType().Name)'." }
            } )]
        $InputObject,
        [Parameter(Mandatory = $false)]
        [switch] $AcceptChocolateyProperties,
        [Parameter(Mandatory = $false)]
        $DependencyMatch
    )
    
    begin {}
    process
    {
        try
        {
            [xml] $Nuspec = Get-Content (Join-Path (Join-Path (Split-Path $PSScriptRoot -Parent) "template") "template.nuspec.xml")
            
            Write-Verbose "Input object type is '$($InputObject.GetType().Name)'"
            $InputObject = $InputObject | ConvertTo-Json | ConvertFrom-Json
        
            $InputObject | Get-Member -MemberType NoteProperty | Where-Object { $InputObject."$($_.Name)" -and ($_.Name -notlike "Exported*") -and ($_.Name -notin "SessionState", "Definition") } | ForEach-Object {
                $Current = $_
                Write-Debug $Current
                switch -Regex ($InputObject."$($Current.Name)".GetType().Name)
                {
                    "PSCustomObject"
                    {
                        $ResolvedProperty = Resolve-NuspecProperty -Name $Current.Name -Value $InputObject."$($Current.Name)" -AcceptChocolateyProperties:$AcceptChocolateyProperties
                        if ($ResolvedProperty) { $ResolvedProperty | Set-NuspecProperty -Nuspec $Nuspec -AcceptChocolateyProperties:$AcceptChocolateyProperties -ErrorAction SilentlyContinue | Out-Null }
                        else 
                        {
                            $InputObject."$($Current.Name)" | Get-Member -MemberType NoteProperty | Where-Object { $InputObject."$($Current.Name)"."$($_.Name)" } | ForEach-Object {
                                $SubCurrent = $_
                                if ($InputObject."$($Current.Name)"."$($SubCurrent.Name)".GetType().Name -eq "PSCustomObject")
                                {
                                    $ResolvedSubProperty = Resolve-NuspecProperty -Name $SubCurrent.Name -Value $InputObject."$($Current.Name)"."$($SubCurrent.Name)" -AcceptChocolateyProperties:$AcceptChocolateyProperties
                                    if ($ResolvedSubProperty) { $ResolvedSubProperty | Set-NuspecProperty -Nuspec $Nuspec -AcceptChocolateyProperties:$AcceptChocolateyProperties -ErrorAction SilentlyContinue | Out-Null }
                                    else
                                    {
                                        $InputObject."$($Current.Name)"."$($SubCurrent.Name)" | Get-Member -MemberType NoteProperty | Where-Object { $InputObject."$($Current.Name)"."$($SubCurrent.Name)"."$($_.Name)" } | ForEach-Object {
                                            Resolve-NuspecProperty -Name $_.Name -Value $InputObject."$($Current.Name)"."$($SubCurrent.Name)"."$($_.Name)" -AcceptChocolateyProperties:$AcceptChocolateyProperties | Set-NuspecProperty -Nuspec $Nuspec -AcceptChocolateyProperties:$AcceptChocolateyProperties -ErrorAction SilentlyContinue | Out-Null
                                        }
                                    }
                                }
                                else { Resolve-NuspecProperty -Name $SubCurrent.Name -Value $InputObject."$($Current.Name)"."$($SubCurrent.Name)" -AcceptChocolateyProperties:$AcceptChocolateyProperties | Set-NuspecProperty -Nuspec $Nuspec -AcceptChocolateyProperties:$AcceptChocolateyProperties -ErrorAction SilentlyContinue | Out-Null }
                            }
                        }
                    }
                    default { Resolve-NuspecProperty -Name $Current.Name -Value $InputObject."$($Current.Name)" -AcceptChocolateyProperties:$AcceptChocolateyProperties | Set-NuspecProperty -Nuspec $Nuspec -AcceptChocolateyProperties:$AcceptChocolateyProperties -ErrorAction SilentlyContinue | Out-Null }
                }
            }

            if ((!$Nuspec.package.metadata.title) -and $Nuspec.package.metadata.id)
            {
                Write-Verbose "No 'title' property found, using 'id' value ($($Nuspec.package.metadata.id)) as 'title'."
                Set-NuspecProperty -Name "title" -Value $Nuspec.package.metadata.id -Nuspec $Nuspec | Out-Null
            }
            if ((!$Nuspec.package.metadata.summary) -and $Nuspec.package.metadata.description -and $AcceptChocolateyProperties)
            {
                Write-Verbose "No 'summary' property found, using 'description' value ($($Nuspec.package.metadata.description)) as 'summary'."
                Set-NuspecProperty -Name "summary" -Value $Nuspec.package.metadata.description -Nuspec $Nuspec | Out-Null
            }

            $Nuspec
        }
        catch
        {
            Write-Error $_
        }
    }
    end {}
}