Public/Test-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
function Test-NuspecManifest
{
    <#
        .SYNOPSIS
            Tests a Nuspec manifest.
        .DESCRIPTION
            Tests a Nuspec manifest against an xsd schema.
        .PARAMETER Path
            A string containing the full path to the Nuspec manifest file.
        .PARAMETER Nuspec
            An XmlDocument containing the Nuspec manifest.
        .PARAMETER Schema
            A string containing either the content or the full path of the xsd schema.
            Will use 'Get-NuspecSchema' if omitted.
        .INPUTS
        .OUTPUTS
            System.Boolean
            Returns a boolean specifying whether or not the manifest is conform to teh schema.
        .EXAMPLE
            Test-NuspecManifest -Path .\package.nuspec -Schema .\nuspec.xsd
 
            Description
            -----------
            This example will test the file 'package.nuspec' against the schema 'nuspec.xsd'.
        .EXAMPLE
            Test-NuspecManifest -Nuspec $NuspecContent
 
            Description
            -----------
            This example will test the xml document contained in a variable against the default Nuspec schema.
        .NOTES
        .LINK
            Get-NuspecSchema
        .LINK
            https://docs.microsoft.com/en-us/nuget/reference/nuspec
        .LINK
            https://raw.githubusercontent.com/MyGet/NuGetPackages/master/NuSpec/tools/nuspec.xsd
    #>

    [CmdLetBinding()]
    param(
        [Parameter(ParameterSetName = "FromFile", Mandatory = $true)]
        [ValidateScript( { Test-Path $_ } )]
        [string] $Path,
        [Parameter(ParameterSetName = "FromXml", Mandatory = $true)]
        [xml] $Nuspec,
        [Parameter(ParameterSetName = "FromFile", Mandatory = $false)]
        [Parameter(ParameterSetName = "FromXml", Mandatory = $false)]
        [string] $Schema = (Get-NuspecSchema)
    )

    try
    {
        $CleanTmp = $false
        if ($PSCmdlet.ParameterSetName -eq "FromXml")
        {
            Write-Verbose "Saving Nuspec xml in a temporary location ($(([System.IO.Path]::GetTempPath()))/temp.nuspec)."
            $NuspecPath = Join-Path ([System.IO.Path]::GetTempPath()) "temp.nuspec"
            $Nuspec.Save($NuspecPath)
            $CleanTmp = $true
            $Path = $NuspecPath
        }
        if (!(Test-Path $Schema))
        {
            Write-Verbose "Saving Nuspec schema in a temporary location ($(([System.IO.Path]::GetTempPath()))/nuspec.xsd)."
            $XsdPath = Join-Path ([System.IO.Path]::GetTempPath()) "nuspec.xsd"
            $Schema | Out-File $XsdPath -Encoding utf8
            $Schema = $XsdPath
        }
        Write-Verbose "Testing Nuspec manifest '$Path' against schema '$Schema'"
        Test-Xml -Path $Path -SchemaPath $Schema
    }
    catch
    {
        Write-Error $_
    }
    finally
    {
        if ($XsdPath -and (Test-Path $XsdPath)) { Remove-Item $XsdPath -Force }
        if ($CleanTmp) { Remove-Item $NuspecPath -Force }
    }
}