-
Notifications
You must be signed in to change notification settings - Fork 511
/
Copy pathVersionTools.psm1
145 lines (133 loc) · 3.92 KB
/
VersionTools.psm1
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
135
136
137
138
139
140
141
142
143
144
145
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
#requires -Version 7.0
using namespace System.Management.Automation
$ChangelogFile = "CHANGELOG.md"
<#
.SYNOPSIS
Given the repository name, execute the script in its directory.
#>
function Use-Repository {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[ValidateSet("vscode-powershell", "PowerShellEditorServices")]
[string]$RepositoryName,
[Parameter(Mandatory)]
[scriptblock]$Script
)
try {
switch ($RepositoryName) {
"vscode-powershell" {
Push-Location -Path "$PSScriptRoot/../"
}
"PowerShellEditorServices" {
Push-Location -Path "$PSScriptRoot/../../PowerShellEditorServices"
}
}
& $Script
} finally {
Pop-Location
}
}
<#
.SYNOPSIS
Gets the unpublished content from the changelog.
.DESCRIPTION
This is used so that we can manually touch-up the automatically updated
changelog, and then bring its contents into the extension's changelog or
the GitHub release. It just gets the first header's contents.
#>
function Get-FirstChangelog {
param(
[Parameter(Mandatory)]
[string]$RepositoryName
)
$Changelog = Use-Repository -RepositoryName $RepositoryName -Script {
Get-Content -Path $ChangelogFile
}
# NOTE: The space after the header marker is important! Otherwise ### matches.
$Header = $Changelog.Where({$_.StartsWith("## ")}, "First")
$Changelog.Where(
{ $_ -eq $Header }, "SkipUntil"
).Where(
{ $_.StartsWith("## ") -and $_ -ne $Header }, "Until"
)
}
<#
.SYNOPSIS
Gets current version from changelog as `[semver]`.
#>
function Get-Version {
param(
[Parameter(Mandatory)]
[string]$RepositoryName
)
# NOTE: The first line should always be the header.
$Changelog = (Get-FirstChangelog -RepositoryName $RepositoryName)[0]
if ($Changelog -match '## v(?<version>\d+\.\d+\.\d+(-preview\.?\d*)?)') {
return [semver]$Matches.version
} else {
Write-Error "Couldn't find version from changelog!"
}
}
<#
.SYNOPSIS
Gets the version as a semantic version string without the 'v' prefix or
pre-release suffix.
#>
function Get-MajorMinorPatch {
param(
[Parameter(Mandatory)]
[semver]$Version
)
return "$($Version.Major).$($Version.Minor).$($Version.Patch)"
}
<#
.SYNOPSIS
Tests if this is a pre-release (specifically for the extension).
#>
function Test-IsPreRelease {
$Version = Get-Version -RepositoryName vscode-powershell
return [bool]$Version.PreReleaseLabel
}
<#
.SYNOPSIS
Validates the given version string.
#>
function Test-VersionIsValid {
param(
[Parameter(Mandatory)]
[ValidateSet("vscode-powershell", "PowerShellEditorServices")]
[string]$RepositoryName,
[Parameter(Mandatory)]
[string]$Version
)
if (!$Version.StartsWith("v")) {
throw "Version should start with 'v' prefix!"
}
$SemanticVersion = [semver]$Version.Substring(1)
switch ($RepositoryName) {
"vscode-powershell" {
$Date = Get-Date
if ($SemanticVersion.Major -ne $Date.Year) {
throw "Major version should be the current year!"
}
if ($SemanticVersion.Minor -ne $Date.Month) {
throw "Minor version should be the current month!"
}
if ($SemanticVersion.PreReleaseLabel) {
if ($SemanticVersion.PreReleaseLabel -ne "preview") {
throw "Suffix should only be 'preview'!"
}
}
}
"PowerShellEditorServices" {
if ($SemanticVersion.PreReleaseLabel) {
if ($SemanticVersion.PreReleaseLabel -ne "preview") {
throw "Suffix should only be 'preview'!"
}
}
}
}
}