-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathUpdateChangeLog.ps1
278 lines (218 loc) · 9.09 KB
/
UpdateChangeLog.ps1
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# .\UpdateChangeLog "2016.11.2" "3.1.0"
[CmdletBinding()]
Param(
[Parameter(Mandatory = $True, Position = 0)]
[string]$ReleaseDate,
[Parameter(Mandatory = $True, Position = 1)]
[string]$ReleaseVersion,
[Parameter(Mandatory = $False, Position = 2)]
[string]$PathToRepo
)
<#
This function updates service change logs by changing "Current Release" to the release
date and version and adding a new "Current Release" section to the top.
This function also returns an array that contains all of the changes for the service this release.
#>
function UpdateServiceChangeLog([string]$PathToChangeLog, [string]$ModuleVersion)
{
# Get the content of the service change log
$content = Get-Content $PathToChangeLog -Encoding UTF8
# Create a new object with enough space for the old content plus the new "Current Release" section
$newContent = New-Object string[] ($content.Length + 2)
$buffer = 0
# This will keep track of whether or not we found the "Current Release" changes and are actively adding
# them to the array we are returning
$found = $False
# This array will keep track of the changes made this release
$changeLogContent = @()
for ($idx = 0; $idx -lt $content.Length; $idx++)
{
# If we have found the "Current Release" section, update the section title,
# add the new "Current Release" section, update the buffer, and switch the $found variable
if (($content[$idx] -ne $null) -and ($content[$idx].StartsWith("## Current Release")))
{
$content[$idx] = "## Version $ModuleVersion"
$found = $True
$newContent[$idx] = "## Current Release"
$newContent[$idx + 1] = ""
$buffer = 2
}
# If we have found the next section after "Current Release", stop adding notes
elseif ($content[$idx] -like "##*")
{
$found = $False
}
# If we are currently looking at the changes for this release, add them to
# the array that we will return at the end
elseif ($found)
{
$changeLogContent += $content[$idx]
}
# Update the content
$newContent[$idx + $buffer] = $content[$idx]
}
# Update the service change log file to include all of the changes we made
$result = $newContent -join "`r`n"
$tempFile = Get-Item $PathToChangeLog
[System.IO.File]::WriteAllText($tempFile.FullName, $result, [Text.Encoding]::UTF8)
# Edge case: for the first release, we need to add an empty line
# that will be used for computation later
if ($found)
{
$changeLogContent += ""
}
# Return the list of changes that were made by this service this release
return $changeLogContent
}
<#
This function updates the "Release Notes" section in a service's psd1 file with
the changes that they made this release.
If no changes were made this release, the message "Updated for common code changes" will
be placed in the release notes instead.
#>
function UpdateModule([string]$PathToModule, [string[]]$ChangeLogContent)
{
$releaseNotes = @()
if ($ChangeLogContent.Length -le 1)
{
$releaseNotes += "Updated for common code changes"
}
else
{
$releaseNotes += $ChangeLogContent
}
Update-ModuleManifest -Path $PathToModule -ReleaseNotes $releaseNotes
}
<#
This function will update the master change log with all of the changes that were made by
services this release.
#>
function UpdateChangeLog([string]$PathToChangeLog, [string[]]$ServiceChangeLog)
{
# Get the content of the change log
$content = Get-Content $PathToChangeLog -Encoding UTF8
# Allocate enough space for all of the changes and the old content
$size = $content.Length + $ServiceChangeLog.Length
# Create a new object with enough space for the old content plus the new changes made this release
$newContent = New-Object string[] $size
# Add all of the new changes to the change log
for ($idx = 0; $idx -lt $ServiceChangeLog.Length; $idx++)
{
$newContent[$idx] = $ServiceChangeLog[$idx]
}
# Update the buffer
$buffer = $ServiceChangeLog.Length
# Add all of the old content to the change log
for ($idx = 0; $idx -lt $content.Length; $idx++)
{
$newContent[$idx + $buffer] = $content[$idx]
}
# Update the master change log to include all of the changes we made
$result = $newContent -join "`r`n"
$tempFile = Get-Item $PathToChangeLog
[System.IO.File]::WriteAllText($tempFile.FullName, $result, [Text.Encoding]::UTF8)
}
<#
This function will use the psd1 file to grab the module version.
#>
function GetModuleVersion([string]$PathToModule)
{
$file = Get-Item $PathToModule
Import-LocalizedData -BindingVariable ModuleMetadata -BaseDirectory $file.DirectoryName -FileName $file.Name
return $ModuleMetadata.ModuleVersion.ToString()
}
<#
This function will update all of the ResourceManager change logs.
#>
function UpdateARMLogs([string]$PathToServices)
{
# Get all of the service change logs
$logs = Get-ChildItem -Path $PathToServices -Filter ChangeLog.md -Recurse
# This array will hold all of the changes that we will add to the master change log
$result = @()
# Add the release header for the master change log
$result += "## $ReleaseDate - Version $ReleaseVersion"
# For each service change log, we are going to update their content and check
# for any changes that they made so we can add them to the master change log
foreach ($log in $logs)
{
# Get the service folder
$Service = Get-Item -Path "$($log.FullName)\.."
$serviceName = $Service.Name
if ($serviceName -eq "AzureBackup") { $serviceName = "Backup" }
if ($serviceName -eq "AzureBatch") { $serviceName = "Batch" }
if (!(Test-Path "$PathToRepo\artifacts\Debug\Az.$serviceName\Az.$serviceName.psd1"))
{
continue
}
# Get the psd1 file
$Module = Get-Item -Path "$PathToRepo\artifacts\Debug\Az.$serviceName\Az.$serviceName.psd1"
# Get the path to the change log
$PathToChangeLog = "$($log.FullName)"
# Get the path to the psd1 file
$PathToModule = "$($module.FullName)"
# Get the changes made to the current service
$serviceResult = UpdateLog -PathToChangeLog $PathToChangeLog -PathToModule $PathToModule -Service $Service.Name
# If there were any changes made, add them to the list that will be added to the master change log
if ($serviceResult.Length -gt 0)
{
$result += $serviceResult
}
Copy-Item -Path $PathToModule -Destination "$($Service.FullName)\AzureRM.$serviceName.psd1" -Force
}
# Return the list of changes
return $result
}
<#
This function will update the change log of a service.
This function will also return an array containing the changes made this release.
#>
function UpdateLog([string]$PathToChangeLog, [string]$PathToModule, [string]$Service)
{
# Get the ServiceManagement change log
$log = Get-Item -Path $PathToChangeLog
# Get the ServiceManagemenet psd1 file
$module = Get-Item -Path $PathToModule
# Get the module version for ServiceManagement
$ModuleVersion = GetModuleVersion -PathToModule $module.FullName
# Update the change log and get the contents of the change log for the current release
$changeLogContent = UpdateServiceChangeLog -PathToChangeLog $log.FullName -ModuleVersion $ModuleVersion
#Update the psd1 file to include the changes made this release
UpdateModule -PathToModule $module.FullName -ChangeLogContent $changeLogContent
$result = @()
# If there were any changes made this release, add them to the array that will
# be added to the master change log
if ($changeLogContent.Length -gt 1)
{
$result += "* $Service"
for ($idx = 0; $idx -lt $changeLogContent.Length - 1; $idx++)
{
$result += " $($changeLogContent[$idx])"
}
}
# Return the list of changes
return $result
}
# ----- START -----
# If there was no path to the Azure PowerShell repository provided, set it based on the location of this script
if (!$PathToRepo)
{
$PathToRepo = "$PSScriptRoot\.."
}
Import-Module PowerShellGet
# Update all of the ResourceManager change logs
$ResourceManagerResult = UpdateARMLogs -PathToServices $PathToRepo\src
$result = @()
# If any changes were made to ARM services, add them to the list to be added to the master change log
# Also, we need to update AzureRM.psd1 with all of the changes made to ARM services
if ($ResourceManagerResult.Length -gt 0)
{
$result += $ResourceManagerResult
UpdateModule -PathToModule "$PathToRepo\tools\AzureRM\AzureRM.psd1" -ChangeLogContent $ResourceManagerResult
}
# Blank space added to separate this release and last release changes in change log
$result += ""
# Get the master change log file
$ChangeLogFile = Get-Item -Path "$PathToRepo\ChangeLog.md"
# Update the master change log file with all of the individual service changes
UpdateChangeLog -PathToChangeLog $ChangeLogFile.FullName -ServiceChangeLog $result