Skip to content

Commit 0e40843

Browse files
committed
Fix side effect logic in ReleaseTools module
This uses a new function `Use-Repository` that executes the given script with the given repository as the current working directory, replacing all the previously side effect driven logic, or at least, making it more explicit. This was frankly easier than dealing with the Azure Pipeline that clones the repository under a different name (specifically `s` if its the only repo, that is, it's not named `vscode-powershell` in the release stage, so the module broke).
1 parent 7410837 commit 0e40843

File tree

1 file changed

+119
-86
lines changed

1 file changed

+119
-86
lines changed

tools/ReleaseTools.psm1

Lines changed: 119 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,41 @@ class RepoNames: IValidateSetValuesGenerator {
1414

1515
$ChangelogFile = "CHANGELOG.md"
1616

17+
<#
18+
.SYNOPSIS
19+
Given the repository name, execute the script in its directory.
20+
#>
21+
function Use-Repository {
22+
[CmdletBinding()]
23+
param(
24+
[Parameter(Mandatory)]
25+
[ValidateSet([RepoNames])]
26+
[string]$RepositoryName,
27+
28+
[Parameter(Mandatory)]
29+
[scriptblock]$Script
30+
)
31+
try {
32+
switch ($RepositoryName) {
33+
"vscode-powershell" {
34+
Push-Location -Path "$PSScriptRoot/../"
35+
}
36+
"PowerShellEditorServices" {
37+
Push-Location -Path "$PSScriptRoot/../../PowerShellEditorServices"
38+
}
39+
}
40+
& $Script
41+
} finally {
42+
Pop-Location
43+
}
44+
}
45+
1746
<#
1847
.SYNOPSIS
1948
Given a collection of PRs, generates a bulleted list.
2049
#>
2150
function Get-Bullets {
51+
[CmdletBinding()]
2252
param(
2353
[Parameter(Mandatory)]
2454
[ValidateSet([RepoNames])]
@@ -124,7 +154,9 @@ function Get-FirstChangelog {
124154
[ValidateSet([RepoNames])]
125155
[string]$RepositoryName
126156
)
127-
$Changelog = Get-Content -Path "$PSScriptRoot/../../$RepositoryName/$ChangelogFile"
157+
$Changelog = Use-Repository -Name $RepositoryName -Script {
158+
Get-Content -Path $ChangelogFile
159+
}
128160
# NOTE: The space after the header marker is important! Otherwise ### matches.
129161
$Header = $Changelog.Where({$_.StartsWith("## ")}, "First")
130162
$Changelog.Where(
@@ -140,10 +172,17 @@ function Get-FirstChangelog {
140172
#>
141173
function Update-Branch {
142174
[CmdletBinding(SupportsShouldProcess)]
143-
$Branch = git branch --show-current
144-
if ($Branch -ne "release") {
145-
if ($PSCmdlet.ShouldProcess("release", "git checkout -b")) {
146-
git checkout -b "release"
175+
param(
176+
[Parameter(Mandatory)]
177+
[ValidateSet([RepoNames])]
178+
[string]$RepositoryName
179+
)
180+
Use-Repository -Name $RepositoryName -Script {
181+
$Branch = git branch --show-current
182+
if ($Branch -ne "release") {
183+
if ($PSCmdlet.ShouldProcess("release", "git checkout -b")) {
184+
git checkout -b "release"
185+
}
147186
}
148187
}
149188
}
@@ -187,12 +226,12 @@ function Update-Changelog {
187226
[ValidateScript({ $_.StartsWith("v") })]
188227
[string]$Version
189228
)
190-
# NOTE: This a side effect neccesary for Git operations to work.
191-
Push-Location -Path "$PSScriptRoot/../../$RepositoryName"
192229

193230
# Get the repo object, latest release, and commits since its tag.
194231
$Repo = Get-GitHubRepository -OwnerName PowerShell -RepositoryName $RepositoryName
195-
$Commits = git rev-list "v$(Get-Version -RepositoryName $RepositoryName)..."
232+
$Commits = Use-Repository -Name $RepositoryName -Script {
233+
git rev-list "v$(Get-Version -RepositoryName $RepositoryName)..."
234+
}
196235

197236
# NOTE: This is a slow API as it gets all PRs, and then filters.
198237
$Bullets = $Repo | Get-GitHubPullRequest -State All |
@@ -220,28 +259,27 @@ function Update-Changelog {
220259
}
221260
}
222261

223-
$CurrentChangelog = Get-Content -Path $ChangelogFile
224-
225-
@(
226-
$CurrentChangelog[0..1]
227-
"## $Version"
228-
"### $([datetime]::Now.ToString('dddd, MMMM dd, yyyy'))"
229-
""
230-
$NewSection
231-
""
232-
$CurrentChangelog[2..$CurrentChangelog.Length]
233-
) | Set-Content -Encoding utf8NoBOM -Path $ChangelogFile
234-
235-
Update-Branch
236-
237-
if ($PSCmdlet.ShouldProcess("$RepositoryName/$ChangelogFile", "git commit")) {
238-
git add $ChangelogFile
239-
git commit -m "Update CHANGELOG for ``$Version``"
262+
Update-Branch -RepositoryName $RepositoryName
263+
264+
Use-Repository -Name $RepositoryName -Script {
265+
$CurrentChangelog = Get-Content -Path $ChangelogFile
266+
@(
267+
$CurrentChangelog[0..1]
268+
"## $Version"
269+
"### $([datetime]::Now.ToString('dddd, MMMM dd, yyyy'))"
270+
""
271+
$NewSection
272+
""
273+
$CurrentChangelog[2..$CurrentChangelog.Length]
274+
) | Set-Content -Encoding utf8NoBOM -Path $ChangelogFile
275+
276+
if ($PSCmdlet.ShouldProcess("$RepositoryName/$ChangelogFile", "git commit")) {
277+
git add $ChangelogFile
278+
git commit -m "Update CHANGELOG for ``$Version``"
279+
}
240280
}
241281

242282
Update-Version -RepositoryName $RepositoryName
243-
244-
Pop-Location
245283
}
246284

247285
<#
@@ -274,63 +312,62 @@ function Update-Version {
274312
[ValidateSet([RepoNames])]
275313
[string]$RepositoryName
276314
)
277-
# NOTE: This a side effect neccesary for Git operations to work.
278-
Push-Location -Path "$PSScriptRoot/../../$RepositoryName"
279-
280315
$Version = Get-Version -RepositoryName $RepositoryName
281316
$v = "$($Version.Major).$($Version.Minor).$($Version.Patch)"
317+
318+
Update-Branch -RepositoryName $RepositoryName
319+
282320
# TODO: Maybe cleanup the replacement logic.
283-
switch ($RepositoryName) {
284-
"vscode-powershell" {
285-
$d = "Develop PowerShell modules, commands and scripts in Visual Studio Code!"
286-
if ($Version.PreReleaseLabel) {
287-
$name = "powershell-preview"
288-
$displayName = "PowerShell Preview"
289-
$preview = "true"
290-
$description = "(Preview) $d"
291-
} else {
292-
$name = "powershell"
293-
$displayName = "PowerShell"
294-
$preview = "false"
295-
$description = $d
321+
Use-Repository -Name $RepositoryName -Script {
322+
switch ($RepositoryName) {
323+
"vscode-powershell" {
324+
$d = "Develop PowerShell modules, commands and scripts in Visual Studio Code!"
325+
if ($Version.PreReleaseLabel) {
326+
$name = "powershell-preview"
327+
$displayName = "PowerShell Preview"
328+
$preview = "true"
329+
$description = "(Preview) $d"
330+
} else {
331+
$name = "powershell"
332+
$displayName = "PowerShell"
333+
$preview = "false"
334+
$description = $d
335+
}
336+
337+
$path = "package.json"
338+
$f = Get-Content -Path $path
339+
# NOTE: The prefix regex match two spaces exactly to avoid matching
340+
# nested objects in the file.
341+
$f = $f -replace '^(?<prefix> "name":\s+")(.+)(?<suffix>",)$', "`${prefix}${name}`${suffix}"
342+
$f = $f -replace '^(?<prefix> "displayName":\s+")(.+)(?<suffix>",)$', "`${prefix}${displayName}`${suffix}"
343+
$f = $f -replace '^(?<prefix> "version":\s+")(.+)(?<suffix>",)$', "`${prefix}${v}`${suffix}"
344+
$f = $f -replace '^(?<prefix> "preview":\s+)(.+)(?<suffix>,)$', "`${prefix}${preview}`${suffix}"
345+
$f = $f -replace '^(?<prefix> "description":\s+")(.+)(?<suffix>",)$', "`${prefix}${description}`${suffix}"
346+
$f | Set-Content -Path $path
347+
git add $path
348+
}
349+
"PowerShellEditorServices" {
350+
$path = "PowerShellEditorServices.Common.props"
351+
$f = Get-Content -Path $path
352+
$f = $f -replace '^(?<prefix>\s+<VersionPrefix>)(.+)(?<suffix></VersionPrefix>)$', "`${prefix}${v}`${suffix}"
353+
$f = $f -replace '^(?<prefix>\s+<VersionSuffix>)(.*)(?<suffix></VersionSuffix>)$', "`${prefix}$($Version.PreReleaseLabel)`${suffix}"
354+
$f | Set-Content -Path $path
355+
git add $path
356+
357+
$path = "module/PowerShellEditorServices/PowerShellEditorServices.psd1"
358+
$f = Get-Content -Path $path
359+
$f = $f -replace "^(?<prefix>ModuleVersion = ')(.+)(?<suffix>')`$", "`${prefix}${v}`${suffix}"
360+
$f | Set-Content -Path $path
361+
git add $path
296362
}
297-
$path = "package.json"
298-
$f = Get-Content -Path $path
299-
# NOTE: The prefix regex match two spaces exactly to avoid matching
300-
# nested objects in the file.
301-
$f = $f -replace '^(?<prefix> "name":\s+")(.+)(?<suffix>",)$', "`${prefix}${name}`${suffix}"
302-
$f = $f -replace '^(?<prefix> "displayName":\s+")(.+)(?<suffix>",)$', "`${prefix}${displayName}`${suffix}"
303-
$f = $f -replace '^(?<prefix> "version":\s+")(.+)(?<suffix>",)$', "`${prefix}${v}`${suffix}"
304-
$f = $f -replace '^(?<prefix> "preview":\s+)(.+)(?<suffix>,)$', "`${prefix}${preview}`${suffix}"
305-
$f = $f -replace '^(?<prefix> "description":\s+")(.+)(?<suffix>",)$', "`${prefix}${description}`${suffix}"
306-
$f | Set-Content -Path $path
307-
git add $path
308-
}
309-
"PowerShellEditorServices" {
310-
$path = "PowerShellEditorServices.Common.props"
311-
$f = Get-Content -Path $path
312-
$f = $f -replace '^(?<prefix>\s+<VersionPrefix>)(.+)(?<suffix></VersionPrefix>)$', "`${prefix}${v}`${suffix}"
313-
$f = $f -replace '^(?<prefix>\s+<VersionSuffix>)(.*)(?<suffix></VersionSuffix>)$', "`${prefix}$($Version.PreReleaseLabel)`${suffix}"
314-
$f | Set-Content -Path $path
315-
git add $path
316-
317-
$path = "module/PowerShellEditorServices/PowerShellEditorServices.psd1"
318-
$f = Get-Content -Path $path
319-
$f = $f -replace "^(?<prefix>ModuleVersion = ')(.+)(?<suffix>')`$", "`${prefix}${v}`${suffix}"
320-
$f | Set-Content -Path $path
321-
git add $path
322363
}
323-
}
324-
325-
Update-Branch
326364

327-
if ($PSCmdlet.ShouldProcess("$RepositoryName/v$Version", "git commit")) {
328-
git commit -m "Bump version to ``v$Version``"
329-
} # TODO: Git reset to unstage
365+
if ($PSCmdlet.ShouldProcess("$RepositoryName/v$Version", "git commit")) {
366+
git commit -m "Bump version to ``v$Version``"
367+
} # TODO: Git reset to unstage
368+
}
330369

331370
New-ReleasePR -RepositoryName $RepositoryName
332-
333-
Pop-Location
334371
}
335372

336373
<#
@@ -346,17 +383,15 @@ function New-ReleasePR {
346383
[ValidateSet([RepoNames])]
347384
[string]$RepositoryName
348385
)
349-
# NOTE: This a side effect neccesary for Git operations to work.
350-
Push-Location -Path "$PSScriptRoot/../../$RepositoryName"
351-
352386
$Version = Get-Version -RepositoryName $RepositoryName
353387
$Branch = "release/v$Version"
354388

355-
Update-Branch -Version $Version
356-
357-
if ($PSCmdlet.ShouldProcess("$RepositoryName/$Branch", "git push")) {
358-
Write-Host "Pushing branch ``$Branch``..."
359-
git push origin $Branch
389+
Update-Branch -RepositoryName $RepositoryName
390+
Use-Repository -Name $RepositoryName -Script {
391+
if ($PSCmdlet.ShouldProcess("$RepositoryName/$Branch", "git push")) {
392+
Write-Host "Pushing branch ``$Branch``..."
393+
git push origin $Branch
394+
}
360395
}
361396

362397
$Repo = Get-GitHubRepository -OwnerName PowerShell -RepositoryName $RepositoryName
@@ -376,8 +411,6 @@ function New-ReleasePR {
376411
# NOTE: The API is weird. According to GitHub, all PRs are Issues, so this
377412
# works, but the module doesn't support it as easily as it could.
378413
$Repo | Add-GitHubIssueLabel -Issue $PR.PullRequestNumber -LabelName "Ignore"
379-
380-
Pop-Location
381414
}
382415

383416
<#

0 commit comments

Comments
 (0)