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
66
<#
.SYNOPSIS
Updates a 'net-framework' all projects in a directory using the in the specified [Manifest] object.
 
.PARAMETER $Manifest
The [Manifest] object.
 
.PARAMETER Path
The project directory.
#>


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

        [string]$Path
    )

    $modifiedFiles = [System.Collections.ArrayList]::new();

    foreach ($csproj in (Get-ChildItem $Path -Recurse -Filter "*.csproj"))
    {
        [string]$assemblyInfo = "$(Split-Path $csproj.FullName -Parent)\Properties\AssemblyInfo.cs";
        if ((Test-Path $assemblyInfo) -and ($PSCmdlet.ShouldProcess($assemblyInfo)))
        {
            $version = $Manifest | Convert-NcrementVersionNumberToString;
            $contents = Get-Content $assemblyInfo | Out-String;
            foreach ($token in @(
                [Ncrement.Token]::new("Title", "`"$($manifest.Name)`""),
                [Ncrement.Token]::new("Product", "`"$($manifest.Name)`""),
                [Ncrement.Token]::new("Company", "`"$($manifest.Company)`""),
                [Ncrement.Token]::new("Description", "`"$($manifest.Description)`""),
                [Ncrement.Token]::new("Copyright", "`"$($manifest.Copyright)`""),
                [Ncrement.Token]::new("InformationalVersion", "`"$($version)`""),
                [Ncrement.Token]::new("FileVersion", "`"$($version)`""),
                [Ncrement.Token]::new("Version", "`"$($version)`"")
            ))
            {
                if (-not [string]::IsNullOrEmpty($token.Value))
                {
                    $matches = [Regex]::Matches($contents, [string]::Format('(?i)Assembly{0}\s*\(\s*(?<value>"?.*"?)\)', $token.TagName));
                    if ($matches.Count -ge 1)
                    {
                        foreach ($match in $matches)
                        {
                            $value = $match.Groups["value"];
                            $contents = $contents.Remove($value.Index, $value.Length);
                            $contents = $contents.Insert($value.Index, $token.Value);
                        }
                    }
                    else
                    {
                        $contents = [string]::Concat($contents.TrimEnd(), [System.Environment]::NewLine, "[assembly: Assembly$($token.TagName)($($token.Value))]");
                    }
                }
            }
            $contents | Out-File $assemblyInfo -Encoding utf8;
        }

        if (Test-Path $assemblyInfo) { $modifiedFiles.Add($assemblyInfo) | Out-Null; }
    }

    return $modifiedFiles;
}