-
Notifications
You must be signed in to change notification settings - Fork 259
/
TestingPlatformEntryPointTask.cs
151 lines (127 loc) · 5.63 KB
/
TestingPlatformEntryPointTask.cs
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#pragma warning disable CS8618 // Properties below are set by MSBuild.
using System.Diagnostics;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
namespace Microsoft.Testing.Platform.MSBuild;
public sealed class TestingPlatformEntryPointTask : Build.Utilities.Task
{
private const string CSharpLanguageSymbol = "C#";
private const string FSharpLanguageSymbol = "F#";
private const string VBLanguageSymbol = "VB";
public TestingPlatformEntryPointTask()
: this(new FileSystem())
{
}
internal TestingPlatformEntryPointTask(IFileSystem fileSystem)
{
if (Environment.GetEnvironmentVariable("TESTINGPLATFORM_MSBUILD_LAUNCH_ATTACH_DEBUGGER") == "1")
{
Debugger.Launch();
}
_fileSystem = fileSystem;
}
[Required]
public ITaskItem TestingPlatformEntryPointSourcePath { get; set; }
[Required]
public ITaskItem Language { get; set; }
[Output]
public ITaskItem TestingPlatformEntryPointGeneratedFilePath { get; set; }
private readonly IFileSystem _fileSystem;
public override bool Execute()
{
Log.LogMessage(MessageImportance.Normal, $"TestingPlatformEntryPointSourcePath: '{TestingPlatformEntryPointSourcePath.ItemSpec}'");
Log.LogMessage(MessageImportance.Normal, $"Language: '{Language.ItemSpec}'");
if (!Language.ItemSpec.Equals(CSharpLanguageSymbol, StringComparison.OrdinalIgnoreCase) &&
!Language.ItemSpec.Equals(VBLanguageSymbol, StringComparison.OrdinalIgnoreCase) &&
!Language.ItemSpec.Equals(FSharpLanguageSymbol, StringComparison.OrdinalIgnoreCase))
{
TestingPlatformEntryPointGeneratedFilePath = default!;
Log.LogError($"Language '{Language.ItemSpec}' is not supported.");
}
else
{
GenerateEntryPoint(Language.ItemSpec, TestingPlatformEntryPointSourcePath, _fileSystem, Log);
TestingPlatformEntryPointGeneratedFilePath = TestingPlatformEntryPointSourcePath;
}
return !Log.HasLoggedErrors;
}
private static void GenerateEntryPoint(string language, ITaskItem testingPlatformEntryPointSourcePath, IFileSystem fileSystem, TaskLoggingHelper taskLoggingHelper)
{
string entryPointSource = GetEntryPointSourceCode(language);
taskLoggingHelper.LogMessage(MessageImportance.Normal, $"Entrypoint source:\n'{entryPointSource}'");
fileSystem.WriteAllText(testingPlatformEntryPointSourcePath.ItemSpec, entryPointSource);
}
private static string GetEntryPointSourceCode(string language)
{
if (language == CSharpLanguageSymbol)
{
return $$"""
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by Microsoft.Testing.Platform.MSBuild
// </auto-generated>
//------------------------------------------------------------------------------
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
internal sealed class TestingPlatformEntryPoint
{
public static async global::System.Threading.Tasks.Task<int> Main(string[] args)
{
global::Microsoft.Testing.Platform.Builder.ITestApplicationBuilder builder = await global::Microsoft.Testing.Platform.Builder.TestApplication.CreateBuilderAsync(args);
SelfRegisteredExtensions.AddSelfRegisteredExtensions(builder, args);
using (global::Microsoft.Testing.Platform.Builder.ITestApplication app = await builder.BuildAsync())
{
return await app.RunAsync();
}
}
}
""";
}
else if (language == VBLanguageSymbol)
{
return $$"""
'------------------------------------------------------------------------------
' <auto-generated>
' This code was generated by Microsoft.Testing.Platform.MSBuild
' </auto-generated>
'------------------------------------------------------------------------------
<System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage>
Module TestingPlatformEntryPoint
Function Main(args As Global.System.String()) As Global.System.Int32
Return MainAsync(args).Result
End Function
Public Async Function MainAsync(ByVal args() As Global.System.String) As Global.System.Threading.Tasks.Task(Of Integer)
Dim builder = Await Global.Microsoft.Testing.Platform.Builder.TestApplication.CreateBuilderAsync(args)
SelfRegisteredExtensions.AddSelfRegisteredExtensions(builder, args)
Using testApplication = Await builder.BuildAsync()
Return Await testApplication.RunAsync()
End Using
End Function
End Module
""";
}
else if (language == FSharpLanguageSymbol)
{
return $$"""
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by Microsoft.Testing.Platform.MSBuild
// </auto-generated>
//------------------------------------------------------------------------------
[<System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage>]
[<EntryPoint>]
let main args =
task {
let! builder = Microsoft.Testing.Platform.Builder.TestApplication.CreateBuilderAsync args
Microsoft.TestingPlatform.Extensions.SelfRegisteredExtensions.AddSelfRegisteredExtensions(builder, args)
use! app = builder.BuildAsync()
return! app.RunAsync()
}
|> Async.AwaitTask
|> Async.RunSynchronously
""";
}
throw new InvalidOperationException($"Language not supported '{language}'");
}
}