Skip to content

Commit 01a86e9

Browse files
authored
Add scripts for automatically updating the version of the PowerShell extension (#1932)
* Add AzureDataStudio update script
1 parent 86de851 commit 01a86e9

File tree

5 files changed

+1234
-0
lines changed

5 files changed

+1234
-0
lines changed

tools/FileUpdateTools.psm1

Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
#requires -Version 6.0
5+
6+
function ConvertToJToken
7+
{
8+
param(
9+
[Parameter()]
10+
$Object
11+
)
12+
13+
if ($null -eq $Object)
14+
{
15+
return [Newtonsoft.Json.Linq.JValue]::CreateNull()
16+
}
17+
18+
if ($Object -is [pscustomobject])
19+
{
20+
$jObject = [Newtonsoft.Json.Linq.JObject]::new()
21+
foreach ($field in $Object)
22+
{
23+
$jObject.Add($field, $Object.$field)
24+
}
25+
return $jObject
26+
}
27+
28+
if ($Object -is [version])
29+
{
30+
return [Newtonsoft.Json.Linq.JToken]::new($Object.ToString())
31+
}
32+
33+
return (,[Newtonsoft.Json.Linq.JToken]::FromObject($Object))
34+
}
35+
36+
function New-StringWithSegment
37+
{
38+
[OutputType([string])]
39+
param(
40+
[Parameter(Mandatory)]
41+
[string]
42+
$String,
43+
44+
[Parameter(Mandatory)]
45+
[string]
46+
$NewSegment,
47+
48+
[Parameter(Mandatory)]
49+
[int]
50+
$StartIndex,
51+
52+
[Parameter()]
53+
[int]
54+
$EndIndex = $StartIndex,
55+
56+
[switch]
57+
$AutoIndent
58+
)
59+
60+
if ($AutoIndent)
61+
{
62+
$indentBuilder = [System.Text.StringBuilder]::new()
63+
$indentIdx = $StartIndex - 1
64+
$currChar = $String[$indentIdx]
65+
while ($currChar -ne "`n")
66+
{
67+
$null = $indentBuilder.Append($currChar)
68+
$indentIdx--
69+
$currChar = $String[$indentIdx]
70+
}
71+
$indent = $indentBuilder.ToString()
72+
}
73+
else
74+
{
75+
$indent = ''
76+
}
77+
78+
$newStringBuilder = [System.Text.StringBuilder]::new()
79+
$null = $newStringBuilder.Append($String.Substring(0, $StartIndex))
80+
81+
$segmentLines = $NewSegment.Split("`n")
82+
83+
$null = $newStringBuilder.Append($segmentLines[0])
84+
for ($i = 1; $i -lt $segmentLines.Length; $i++)
85+
{
86+
$null = $newStringBuilder.Append("`n").Append($indent).Append($segmentLines[$i])
87+
}
88+
89+
$null = $newStringBuilder.Append($String.Substring($EndIndex))
90+
91+
return $newStringBuilder.ToString()
92+
}
93+
94+
function Get-StringOffsetFromSpan
95+
{
96+
[OutputType([int])]
97+
param(
98+
[Parameter()]
99+
[string]
100+
$String,
101+
102+
[Parameter()]
103+
[int]
104+
$EndLine,
105+
106+
[Parameter()]
107+
[int]
108+
$StartLine = 1,
109+
110+
[Parameter()]
111+
[int]
112+
$Column = 0,
113+
114+
[Parameter()]
115+
[int]
116+
$InitialOffset = 0
117+
)
118+
119+
$lfChar = 0xA
120+
121+
$idx = $InitialOffset
122+
$spanLines = $EndLine - $StartLine
123+
for ($i = 0; $i -lt $spanLines; $i++)
124+
{
125+
$idx = $String.IndexOf($lfChar, $idx + 1)
126+
127+
if ($idx -lt 0)
128+
{
129+
return $idx
130+
}
131+
}
132+
133+
return $idx + $Column
134+
}
135+
136+
function ConvertTo-IndentedJson
137+
{
138+
param(
139+
[Parameter(Position=0)]
140+
$Object,
141+
142+
[Parameter()]
143+
[int]
144+
$IndentWidth = 4,
145+
146+
[Parameter()]
147+
[char]
148+
$IndentChar = ' '
149+
)
150+
151+
# Convert the object to a JToken
152+
$jObject = ConvertToJToken $Object
153+
154+
# Reformat the entry with tab-based indentation, like the existing file
155+
$stringBuilder = [System.Text.StringBuilder]::new()
156+
try
157+
{
158+
$stringWriter = [System.IO.StringWriter]::new($stringBuilder)
159+
$jsonWriter = [Newtonsoft.Json.JsonTextWriter]::new($stringWriter)
160+
$jsonWriter.Indentation = $IndentWidth
161+
$jsonWriter.IndentChar = $IndentChar
162+
$jsonWriter.Formatting = 'Indented'
163+
$null = $jObject.WriteTo($jsonWriter)
164+
}
165+
finally
166+
{
167+
$jsonWriter.Dispose()
168+
$stringWriter.Dispose()
169+
}
170+
return $stringBuilder.ToString().Replace([System.Environment]::NewLine, "`r`n")
171+
}
172+
173+
function Get-IncrementedVersion
174+
{
175+
param(
176+
[Parameter(Mandatory)]
177+
[semver]
178+
$Version,
179+
180+
[Parameter(Mandatory)]
181+
[ValidateSet('Major', 'Minor', 'Patch', 'Preview')]
182+
[string]
183+
$IncrementLevel
184+
)
185+
186+
switch ($IncrementLevel)
187+
{
188+
'Major'
189+
{
190+
return [semver]::new($version.Major+1, $version.Minor, $version.Patch, $version.PreReleaseLabel)
191+
}
192+
193+
'Minor'
194+
{
195+
return [semver]::new($version.Major, $version.Minor+1, $version.Patch, $version.PreReleaseLabel)
196+
}
197+
198+
'Patch'
199+
{
200+
return [semver]::new($version.Major, $version.Minor, $version.Patch+1, $version.PreReleaseLabel)
201+
}
202+
203+
'Preview'
204+
{
205+
$newPreviewNumber = [int]$version.PreReleaseLabel.Substring(8) + 1
206+
return [semver]::new($version.Major, $version.Minor, $version.Patch, "preview.$newPreviewNumber")
207+
}
208+
}
209+
}
210+
211+
function Get-VersionFromSemVer
212+
{
213+
[OutputType([version])]
214+
param(
215+
[Parameter(Mandatory)]
216+
[semver]
217+
$SemVer
218+
)
219+
220+
$svStr = $SemVer.ToString()
221+
222+
if (-not $SemVer.PreReleaseLabel)
223+
{
224+
return [version]$svStr
225+
}
226+
227+
return $svStr.Substring(0, $svStr.IndexOf('-'))
228+
}
229+
230+
Export-ModuleMember -Function New-StringWithSegment,Get-StringOffsetFromSpan,ConvertTo-IndentedJson,Get-IncrementedVersion,Get-VersionFromSemVer

0 commit comments

Comments
 (0)