Skip to content

Commit 9667eb4

Browse files
author
Kapil Borle
committed
Add release maker module to utilities
1 parent f5c5efb commit 9667eb4

File tree

1 file changed

+139
-0
lines changed

1 file changed

+139
-0
lines changed

Utils/ReleaseMaker.psm1

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
Function Get-SolutionPath
2+
{
3+
Split-Path $PSScriptRoot -Parent
4+
}
5+
6+
Function New-Release
7+
{
8+
[CmdletBinding()]
9+
param($newVer, $oldVer)
10+
11+
$isVersionGiven = $true
12+
if ($null -eq $newVer -or $null -eq $oldVer)
13+
{
14+
Write-Warning "Parameters are null. Checking changelog for version..."
15+
$isVersionGiven = $false
16+
}
17+
18+
$solutionRoot = (Get-SolutionPath)
19+
20+
$enginePath = Join-Path $solutionRoot "Engine"
21+
# Check if the changelog has entry for $newVer
22+
$moduleManifestPath = Join-Path $enginePath "PSScriptAnalyzer.psd1"
23+
$changelogPath = Join-Path $solutionRoot 'CHANGELOG.MD'
24+
$matches = [regex]::new("\[(\d+\.\d+\.\d+)\]").Matches((get-content $changelogPath -raw))
25+
$versions = $matches | ForEach-Object {$_.Groups[1].Value}
26+
if ($versions.Count -le 2)
27+
{
28+
throw "This edge condition for the number versions less that 2 is not implemented."
29+
}
30+
31+
if ($isVersionGiven)
32+
{
33+
Function Test-IfNotPresentInChangelog
34+
{
35+
param($extractedVersion, $inputVersion)
36+
if ($extractedVersion -ne $inputVersion)
37+
{
38+
throw ("Version {0} does not exist in changelog. Please update changelog." -f $inputVersion)
39+
}
40+
}
41+
42+
Test-IfNotPresentInChangelog $versions[0] $newVer
43+
Test-IfNotPresentInChangelog $versions[1] $oldVer
44+
}
45+
else
46+
{
47+
$newVer = $versions[0]
48+
$oldVer = $versions[1]
49+
$caption = "Version Check"
50+
$query = "Is version {0} the next release and version {1} the previous release ?" -f $newVer,$oldVer
51+
[bool] $yesToAll = $false
52+
[bool] $noToAll = $false
53+
54+
if (!$PSCmdlet.ShouldContinue($query, $caption, $false, [ref] $yesToAll, [ref] $noToAll))
55+
{
56+
return "Aborting..."
57+
}
58+
}
59+
60+
Update-Version $newVer $oldVer $solutionRoot
61+
62+
63+
$changelogRegexPattern = "##\s\[{0}\].*\n((?:.*\n)+)##\s\[{1}\].*" `
64+
-f [regex]::Escape($newVer),[regex]::Escape($oldVer)
65+
$changelogRegex = [regex]::new($changelogRegexPattern)
66+
$matches = $changelogRegex.Match((get-content $changelogPath -raw))
67+
$changelog = $matches.Groups[1].Value.Trim()
68+
Write-Host $changelog
69+
70+
$releaseNotesPattern = `
71+
"(?<releaseNotesBegin>ReleaseNotes\s*=\s*@')(?<releaseNotes>(?:.*\n)*)(?<releaseNotesEnd>'@)"
72+
$replacement = "`${releaseNotesBegin}" `
73+
+ [environment]::NewLine `
74+
+ $changelog `
75+
+ [environment]::NewLine `
76+
+ "`${releaseNotesEnd}"
77+
$r = [regex]::new($releaseNotesPattern)
78+
$updatedManifestContent = $r.Replace([System.IO.File]::ReadAllText($moduleManifestPath), $replacement)
79+
Set-ContentUtf8NoBom $moduleManifestPath $updatedManifestContent
80+
81+
# build the module
82+
pushd $solutionRoot
83+
remove-item out/ -recurse -force
84+
.\buildCoreClr.ps1 -Framework net451 -Configuration Release -Build
85+
.\buildCoreClr.ps1 -Framework net451 -Configuration PSV3Release -Build
86+
.\buildCoreClr.ps1 -Framework netstandard1.6 -Configuration Release -Build
87+
.\build.ps1 -BuildDocs
88+
popd
89+
90+
}
91+
92+
function Combine-Path
93+
{
94+
if ($args.Count -lt 2)
95+
{
96+
throw "give more than equal to 2 arguments"
97+
}
98+
99+
$path = Join-Path $args[0] $args[1]
100+
for ($k = 2; $k -lt $args.Count; $k++)
101+
{
102+
$path = Join-Path $path $args[$k]
103+
}
104+
105+
$path
106+
}
107+
108+
function Update-Version
109+
{
110+
param(
111+
[string] $newVer,
112+
[string] $oldVer,
113+
[string] $solutionPath
114+
)
115+
116+
$ruleJson = Combine-Path $solutionPath 'Rules' 'project.json'
117+
$engineJson = Combine-Path $solutionPath 'Engine' 'project.json'
118+
$pssaManifest = Combine-Path $solutionPath 'Engine' 'PSScriptAnalyzer.psd1'
119+
120+
Update-PatternInFile $ruleJson '"version": "{0}"' $oldVer $newVer
121+
Update-PatternInFile $ruleJson '"Engine": "{0}"' $oldVer $newVer
122+
Update-PatternInFile $engineJson '"version": "{0}"' $oldVer $newVer
123+
Update-PatternInFile $pssaManifest "ModuleVersion = '{0}'" $oldVer $newVer
124+
}
125+
126+
function Update-PatternInFile
127+
{
128+
param ($path, $unformattedPattern, $oldVal, $newVal)
129+
130+
$content = Get-Content $path
131+
$newcontent = $content -replace ($unformattedPattern -f $oldVal),($unformattedPattern -f $newVal)
132+
Set-ContentUtf8NoBom $path $newcontent
133+
}
134+
135+
function Set-ContentUtf8NoBom {
136+
param($path, $content)
137+
$utfNoBom = [System.Text.UTF8Encoding]::new($false)
138+
[System.IO.File]::WriteAllLines($path, $content, $utfNoBom)
139+
}

0 commit comments

Comments
 (0)