Skip to content

Commit 80277da

Browse files
committed
Move version functions to separate module
So the GitHub module isn't required.
1 parent 171e717 commit 80277da

File tree

4 files changed

+162
-156
lines changed

4 files changed

+162
-156
lines changed

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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 10 additions & 154 deletions
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

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

vscode-powershell.build.ps1

Lines changed: 1 addition & 1 deletion
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)