Skip to content

Add scripts for automatically updating the version of the PowerShell extension #1932

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
May 13, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
245 changes: 245 additions & 0 deletions tools/FileUpdateTools.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

function ReplaceStringSegment
{
[OutputType([string])]
param(
[Parameter(Mandatory)]
[string]
$String,

[Parameter(Mandatory)]
[string]
$NewSegment,

[Parameter(Mandatory)]
[int]
$StartIndex,

[Parameter(Mandatory)]
[int]
$EndIndex,

[switch]
$AutoIndent
)

if ($AutoIndent)
{
$indentBuilder = [System.Text.StringBuilder]::new()
$indentIdx = $StartIndex - 1
$currChar = $String[$indentIdx]
while ($currChar -ne "`n")
{
$null = $indentBuilder.Append($currChar)
$indentIdx--
$currChar = $String[$indentIdx]
}
$indent = $indentBuilder.ToString()
}
else
{
$indent = ''
}

$newStringBuilder = [System.Text.StringBuilder]::new()
$null = $newStringBuilder.Append($String.Substring(0, $StartIndex))

$segmentLines = $NewSegment.Split("`n")

$null = $newStringBuilder.Append($segmentLines[0])
for ($i = 1; $i -lt $segmentLines.Length; $i++)
{
$null = $newStringBuilder.Append("`n").Append($indent).Append($segmentLines[$i])
}

$null = $newStringBuilder.Append($String.Substring($EndIndex))

return $newStringBuilder.ToString()
}

function GetStringOffsetFromSpan
{
[OutputType([int])]
param(
[Parameter()]
[string]
$String,

[Parameter()]
[int]
$EndLine,

[Parameter()]
[int]
$StartLine = 1,

[Parameter()]
[int]
$Column = 0,

[Parameter()]
[int]
$InitialOffset = 0
)

$lfChar = 0xA

$idx = $InitialOffset
$spanLines = $EndLine - $StartLine
for ($i = 0; $i -lt $spanLines; $i++)
{
$idx = $String.IndexOf($lfChar, $idx + 1)

if ($idx -lt 0)
{
return $idx
}
}

return $idx + $Column
}

function ConvertToJToken
{
param(
[Parameter()]
$Object
)

if ($null -eq $Object)
{
return [Newtonsoft.Json.Linq.JValue]::CreateNull()
}

if ($Object -is [pscustomobject])
{
$jObject = [Newtonsoft.Json.Linq.JObject]::new()
foreach ($field in $Object)
{
$jObject.Add($field, $Object.$field)
}
return $jObject
}

if ($Object -is [version])
{
return [Newtonsoft.Json.Linq.JToken]::new($Object.ToString())
}

return (,[Newtonsoft.Json.Linq.JToken]::FromObject($Object))
}

function ConvertToIndentedJson
{
param(
[Parameter(Position=0)]
$Object,

[Parameter()]
[int]
$IndentWidth = 4,

[Parameter()]
[char]
$IndentChar = ' '
)

# Convert the object to a JToken
$jObject = ConvertToJToken $Object

# Reformat the entry with tab-based indentation, like the existing file
$stringBuilder = [System.Text.StringBuilder]::new()
try
{
$stringWriter = [System.IO.StringWriter]::new($stringBuilder)
$jsonWriter = [Newtonsoft.Json.JsonTextWriter]::new($stringWriter)
$jsonWriter.Indentation = $IndentWidth
$jsonWriter.IndentChar = $IndentChar
$jsonWriter.Formatting = 'Indented'
$null = $jObject.WriteTo($jsonWriter)
}
finally
{
$jsonWriter.Dispose()
$stringWriter.Dispose()
}
return $stringBuilder.ToString().Replace([System.Environment]::NewLine, "`r`n")
}

function SetFileContent
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use Set-Content?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was initially to work around the UTF8 BOM thing, but I think you're right that Set-Content is best

{
param(
[Parameter(Mandatory, Position=0)]
[string]
$FilePath,

[Parameter(Mandatory, Position=1)]
[string]
$Value,

[Parameter()]
$Encoding = ([System.Text.UTF8Encoding]::new(<# BOM #> $false))
)

$FilePath = $PSCmdlet.GetUnresolvedProviderPathFromPSPath($FilePath)
[System.IO.File]::WriteAllText($FilePath, $Value, $Encoding)
}

function IncrementVersion
{
param(
[Parameter(Mandatory)]
[semver]
$Version,

[Parameter(Mandatory)]
[ValidateSet('Major', 'Minor', 'Patch', 'Preview')]
[string]
$IncrementLevel
)

switch ($IncrementLevel)
{
'Major'
{
return [semver]::new($version.Major+1, $version.Minor, $version.Patch, $version.PreReleaseLabel)
}

'Minor'
{
return [semver]::new($version.Major, $version.Minor+1, $version.Patch, $version.PreReleaseLabel)
}

'Patch'
{
return [semver]::new($version.Major, $version.Minor, $version.Patch+1, $version.PreReleaseLabel)
}

'Preview'
{
$newPreviewNumber = [int]$version.PreReleaseLabel.Substring(8) + 1
return [semver]::new($version.Major, $version.Minor, $version.Patch, "preview.$newPreviewNumber")
}
}
}

function GetVersionFromSemVer
{
[OutputType([version])]
param(
[Parameter(Mandatory)]
[semver]
$SemVer
)

$svStr = $SemVer.ToString()

if (-not $SemVer.PreReleaseLabel)
{
return [version]$svStr
}

return $svStr.Substring(0, $svStr.IndexOf('-'))
}
Loading