Private/Edit-VSIXManifest.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
function Edit-VSIXManifest
{
    [CmdletBinding(ConfirmImpact = "Medium", SupportsShouldProcess)]
    Param(
        [Parameter(Mandatory)]
        $Manifest,

        [Parameter(Mandatory, ValueFromPipeline)]
        $InputObject
    )

    BEGIN
    {
        $path = ConvertTo-Path $Manifest;
        if ((-not [string]::IsNullOrEmpty($path)) -and (Test-Path $path -PathType Leaf)) { $Manifest = Get-Content $path | ConvertFrom-Json; }
    }

    PROCESS
    {
        $vsix = Test-VSIXManifest $InputObject;
        if ($vsix)
        {
            $madeChanged = $false;
            $ns = $vsix.Xmlns;
            $metadata = $vsix.Document.SelectSingleNode("//x:Metadata", $ns);

            if ($metadata -ne $null)
            {
                $version = ConvertTo-NcrementVersionNumber $Manifest | Select-Object -ExpandProperty Version;
                $identity = $metadata.SelectSingleNode("x:Identity", $ns);

                foreach ($token in @{
                    "Version"=$version;
                }.GetEnumerator())
                {
                    if (($token.Value -ne $null) -and (-not [string]::IsNullOrWhiteSpace($token.Value)))
                    {
                        $madeChanged = $true;
                        $attr = $identity.Attributes[$token.Name];
                        if ($attr -eq $null)
                        {
                            $attr = $vsix.Document.CreateAttribute($token.Name);
                            $attr.Value = $token.Value;
                            $identity.Attributes.Append($attr) | Out-Null;
                        }
                        else { $attr.Value = $token.Value; }
                    }
                }

                foreach ($token in @{
                    "DisplayName"=$Manifest.Name;
                    "Description"=$Manifest.Description;
                    "Tags"=$Manifest.Tags;
                }.GetEnumerator())
                {
                    if (($token.Value -ne $null) -and (-not [string]::IsNullOrWhiteSpace($token.Value)))
                    {
                        $data = $null;
                        if ($token.Value -match '[\n><]') { $data = $vsix.Document.CreateCDataSection($token.Value); }
                        else { $data = $vsix.Document.CreateTextNode($token.Value); }

                        $madeChanged = $true;
                        $node  = $metadata.SelectSingleNode("x:$($token.Name)", $ns);

                        if ($node -eq $null)
                        {
                            $item = $vsix.Document.CreateElement($token.Name, "http://schemas.microsoft.com/developer/vsx-schema/2011");
                            $item.AppendChild($data) | Out-Null;
                            $metadata.AppendChild($item) | Out-Null;
                        }
                        else
                        {
                            $node.RemoveAll() | Out-Null;
                            $node.AppendChild($data) | Out-Null;
                        }
                    }
                }

                if ($PSCmdlet.ShouldProcess($InputObject)) { $vsix.Document.Save($vsix.Path); }
                return $InputObject;
            }
        }
    }
}