Skip to content

Commit 496d763

Browse files
Improved build
1 parent 4e80a5a commit 496d763

File tree

2 files changed

+58
-37
lines changed

2 files changed

+58
-37
lines changed

Build/Program.cs

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,18 @@
8282
}
8383
}
8484

85-
var buildProps = new[] {("version", packageVersion.ToString())};
85+
var buildProps = new[]
86+
{
87+
("configuration", configuration),
88+
("version", packageVersion.ToString())
89+
};
90+
8691
new MSBuild()
8792
.WithProject(Path.Combine(currentDir, "CSharpInteractive", "CSharpInteractive.Tool.csproj"))
8893
.WithRestore(true)
8994
.WithTarget("Rebuild;GetDependencyTargetPaths")
9095
.WithProps(buildProps)
91-
.Build()
92-
.EnsureSuccess();
96+
.Build().EnsureSuccess();
9397

9498
const string templateJson = "CSharpInteractive.Templates/content/ConsoleApplication-CSharp/.template.config/template.json";
9599
var content = File.ReadAllText(templateJson);
@@ -173,13 +177,17 @@
173177
}
174178
}
175179

176-
var uninstallTool = new DotNetToolUninstall()
180+
var hasTool = new DotNetToolList()
177181
.WithPackage(toolPackageId)
178-
.WithGlobal(true);
182+
.WithGlobal(true)
183+
.Run().ExitCode == 0;
179184

180-
if (uninstallTool.Run(_ => { }).ExitCode != 0)
185+
if (hasTool)
181186
{
182-
Warning($"{uninstallTool} failed.");
187+
new DotNetToolUninstall()
188+
.WithPackage(toolPackageId)
189+
.WithGlobal(true)
190+
.Run().EnsureSuccess();
183191
}
184192

185193
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
@@ -196,28 +204,22 @@
196204
.WithProject(Path.Combine("Samples", "MySampleLib"))
197205
.BuildAsync().EnsureSuccess();
198206

199-
var installTool = new DotNetToolInstall()
207+
new DotNetToolInstall()
200208
.WithPackage(toolPackageId)
201209
.WithGlobal(true)
210+
.WithNoCache(true)
202211
.WithVersion(packageVersion.ToString())
203-
.AddSources(Path.Combine(outputDir, "CSharpInteractive.Tool"));
204-
205-
installTool.Run(output =>
206-
{
207-
output.Handled = true;
208-
WriteLine(output.Line);
209-
}).EnsureSuccess(_ => true);
212+
.AddSources(Path.Combine(outputDir, "CSharpInteractive.Tool"))
213+
.Run().EnsureSuccess();
210214

211-
new DotNetCsi().WithVersion(true).WithShortName("Checking csi tool").Run().EnsureSuccess();
215+
new DotNetCsi()
216+
.WithVersion(true)
217+
.WithShortName("Checking csi tool")
218+
.Run().EnsureSuccess();
212219

213-
var uninstallTemplates = new DotNetNewUninstall()
214-
.WithPackage(templatesPackageId);
215-
216-
uninstallTemplates.Run(output =>
217-
{
218-
output.Handled = true;
219-
WriteLine(output.Line);
220-
}).EnsureSuccess(_ => true);
220+
new DotNetNewUninstall()
221+
.WithPackage(templatesPackageId)
222+
.Run();
221223

222224
var installTemplates = new DotNetNewInstall()
223225
.WithPackage($"{templatesPackageId}::{packageVersion.ToString()}")
@@ -243,7 +245,7 @@
243245
Info("Pushing NuGet packages were skipped.");
244246
}
245247

