-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
137 lines (109 loc) · 4.01 KB
/
Copy pathProgram.cs
File metadata and controls
137 lines (109 loc) · 4.01 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
using System.Web;
using HostApi;
using Microsoft.Extensions.DependencyInjection;
using NuGet.Versioning;
// ReSharper disable SeparateLocalFunctionsWithJumpStatement
// ReSharper disable UnusedVariable
// Output, logging and tracing API
WriteLine("Hello");
WriteLine("Hello !!!", Color.Highlighted);
Summary("Summary message");
Error("Error details", "ErrorId");
Warning("Warning");
Info("Some info");
Trace("Trace message");
// API for arguments and parameters
Info("First argument: " + (Args.Count > 0 ? Args[0] : "empty"));
Info("Version: " + Props.Get("version", "1.0.0"));
Props["version"] = "1.0.1";
var configuration = Props.Get("configuration", "Release");
Info($"Configuration: {configuration}");
// Command line API
var cmd = new CommandLine("whoami");
cmd.Run().EnsureSuccess();
// Asynchronous way
await cmd.RunAsync().EnsureSuccess();
// API for Docker CLI
await new DockerRun("ubuntu")
.WithCommandLine(cmd)
.WithPull(DockerPullType.Always)
.WithAutoRemove(true)
.RunAsync()
.EnsureSuccess();
// Microsoft DI API to resolve dependencies
var nuget = GetService<INuGet>();
// Creating a custom service provider
var serviceCollection = GetService<IServiceCollection>();
serviceCollection.AddSingleton<MyTool>();
var myServiceProvider = serviceCollection.BuildServiceProvider();
var tool = myServiceProvider.GetRequiredService<MyTool>();
// API for NuGet
var settings = new NuGetRestoreSettings("MySampleLib")
.WithVersionRange(VersionRange.Parse("[1.0.14, 1.1)"))
.WithTargetFrameworkMoniker("net10.0")
.WithPackagesPath(".packages");
var packages = nuget.Restore(settings);
foreach (var package in packages)
{
Info(package.Path);
}
// API for .NET CLI
var buildResult = new DotNetBuild()
.WithConfiguration(configuration)
.WithNoLogo(true)
.Build().EnsureSuccess();
var warnings = buildResult.Warnings
.Where(warn => Path.GetFileName(warn.File) == "Calculator.cs")
.Select(warn => $"{warn.Code}({warn.LineNumber}:{warn.ColumnNumber})")
.Distinct();
foreach (var warning in warnings)
{
await new HttpClient().GetAsync(
"https://api.telegram.org/bot7102686717:AAEHw7HZinme_5kfIRV7TwXK4Xql9WPPpM3/" +
"sendMessage?chat_id=878745093&text="
+ HttpUtility.UrlEncode(warning));
}
// Asynchronous way
var cts = new CancellationTokenSource();
await new DotNetTest()
.WithConfiguration(configuration)
.WithNoLogo(true)
.WithNoBuild(true)
.BuildAsync(CancellationOnFirstFailedTest, cts.Token);
// Parallel tests
var results = await Task.WhenAll(
RunTestsAsync("7.0", "bookworm-slim", "alpine"),
RunTestsAsync("8.0", "bookworm-slim", "alpine", "noble"));
results.SelectMany(i => i).EnsureSuccess();
return;
void CancellationOnFirstFailedTest(BuildMessage message)
{
if (message.TestResult is {State: TestState.Failed}) cts.Cancel();
}
async Task<IEnumerable<IBuildResult>> RunTestsAsync(string framework, params string[] platforms)
{
var publish = new DotNetPublish()
.WithWorkingDirectory("MySampleLib.Tests")
.WithFramework($"net{framework}")
.WithConfiguration(configuration)
.WithNoBuild(true);
await publish.BuildAsync(cancellationToken: cts.Token).EnsureSuccess();
var publishPath = Path.Combine(publish.WorkingDirectory, "bin", configuration, $"net{framework}", "publish");
var test = new VSTest()
.WithTestFileNames("*.Tests.dll");
var testInDocker = new DockerRun()
.WithCommandLine(test)
.WithAutoRemove(true)
.WithQuiet(true)
.WithVolumes((Path.GetFullPath(publishPath), "/app"))
.WithContainerWorkingDirectory("/app");
var tasks = from platform in platforms
let image = $"mcr.microsoft.com/dotnet/sdk:{framework}-{platform}"
select testInDocker
.WithImage(image)
.BuildAsync(CancellationOnFirstFailedTest, cts.Token);
return await Task.WhenAll(tasks);
}
#pragma warning disable CS9113// Parameter is unread.
internal class MyTool(INuGet nuGet);
#pragma warning restore CS9113// Parameter is unread.