Public/Add-NuspecContentFile.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
function Add-NuspecContentFile
{
    <#
        .SYNOPSIS
            Adds a content file in a Nuspec manifest.
        .DESCRIPTION
            Adds a content file in a Nuspec manifest.
        .PARAMETER Include
            A string containing path 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 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 BuildAction
            A string containing the build action to assign to the content item for MSBuild.
        .PARAMETER CopyToOutput
            A switch specifying whether or not to copy content items to the build (or publish) output folder.
        .PARAMETER Flatten
            A switch specifying whether or not to copy content items to a single folder in the build output (specified), or to preserve the folder structure in the package (not specified). This flag only works when copyToOutput flag is specified.
        .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("i")]
        [string] $Include,
        [Parameter(Mandatory = $false)]
        [Alias("x")]
        [string] $Exclude,
        [Parameter(Mandatory = $false)]
        [ValidateSet("Content", "None", "EmbeddedResource", "Compile")]
        [Alias("b")]
        [string] $BuildAction = "Compile",
        [Parameter(Mandatory = $false)]
        [Alias("o")]
        [switch] $CopyToOutput,
        [Parameter(Mandatory = $false)]
        [Alias("f")]
        [switch] $Flatten,
        [Parameter(Mandatory = $true)]
        [xml] $Nuspec
    )

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

        Write-Verbose "Adding content file:"
        Write-Verbose " Include`t`t`t`t`t`t`t`t`t:`t`t$Include"
        Write-Verbose " Exclude`t`t`t`t`t`t`t`t`t:`t`t$Exclude"
        Write-Verbose " Build Action`t`t`t`t:`t`t$BuilAction"
        Write-Verbose " Copy to output`t`t:`t`t$CopyToOutput"
        Write-Verbose " Flatten`t`t`t`t`t`t`t`t`t:`t`t$Flatten"
        $File = $Nuspec.CreateElement("file", $Nuspec.package.xmlns)
        $File.SetAttribute("include", $Include)
        if ($Exclude) { $File.SetAttribute("exclude", $Exclude) }
        $File.SetAttribute("buildAction", $BuildAction)
        if ($CopyToOutput)
        {
            $File.SetAttribute("copyToOutput", $CopyToOutput)
            $File.SetAttribute("flatten", $Flatten)
        }
        $Nuspec.GetElementsByTagName("contentFiles").AppendChild($File) | Out-Null

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