Public/Resolve-NuspecProperty.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
function Resolve-NuspecProperty
{
    <#
        .SYNOPSIS
            Resolves a property to be used in a Nuspec manifest.
        .DESCRIPTION
            Resolves a property that matches some mappings to be used in a Nuspec manifest.
            This CmdLet supports most of PowerShell module manifest file (psd1) properties.
        .PARAMETER Name
            A string containing the name of the property to be resolved.
        .PARAMETER Value
            An object containing the value(s) of the property.
        .PARAMETER AcceptChocolateyProperties
            A switch specifying whether or not to accept Chocolatey-specific properties.
        .INPUTS
        .OUTPUTS
            System.Management.Automation.PSCustomObject
            Returns a PSCustomObject containing the name and value of the mapped property.
        .EXAMPLE
            Resolve-NuspecProperty -Name "version" -Value "1.0.0"
 
            Description
            -----------
            This example will return the mapped property for "version" (which is "version") and the value specified.
        .EXAMPLE
            Resolve-NuspecProperty -Name "RequiredModules" -Value @( "MyModule1", @{ ModuleName = "MyModule2" ; ModuleVersion = "1.0.0" }, @{ ModuleName = "MyModule3" ; RequiredVersion = "1.0.0" } )
 
            Description
            -----------
            This example will return the mapped property for "RequiredModules" (which is "dependencies") and the value specified.
        .EXAMPLE
            Resolve-NuspecProperty -Name "CompanyName"
 
            Description
            -----------
            This example will return the mapped property for "CompanyName" (which is "owners").
        .NOTES
        .LINK
            Set-NuspecProperty
        .LINK
            https://docs.microsoft.com/en-us/nuget/reference/nuspec
    #>

    [CmdLetBinding()]
    param(
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [string] $Name,
        [Parameter(Mandatory = $false)]
        $Value,
        [Parameter(Mandatory = $false)]
        [switch] $AcceptChocolateyProperties
    )
    
    $ResolvedProperty = New-Object -TypeName PSCustomObject
    switch -Regex ($Name)
    {
        "description"
        {
            $ResolvedProperty | Add-Member -MemberType NoteProperty -Name "Name" -Value "description" -PassThru | Out-Null
            $ResolvedProperty | Add-Member -MemberType NoteProperty -Name "Value" -Value $Value -PassThru | Out-Null
        }
        "^summary$"
        {
            $ResolvedProperty | Add-Member -MemberType NoteProperty -Name "Name" -Value "summary" -PassThru | Out-Null
            $ResolvedProperty | Add-Member -MemberType NoteProperty -Name "Value" -Value $Value -PassThru | Out-Null
        }
        "^version$|ModuleVersion"
        {
            switch -Regex ($Value.GetType().Name)
            {
                "version" { $Value = $Value.ToString() }
                "object" { $Value = "$($Value.Major).$($Value.Minor).$($Value.Build)" }
            }
            $ResolvedProperty | Add-Member -MemberType NoteProperty -Name "Name" -Value "version" -PassThru | Out-Null
            $ResolvedProperty | Add-Member -MemberType NoteProperty -Name "Value" -Value $Value -PassThru | Out-Null
        }
        "Prerelease"
        {
            $ResolvedProperty | Add-Member -MemberType NoteProperty -Name "Name" -Value "Prerelease" -PassThru | Out-Null
            $ResolvedProperty | Add-Member -MemberType NoteProperty -Name "Value" -Value $Value -PassThru | Out-Null
        }
        "^name$|^id$|RootModule|ModuleToProcess"
        {
            $ResolvedProperty | Add-Member -MemberType NoteProperty -Name "Name" -Value "id" -PassThru | Out-Null
            if ((($Name -eq "RootModule") -or ($Name -eq "ModuleToProcess")) -and ($Value)) { $Value = $Value.Replace(".psm1", "") }
            $ResolvedProperty | Add-Member -MemberType NoteProperty -Name "Value" -Value $Value -PassThru | Out-Null
        }
        "^title$"
        {
            $ResolvedProperty | Add-Member -MemberType NoteProperty -Name "Name" -Value "title" -PassThru | Out-Null
            $ResolvedProperty | Add-Member -MemberType NoteProperty -Name "Value" -Value $Value -PassThru | Out-Null
        }
        "^owners$|CompanyName"
        {
            $ResolvedProperty | Add-Member -MemberType NoteProperty -Name "Name" -Value "owners" -PassThru | Out-Null
            $ResolvedProperty | Add-Member -MemberType NoteProperty -Name "Value" -Value $Value -PassThru | Out-Null
        }
        "^authors$|Author"
        {
            $ResolvedProperty | Add-Member -MemberType NoteProperty -Name "Name" -Value "authors" -PassThru | Out-Null
            $ResolvedProperty | Add-Member -MemberType NoteProperty -Name "Value" -Value $Value -PassThru | Out-Null
        }
        "Copyright"
        {
            $ResolvedProperty | Add-Member -MemberType NoteProperty -Name "Name" -Value "copyright" -PassThru | Out-Null
            $ResolvedProperty | Add-Member -MemberType NoteProperty -Name "Value" -Value $Value -PassThru | Out-Null
        }
        "projectUrl|ProjectUri"
        {
            $ResolvedProperty | Add-Member -MemberType NoteProperty -Name "Name" -Value "projectUrl" -PassThru | Out-Null
            $ResolvedProperty | Add-Member -MemberType NoteProperty -Name "Value" -Value $Value -PassThru | Out-Null
        }
        "licenseUrl|LicenseUri"
        {
            $ResolvedProperty | Add-Member -MemberType NoteProperty -Name "Name" -Value "licenseUrl" -PassThru | Out-Null
            $ResolvedProperty | Add-Member -MemberType NoteProperty -Name "Value" -Value $Value -PassThru | Out-Null
        }
        "^license$"
        {
            $ResolvedProperty | Add-Member -MemberType NoteProperty -Name "Name" -Value "license" -PassThru | Out-Null
            $ResolvedProperty | Add-Member -MemberType NoteProperty -Name "Value" -Value $Value -PassThru | Out-Null
        }
        "^requireLicenseAcceptance$"
        {
            $ResolvedProperty | Add-Member -MemberType NoteProperty -Name "Name" -Value "requireLicenseAcceptance" -PassThru | Out-Null
            $ResolvedProperty | Add-Member -MemberType NoteProperty -Name "Value" -Value $Value.ToString().ToLower() -PassThru | Out-Null
        }
        "iconUrl|IconUri"
        {
            $ResolvedProperty | Add-Member -MemberType NoteProperty -Name "Name" -Value "iconUrl" -PassThru | Out-Null
            $ResolvedProperty | Add-Member -MemberType NoteProperty -Name "Value" -Value $Value -PassThru | Out-Null
        }
        "^icon$"
        {
            $ResolvedProperty | Add-Member -MemberType NoteProperty -Name "Name" -Value "icon" -PassThru | Out-Null
            $ResolvedProperty | Add-Member -MemberType NoteProperty -Name "Value" -Value $Value -PassThru | Out-Null
        }
        "dependencies|RequiredModules|RequiredScripts"
        {
            $ResolvedProperty | Add-Member -MemberType NoteProperty -Name "Name" -Value "dependencies" -PassThru | Out-Null
            $ResolvedProperty | Add-Member -MemberType NoteProperty -Name "Value" -Value $Value -PassThru | Out-Null
            
        }
        "repository"
        {
            $ResolvedProperty | Add-Member -MemberType NoteProperty -Name "Name" -Value "repository" -PassThru | Out-Null
            switch -Regex ($Name)
            {
                "^repository$" { $ResolvedProperty | Add-Member -MemberType NoteProperty -Name "Value" -Value $Value -PassThru | Out-Null }
                "repositoryUrl"
                { 
                    $Repository = Resolve-NuspecRepository -Uri $Value
                    $ResolvedProperty | Add-Member -MemberType NoteProperty -Name "Value" -Value $Repository -PassThru | Out-Null
                } 
            }
        }
        "tags"
        {
            if ($Value.GetType().Name -eq "string") { $Value = $Value.Split(",") -join " " }
            else { $Value = $Value | Select-Object -Unique }
            $ResolvedProperty | Add-Member -MemberType NoteProperty -Name "Name" -Value "tags" -PassThru | Out-Null
            $ResolvedProperty | Add-Member -MemberType NoteProperty -Name "Value" -Value $($Value.Split(" ") -join " ") -PassThru | Out-Null
        }
        "releaseNotes"
        {
            $ResolvedProperty | Add-Member -MemberType NoteProperty -Name "Name" -Value "releaseNotes" -PassThru | Out-Null
            if ($Value -like "*.md") { $Value = Get-Content $Value }
            $ResolvedProperty | Add-Member -MemberType NoteProperty -Name "Value" -Value ($Value -join "`r`n") -PassThru | Out-Null   
        }
        "^docsUrl$|^mailingListUrl$|^bugTrackerUrl$|^packageSourceUrl$|^projectSourceUrl$"
        {
            if ($AcceptChocolateyProperties)
            {
                $ResolvedProperty | Add-Member -MemberType NoteProperty -Name "Name" -Value $Name -PassThru | Out-Null
                $ResolvedProperty | Add-Member -MemberType NoteProperty -Name "Value" -Value $Value -PassThru | Out-Null
            }
            else { Write-Warning "Property '$Name' does not match any mapped Nuspec property." }    
        }
        default { Write-Warning "Property '$Name' does not match any mapped Nuspec property." }
    }
    if ($ResolvedProperty.Name)
    { 
        Write-Verbose "Property '$Name' resolved to '$($ResolvedProperty.Name)'."
        if ($ResolvedProperty.Value) { Write-Verbose "Value returned for this property is '$($ResolvedProperty.Value)' ($($ResolvedProperty.Value.GetType().Name))." }
    }
    else { $ResolvedProperty = $null }
    $ResolvedProperty
}