Public/Save-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
82
83
84
85
86
87
88
89
function Save-NuspecManifest
{
    <#
        .SYNOPSIS
            Saves a Nuspec manifest.
        .DESCRIPTION
            Saves a Nuspec manifest to a file.
        .PARAMETER Path
            A string containing the the full path to save the Nuspec manifest file.
        .PARAMETER Nuspec
            An XmlDocument containing the Nuspec manifest.
        .INPUTS
            System.Xml.XmlDocument
            Accepts an XmlDocument containing the manifest.
        .OUTPUTS
            System.String
            Returns a string containing the path to the manifest file
        .EXAMPLE
            Save-NuspecManifest -Nuspec $Nuspec -Path .\mypackage.nuspec
 
            Description
            -----------
            This example will save the Nuspec manifest in the file mypackage.nuscpec.
        .EXAMPLE
            Import-PowerShellDataFile .\MyModule.psd1 | ConvertTo-NuspecManifest | Save-NuspecManifest -Path .\mymodule.nuspec
 
            Description
            -----------
            This example will save the Nuspec manifest in the file mypackage.nuscpec.
        .NOTES
        .LINK
            ConvertTo-NuspecManifest
        .LINK
            https://docs.microsoft.com/en-us/nuget/reference/nuspec
    #>

    [CmdLetBinding()]
    param(
        [Parameter(Mandatory = $true)]
        [ValidateScript( {
            if ((Test-Path $_))
            {
                $Item = Get-Item $_
                if ($Item.PSIsContainer) { $true }
                else
                {
                    if ($Item.Extension -eq ".nuspec") { $true }
                    else { throw "Could not validate path '$_' because it is not a Nuspec file." }
                }
            }
            else { $true}
        } )]
        [string] $Path,
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [xml] $Nuspec
    )

    try
    {
        Write-Verbose "coucou"
        $NuspecId = (Get-NuspecProperty -Name id -Nuspec $Nuspec).Value
        if ((Test-Path $Path))
        {
            $Item = Get-Item $Path
            if ($Item.PSIsContainer) { $Path = Join-Path $Item.FullName ("{0}.nuspec" -f $NuspecId) }
            else { $Path = $Item.FullName }
        }
        elseif ($Path.EndsWith(".nuspec"))
        {
            $Directory = Split-Path $Path -Parent
            if (!(Test-Path $Directory)) { $Directory = New-Item -Path (Split-Path $Path -Parent) -ItemType Directory -Force | Out-Null }
            else { $Directory = Get-Item $Directory }
            $Path = Join-Path $Directory.FullName (Split-Path $Path -Leaf)
        }
        else
        { 
            $Directory = New-Item -Path $Path -ItemType Directory -Force
            $Path = Join-Path $Directory.FullName ("{0}.nuspec" -f $NuspecId)
        }
        Write-Verbose $Path
        $Nuspec.Save($Path) | Out-Null
        (Get-Item $Path).FullName
    }
    catch
    {
        Write-Error $_
    }


}