forked from getsentry/sentry-unity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Directory.Build.targets
453 lines (380 loc) · 21.7 KB
/
Directory.Build.targets
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
<Project InitialTargets="FindUnity">
<PropertyGroup>
<!-- Assumes building projects in this repo (not submodules). i.e: src/Sentry.Unity -->
<UnitySampleProjectPath>$(MSBuildProjectDirectory)/../../samples/unity-of-bugs/</UnitySampleProjectPath>
<PlayerBuildPath>$(UnitySampleProjectPath)../artifacts/builds/</PlayerBuildPath>
<AndroidBuildMethod>Builder.BuildAndroidIl2CPPPlayer</AndroidBuildMethod>
<AndroidBuildPath>$(PlayerBuildPath)Android/IL2CPP_Player.apk</AndroidBuildPath>
<!-- Assumes running `dotnet` from the root of the repo: -->
<RepoRoot>$([System.IO.Path]::GetDirectoryName($([MSBuild]::GetPathOfFileAbove('.gitignore', '$(MSBuildThisFileDirectory)'))))/</RepoRoot>
<UnityPackageProject>$(RepoRoot)temp/unity-packages</UnityPackageProject>
<UnityTestPath>$(UnityPackageProject)/Library/ScriptAssemblies</UnityTestPath>
<UnitySampleProjectUnityVersion>$(RepoRoot)samples/unity-of-bugs/ProjectSettings/ProjectVersion.txt</UnitySampleProjectUnityVersion>
<UnityTestPlayModeResultFilePath>../../artifacts/test/playmode/results.xml</UnityTestPlayModeResultFilePath>
<UnityTestEditModeResultFilePath>../../artifacts/test/editmode/results.xml</UnityTestEditModeResultFilePath>
<!-- Cocoa -->
<SentryCocoaRoot>$(RepoRoot)modules/sentry-cocoa/</SentryCocoaRoot>
<SentryCocoaDeviceArtifactsDestination>$(RepoRoot)package-dev/Plugins/iOS/Device/Sentry.framework/</SentryCocoaDeviceArtifactsDestination>
<SentryCocoaSimulatorArtifactsDestination>$(RepoRoot)package-dev/Plugins/iOS/Simulator/Sentry.framework/</SentryCocoaSimulatorArtifactsDestination>
<!-- Android -->
<SentryAndroidRoot>$(RepoRoot)modules/sentry-java/</SentryAndroidRoot>
<SentryAndroidArtifactsDestination>$(RepoRoot)package-dev/Plugins/Android/Sentry/</SentryAndroidArtifactsDestination>
</PropertyGroup>
<!-- Use the Unity Editor version set in the sample project of the repo -->
<Target Name="FindUnity">
<LocateUnityVersion ProjectSettingsPath="$(UnitySampleProjectUnityVersion)">
<Output PropertyName="UnityVersion" TaskParameter="UnityVersion" />
</LocateUnityVersion>
<Message Text="Unity Version: $(UnityVersion)" Importance="Normal" />
<!-- Unity paths on Windows -->
<PropertyGroup Condition="$([MSBuild]::IsOSPlatform('Windows'))">
<UnityRoot Condition="Exists('C:\Program Files\Unity\Hub\Editor\$(UnityVersion)\Editor\Data\Managed\UnityEngine.dll')">C:\Program Files\Unity\Hub\Editor\$(UnityVersion)\Editor</UnityRoot>
<!--If not using Unity Hub, tries to pick whatever Unity version is installed on the machine-->
<UnityRoot Condition="$(UnityRoot) == '' AND Exists('C:\Program Files\Unity\Editor\Data\Managed\UnityEngine.dll')">C:\Program Files\Unity\Editor</UnityRoot>
<!--Short version for GitHub Actions to avoid long path names errors on Windows-->
<UnityRoot Condition="Exists('C:\$(UnityVersion)\Editor\Data\Managed\UnityEngine.dll')">C:\$(UnityVersion)\Editor</UnityRoot>
<UnityManagedPath>$(UnityRoot)\Data\Managed</UnityManagedPath>
<UnityExec>$(UnityRoot)\Unity.exe</UnityExec>
<StandaloneBuildMethod>Builder.BuildWindowsIl2CPPPlayer</StandaloneBuildMethod>
<StandaloneBuildPath>$(PlayerBuildPath)Windows/IL2CPP_Player.exe</StandaloneBuildPath>
<StandaloneExecutablePath>$(StandaloneBuildPath)</StandaloneExecutablePath>
</PropertyGroup>
<Error Condition="!Exists('$(UnityRoot)') AND $([MSBuild]::IsOSPlatform('Windows'))"
Text="UnityRoot not found. Ensure Unity is installed.
See the CONTRIBUTING.md.
UnityVersion: $(UnityVersion)
Resolved directory: $(UnityRoot)
Expected to exist:
* C:\Program Files\Unity\Hub\Editor\$(UnityVersion)\Editor\Data\Managed\UnityEngine.dll
or
* C:\Program Files\Unity\Editor\Data\Managed\UnityEngine.dll" />
<!-- Unity paths on macOS -->
<PropertyGroup Condition="$([MSBuild]::IsOSPlatform('OSX'))">
<UnityRoot Condition="Exists('/Applications/Unity/Hub/Editor/$(UnityVersion)/Unity.app/Contents/Managed/UnityEngine.dll')">/Applications/Unity/Hub/Editor/$(UnityVersion)/Unity.app/</UnityRoot>
<!--If not using Unity Hub, tries to pick whatever Unity version is installed on the machine-->
<UnityRoot Condition="$(UnityRoot) == '' AND Exists('/Applications/Unity/Unity.app/Contents/Managed/UnityEngine.dll')">/Applications/Unity/Unity.app/</UnityRoot>
<UnityManagedPath>$(UnityRoot)/Contents/Managed</UnityManagedPath>
<UnityExec>$(UnityRoot)/Contents/MacOS/Unity</UnityExec>
<StandaloneBuildMethod>Builder.BuildMacIl2CPPPlayer</StandaloneBuildMethod>
<StandaloneBuildPath>$(PlayerBuildPath)MacOS/IL2CPP_Player.app</StandaloneBuildPath>
<StandaloneExecutablePath>$(StandaloneBuildPath)/Contents/MacOS/unity-of-bugs</StandaloneExecutablePath>
<IOSBuildMethod>Builder.BuildIOSPlayer</IOSBuildMethod>
<IOSBuildPath>$(PlayerBuildPath)iOS/Xcode</IOSBuildPath>
</PropertyGroup>
<Error Condition="!Exists('$(UnityRoot)') AND $([MSBuild]::IsOSPlatform('OSX'))"
Text="UnityRoot not found. Ensure Unity is installed.
See the CONTRIBUTING.md.
UnityVersion: $(UnityVersion)
Resolved directory: $(UnityRoot)
Expected to exist:
* /Applications/Unity/Hub/Editor/$(UnityVersion)/Unity.app/Contents/Managed/UnityEngine.dll
or
* /Applications/Unity/Unity.app/Contents/Managed/UnityEngine.dll" />
</Target>
<Target Name="CleanUnityTestResults" AfterTargets="Clean">
<Delete Files="$(UnityTestPlayModeResultFilePath)" />
<Delete Files="$(UnityTestEditModeResultFilePath)" />
<RemoveDir Directories="$(PlayerBuildPath)" />
<RemoveDir Condition="Exists('$(UnityPackageProject)')" Directories="$(UnityPackageProject)" />
</Target>
<Target Name="CleanCocoaSDK" AfterTargets="Clean" Condition="'$(MSBuildProjectName)' == 'Sentry.Unity'">
<RemoveDir Directories="$(SentryCocoaDeviceArtifactsDestination)" ContinueOnError="true" />
<RemoveDir Directories="$(SentryCocoaSimulatorArtifactsDestination)" ContinueOnError="true" />
</Target>
<Target Name="CleanAndroidSDK" AfterTargets="Clean" Condition="'$(MSBuildProjectName)' == 'Sentry.Unity'">
<!-- The jar file is version appended and we copy by glob so to avoid duplicates: -->
<RemoveDir Directories="$(SentryAndroidRoot)sentry/build/libs/" ContinueOnError="true" />
<RemoveDir Directories="$(SentryAndroidArtifactsDestination)" ContinueOnError="true" />
</Target>
<!-- Build the iOS SDK: dotnet msbuild /t:BuildCocoaSDK src/Sentry.Unity -->
<Target Name="BuildCocoaSDK"
Condition="$([MSBuild]::IsOSPlatform('OSX'))
And '$(MSBuildProjectName)' == 'Sentry.Unity'
And (!Exists('$(SentryCocoaDeviceArtifactsDestination)') OR !Exists('$(SentryCocoaSimulatorArtifactsDestination)'))"
BeforeTargets="BeforeBuild">
<Error Condition="!Exists('$(SentryCocoaRoot)')" Text="Couldn't find the Cocoa root at $(SentryCocoaRoot)."></Error>
<Message Importance="High" Text="Building Sentry iOS SDK."></Message>
<Exec WorkingDirectory="$(SentryCocoaRoot)" Command="carthage build --use-xcframeworks --no-skip-current --platform iOS"></Exec>
<!-- Itemgroup for the output Sentry.framework so we have access to '%(RecursiveDir)' when copying -->
<ItemGroup>
<CocoaDeviceBuildPath Include="$(SentryCocoaRoot)Carthage/Build/Sentry.xcframework/ios-arm64_armv7/Sentry.framework/**/*" />
</ItemGroup>
<Copy SourceFiles="@(CocoaDeviceBuildPath)"
DestinationFiles="@(CocoaDeviceBuildPath->'$(SentryCocoaDeviceArtifactsDestination)%(RecursiveDir)%(Filename)%(Extension)')">
</Copy>
<ItemGroup>
<CocoaSimulatorBuildPath Include="$(SentryCocoaRoot)Carthage/Build/Sentry.xcframework/ios-arm64_i386_x86_64-simulator/Sentry.framework/**/*" />
</ItemGroup>
<Copy SourceFiles="@(CocoaSimulatorBuildPath)"
DestinationFiles="@(CocoaSimulatorBuildPath->'$(SentryCocoaSimulatorArtifactsDestination)%(RecursiveDir)%(Filename)%(Extension)')">
</Copy>
<Error Condition="(!Exists('$(SentryCocoaDeviceArtifactsDestination)') OR !Exists('$(SentryCocoaSimulatorArtifactsDestination)'))"
Text="Failed to build the Cocoa SDK.">
</Error>
</Target>
<!-- Build the Android SDK: dotnet msbuild /t:BuildAndroidSDK src/Sentry.Unity -->
<Target Name="BuildAndroidSDK"
Condition="'$(MSBuildProjectName)' == 'Sentry.Unity'
And !Exists('$(SentryAndroidArtifactsDestination)')"
BeforeTargets="BeforeBuild">
<Error Condition="!Exists('$(SentryAndroidRoot)')" Text="Couldn't find the Android root at $(SentryAndroidRoot)."></Error>
<Message Importance="High" Text="Building Sentry Android SDK."></Message>
<Exec WorkingDirectory="$(SentryAndroidRoot)" Command="./gradlew :sentry-android-core:assembleRelease :sentry-android-ndk:assembleRelease :sentry:jar --no-daemon --stacktrace --warning-mode none"></Exec>
<ItemGroup>
<!-- building snapshot based on version, i.e: sentry-5.0.0-beta.3-SNAPSHOT.jar -->
<AndroidSdkArtifacts Include="$(SentryAndroidRoot)sentry/build/libs/sentry*.jar" />
<AndroidSdkArtifacts Include="$(SentryAndroidRoot)sentry-android-ndk/build/outputs/aar/sentry-android-ndk-release.aar" />
<AndroidSdkArtifacts Include="$(SentryAndroidRoot)sentry-android-core/build/outputs/aar/sentry-android-core-release.aar" />
<AndroidSdkArtifacts Include="$(RepoRoot)lib/gson-2.8.5.jar" />
</ItemGroup>
<Copy SourceFiles="@(AndroidSdkArtifacts)"
DestinationFiles="@(AndroidSdkArtifacts->'$(SentryAndroidArtifactsDestination)%(RecursiveDir)%(Filename)%(Extension)')">
</Copy>
<Error Condition="!Exists('$(SentryAndroidArtifactsDestination)')" Text="Failed to build the Android SDK."></Error>
</Target>
<!-- Even with a successful build, Unity will error on 'usbmuxd' or log out to std-error which breaks msbuild.
We need to run a unity build to restore the test packages and for that reason we'll ignore errors here and assume a later step will validate the build is actually working:
The offending error:
[usbmuxd] Stop listen thread
[usbmuxd] Error:
[usbmuxd] Listen thread exitingit -batchmode -nographics -logFile - -projectPath $(UnitySampleProjectPath) -$(StandalonePlayerName) $(ArtifactName)
For that reason, Exec tasks to unity all ignore error. That means that subsequent tasks must exist to validate the command (i.e: parse logs, etc).
Related: https://forum.unity.com/threads/6572-debugger-agent-unable-to-listen-on-27.500387/ -->
<!-- Build a player with IL2CPP: dotnet msbuild /t:UnityBuildStandalonePlayerIL2CPP src/Sentry.Unity -->
<Target Name="UnityBuildStandalonePlayerIL2CPP"
Condition="'$(MSBuildProjectName)' == 'Sentry.Unity'">
<Error Condition="$(UnityRoot) == ''" Text="Couldn't find Unity."></Error>
<Message Importance="High" Text="Building Player with IL2CPP."></Message>
<Exec Command=""$(UnityExec)" -quit -batchmode -nographics -logFile - -projectPath $(UnitySampleProjectPath) -executeMethod $(StandaloneBuildMethod) -buildPath $(StandaloneBuildPath)"
IgnoreStandardErrorWarningFormat="true"></Exec>
</Target>
<!-- Run smoke test on player: dotnet msbuild /t:UnitySmokeTestStandalonePlayerIL2CPP src/Sentry.Unity -->
<Target Name="UnitySmokeTestStandalonePlayerIL2CPP"
Condition="'$(MSBuildProjectName)' == 'Sentry.Unity'">
<Error Condition="$(UnityRoot) == ''" Text="Couldn't find Unity."></Error>
<Message Importance="High" Text="Running smoke test on player."></Message>
<Exec Command="$(StandaloneExecutablePath) --test smoke"
IgnoreExitCode="true"
ContinueOnError="true">
<Output TaskParameter="ExitCode" PropertyName="SmokeTestExitCode"/>
</Exec>
<Error Text="Smoke test failed with code $(SmokeTestExitCode)" Condition="'$(SmokeTestExitCode)' != '200'" />
</Target>
<!-- Build an Android player with IL2CPP: dotnet msbuild /t:UnityBuildPlayerAndroidIL2CPP src/Sentry.Unity -->
<Target Name="UnityBuildPlayerAndroidIL2CPP"
Condition="'$(MSBuildProjectName)' == 'Sentry.Unity'">
<Error Condition="$(UnityRoot) == ''" Text="Couldn't find Unity."></Error>
<Message Importance="High" Text="Building Android Player with IL2CPP."></Message>
<Exec Command=""$(UnityExec)" -quit -batchmode -nographics -logFile - -projectPath $(UnitySampleProjectPath) -executeMethod $(AndroidBuildMethod) -buildPath $(AndroidBuildPath)"
IgnoreStandardErrorWarningFormat="true"></Exec>
<Error Condition="!Exists('$(AndroidBuildPath)')" Text="Android APK not found. Did something go wrong?"></Error>
</Target>
<!-- Build an iOS player: dotnet msbuild /t:UnityBuildPlayerIOS src/Sentry.Unity -->
<Target Name="UnityBuildPlayerIOS"
Condition="'$(MSBuildProjectName)' == 'Sentry.Unity'">
<Error Condition="$(UnityRoot) == ''" Text="Couldn't find Unity."></Error>
<Message Importance="High" Text="Building iOS Xcode project."></Message>
<Exec Command=""$(UnityExec)" -quit -batchmode -nographics -logFile - -projectPath $(UnitySampleProjectPath) -executeMethod $(IOSBuildMethod) -buildPath $(IOSBuildPath)"
IgnoreStandardErrorWarningFormat="true"></Exec>
<!-- <Error Condition="!Exists('$(IOSBuildPath)')" Text="iOS build not found. Did something go wrong?"></Error> -->
</Target>
<!-- If Unity Library Project doesn't exist, create a Unity project. We use this project to restore packages needed to build
this solution without using the sample project which depends on the output of this build. -->
<Target Name="UnityCreatePackages"
Condition="!Exists('$(UnityPackageProject)') AND '$(MSBuildProjectName)' == 'Sentry.Unity'"
AfterTargets="FindUnity">
<Error Condition="$(UnityRoot) == ''" Text="Couldn't find Unity."></Error>
<Message Importance="High" Text="Running Unity Create Packages."></Message>
<Exec Command=""$(UnityExec)" -quit -batchmode -nographics -logFile - -createProject $(UnityPackageProject) "
IgnoreStandardErrorWarningFormat="true"
IgnoreExitCode="true"></Exec>
<PropertyGroup>
<SourceManifestFile>$(UnitySampleProjectPath)/Packages/manifest.json</SourceManifestFile>
<DestinationManifestFile>$(UnityPackageProject)/Packages/manifest.json</DestinationManifestFile>
</PropertyGroup>
<Copy SourceFiles="$(SourceManifestFile)" DestinationFiles="$(DestinationManifestFile)" />
<ExcludePackage PackageManifestFile="$(DestinationManifestFile)" PackageToRemove="io.sentry.unity.dev" />
</Target>
<!-- If Unity Libraries don't exist, load a Unity project to restore UPM packages -->
<Target Name="UnityRestorePackages"
DependsOnTargets="UnityCreatePackages"
Condition="!Exists('$(UnityPackageProject)/Library/ScriptAssemblies/UnityEngine.TestRunner.dll') AND '$(MSBuildProjectName)' == 'Sentry.Unity'"
BeforeTargets="BeforeBuild">
<Error Condition="$(UnityRoot) == ''" Text="Couldn't find Unity."></Error>
<Message Importance="High" Text="Running Unity Restore Packages."></Message>
<Exec Command=""$(UnityExec)" -quit -batchmode -nographics -logFile - -projectPath $(UnityPackageProject) "
IgnoreStandardErrorWarningFormat="true"
IgnoreExitCode="true"></Exec>
<Error Condition="!Exists('$(UnityPackageProject)/Library/ScriptAssemblies/UnityEngine.TestRunner.dll')" Text="TestRunner not found. Expected: $(UnityPackageProject)/Library/ScriptAssemblies/UnityEngine.TestRunner.dll"></Error>
</Target>
<!-- Run PlayMode tests with dotnet msbuild /t:UnityPlayModeTest test/Sentry.Unity.Tests -->
<Target Name="UnityPlayModeTest"
DependsOnTargets="Build"
Condition="'$(MSBuildProjectName)' == 'Sentry.Unity.Tests'">
<Error Condition="$(UnityRoot) == ''" Text="Couldn't find Unity."></Error>
<Message Importance="High" Text="Running Unity PlayMode tests."></Message>
<Exec Command=""$(UnityExec)" -batchmode -nographics -logFile - -runTests -testPlatform PlayMode -projectPath $(UnitySampleProjectPath) -testResults $(UnityTestPlayModeResultFilePath) "
IgnoreStandardErrorWarningFormat="true"
IgnoreExitCode="true"></Exec>
<UnityTestResults Path="$(UnityTestPlayModeResultFilePath)" />
</Target>
<!-- Run EditMode tests with dotnet msbuild /t:UnityPlayModeTest test/Sentry.Unity.Tests -->
<Target Name="UnityEditModeTest"
DependsOnTargets="Build"
Condition="'$(MSBuildProjectName)' == 'Sentry.Unity.Editor.Tests'">
<Error Condition="$(UnityRoot) == ''" Text="Couldn't find Unity."></Error>
<Message Importance="High" Text="Running Unity EditMode tests."></Message>
<Exec Command=""$(UnityExec)" -batchmode -nographics -logFile - -runTests -testPlatform EditMode -projectPath $(UnitySampleProjectPath) -testResults $(UnityTestEditModeResultFilePath) "
IgnoreStandardErrorWarningFormat="true"
IgnoreExitCode="true"></Exec>
<UnityTestResults Path="$(UnityTestEditModeResultFilePath)" />
</Target>
<!-- Read Unity Version -->
<UsingTask
TaskName="LocateUnityVersion"
TaskFactory="RoslynCodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll" >
<ParameterGroup>
<ProjectSettingsPath ParameterType="System.String" Required="true" />
<UnityVersion ParameterType="System.String" Output="true" />
</ParameterGroup>
<Task>
<Using Namespace="System.IO" />
<Using Namespace="System.Linq" />
<Code Type="Fragment" Language="cs">
<![CDATA[
if (Environment.GetEnvironmentVariable("CI") is not null
&& Environment.GetEnvironmentVariable("UNITY_VERSION") is { } unityVersion)
{
Log.LogMessage("Unity Version from environment variable: " + unityVersion);
UnityVersion = unityVersion;
return true;
}
if (!File.Exists(ProjectSettingsPath))
{
Log.LogError("Can't find Unity version because project settings file not found at " + ProjectSettingsPath);
return false;
}
var version = File.ReadLines(ProjectSettingsPath).FirstOrDefault(l => l.StartsWith("m_EditorVersion: "));
if (version == null)
{
Log.LogError("Unity version not found in file: " + ProjectSettingsPath);
return false;
}
UnityVersion = version.Substring("m_EditorVersion: ".Length);
Log.LogMessage("Unity Version: " + version);
]]>
</Code>
</Task>
</UsingTask>
<!-- Exclude package from manifest -->
<UsingTask
TaskName="ExcludePackage"
TaskFactory="RoslynCodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll" >
<ParameterGroup>
<PackageManifestFile ParameterType="System.String" Required="true" />
<PackageToRemove ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Using Namespace="System.IO" />
<Using Namespace="System.Linq" />
<Code Type="Fragment" Language="cs">
<![CDATA[
if (!File.Exists(PackageManifestFile))
{
Log.LogError("No manifest file at " + PackageManifestFile);
return false;
}
var lines = File.ReadLines(PackageManifestFile).Where(l => !l.Contains($"\"{PackageToRemove}\"")).ToArray();
File.WriteAllLines(PackageManifestFile, lines);
]]>
</Code>
</Task>
</UsingTask>
<!-- Parse test results -->
<UsingTask
TaskName="UnityTestResults"
TaskFactory="RoslynCodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll" >
<ParameterGroup>
<Path ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Using Namespace="System" />
<Using Namespace="System.IO" />
<Using Namespace="System.Linq" />
<Using Namespace="System.Xml.Linq" />
<Code Type="Fragment" Language="cs">
<![CDATA[
if (!File.Exists(Path))
{
Log.LogError("Test results file not found at " + Path);
return false;
}
var document = XDocument.Load(Path);
var testRun = document.Descendants("test-run").First();
var total = testRun.Attribute("total").Value;
if (total == "0")
{
Log.LogError("Unity test results is empty.");
return false;
}
var result = testRun.Attribute("result").Value;
var failed = testRun.Attribute("failed").Value;
var message = $@"Result: {result.Replace("(Child)", "")} in {testRun.Attribute("duration").Value}s
-- Passed: {testRun.Attribute("passed").Value}
-- Failed: {failed}
-- Inconclusive: {testRun.Attribute("inconclusive").Value}
-- Skipped: {testRun.Attribute("skipped").Value}";
Log.LogMessage(message);
if (result == "Passed")
{
return true;
}
PrintFailedTests(testRun);
var errorMessage = $"Test run completed with {failed} failing test{(int.Parse(failed) > 1 ? "s" : "")}.";
Log.LogError(errorMessage);
Success = false;
void PrintFailedTests(XElement element)
{
foreach (var descendant in element.Descendants())
{
if (descendant.Name != "test-case"
|| descendant.Attribute("result")?.Value != "Failed")
{
continue;
}
if (descendant.Descendants().Any(d => d.Name == "test-case"))
{
PrintFailedTests(descendant);
}
else
{
var sb = new StringBuilder()
.Append("Test ")
.Append(descendant.Attribute("id")?.Value)
.Append(": ")
.AppendLine(descendant.Attribute("name")?.Value);
var failure = descendant.Descendants("failure")
.Descendants("message")
.FirstOrDefault()
?.Value;
var stack = descendant.Descendants("failure")
.Descendants("stack-trace")
.FirstOrDefault()
?.Value;
sb.AppendLine(failure)
.Append("Test StackTrace: ")
.AppendLine(stack);
// MSBuild is breaking each line as if it was an error per line and not a single error.
// So Log.LogError got replaced by Console.WriteLine for now.
Console.WriteLine(sb.ToString());
}
}
}
]]>
</Code>
</Task>
</UsingTask>
</Project>