Private/Edit-VSIXManifestFile.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
<#
.SYNOPSIS
Updates a 'netcore' all projects in a directory using the in the specified [Manifest] object.
 
.PARAMETER $Manifest
The [Manifest] object.
 
.PARAMETER Path
The project directory.
#>


function Edit-VSIXManifestFile
{
    Param(
        [Parameter(Mandatory, ValueFromPipeline)]
        $Manifest,

        [string]$Path
    )

    $modifiedFiles = [System.Collections.ArrayList]::new();
    foreach ($vsixmanifest in (Get-ChildItem $Path -Recurse -Filter "*.vsixmanifest"))
    {
        if ($PSCmdlet.ShouldProcess($vsixmanifest.FullName))
        {
            [xml]$doc = Get-Content $vsixmanifest.FullName;
            $ns = New-Object Xml.XmlNamespaceManager $doc.NameTable;
            $ns.AddNamespace("x", "http://schemas.microsoft.com/developer/vsx-schema/2011");

            $metadata = $doc.SelectSingleNode("//x:Metadata", $ns);

            if ($metadata -ne $null)
            {
                $version = $Manifest | Convert-NcrementVersionNumberToString;
                $identity = $metadata.SelectSingleNode("x:Identity", $ns);
                foreach ($token in @(
                    [Ncrement.Token]::new("Version", $version),
                    [Ncrement.Token]::new("Publisher", $manifest.Owner)
                ))
                {
                    if (-not [string]::IsNullOrEmpty($token.Value))
                    {
                        $attribute = $identity.Attributes[$token.TagName];
                        if ($attribute -eq $null)
                        {
                            $attr = $doc.CreateAttribute($token.TagName);
                            $attr.Value = $token.Value;
                            $identity.Attributes.Append($attr) | Out-Null;
                        }
                        else
                        {
                            $attribute.Value = $token.Value;
                        }
                    }
                }

                foreach ($token in @(
                    [Ncrement.Token]::new("DisplayName", $manifest.Name),
                    [Ncrement.Token]::new("Description", $manifest.Description),
                    [Ncrement.Token]::new("Tags", $manifest.Tags)
                ))
                {
                    if (-not [string]::IsNullOrEmpty($token.Value))
                    {
                        $node = $metadata.SelectSingleNode("x:$($token.TagName)", $ns);
                        if ($node -eq $null)
                        {
                            $n = $doc.CreateElement($token.TagName, "http://schemas.microsoft.com/developer/vsx-schema/2011");
                            $data = &{ if ($token.Value -match '[\n><]') { return $doc.CreateCDataSection($token.Value); } else { return $doc.CreateTextNode($token.Value); } };
                            $n.AppendChild($data) | Out-Null;
                            $metadata.AppendChild($n) | Out-Null;
                        }
                        else
                        {
                            $node.InnerText = $token.Value;
                        }
                    }
                }
                $doc.Save($vsixmanifest.FullName) | Out-Null;
            }
        }

        $modifiedFiles.Add($vsixmanifest.FullName) | Out-Null;
    }

    return $modifiedFiles;
}