Skip to content

Commit b474b46

Browse files
committed
Move version functions to separate module
So the GitHub module isn't required.
1 parent e5528a9 commit b474b46

File tree

4 files changed

+164
-156
lines changed

4 files changed

+164
-156
lines changed

.vsts-ci/templates/publish-markets.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ steps:
77

88
- pwsh: |
99
npm ci --loglevel=error
10-
Import-Module $(Build.SourcesDirectory)/tools/ReleaseTools.psm1
10+
Import-Module $(Build.SourcesDirectory)/tools/VersionTools.psm1
1111
$Version = Get-Version -RepositoryName vscode-powershell
1212
$PackageVersion = Get-MajorMinorPatch -Version $Version
1313
$PublishArgs = @(

tools/ReleaseTools.psm1

+10-154
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,26 @@
66
using module PowerShellForGitHub
77
using namespace System.Management.Automation
88

9-
class RepoNames: IValidateSetValuesGenerator {
10-
# NOTE: This is super over-engineered, but it was fun.
11-
static [string[]] $Values = "vscode-powershell", "PowerShellEditorServices"
12-
[String[]] GetValidValues() { return [RepoNames]::Values }
13-
}
14-
15-
$ChangelogFile = "CHANGELOG.md"
9+
Import-Module ./VersionTools.psm1
1610

1711
<#
1812
.SYNOPSIS
19-
Given the repository name, execute the script in its directory.
13+
Creates and checks out `release` if not already on it.
2014
#>
21-
function Use-Repository {
22-
[CmdletBinding()]
15+
function Update-Branch {
16+
[CmdletBinding(SupportsShouldProcess)]
2317
param(
2418
[Parameter(Mandatory)]
2519
[ValidateSet([RepoNames])]
26-
[string]$RepositoryName,
27-
28-
[Parameter(Mandatory)]
29-
[scriptblock]$Script
20+
[string]$RepositoryName
3021
)
31-
try {
32-
switch ($RepositoryName) {
33-
"vscode-powershell" {
34-
Push-Location -Path "$PSScriptRoot/../"
35-
}
36-
"PowerShellEditorServices" {
37-
Push-Location -Path "$PSScriptRoot/../../PowerShellEditorServices"
22+
Use-Repository -RepositoryName $RepositoryName -Script {
23+
$Branch = git branch --show-current
24+
if ($Branch -ne "release") {
25+
if ($PSCmdlet.ShouldProcess("release", "git checkout -B")) {
26+
git checkout -B "release"
3827
}
3928
}
40-
& $Script
41-
} finally {
42-
Pop-Location
4329
}
4430
}
4531

@@ -143,136 +129,6 @@ function Get-Bullets {
143129
}
144130
}
145131

146-
<#
147-
.SYNOPSIS
148-
Gets the unpublished content from the changelog.
149-
.DESCRIPTION
150-
This is used so that we can manually touch-up the automatically updated
151-
changelog, and then bring its contents into the extension's changelog or
152-
the GitHub release. It just gets the first header's contents.
153-
#>
154-
function Get-FirstChangelog {
155-
param(
156-
[Parameter(Mandatory)]
157-
[ValidateSet([RepoNames])]
158-
[string]$RepositoryName
159-
)
160-
$Changelog = Use-Repository -RepositoryName $RepositoryName -Script {
161-
Get-Content -Path $ChangelogFile
162-
}
163-
# NOTE: The space after the header marker is important! Otherwise ### matches.
164-
$Header = $Changelog.Where({$_.StartsWith("## ")}, "First")
165-
$Changelog.Where(
166-
{ $_ -eq $Header }, "SkipUntil"
167-
).Where(
168-
{ $_.StartsWith("## ") -and $_ -ne $Header }, "Until"
169-
)
170-
}
171-
172-
<#
173-
.SYNOPSIS
174-
Creates and checks out `release` if not already on it.
175-
#>
176-
function Update-Branch {
177-
[CmdletBinding(SupportsShouldProcess)]
178-
param(
179-
[Parameter(Mandatory)]
180-
[ValidateSet([RepoNames])]
181-
[string]$RepositoryName
182-
)
183-
Use-Repository -RepositoryName $RepositoryName -Script {
184-
$Branch = git branch --show-current
185-
if ($Branch -ne "release") {
186-
if ($PSCmdlet.ShouldProcess("release", "git checkout -B")) {
187-
git checkout -B "release"
188-
}
189-
}
190-
}
191-
}
192-
193-
<#
194-
.SYNOPSIS
195-
Gets current version from changelog as `[semver]`.
196-
#>
197-
function Get-Version {
198-
param(
199-
[Parameter(Mandatory)]
200-
[ValidateSet([RepoNames])]
201-
[string]$RepositoryName
202-
)
203-
# NOTE: The first line should always be the header.
204-
$Changelog = (Get-FirstChangelog -RepositoryName $RepositoryName)[0]
205-
if ($Changelog -match '## v(?<version>\d+\.\d+\.\d+(-preview\.?\d*)?)') {
206-
return [semver]$Matches.version
207-
} else {
208-
Write-Error "Couldn't find version from changelog!"
209-
}
210-
}
211-
212-
<#
213-
.SYNOPSIS
214-
Gets the version as a semantic version string without the 'v' prefix or
215-
pre-release suffix.
216-
#>
217-
function Get-MajorMinorPatch {
218-
param(
219-
[Parameter(Mandatory)]
220-
[semver]$Version
221-
)
222-
return "$($Version.Major).$($Version.Minor).$($Version.Patch)"
223-
}
224-
225-
<#
226-
.SYNOPSIS
227-
Tests if this is a pre-release (specifically for the extension).
228-
#>
229-
function Test-IsPreRelease {
230-
$Version = Get-Version -RepositoryName vscode-powershell
231-
return [bool]$Version.PreReleaseLabel
232-
}
233-
234-
<#
235-
.SYNOPSIS
236-
Validates the given version string.
237-
#>
238-
function Test-VersionIsValid {
239-
param(
240-
[Parameter(Mandatory)]
241-
[ValidateSet([RepoNames])]
242-
[string]$RepositoryName,
243-
244-
[Parameter(Mandatory)]
245-
[string]$Version
246-
)
247-
if (!$Version.StartsWith("v")) {
248-
throw "Version should start with 'v' prefix!"
249-
}
250-
251-
$SemanticVersion = [semver]$Version.Substring(1)
252-
switch ($RepositoryName) {
253-
"vscode-powershell" {
254-
$Date = Get-Date
255-
if ($SemanticVersion.Major -ne $Date.Year) {
256-
throw "Major version should be the current year!"
257-
}
258-
if ($SemanticVersion.Minor -ne $Date.Month) {
259-
throw "Minor version should be the current month!"
260-
}
261-
if ($SemanticVersion.PreReleaseLabel) {
262-
if ($SemanticVersion.PreReleaseLabel -ne "preview") {
263-
throw "Suffix should only be 'preview'!"
264-
}
265-
}
266-
}
267-
"PowerShellEditorServices" {
268-
if ($SemanticVersion.PreReleaseLabel) {
269-
throw "Version shouldn't have a pre-release label!"
270-
}
271-
}
272-
}
273-
return $true
274-
}
275-
276132
<#
277133
.SYNOPSIS
278134
Updates the CHANGELOG file with PRs merged since the last release.

tools/VersionTools.psm1

+152
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT License.
3+
4+
#requires -Version 7.0
5+
6+
using namespace System.Management.Automation
7+
8+
class RepoNames: IValidateSetValuesGenerator {
9+
# NOTE: This is super over-engineered, but it was fun.
10+
static [string[]] $Values = "vscode-powershell", "PowerShellEditorServices"
11+
[String[]] GetValidValues() { return [RepoNames]::Values }
12+
}
13+
14+
$ChangelogFile = "CHANGELOG.md"
15+
16+
<#
17+
.SYNOPSIS
18+
Given the repository name, execute the script in its directory.
19+
#>
20+
function Use-Repository {
21+
[CmdletBinding()]
22+
param(
23+
[Parameter(Mandatory)]
24+
[ValidateSet([RepoNames])]
25+
[string]$RepositoryName,
26+
27+
[Parameter(Mandatory)]
28+
[scriptblock]$Script
29+
)
30+
try {
31+
switch ($RepositoryName) {
32+
"vscode-powershell" {
33+
Push-Location -Path "$PSScriptRoot/../"
34+
}
35+
"PowerShellEditorServices" {
36+
Push-Location -Path "$PSScriptRoot/../../PowerShellEditorServices"
37+
}
38+
}
39+
& $Script
40+
} finally {
41+
Pop-Location
42+
}
43+
}
44+
45+
<#
46+
.SYNOPSIS
47+
Gets the unpublished content from the changelog.
48+
.DESCRIPTION
49+
This is used so that we can manually touch-up the automatically updated
50+
changelog, and then bring its contents into the extension's changelog or
51+
the GitHub release. It just gets the first header's contents.
52+
#>
53+
function Get-FirstChangelog {
54+
param(
55+
[Parameter(Mandatory)]
56+
[ValidateSet([RepoNames])]
57+
[string]$RepositoryName
58+
)
59+
$Changelog = Use-Repository -RepositoryName $RepositoryName -Script {
60+
Get-Content -Path $ChangelogFile
61+
}
62+
# NOTE: The space after the header marker is important! Otherwise ### matches.
63+
$Header = $Changelog.Where({$_.StartsWith("## ")}, "First")
64+
$Changelog.Where(
65+
{ $_ -eq $Header }, "SkipUntil"
66+
).Where(
67+
{ $_.StartsWith("## ") -and $_ -ne $Header }, "Until"
68+
)
69+
}
70+
71+
<#
72+
.SYNOPSIS
73+
Gets current version from changelog as `[semver]`.
74+
#>
75+
function Get-Version {
76+
param(
77+
[Parameter(Mandatory)]
78+
[ValidateSet([RepoNames])]
79+
[string]$RepositoryName
80+
)
81+
# NOTE: The first line should always be the header.
82+
$Changelog = (Get-FirstChangelog -RepositoryName $RepositoryName)[0]
83+
if ($Changelog -match '## v(?<version>\d+\.\d+\.\d+(-preview\.?\d*)?)') {
84+
return [semver]$Matches.version
85+
} else {
86+
Write-Error "Couldn't find version from changelog!"
87+
}
88+
}
89+
90+
<#
91+
.SYNOPSIS
92+
Gets the version as a semantic version string without the 'v' prefix or
93+
pre-release suffix.
94+
#>
95+
function Get-MajorMinorPatch {
96+
param(
97+
[Parameter(Mandatory)]
98+
[semver]$Version
99+
)
100+
return "$($Version.Major).$($Version.Minor).$($Version.Patch)"
101+
}
102+
103+
<#
104+
.SYNOPSIS
105+
Tests if this is a pre-release (specifically for the extension).
106+
#>
107+
function Test-IsPreRelease {
108+
$Version = Get-Version -RepositoryName vscode-powershell
109+
return [bool]$Version.PreReleaseLabel
110+
}
111+
112+
<#
113+
.SYNOPSIS
114+
Validates the given version string.
115+
#>
116+
function Test-VersionIsValid {
117+
param(
118+
[Parameter(Mandatory)]
119+
[ValidateSet([RepoNames])]
120+
[string]$RepositoryName,
121+
122+
[Parameter(Mandatory)]
123+
[string]$Version
124+
)
125+
if (!$Version.StartsWith("v")) {
126+
throw "Version should start with 'v' prefix!"
127+
}
128+
129+
$SemanticVersion = [semver]$Version.Substring(1)
130+
switch ($RepositoryName) {
131+
"vscode-powershell" {
132+
$Date = Get-Date
133+
if ($SemanticVersion.Major -ne $Date.Year) {
134+
throw "Major version should be the current year!"
135+
}
136+
if ($SemanticVersion.Minor -ne $Date.Month) {
137+
throw "Minor version should be the current month!"
138+
}
139+
if ($SemanticVersion.PreReleaseLabel) {
140+
if ($SemanticVersion.PreReleaseLabel -ne "preview") {
141+
throw "Suffix should only be 'preview'!"
142+
}
143+
}
144+
}
145+
"PowerShellEditorServices" {
146+
if ($SemanticVersion.PreReleaseLabel) {
147+
throw "Version shouldn't have a pre-release label!"
148+
}
149+
}
150+
}
151+
return $true
152+
}

vscode-powershell.build.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ param(
1010
#Requires -Modules @{ ModuleName = "InvokeBuild"; ModuleVersion = "3.0.0" }
1111

1212
# Sanity check our changelog version versus package.json (which lacks pre-release label)
13-
Import-Module $PSScriptRoot/tools/ReleaseTools.psm1
13+
Import-Module $PSScriptRoot/tools/VersionTools.psm1
1414
$script:Version = Get-Version -RepositoryName vscode-powershell
1515
$script:PackageVersion = Get-MajorMinorPatch -Version $Version
1616
$script:PackageJson = Get-Content -Raw $PSScriptRoot/package.json | ConvertFrom-Json

0 commit comments

Comments
 (0)