Private/Edit-NetFrameworkProjectFile.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
function Edit-NetFrameworkProjectFile
{
    [CmdletBinding(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
    {
        $proj = Test-NetFrameworkProjectFile $InputObject;
        if ($proj)
        {
            $path = $proj.Path;
            $hasChanges = $false;
            $version = ConvertTo-NcrementVersionNumber $Manifest;
            $infoFile = Join-Path (Split-Path $path -Parent) "Properties/AssemblyInfo.cs";
            $contents = Get-Content $infoFile | Out-String;

            if ((Test-Path $infoFile))
            {
                foreach ($token in @{
                    "AssemblyTitle"=$Manifest.Name;
                    "AssemblyProduct"=$Manifest.Name;
                    "AssemblyCompany"=$Manifest.Company;
                    "AssemblyDescription"=$Manifest.Description;
                    "AssemblyCopyright"=$Manifest.Copyright;
                    "AssemblyInformationalVersion"=$version;
                    "AssemblyFileVersion"=$version;
                    "AssemblyVersion"=$version;
                }.GetEnumerator())
                {
                    if (($token.Value -ne $null) -and (-not [string]::IsNullOrWhiteSpace($token.Value)))
                    {
                        $hasChanges = $true;
                        $pattern = [string]::Format('(?i){0}\s*\(\s*(?<value>"?.*"?)\)', $token.Name);
                        $match = [Regex]::Match($contents, $pattern);

                        if ($match.Success)
                        {
                            $contents = [Regex]::Replace($contents, $pattern, "$($token.Name)(`"$($token.Value)`")");
                        }
                        else
                        {
                            $contents = [string]::Concat($contents.TrimEnd(), [Environment]::NewLine, "[assembly: $($token.Name)(`"$($token.Value)`")");
                        }
                    }
                }
                if ($PSCmdlet.ShouldProcess($infoFile)) { $contents | Out-File $infoFile -Encoding utf8; }
                return $infoFile;
            }
        }

        return $false;
    }
}