Public/Update-NcrementProjectFile.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<#
.SYNOPSIS
Updates all project/manifest files within the specified directory.
 
.DESCRIPTION
This cmdlet will update all project/manifest files within the specified directory using the provided [Manifest] object. When the -CommitChanges flag is present, all files modified by this cmdlet will be committed to GIT. Aslo when both the -TagCommit flag and -CommitChanges flag is present this cmdlet will tag the commit with the [Manifest] version number. Use the -Major and -Minor flags to increment the manifest version number.
 
The cmdlet returns a [PSCustomObject] containing a list of the files it modified and the [Manifest] object.
 
.PARAMETER Path
The root directory of your project(s).
 
.PARAMETER Manifest
A [Manifest] object.
 
.PARAMETER CommitMessage
The message to use when the -CommitChanges flag is present.
 
.PARAMETER CommitChanges
Determines whether to commit the modified file to source control (using Git).
 
.PARAMETER TagCommit
Determines whether the Git commit should be tagged with the current version number.
 
.OUTPUTS
[System.Management.Automation.PSCustomObject]
 
.EXAMPLE
"C:\manifest.json" | Get-NcrementManifest | Update-AllProjectManifests "C:\projects\new_idea" -Minor;
This example increments the project's version number.
 
.EXAMPLE
"C:\manifest.json" | Get-NcrementManifest | Update-AllProjectManifests "C:\projects\new_idea" -Major -Commit -Tag;
This example increments the project's version number and commits the changes to source control.
 
.LINK
Get-NcrementManifest
 
.LINK
Save-NcrementManifest
#>


function Update-NcrementProjectFile
{
    [CmdletBinding(SupportsShouldProcess)]
    Param(
        [Parameter(Mandatory, ValueFromPipeline, Position=1)]
        $Manifest,

        [Parameter(Position=0)]
        [Alias("root", "dir", "folder")]
        [string]$Path,

        [Alias('m', "msg", "message")]
        [string]$CommitMessage,

        [Alias('c', "commit")]
        [switch]$CommitChanges,

        [Alias('t', "tag")]
        [switch]$TagCommit
    )

    # Resolving the path.
    if ([string]::IsNullOrEmpty($Path))
    { $Path = $PWD; }
    elseif (Test-Path $Path -PathType Leaf)
    { $Path = Split-Path $Path -Parent; }
    elseif (-not (Test-Path $Path))
    { throw "Could not find directory at '$Path'."; }

    # Update project files.
    $modifiedFiles = [System.Collections.ArrayList]::new();
    $modifiedFiles.Add($Manifest.Path);

    foreach ($result in @(
        (Edit-NetCoreProjectFile $Manifest $Path),
        (Edit-NetFrameworkProjectFile $Manifest $Path),
        (Edit-VSIXManifestFile $Manifest $Path),
        (Edit-PSManifestFile $Manifest $Path),
        (Edit-PackageJsonFile $Manifest $Path)
    ))
    {
        if ($result -ne $null)
        {
                foreach ($file in $result)
                {
                    $modifiedFiles.Add($file);
                    Write-Debug "'$($file.Replace($Path, '.'))' was modified.";
                }
        }
    }

    if ($CommitChanges)
    {
        try
        {
            Push-Location $Path;
            # Staging the modified files.
            foreach ($file in $modifiedFiles)
            {
                if ($PSCmdlet.ShouldProcess($file, "git add"))
                {
                    &git add "$file" | Out-Null;
                    Write-Debug "Staged '$($file.Replace($Path, '.'))'.";
                }
            }

            # Resolving the commit message.
            $version = $Manifest | Convert-NcrementVersionNumberToString;
            if ([string]::IsNullOrEmpty($CommitMessage))
            { $CommitMessage = "Increment the version number to '$version'."; }
            else
            { $CommitMessage = [string]::Format($CommitMessage, $version); }

            # Commiting files to source control.
            if ($PSCmdlet.ShouldProcess($Path, "git commit"))
            {
                &git commit -m $CommitMessage | Out-Null;
                if ($TagCommit) { &git tag v$version | Out-Null; }
                Write-Information "Committed modified project files to source control.";
            }
        }
        finally { Pop-Location; }
    }

    return New-Object PSObject -Property @{
        "Manifest"=$Manifest;
        "ModifiedFiles"=$modifiedFiles;
    };
}

Set-Alias -Name "n-update" -Value "Update-NcrementProjectFile";
Set-Alias -Name "Update-Project" -Value "Update-NcrementProjectFile";