Skip to content

Commit

Permalink
Improved build
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Mar 6, 2024
1 parent 7e54741 commit 0c0cb1b
Show file tree
Hide file tree
Showing 29 changed files with 191 additions and 225 deletions.
2 changes: 1 addition & 1 deletion .space.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
job("Pack") {
job("Build and check") {
container(displayName = "Build and check", image = "mcr.microsoft.com/dotnet/sdk") {
shellScript {
content = "dotnet run --project ./build -- check"
Expand Down
17 changes: 8 additions & 9 deletions build/Targets/BenchmarksTarget.cs → build/BenchmarksTarget.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// ReSharper disable ClassNeverInstantiated.Global
namespace Build.Targets;

namespace Build;

internal class BenchmarksTarget(
Settings settings,
ICommands commands,
IPaths paths,
Commands commands,
Paths paths,
ITeamCityArtifactsWriter artifactsWriter)
: IInitializable, ITarget<int>
{
Expand All @@ -25,7 +26,6 @@ public Task InitializeAsync() => commands.Register(

public Task<int> RunAsync(CancellationToken cancellationToken)
{
Info("Benchmarking");
var solutionDirectory = paths.SolutionDirectory;
var logsDirectory = Path.Combine(solutionDirectory, ".logs");
Directory.CreateDirectory(logsDirectory);
Expand All @@ -37,14 +37,13 @@ public Task<int> RunAsync(CancellationToken cancellationToken)
else
{
Directory.CreateDirectory(artifactsDirectory);

var benchmark = new DotNetRun()
new DotNetRun()
.WithProject(Path.Combine("benchmarks", "Pure.DI.Benchmarks", "Pure.DI.Benchmarks.csproj"))
.WithConfiguration(settings.Configuration)
.WithArgs("--artifacts", artifactsDirectory, "--", "--filter")
.AddArgs(Reports.Select(filter => $"*{filter}*").ToArray());

benchmark.Run().Succeed("Benchmarking");
.AddArgs(Reports.Select(filter => $"*{filter}*").ToArray())
.Run()
.Succeed("Benchmarking");
}

var index = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
// ReSharper disable ReturnTypeCanBeEnumerable.Local
// ReSharper disable InvertIf

namespace Build.Targets;
namespace Build;

using NuGet.Versioning;

internal class CompatibilityCheckTarget(
Settings settings,
ICommands commands,
IPaths paths,
Commands commands,
Paths paths,
INuGet nuGet,
[Tag(typeof(GeneratorTarget))] ITarget<Package> generatorTarget,
[Tag(typeof(LibrariesTarget))] ITarget<IReadOnlyCollection<Library>> librariesTarget)
Expand All @@ -27,7 +27,6 @@ public Task InitializeAsync() => commands.Register(
[SuppressMessage("Performance", "CA1861:Avoid constant arrays as arguments")]
public async Task<IReadOnlyCollection<Package>> RunAsync(CancellationToken cancellationToken)
{
Info("Compatibility checks");
var generatorPackage = await generatorTarget.RunAsync(cancellationToken);
var libraries = await librariesTarget.RunAsync(cancellationToken);

Expand Down Expand Up @@ -68,7 +67,7 @@ public async Task<IReadOnlyCollection<Package>> RunAsync(CancellationToken cance
{
await CompatibilityCheckAsync(generatorPackage.Path, framework, cancellationToken);
}

var packages = new List<Package> { generatorPackage };

// Libraries
Expand Down
38 changes: 38 additions & 0 deletions build/DeployTarget.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// ReSharper disable ClassNeverInstantiated.Global

namespace Build;

internal class DeployTarget(
Settings settings,
Commands commands,
[Tag(typeof(PackTarget))] ITarget<IReadOnlyCollection<Package>> packTarget)
: IInitializable, ITarget<int>
{
public Task InitializeAsync() => commands.Register(
this,
"Deploys packages",
"deploy",
"dp");

public async Task<int> RunAsync(CancellationToken cancellationToken)
{
if (string.IsNullOrWhiteSpace(settings.NuGetKey))
{
Warning("The NuGet key was not specified, the packages will not be pushed.");
return 0;
}

var packages = await packTarget.RunAsync(cancellationToken);
foreach (var package in packages.Where(i => i.Deploy))
{
new DotNetNuGetPush()
.WithPackage(package.Path)
.WithSources("https://api.nuget.org/v3/index.json")
.WithApiKey(settings.NuGetKey)
.Build()
.Succeed();
}

return 0;
}
}
62 changes: 36 additions & 26 deletions build/Targets/GeneratorTarget.cs → build/GeneratorTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,45 @@
// ReSharper disable ClassNeverInstantiated.Global
// ReSharper disable ReturnTypeCanBeEnumerable.Local
// ReSharper disable InvertIf
namespace Build.Targets;

namespace Build;

using System.IO.Compression;
using NuGet.Versioning;

internal class GeneratorTarget(
Settings settings,
ICommands commands)
Commands commands)
: IInitializable, ITarget<Package>
{
public Task InitializeAsync() => commands.Register(
this,
"Builds and tests generator",
"generator",
"g");

[SuppressMessage("Performance", "CA1861:Avoid constant arrays as arguments")]
public Task<Package> RunAsync(CancellationToken cancellationToken)
{
Info("Building generator");
// Generator package
var generatorProjectDirectory = Path.Combine("src", "Pure.DI");
var generatorPackages = settings.CodeAnalysis
.Select(codeAnalysis => CreateGeneratorPackage(settings.Version, codeAnalysis, generatorProjectDirectory));

return Task.FromResult(new Package(Path.GetFullPath(MergeGeneratorPackages(settings.Version, generatorPackages, generatorProjectDirectory)), true));
return Task.FromResult(
new Package(
Path.GetFullPath(MergeGeneratorPackages(settings.Version, generatorPackages, generatorProjectDirectory)),
true));
}

private string CreateGeneratorPackage(NuGetVersion packageVersion, CodeAnalysis codeAnalysis, string projectDirectory)
{
var analyzerRoslynPackageVersion = codeAnalysis.AnalyzerRoslynPackageVersion;
var analyzerRoslynVersion = new Version(analyzerRoslynPackageVersion.Major, analyzerRoslynPackageVersion.Minor);
Info($"Build package for Roslyn {analyzerRoslynVersion}.");

List<(string, string)> props = [
List<(string, string)> props =
[
("configuration", settings.Configuration),
("version", packageVersion.ToString()),
("AnalyzerRoslynVersion", analyzerRoslynVersion.ToString()),
Expand All @@ -49,39 +53,44 @@ private string CreateGeneratorPackage(NuGetVersion packageVersion, CodeAnalysis
{
props.Add(("CI", "true"));
}
Info($"Building {codeAnalysis.AnalyzerRoslynPackageVersion}");
var build = new MSBuild()

new MSBuild()
.WithShortName($"Building {codeAnalysis.AnalyzerRoslynPackageVersion}")
.WithTarget("clean;rebuild")
.WithRestore(true)
.WithProps(props);

var buildResult = build.Build();
buildResult.Succeed();
.WithProps(props)
.Build()
.Succeed();

Info($"Testing {codeAnalysis.AnalyzerRoslynPackageVersion}");
var test = new DotNetTest()
var testResult = new DotNetTest()
.WithShortName($"Testing {codeAnalysis.AnalyzerRoslynPackageVersion}")
.WithProps(props)
.WithConfiguration(settings.Configuration)
.WithNoBuild(true)
.WithNoLogo(true);
.WithNoLogo(true)
.Build();

var testResult = test.Build();
WriteLine(testResult.ToString(), Color.Details);
testResult.Succeed();

Info($"Packing {codeAnalysis.AnalyzerRoslynPackageVersion}");
var pack = new DotNetPack()
new DotNetPack()
.WithShortName($"Packing {codeAnalysis.AnalyzerRoslynPackageVersion}")
.WithProps(props)
.WithConfiguration(settings.Configuration)
.WithNoBuild(true)
.WithNoLogo(true)
.WithProject(Path.Combine(projectDirectory, "Pure.DI.csproj"));

pack.Build().Succeed();
return Path.Combine(projectDirectory, "bin", $"roslyn{analyzerRoslynVersion}", settings.Configuration, $"Pure.DI.{packageVersion.ToString()}.nupkg");
.WithProject(Path.Combine(projectDirectory, "Pure.DI.csproj"))
.Build()
.Succeed();

return Path.Combine(
projectDirectory,
"bin",
$"roslyn{analyzerRoslynVersion}",
settings.Configuration,
$"Pure.DI.{packageVersion.ToString()}.nupkg");
}

private string MergeGeneratorPackages(NuGetVersion packageVersion, IEnumerable<string> mergingPackages, string projectDirectory)
{
var targetPackage = Path.GetFullPath(Path.Combine(projectDirectory, "bin", settings.Configuration, $"Pure.DI.{packageVersion}.nupkg"));
Expand Down Expand Up @@ -126,11 +135,12 @@ private string MergeGeneratorPackages(NuGetVersion packageVersion, IEnumerable<s
newStream.Write(buffer, 0, size);
}
} while (size > 0);

newStream.Flush();
WriteLine($"{entry.FullName,-100} - merged", Color.Details);
}
}

return targetPackage;
}
}
1 change: 0 additions & 1 deletion build/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

global using System.CommandLine;
global using System.Diagnostics.CodeAnalysis;
global using Build.Targets;
global using Build.Tools;
global using HostApi;
global using JetBrains.TeamCity.ServiceMessages.Write.Special;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Build.Targets;
namespace Build;

internal interface IInitializable
{
Expand Down
2 changes: 1 addition & 1 deletion build/Targets/ITarget.cs → build/ITarget.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Build.Targets;
namespace Build;

internal interface ITarget<T>
{
Expand Down
22 changes: 10 additions & 12 deletions build/Targets/LibrariesTarget.cs → build/LibrariesTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,26 @@
// ReSharper disable ClassNeverInstantiated.Global
// ReSharper disable ReturnTypeCanBeEnumerable.Local
// ReSharper disable InvertIf
namespace Build.Targets;

namespace Build;

using NuGet.Versioning;

internal class LibrariesTarget(
Settings settings,
ICommands commands,
ISdk sdk)
Commands commands,
Sdk sdk)
: IInitializable, ITarget<IReadOnlyCollection<Library>>
{
public Task InitializeAsync() => commands.Register(
this,
"Builds and tests libraries",
"libs",
"l");

[SuppressMessage("Performance", "CA1861:Avoid constant arrays as arguments")]
public async Task<IReadOnlyCollection<Library>> RunAsync(CancellationToken cancellationToken)
public Task<IReadOnlyCollection<Library>> RunAsync(CancellationToken cancellationToken)
{
Info("Building libraries");

// Libraries
List<Library> libraries =
[
Expand All @@ -46,18 +45,17 @@ public async Task<IReadOnlyCollection<Library>> RunAsync(CancellationToken cance
("version", settings.Version.ToString())
};

var libraryPackResult = await new DotNetPack()
new DotNetPack()
.WithProps(props)
.WithConfiguration(settings.Configuration)
.WithNoBuild(true)
.WithNoLogo(true)
.WithProject(Path.Combine(Path.GetFullPath(Path.Combine("src", library.Name)), $"{library.Name}.csproj"))
.BuildAsync(cancellationToken: cancellationToken);

libraryPackResult.Succeed();
.Build()
.Succeed();
}

return libraries;
return Task.FromResult<IReadOnlyCollection<Library>>(libraries);
}

private string GetPackagePath(string library, NuGetVersion version)
Expand Down
2 changes: 1 addition & 1 deletion build/Targets/Library.cs → build/Library.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Build.Targets;
namespace Build;

internal record Library(
string Name,
Expand Down
5 changes: 2 additions & 3 deletions build/Targets/PackTarget.cs → build/PackTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
// ReSharper disable ReturnTypeCanBeEnumerable.Local
// ReSharper disable InvertIf

namespace Build.Targets;
namespace Build;

internal class PackTarget(
ICommands commands,
Commands commands,
ITeamCityArtifactsWriter artifactsWriter,
[Tag(typeof(CompatibilityCheckTarget))] ITarget<IReadOnlyCollection<Package>> compatibilityCheckTarget)
: IInitializable, ITarget<IReadOnlyCollection<Package>>
Expand All @@ -22,7 +22,6 @@ public Task InitializeAsync() => commands.Register(
[SuppressMessage("Performance", "CA1861:Avoid constant arrays as arguments")]
public async Task<IReadOnlyCollection<Package>> RunAsync(CancellationToken cancellationToken)
{
Info("Packing");
var packages = await compatibilityCheckTarget.RunAsync(cancellationToken);
foreach (var package in packages)
{
Expand Down
2 changes: 1 addition & 1 deletion build/Targets/Package.cs → build/Package.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
namespace Build.Targets;
namespace Build;

internal record Package(string Path, bool Deploy);
12 changes: 2 additions & 10 deletions build/Program.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
using Build;
using IProperties = Build.Tools.IProperties;

DI.Setup(nameof(Composition))
.Root<ITarget<int>>("RootTarget")
.Root<RootTarget>("RootTarget")
.DefaultLifetime(Lifetime.PerBlock)
.Bind<Settings>().To<Settings>()
.Bind<RootCommand>().To<RootCommand>()
.Bind<Settings>().To<Settings>()
.Bind<ITeamCityArtifactsWriter>().To(_ => GetService<ITeamCityWriter>())
.Bind<INuGet>().To(_ => GetService<INuGet>())
// Tools
.Bind<ICommands>().To<Commands>()
.Bind<IPaths>().To<Paths>()
.Bind<IProperties>().To<Properties>()
.Bind<ISdk>().To<Sdk>()
.Bind<IVersions>().To<Versions>()
// Targets
.Bind<ITarget<int>>().To<RootTarget>()
.Bind<IInitializable, ITarget<Package>>(typeof(GeneratorTarget)).To<GeneratorTarget>()
.Bind<IInitializable, ITarget<IReadOnlyCollection<Library>>>(typeof(LibrariesTarget)).To<LibrariesTarget>()
.Bind<IInitializable, ITarget<IReadOnlyCollection<Package>>>(typeof(CompatibilityCheckTarget)).To<CompatibilityCheckTarget>()
Expand Down
Loading

0 comments on commit 0c0cb1b

Please sign in to comment.