-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathupdate-changelog.ps1
More file actions
197 lines (171 loc) · 6.6 KB
/
update-changelog.ps1
File metadata and controls
197 lines (171 loc) · 6.6 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
param(
[Parameter(Mandatory = $true)][string] $Name,
[Parameter(Mandatory = $true)][string] $PR,
[Parameter(Mandatory = $true)][string] $RepoUrl,
[Parameter(Mandatory = $true)][string] $MainBranch,
[Parameter(Mandatory = $true)][string] $OldTag,
[Parameter(Mandatory = $true)][string] $NewTag,
[Parameter(Mandatory = $true)][string] $Section
)
Set-StrictMode -Version latest
$file = $(Get-ChildItem | Where-Object { $_.Name -match '^changelog(\.md|\.txt|)$' } )
if ("$file" -eq "")
{
throw "Couldn't find a changelog"
}
elseif ($file -is [Array])
{
throw "Multiple changelogs found: $file"
}
Write-Host "Found changelog: $file"
[string[]]$lines = Get-Content $file
$skippingComment = $false
# Make sure that there's an `Unreleased` header
for ($i = 0; $i -lt $lines.Count; $i++)
{
$line = $lines[$i]
if ($line -match "changelog" -or $line.Trim().Length -eq 0)
{
continue
}
# Skip the prettier comment that may be found before the Unreleased version.
if ($line -match "<!-- prettier-ignore-start -->" -and -not $skippingComment)
{
$skippingComment = $true
continue
}
if ($skippingComment) {
if ($line -match "<!-- prettier-ignore-end -->") {
$skippingComment = $false
continue
}
if ($line -match "^> ") {
continue
}
throw "Prettier comment format - expected <!-- prettier-ignore-end -->, but found: '$line'"
}
# End of prettier comment
# Next, we expect a header for the current version or "Unreleased".
if (-not $line.StartsWith("#"))
{
throw "Unexpected changelog line - expecting a version header at this point, such as '## Unreleased', but found: '$line'"
}
# If it's an existing version instead of "Unreleased".
if (-not ($line -match "unreleased"))
{
Write-Host "Adding a new '## Unreleased' section"
$lines = $lines[0..($i - 1)] + @("## Unreleased", "") + $lines[$i..($lines.Count - 1)]
}
break
}
# Make sure that there's the requested section header
:outer for ($i = 0; $i -lt $lines.Count; $i++)
{
$line = $lines[$i]
# Skip the "Changelog" header and empty lines at the beginning.
if ($line -match "changelog" -or $line -match "unreleased" -or $line.Trim().Length -eq 0)
{
continue
}
# Skip the prettier comment that may be found before the Unreleased version.
if ($line -match "<!-- prettier-ignore-start -->" -and -not $skippingComment)
{
$skippingComment = $true
continue
}
if ($skippingComment) {
if ($line -match "<!-- prettier-ignore-end -->") {
$skippingComment = $false
continue
}
if ($line -match "^> ") {
continue
}
throw "Prettier comment format - expected <!-- prettier-ignore-end -->, but found: '$line'"
}
# End of prettier comment
# Next, we expect a header
if (-not $line.StartsWith("#"))
{
throw "Unexpected changelog line - expecting a section header at this point, such as '### $Section', but found: '$line'"
}
if (-not ($line -match "### $Section"))
{
# If it's a version-specific section header but not the requested section header, skip all the items in this section
if ($line.StartsWith("###"))
{
for ($i = $i + 1; $i -lt $lines.Count - 1; $i++)
{
if ($lines[$i + 1].StartsWith("#"))
{
continue outer
}
}
}
# add the section header as the first sub-header
Write-Host "Adding a new '### $Section' section at line $i"
$lines = $lines[0..($i - 1)] + @("### $Section", "", "") + $lines[$i..($lines.Count - 1)]
}
break
}
# Find the last point in the first requested section header
for ($i = 0; $i -lt $lines.Count; $i++)
{
$line = $lines[$i]
if ($line -match "### $Section")
{
Write-Host "Found a $Section header at $i"
# Find the next header and then go backward until we find a non-empty line
for ($i++; $i -lt $lines.Count -and -not $lines[$i].StartsWith("#"); $i++) {}
for ($i--; $i -gt 0 -and $lines[$i].Trim().Length -eq 0; $i--) {}
$i += ($lines[$i] -match "### $Section") ? 2 : 1
break
}
}
# What line we want to insert at - the empty line at the end of the currently unreleased section.
$sectionEnd = $i
$tagAnchor = $NewTag.Replace('.', '')
$oldTagNice = ($OldTag -match "^[0-9]") ? "v$OldTag" : $OldTag
$newTagNice = ($NewTag -match "^[0-9]") ? "v$NewTag" : $NewTag
$PullRequestMD = "[#$($PR | Split-Path -Leaf)]($PR)"
# First check if an existing entry for the same dependency exists among unreleased $Section - if so, update it instead of adding a new one.
$updated = $false
for ($i = 0; $i -lt $sectionEnd; $i++)
{
if (($lines[$i] -match "^[-*] Bump $Name.*to") -and `
($lines[$i + 1] -match "^ [-*] \[changelog\]\($RepoUrl") -and `
($lines[$i + 2] -match "^ [-*] \[diff\]\($RepoUrl"))
{
Write-Host "Found an existing changelog entry at $($i):"
Write-Host " ", $lines[$i]
Write-Host " ", $lines[$i + 1]
Write-Host " ", $lines[$i + 2]
$lines[$i] = $lines[$i] -replace "(Bump $Name.*)to .* \(", "`$1to $newTagNice ("
$lines[$i] = $lines[$i] -replace "\)$", ", $PullRequestMD)"
$lines[$i + 1] = $lines[$i + 1] -replace "#.*\)", "#$tagAnchor)"
$lines[$i + 2] = $lines[$i + 2] -replace "\.\.\..*\)$", "...$NewTag)"
Write-Host "Updating the entry to: "
Write-Host " ", $lines[$i]
Write-Host " ", $lines[$i + 1]
Write-Host " ", $lines[$i + 2]
$updated = $true
break;
}
}
if (!$updated)
{
# Find what character is used as a bullet-point separator - look for the first bullet-point object that wasn't created by this script.
$bulletPoint = $lines | Where-Object { ($_ -match '^ *[-*] ') -and -not ($_ -match '(Bump .* to|\[changelog\]|\[diff\])') } | Select-Object -First 1
$bulletPoint = "$("$bulletPoint".Trim())-"[0]
$entry = @("$bulletPoint Bump $Name from $oldTagNice to $newTagNice ($PullRequestMD)",
" $bulletPoint [changelog]($RepoUrl/blob/$MainBranch/CHANGELOG.md#$tagAnchor)",
" $bulletPoint [diff]($RepoUrl/compare/$OldTag...$NewTag)")
Write-Host "Adding a changelog entry at line $($sectionEnd):"
foreach ($line in $entry)
{
Write-Host " ", $line
}
$linesPost = $lines.Count -gt $sectionEnd ? $lines[$sectionEnd..($lines.Count - 1)] : @()
$lines = $lines[0..($sectionEnd - 1)] + $entry + $linesPost
}
$lines | Out-File $file