Skip to content

Commit d1ee082

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

File tree

4 files changed

+155
-151
lines changed

4 files changed

+155
-151
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: 11 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,32 @@
66
using module PowerShellForGitHub
77
using namespace System.Management.Automation
88

9+
Import-Module ./VersionTools.psm1
10+
911
class RepoNames: IValidateSetValuesGenerator {
1012
# NOTE: This is super over-engineered, but it was fun.
1113
static [string[]] $Values = "vscode-powershell", "PowerShellEditorServices"
1214
[String[]] GetValidValues() { return [RepoNames]::Values }
1315
}
1416

15-
$ChangelogFile = "CHANGELOG.md"
16-
1717
<#
1818
.SYNOPSIS
19-
Given the repository name, execute the script in its directory.
19+
Creates and checks out `release` if not already on it.
2020
#>
21-
function Use-Repository {
22-
[CmdletBinding()]
21+
function Update-Branch {
22+
[CmdletBinding(SupportsShouldProcess)]
2323
param(
2424
[Parameter(Mandatory)]
2525
[ValidateSet([RepoNames])]
26-
[string]$RepositoryName,
27-
28-
[Parameter(Mandatory)]
29-
[scriptblock]$Script
26+
[string]$RepositoryName
3027
)
31-
try {
32-
switch ($RepositoryName) {
33-
"vscode-powershell" {
34-
Push-Location -Path "$PSScriptRoot/../"
35-
}
36-
"PowerShellEditorServices" {
37-
Push-Location -Path "$PSScriptRoot/../../PowerShellEditorServices"
28+
Use-Repository -RepositoryName $RepositoryName -Script {
29+
$Branch = git branch --show-current
30+
if ($Branch -ne "release") {
31+
if ($PSCmdlet.ShouldProcess("release", "git checkout -B")) {
32+
git checkout -B "release"
3833
}
3934
}
40-
& $Script
41-
} finally {
42-
Pop-Location
4335
}
4436
}
4537

@@ -143,136 +135,6 @@ function Get-Bullets {
143135
}
144136
}
145137

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-
276138
<#
277139
.SYNOPSIS
278140
Updates the CHANGELOG file with PRs merged since the last release.

tools/VersionTools.psm1

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

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)