Public/Add-NuspecFile.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
function Add-NuspecFile
{
    <#
        .SYNOPSIS
            Adds a file in a Nuspec manifest.
        .DESCRIPTION
            Adds a file in a Nuspec manifest.
        .PARAMETER Source
            A string containing the source of the file or file pattern to be added, relative to the .nuspec file.
            The wildcard character * is allowed, and the double wildcard ** implies a recursive folder search.
        .PARAMETER Destination
            A string containing the destination of the file to be added.
        .PARAMETER Exclude
            A semicolon-delimited string containing a list of file or file patterns to exclude from the source.
            The wildcard character * is allowed, and the double wildcard ** implies a recursive folder search.
        .PARAMETER Nuspec
            An XmlDocument containing the Nuspec manifest.
        .OUTPUTS
            System.Xml.XmlDocument
            Returns an XmlDocument containing the manifest.
        .EXAMPLE
            Add-NuspecFile -Source "bin\Debug\*.dll" -Destination "lib" -Nuspec $NuspecManifest
 
            Description
            -----------
            This example will add a node file with source "bin\Debug\*.dll" and destination "lib" to the manifest, and return the XmlDocument.
        .EXAMPLE
            Add-NuspecFile -Source "tools\**\*.*" -Exclude "**\*.log" -Nuspec $NuspecManifest
 
            Description
            -----------
            This example will add a node file with source "tools\**\*.*" excluding log files to the manifest, and return the XmlDocument.
        .NOTES
        .LINK
            https://docs.microsoft.com/en-us/nuget/reference/nuspec
    #>

    [CmdLetBinding()]
    param(
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [Alias("src", "s")]
        [string] $Source,
        [Parameter(Mandatory = $false)]
        [Alias("Target", "d", "t")]
        [string] $Destination = "",
        [Parameter(Mandatory = $false)]
        [Alias("x")]
        [string] $Exclude = "",
        [Parameter(Mandatory = $true)]
        [xml] $Nuspec
    )

    try
    {
        $Files = $Nuspec.GetElementsByTagName("files")
        if (!$Files.Name)
        {
            $FilesNode = $Nuspec.CreateElement("files", $Nuspec.package.xmlns)
            $Nuspec.GetElementsByTagName("package").AppendChild($FilesNode) | Out-Null
        }

        Write-Verbose "Adding file:"
        Write-Verbose " Source`t`t`t`t`t`t:`t`t$Source"
        Write-Verbose " Destination`t:`t`t$Destination"
        Write-Verbose " Exclusions`t`t:`t`t$Exclude"
        $File = $Nuspec.CreateElement("file", $Nuspec.package.xmlns)
        $File.SetAttribute("src", $Source)
        $File.SetAttribute("target", $Destination)
        if ($Exclude) { $File.SetAttribute("exclude", $Exclude) }
        $Nuspec.GetElementsByTagName("files").AppendChild($File) | Out-Null

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