246-
if (integrationTests || dockerLinuxTests)
248+
if (!skipTests && (integrationTests || dockerLinuxTests))
247249
{
248250
var logicOp = integrationTests && dockerLinuxTests ? "|" : "&";
249251
var filter = $"Integration={integrationTests}{logicOp}Docker={dockerLinuxTests}";

Samples/MySampleLib/Build/Program.cs

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using HostApi;
33
using Microsoft.Extensions.DependencyInjection;
44
using NuGet.Versioning;
5+
// ReSharper disable SeparateLocalFunctionsWithJumpStatement
6+
// ReSharper disable UnusedVariable
57

68
// Output, logging and tracing API
79
WriteLine("Hello");
@@ -58,7 +60,9 @@
5860
}
5961

6062
// API for .NET CLI
61-
var buildResult = new DotNetBuild().WithConfiguration(configuration).WithNoLogo(true)
63+
var buildResult = new DotNetBuild()
64+
.WithConfiguration(configuration)
65+
.WithNoLogo(true)
6266
.Build().EnsureSuccess();
6367

6468
var warnings = buildResult.Warnings
@@ -76,11 +80,11 @@
7680

7781
// Asynchronous way
7882
var cts = new CancellationTokenSource();
79-
/*await new DotNetTest()
83+
await new DotNetTest()
8084
.WithConfiguration(configuration)
81-
.WithNoLogo(true).WithNoBuild(true)
82-
.BuildAsync(CancellationOnFirstFailedTest, cts.Token)
83-
.EnsureSuccess();*/
85+
.WithNoLogo(true)
86+
.WithNoBuild(true)
87+
.BuildAsync(CancellationOnFirstFailedTest, cts.Token);
8488

8589
void CancellationOnFirstFailedTest(BuildMessage message)
8690
{
@@ -95,20 +99,35 @@ void CancellationOnFirstFailedTest(BuildMessage message)
9599

96100
async Task<IEnumerable<IBuildResult>> RunTestsAsync(string framework, params string[] platforms)
97101
{
98-
var publish = new DotNetPublish().WithWorkingDirectory("MySampleLib.Tests")
99-
.WithFramework($"net{framework}").WithConfiguration(configuration).WithNoBuild(true);
102+
var publish = new DotNetPublish()
103+
.WithWorkingDirectory("MySampleLib.Tests")
104+
.WithFramework($"net{framework}")
105+
.WithConfiguration(configuration)
106+
.WithNoBuild(true);
107+
100108
await publish.BuildAsync(cancellationToken: cts.Token).EnsureSuccess();
101109
var publishPath = Path.Combine(publish.WorkingDirectory, "bin", configuration, $"net{framework}", "publish");
102110

103-
var test = new VSTest().WithTestFileNames("*.Tests.dll");
104-
var testInDocker = new DockerRun().WithCommandLine(test).WithAutoRemove(true).WithQuiet(true)
105-
.WithVolumes((Path.GetFullPath(publishPath), "/app")).WithContainerWorkingDirectory("/app");
111+
var test = new VSTest()
112+
.WithTestFileNames("*.Tests.dll");
113+
114+
var testInDocker = new DockerRun()
115+
.WithCommandLine(test)
116+
.WithAutoRemove(true)
117+
.WithQuiet(true)
118+
.WithVolumes((Path.GetFullPath(publishPath), "/app"))
119+
.WithContainerWorkingDirectory("/app");
120+
106121
var tasks = from platform in platforms
107122
let image = $"mcr.microsoft.com/dotnet/sdk:{framework}-{platform}"
108-
select testInDocker.WithImage(image).BuildAsync(CancellationOnFirstFailedTest, cts.Token);
123+
select testInDocker
124+
.WithImage(image)
125+
.BuildAsync(CancellationOnFirstFailedTest, cts.Token);
109126
return await Task.WhenAll(tasks);
110127
}
111128

112129
#pragma warning disable CS9113// Parameter is unread.
113-
class MyTool(INuGet nuGet);
130+
131+
internal class MyTool(INuGet nuGet);
132+
114133
#pragma warning restore CS9113// Parameter is unread.

0 commit comments

Comments
 (0)