-
Notifications
You must be signed in to change notification settings - Fork 335
/
Copy pathbuild-compatibility-matrix.ps1
234 lines (192 loc) · 11 KB
/
build-compatibility-matrix.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
param (
[VerifyNotNull]
[string] $RootPath,
[VerifyNotNull]
[string] $TestAssetsPath,
[VerifyNotNull]
[string] $TestArtifactsPath,
[VerifyNotNull]
[string] $PackagesPath,
[VerifySet("Debug", "Release")]
[string] $Configuration,
[VerifyNotNull]
[string] $DotnetExe,
[switch] $Ci,
[switch] $LocalizedBuild,
[VerifyNotNull]
[string] $NugetExeVersion
)
. $PSScriptRoot\common.lib.ps1
Write-Log "Invoke-CompatibilityTestAssetsBuild: Start test assets build."
$timer = Start-Timer
$generated = Join-Path (Split-Path -Path $TestAssetsPath) -ChildPath "GeneratedTestAssets"
$generatedSln = Join-Path $generated "CompatibilityTestAssets.sln"
# Figure out if the versions or the projects to build changed, and if they did not
# and the solution is already in place just build it.
# Otherwise delete everything and regenerate and re-build.
$dependenciesPath = "$RootPath\scripts\build\TestPlatform.Dependencies.props"
$dependenciesXml = [xml](Get-Content -Raw -Encoding UTF8 $dependenciesPath)
$cacheId = [ordered]@{ }
# Restore previous versions of TestPlatform (for vstest.console.exe), and TestPlatform.CLI (for vstest.console.dll).
# These properties are coming from TestPlatform.Dependencies.props.
$vstestConsoleVersionProperties = @(
"VSTestConsoleLatestVersion"
"VSTestConsoleLatestPreviewVersion"
"VSTestConsoleLatestStableVersion"
"VSTestConsoleRecentStableVersion"
"VSTestConsoleMostDownloadedVersion"
"VSTestConsolePreviousStableVersion"
"VSTestConsoleLegacyStableVersion"
)
# Build with multiple versions of MSTest. The projects are directly in the root.
# The folder structure in VS is not echoed in the TestAssets directory.
$projects = @(
"$RootPath\test\TestAssets\MSTestProject1\MSTestProject1.csproj"
"$RootPath\test\TestAssets\MSTestProject2\MSTestProject2.csproj"
# Don't use this one, it does not use the variables for mstest and test sdk.
# "$RootPath\test\TestAssets\SimpleTestProject2\SimpleTestProject2.csproj"
)
$msTestVersionProperties = @(
"MSTestFrameworkLatestPreviewVersion"
"MSTestFrameworkLatestStableVersion"
"MSTestFrameworkRecentStableVersion"
"MSTestFrameworkMostDownloadedVersion"
"MSTestFrameworkPreviousStableVersion"
"MSTestFrameworkLegacyStableVersion"
)
# We use the same version properties for NET.Test.Sdk as for VSTestConsole, for now.
foreach ($sdkPropertyName in $vstestConsoleVersionProperties) {
if ("VSTestConsoleLatestVersion" -eq $sdkPropertyName) {
# NETTestSdkVersion has the version of the locally built package.
$netTestSdkVersion = $dependenciesXml.Project.PropertyGroup."NETTestSdkVersion"
}
else {
$netTestSdkVersion = $dependenciesXml.Project.PropertyGroup.$sdkPropertyName
}
if (-not $netTestSdkVersion) {
throw "NetTestSdkVersion for $sdkPropertyName is empty."
}
$cacheId[$sdkPropertyName] = $netTestSdkVersion
# We don't use the results of this build anywhere, we just use them to restore the packages to nuget cache
# because using nuget.exe install errors out in various weird ways.
Invoke-Exe $dotnetExe -Arguments "build $RootPath\test\TestAssets\Tools\Tools.csproj --configuration $Configuration -v:minimal -p:CIBuild=$Ci -p:LocalizedBuild=$LocalizedBuild -p:NETTestSdkVersion=$netTestSdkVersion"
}
foreach ($propertyName in $msTestVersionProperties) {
$mstestVersion = $dependenciesXml.Project.PropertyGroup.$propertyName
if (-not $mstestVersion) {
throw "MSTestVersion for $propertyName is empty."
}
$cacheId[$propertyName] = $mstestVersion
}
$cacheId["projects"] = $projects
$cacheIdText = $cacheId | ConvertTo-Json
$currentCacheId = if (Test-Path "$generated/checksum.json") { Get-Content "$generated/checksum.json" -Raw }
$rebuild = $true
if ($cacheIdText -eq $currentCacheId) {
if (Test-Path $generatedSln) {
Write-Log ".. .. Build: Source: $generatedSln, cache is up to date, just building the solution."
Invoke-Exe $dotnetExe -Arguments "build $generatedSln --configuration $Configuration -v:minimal -p:CIBuild=$Ci -p:LocalizedBuild=$LocalizedBuild"
$rebuild = $false
}
}
if ($rebuild) {
if (Test-Path $generated) {
Remove-Item $generated -Recurse -Force
}
New-Item -ItemType Directory -Force -Path $generated | Out-Null
Write-Log ".. .. Generate: Source: $generatedSln"
$nugetExe = Join-Path $PackagesPath -ChildPath "Nuget.CommandLine" | Join-Path -ChildPath $NugetExeVersion | Join-Path -ChildPath "tools\NuGet.exe"
$nugetConfigSource = Join-Path $TestAssetsPath "NuGet.config"
$nugetConfig = Join-Path $generated "NuGet.config"
Invoke-Exe $dotnetExe -Arguments "new sln --name CompatibilityTestAssets --output ""$generated"""
Write-Log ".. .. Build: Source: $generatedSln"
try {
$projectsToAdd = @()
$nugetConfigSource = Join-Path $TestAssetsPath "NuGet.config"
$nugetConfig = Join-Path $generated "NuGet.config"
Copy-Item -Path $nugetConfigSource -Destination $nugetConfig
Write-Log ".. .. Build: Source: $generatedSln -- add NuGet source"
Invoke-Exe -IgnoreExitCode 1 $nugetExe -Arguments "sources add -Name ""locally-built-testplatform-packages"" -Source $TestArtifactsPath\packages\ -ConfigFile ""$nugetConfig"""
Write-Log ".. .. Build: Source: $generatedSln -- generate solution"
foreach ($project in $projects) {
$projectName = Split-Path -Path $project -Leaf
$projectBaseName = [IO.Path]::GetFileNameWithoutExtension($projectName)
$projectDir = Split-Path -Path $project
$projectItems = Get-ChildItem $projectDir | Where-Object { $_.Name -notin "bin", "obj" } | ForEach-Object { if ($_.PsIsContainer) { Get-ChildItem $_ -Recurse -File } else { $_ } }
Write-Log ".. .. .. Project $project has $($projectItems.Count) project items."
# We use the same version properties for NET.Test.Sdk as for VSTestConsole, for now.
foreach ($sdkPropertyName in $vstestConsoleVersionProperties) {
if ("VSTestConsoleLatestVersion" -eq $sdkPropertyName) {
# NETTestSdkVersion has the version of the locally built package.
$netTestSdkVersion = $dependenciesXml.Project.PropertyGroup."NETTestSdkVersion"
}
else {
$netTestSdkVersion = $dependenciesXml.Project.PropertyGroup.$sdkPropertyName
}
if (-not $netTestSdkVersion) {
throw "NetTestSdkVersion for $sdkPropertyName is empty."
}
$dirNetTestSdkVersion = $netTestSdkVersion -replace "\[|\]"
$dirNetTestSdkPropertyName = $sdkPropertyName -replace "Framework" -replace "Version" -replace "VSTestConsole", "NETTestSdk"
foreach ($propertyName in $msTestVersionProperties) {
$mstestVersion = $dependenciesXml.Project.PropertyGroup.$propertyName
if (-not $mstestVersion) {
throw "MSTestVersion for $propertyName is empty."
}
$dirMSTestVersion = $mstestVersion -replace "\[|\]"
$dirMSTestPropertyName = $propertyName -replace "Framework" -replace "Version"
# Do not make this a folder structure, it will break the relative reference to scripts\build\TestAssets.props that we have in the project,
# because the relative path will be different.
#
# It would be nice to use fully descriptive name but it is too long, hash the versions instead.
# $compatibilityProjectDir = "$generated/$projectBaseName--$dirNetTestSdkPropertyName-$dirNetTestSdkVersion--$dirMSTestPropertyName-$dirMSTestVersion"
$versions = "$dirNetTestSdkPropertyName-$dirNetTestSdkVersion--$dirMSTestPropertyName-$dirMSTestVersion"
$hash = Get-Hash $versions
Write-Host Hashed $versions to $hash
$projectShortName = "$projectBaseName--" + $hash
$compatibilityProjectDir = "$generated/$projectShortName"
if (Test-path $compatibilityProjectDir) {
throw "Path '$compatibilityProjectDir' does not exist"
}
New-Item -ItemType Directory -Path $compatibilityProjectDir | Out-Null
$compatibilityProjectDir = Resolve-Path $compatibilityProjectDir
foreach ($projectItem in $projectItems) {
$relativePath = ($projectItem.FullName -replace [regex]::Escape($projectDir)).TrimStart("\")
$fullPath = Join-Path $compatibilityProjectDir $relativePath
try {
Copy-Item -Path $projectItem.FullName -Destination $fullPath -Verbose
}
catch {
# can throw on wrong path, this makes the error more verbose
throw "$_, Source: '$($projectItem.FullName)', Destination: '$fullPath'"
}
}
$compatibilityCsproj = Get-ChildItem -Path $compatibilityProjectDir -Filter *.csproj
if (-not $compatibilityCsproj) {
throw "No .csproj files found in directory $compatibilityProjectDir."
}
$compatibilityCsproj = $compatibilityCsproj.FullName
$csprojContent = (Get-Content $compatibilityCsproj -Encoding UTF8) `
-replace "\$\(MSTestFrameworkVersion\)", $mstestVersion `
-replace "\$\(MSTestAdapterVersion\)", $mstestVersion `
-replace "\$\(NETTestSdkVersion\)", $netTestSdkVersion
$csprojContent | Set-Content -Encoding UTF8 -Path $compatibilityCsproj -Force
$uniqueCsprojName = Join-Path $compatibilityProjectDir "$projectShortName.csproj"
Rename-Item $compatibilityCsproj $uniqueCsprojName
$projectsToAdd += $uniqueCsprojName
Write-Log ".. .. .. Generated: $uniqueCsprojName"
}
}
}
Write-Log ".. .. .. Building: generatedSln"
Invoke-Exe $dotnetExe -Arguments "sln $generatedSln add $projectsToAdd"
Invoke-Exe $dotnetExe -Arguments "build $generatedSln --configuration $Configuration -v:minimal -p:CIBuild=$Ci -p:LocalizedBuild=$LocalizedBuild"
$cacheIdText | Set-Content "$generated/checksum.json" -NoNewline
}
finally {
Write-Log ".. .. Build: Source: $TestAssetsPath_Solution -- remove NuGet source"
Invoke-Exe -IgnoreExitCode 1 $nugetExe -Arguments "sources remove -Name ""locally-built-testplatform-packages"" -ConfigFile ""$nugetConfig"""
}
}
Write-Log ".. .. Build: Complete."
Write-Log "Invoke-CompatibilityTestAssetsBuild: Complete. {$(Get-ElapsedTime($timer))}"