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

        [Parameter(Mandatory, ValueFromPipeline)]
        [ValidateNotNull()]
        $InputObject
    )

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

    PROCESS
    {
        $path = ConvertTo-Path $InputObject;
        $doc = Test-NetcoreProjectFile $InputObject;
        if ($doc)
        {
            $hasChanges = $false;
            $hasNewNodes = $false;
            $myGroup = $doc.CreateElement("PropertyGroup");
            $version = ConvertTo-NcrementVersionNumber $Manifest | Select-Object -ExpandProperty Version;

            foreach ($token in @{
                "Title"=$Manifest.Name;
                "PackageVersion"=$version;
                "AssemblyVersion"=$version;
                "Description"=$Manifest.Description;
                "Authors"=$Manifest.Author;
                "Company"=$Manifest.Company;
                "Copyright"=$Manifest.Copyright;
                "PackageTags"=$Manifest.Tags;

                "PackageProjectUrl"=$Manifest.Website;
                "PackageIconUrl"=$Manifest.Icon;
                "PackageReleaseNotes"=$Manifest.ReleaseNotes;
                "RepositoryUrl"=$Manifest.Repository;
                "PackageLicenseUrl"=$Manifest.License;
            }.GetEnumerator())
            {
                if (($token.Value -ne $null) -and (-not [string]::IsNullOrWhiteSpace($token.Value)))
                {
                    $hasChanges = $true;
                    $element = $doc.SelectSingleNode("//PropertyGroup/$($token.Name)");

                    $data = $null;
                    if ($token -match '(\n|[><])') { $data = $doc.CreateCDataSection($token.Value) }
                    else { $data = $doc.CreateTextNode($token.Value); }

                    if ($element -eq $null)
                    {
                        $hasNewNodes = $true;
                        $node = $doc.CreateElement($token.Name);
                        $node.AppendChild($data) | Out-Null;
                        $myGroup.AppendChild($node) | Out-Null;
                    }
                    else
                    {
                        $element.RemoveAll() | Out-Null;
                        $element.AppendChild($data) | Out-Null;
                    }
                }
            }

            if ($hasNewNodes)
            {
                $target = $doc.SelectSingleNode("/Project/PropertyGroup[1]");
                $doc.DocumentElement.InsertAfter($myGroup, $target) | Out-Null;
            }

            if ($PSCmdlet.ShouldProcess($InputObject)) { $doc.Save($path); }
            return $InputObject;
        }
        else { return $false; }
    }
}