public/Update-Metadata.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
$ErrorActionPreference = 'Stop'

<#
.SYNOPSIS
    Updates the metadata nuspec file with the specified information.
 
.DESCRIPTION
    When a key and value is specified, update the metadata element with the specified key
    and the corresponding value in the specified NuspecFile.
    Singlular metadata elements are the only ones changed at this time.
 
.PARAMETER key
    The element that should be updated in the metadata section.
 
.PARAMETER value
    The value to update with.
 
.PARAMETER NuspecFile
    The metadata/nuspec file to update
 
.EXAMPLE
    PS> Update-Metadata -key releaseNotes -value "https://github.com/majkinetor/AU/releases/latest"
 
.EXAMPLE
    PS> Update-Metadata -key releaseNotes -value "https://github.com/majkinetor/AU/releases/latest" -NuspecFile ".\package.nuspec"
 
.EXAMPLE
    PS> @{ title = 'My Awesome Title' } | Update-Metadata
 
    This is an example of changing the Title of the nuspec file
    Update-Metadata -data @{ title = 'My Awesome Title' }
 
.EXAMPLE
    PS> Update-Metadata -data @{ dependency = 'kb2919355|1.0.20160915' }
 
    This is an example of changing the id and version attributes for the dependency key
 
.EXAMPLE
    PS> @{ dependency = 'kb2919355|1.0.20160915' } | Update-Metadata
 
.EXAMPLE
    PS> Update-Metadata -data @{ file = 'tools\**,tools' }
 
    This is an example of changing the src and target attributes
 
.EXAMPLE
    PS> @{ file = 'tools\**|tools' } | Update-Metadata
 
.EXAMPLE
    PS> @{ file = 'tools\**|tools,1' } | Update-Metadata
 
    This is an example of changing the file src and target attributes for the first file element in the nuspec file.
    If only one file element is found the change value is omitted.
 
.INPUTS
    A hashtable of key+value pairs can be used instead of specifically use an argument.
 
.NOTES
    Will now show a warning if the specified key doesn't exist in the nuspec file.
    Will now show a warning when change for file/dependency key is greater than currently in the nuspec file.
    Will now show a warning when change has been omitted due to nodes not allowing change at that key.
    Will now show a warning when file/dependency doesn't have any attributes defined to be updated.
 
    While the parameter `NuspecFile` accepts globbing patterns,
    it is expected to only match a single file.
 
    The ability to update the file and dependency metadata was included in version 0.4.0.
 
.LINK
    https://wormiecorp.github.io/Wormies-AU-Helpers/docs/functions/update-metadata
#>

function Update-Metadata {
    param(
        [Parameter(Mandatory = $true, ParameterSetName = "Single")]
        [string]$key,
        [Parameter(Mandatory = $true, ParameterSetName = "Single")]
        [string]$value,
        [Parameter(Mandatory = $true, ParameterSetName = "Multiple", ValueFromPipeline = $true)]
        [hashtable]$data = @{ $key = $value },
        [ValidateScript( { Test-Path $_ })]
        [SupportsWildcards()]
        [string]$NuspecFile = ".\*.nuspec"

    )

    $NuspecFile = Resolve-Path $NuspecFile

    $nu = New-Object xml
    $nu.PSBase.PreserveWhitespace = $true
    $nu.Load($NuspecFile)
    $omitted = $true
    $data.Keys | ForEach-Object {
        switch -Regex ($_) {
            '^(file)$' {
                $metaData = "files"
                $NodeGroup = $nu.package.$metaData
                $NodeData, [int]$change = $data[$_] -split (",")
                $NodeCount = $nu.package.$metaData.ChildNodes.Count
                $src, $target, $exclude = $NodeData -split ("\|")
                $NodeAttributes = [ordered] @{
                    "src"     = $src
                    "target"  = $target
                    "exclude" = $exclude
                }
                $change = @{$true = "0"; $false = ($change - 1) }[ ([string]::IsNullOrEmpty($change)) ]
                if ($NodeCount -eq 3) {
                    $NodeGroup = $NodeGroup."$_"
                }
                else {
                    $NodeGroup = $NodeGroup.$_[$change]
                }
            }
            '^(dependency)$' {
                $MetaNode = $_ -replace ("y", "ies")
                $metaData = "metadata"
                $NodeData, [int]$change = $data[$_] -split (",")
                $NodeGroup = $nu.package.$metaData.$MetaNode
                $NodeCount = $nu.package.$metaData.$MetaNode.ChildNodes.Count
                $id, $version, $include, $exclude = $NodeData -split ("\|")
                $NodeAttributes = [ordered] @{
                    "id"      = $id
                    "version" = $version
                    "include" = $include
                    "exclude" = $exclude
                }
                $change = @{$true = "0"; $false = ($change - 1) }[ ([string]::IsNullOrEmpty($change)) ]
                if ($NodeCount -eq 3) {
                    $NodeGroup = $NodeGroup."$_"
                }
                else {
                    $NodeGroup = $NodeGroup.$_[$change]
                }
            }
            default {
                if ( $nu.package.metadata."$_" ) {
                    $nu.package.metadata."$_" = [string]$data[$_]
                }
                else {
                    Write-Warning "$_ does not exist on the metadata element in the nuspec file"
                }
            }
        }
        if ($_ -match '^(dependency)$|^(file)$') {
            if (($change -gt $NodeCount)) {
                Write-Warning "$change is greater than $NodeCount of $_ Nodes"
            }
            if ($omitted) {
                Write-Warning "Change has been omitted due to $_ not having that number of Nodes"
            }
            foreach ( $attrib in $NodeAttributes.keys ) {
                if (!([string]::IsNullOrEmpty($NodeAttributes[$attrib])) ) {
                    if (![string]::IsNullOrEmpty( $NodeGroup.Attributes ) ) {
                        $NodeGroup.SetAttribute($attrib, $NodeAttributes[$attrib] )
                    }
                    else {
                        Write-Warning "Attribute $attrib not defined for $_ in the nuspec file"
                    }
                }
            }
        }
    }

    $utf8NoBom = New-Object System.Text.UTF8Encoding($false)
    [System.IO.File]::WriteAllText($NuspecFile, $nu.InnerXml, $utf8NoBom)
}