Skip to content

Commit

Permalink
Improved build
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Oct 30, 2024
1 parent 4e80a5a commit 496d763
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 37 deletions.
52 changes: 27 additions & 25 deletions Build/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,18 @@
}
}

var buildProps = new[] {("version", packageVersion.ToString())};
var buildProps = new[]
{
("configuration", configuration),
("version", packageVersion.ToString())
};

new MSBuild()
.WithProject(Path.Combine(currentDir, "CSharpInteractive", "CSharpInteractive.Tool.csproj"))
.WithRestore(true)
.WithTarget("Rebuild;GetDependencyTargetPaths")
.WithProps(buildProps)
.Build()
.EnsureSuccess();
.Build().EnsureSuccess();

const string templateJson = "CSharpInteractive.Templates/content/ConsoleApplication-CSharp/.template.config/template.json";
var content = File.ReadAllText(templateJson);
Expand Down Expand Up @@ -173,13 +177,17 @@
}
}

var uninstallTool = new DotNetToolUninstall()
var hasTool = new DotNetToolList()
.WithPackage(toolPackageId)
.WithGlobal(true);
.WithGlobal(true)
.Run().ExitCode == 0;

if (uninstallTool.Run(_ => { }).ExitCode != 0)
if (hasTool)
{
Warning($"{uninstallTool} failed.");
new DotNetToolUninstall()
.WithPackage(toolPackageId)
.WithGlobal(true)
.Run().EnsureSuccess();
}

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

var installTool = new DotNetToolInstall()
new DotNetToolInstall()
.WithPackage(toolPackageId)
.WithGlobal(true)
.WithNoCache(true)
.WithVersion(packageVersion.ToString())
.AddSources(Path.Combine(outputDir, "CSharpInteractive.Tool"));

installTool.Run(output =>
{
output.Handled = true;
WriteLine(output.Line);
}).EnsureSuccess(_ => true);
.AddSources(Path.Combine(outputDir, "CSharpInteractive.Tool"))
.Run().EnsureSuccess();

new DotNetCsi().WithVersion(true).WithShortName("Checking csi tool").Run().EnsureSuccess();
new DotNetCsi()
.WithVersion(true)
.WithShortName("Checking csi tool")
.Run().EnsureSuccess();

var uninstallTemplates = new DotNetNewUninstall()
.WithPackage(templatesPackageId);

uninstallTemplates.Run(output =>
{
output.Handled = true;
WriteLine(output.Line);
}).EnsureSuccess(_ => true);
new DotNetNewUninstall()
.WithPackage(templatesPackageId)
.Run();

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

if (integrationTests || dockerLinuxTests)
if (!skipTests && (integrationTests || dockerLinuxTests))
{
var logicOp = integrationTests && dockerLinuxTests ? "|" : "&";
var filter = $"Integration={integrationTests}{logicOp}Docker={dockerLinuxTests}";
Expand Down
43 changes: 31 additions & 12 deletions Samples/MySampleLib/Build/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using HostApi;
using Microsoft.Extensions.DependencyInjection;
using NuGet.Versioning;
// ReSharper disable SeparateLocalFunctionsWithJumpStatement
// ReSharper disable UnusedVariable

// Output, logging and tracing API
WriteLine("Hello");
Expand Down Expand Up @@ -58,7 +60,9 @@
}

// API for .NET CLI
var buildResult = new DotNetBuild().WithConfiguration(configuration).WithNoLogo(true)
var buildResult = new DotNetBuild()
.WithConfiguration(configuration)
.WithNoLogo(true)
.Build().EnsureSuccess();

var warnings = buildResult.Warnings
Expand All @@ -76,11 +80,11 @@

// Asynchronous way
var cts = new CancellationTokenSource();
/*await new DotNetTest()
await new DotNetTest()
.WithConfiguration(configuration)
.WithNoLogo(true).WithNoBuild(true)
.BuildAsync(CancellationOnFirstFailedTest, cts.Token)
.EnsureSuccess();*/
.WithNoLogo(true)
.WithNoBuild(true)
.BuildAsync(CancellationOnFirstFailedTest, cts.Token);

void CancellationOnFirstFailedTest(BuildMessage message)
{
Expand All @@ -95,20 +99,35 @@ void CancellationOnFirstFailedTest(BuildMessage message)

async Task<IEnumerable<IBuildResult>> RunTestsAsync(string framework, params string[] platforms)
{
var publish = new DotNetPublish().WithWorkingDirectory("MySampleLib.Tests")
.WithFramework($"net{framework}").WithConfiguration(configuration).WithNoBuild(true);
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 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);
select testInDocker
.WithImage(image)
.BuildAsync(CancellationOnFirstFailedTest, cts.Token);
return await Task.WhenAll(tasks);
}

#pragma warning disable CS9113// Parameter is unread.
class MyTool(INuGet nuGet);

internal class MyTool(INuGet nuGet);

#pragma warning restore CS9113// Parameter is unread.

0 comments on commit 496d763

Please sign in to comment.