Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch build to Cake Frosting #957

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ install:
# Build Script #
#---------------------------------#
build_script:
- ps: .\build.ps1 --target=CI
- ps: .\build.ps1 --target=Publish

# Tests
test: off
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ stages:
vmImage: 'ubuntu-22.04'
steps:
- template: ../steps/install-required-dotnet-versions-for-building.yml
- bash: ./build.sh --target=Create-NuGet-Packages
- bash: ./build.sh --target=Package
displayName: 'Build'
- publish: $(Build.SourcesDirectory)/BuildArtifacts/Packages/NuGet
artifact: NuGet Package
Expand Down
12 changes: 0 additions & 12 deletions .config/dotnet-tools.json

This file was deleted.

13 changes: 13 additions & 0 deletions .github/renovate.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@
],
"datasourceTemplate": "nuget",
"versioningTemplate": "nuget"
},
{
"customType": "regex",
"description": "Update NuGet package versions in C# files",
"fileMatch": [
"build/{{arg0}}$",
"src/{{arg1}}$"
],
"matchStrings": [
"// renovate:( datasource=(?<datasource>.*?))? depName=(?<depName>.*?)( versioning=(?<versioning>.*?))?( extractVersion=(?<extractVersion>.*?))?\\s*const string \\w*[vV]ersion = \"(?<currentValue>\\S*)\";\\s?"
],
"datasourceTemplate": "nuget",
"versioningTemplate": "nuget"
}
],
"packageRules": [
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/integrationtests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
8.x
9.x
- name: Build
run: ./build.sh --target=Create-NuGet-Packages
run: ./build.sh --target=Package
shell: bash
- name: Publish NuGet package as build artifact
uses: actions/upload-artifact@65c4c4a1ddee5b72f698fdd19549f0f0fb45cf08 # v4
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -305,4 +305,5 @@ __pycache__/
# Project specific

BuildArtifacts/
build/tools/*
docs/.cache/
17 changes: 2 additions & 15 deletions build.ps1
Original file line number Diff line number Diff line change
@@ -1,15 +1,2 @@
$ErrorActionPreference = 'Stop'

$SCRIPT_NAME = "recipe.cake"

Write-Host "Restoring .NET Core tools"
dotnet tool restore
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }

Write-Host "Bootstrapping Cake"
dotnet cake $SCRIPT_NAME --bootstrap
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }

Write-Host "Running Build"
dotnet cake $SCRIPT_NAME @args
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
dotnet run --project build/Build.csproj -- $args
exit $LASTEXITCODE;
11 changes: 1 addition & 10 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,2 @@
#!/bin/bash
SCRIPT_NAME="recipe.cake"

echo "Restoring .NET Core tools"
dotnet tool restore

echo "Bootstrapping Cake"
dotnet cake $SCRIPT_NAME --bootstrap

echo "Running Build"
dotnet cake $SCRIPT_NAME "$@"
dotnet run --project ./build/Build.csproj -- "$@"
19 changes: 19 additions & 0 deletions build/Build.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>

<!-- Make sure start same folder .NET Core CLI and Visual Studio. -->
<!-- Also expected from Cake.Issues.Recipe to have build directory set as working directory. -->
<RunWorkingDirectory>$(MSBuildProjectDirectory)</RunWorkingDirectory>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Cake.Codecov" Version="3.0.0" />
<PackageReference Include="Cake.Frosting" Version="5.0.0" />
<PackageReference Include="Cake.Frosting.Issues.Recipe" Version="5.1.1" />
</ItemGroup>

</Project>
52 changes: 52 additions & 0 deletions build/BuildLifetime.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using Cake.Common;
using Cake.Common.Build;
using Cake.Common.Diagnostics;
using Cake.Common.IO;
using Cake.Common.Tools.GitVersion;
using Cake.Core;
using Cake.Frosting;
using Spectre.Console;

public class BuildLifetime : FrostingLifetime<BuildContext>
{
public override void Setup(BuildContext context, ISetupContext info)
{
AnsiConsole.Write(new FigletText("Cake.Issues").Centered());

// Determine version
DetermineVersion(context);

// Cleanup "dist" folder
context.CleanDirectory(context.Parameters.OutputDirectory);
}

public override void Teardown(BuildContext context, ITeardownContext info)
{
}

private static void DetermineVersion(BuildContext context)
{
var settings = new GitVersionSettings
{
ToolPath = context.Tools.Resolve("dotnet-gitversion.exe")
};

if (settings.ToolPath == null)
{
settings.ToolPath = context.Tools.Resolve("dotnet-gitversion");
}

if (!context.BuildSystem().IsLocalBuild)
{
settings.UpdateAssemblyInfo = true;
settings.UpdateAssemblyInfoFilePath =
context.State.RepositoryRootDirectory
.GetRelativePath(context.Paths.SrcDirectoryPath)
.CombineWithFilePath("SolutionInfo.cs");
}

context.State.Version = context.GitVersion(settings);

context.Information("Building version {0}", context.State.Version.FullSemVer);
}
}
25 changes: 25 additions & 0 deletions build/Context/BuildContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Cake.Core;

/// <summary>
/// Class holding state during execution of build.
/// </summary>
public class BuildContext : IssuesContext<BuildParameters, BuildState>
{
public Paths Paths { get; }

public BuildContext(ICakeContext context)
: base(context)
{
Paths = new Paths(this);
}

protected override BuildParameters CreateIssuesParameters()
{
return new BuildParameters(this);
}

protected override BuildState CreateIssuesState()
{
return new BuildState(this);
}
}
18 changes: 18 additions & 0 deletions build/Context/Parameters/BuildParameters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Cake.Core;

public class BuildParameters : IssuesParameters
{
public CodeCoverageParameters CodeCoverage { get; }

public MsBuildParameters Build { get; }

public PackagingParameters Packaging { get; }

public BuildParameters(ICakeContext context)
{
this.OutputDirectory = "../BuildArtifacts";
this.CodeCoverage = new CodeCoverageParameters(context);
this.Build = new MsBuildParameters(context);
this.Packaging = new PackagingParameters(context);
}
}
7 changes: 7 additions & 0 deletions build/Context/Parameters/CodeCoverageParameters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using Cake.Common;
using Cake.Core;

public class CodeCoverageParameters(ICakeContext context)
{
public string CodecovRepoToken { get; } = context.EnvironmentVariable("CODECOV_REPO_TOKEN");
}
15 changes: 15 additions & 0 deletions build/Context/Parameters/MsBuildParameters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Cake.Common.Build;
using Cake.Common.Tools.DotNet.MSBuild;
using Cake.Core;

public class MsBuildParameters(ICakeContext context)
{
public string Configuration { get; } = "Release"; // Configuration is also hardcoded in nuspec files

public DotNetMSBuildSettings GetSettings()
{
return new DotNetMSBuildSettings()
.SetConfiguration(this.Configuration)
.WithProperty("ContinuousIntegrationBuild", (!context.BuildSystem().IsLocalBuild).ToString());
}
}
7 changes: 7 additions & 0 deletions build/Context/Parameters/PackagingParameters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using Cake.Common;
using Cake.Core;

public class PackagingParameters(ICakeContext context)
{
public string NuGetApiKey { get; } = context.EnvironmentVariable("NUGET_API_KEY");
}
31 changes: 31 additions & 0 deletions build/Context/Paths.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Cake.Common.IO;
using Cake.Core.IO;

public class Paths
{
public FilePath SolutionFilePath { get; }

public DirectoryPath SrcDirectoryPath { get; }

public DirectoryPath NuspecDirectoryPath { get; }

public DirectoryPath OutputLogsDirectoryPath { get; }

public DirectoryPath OutputPackagesNuGetDirectoryPath { get; }

public DirectoryPath OutputTestResultsDirectoryPath { get; }

public Paths(BuildContext context)
{
this.SrcDirectoryPath = context.State.RepositoryRootDirectory.Combine("src");
this.NuspecDirectoryPath = context.State.RepositoryRootDirectory.Combine("nuspec").Combine("nuget");

this.OutputLogsDirectoryPath = context.Parameters.OutputDirectory.Combine("logs");
this.OutputPackagesNuGetDirectoryPath = context.Parameters.OutputDirectory.Combine("Packages").Combine("NuGet");
this.OutputTestResultsDirectoryPath = context.Parameters.OutputDirectory.Combine("TestResults");

this.SolutionFilePath = this.SrcDirectoryPath.CombineWithFilePath("Cake.Issues.sln");

context.EnsureDirectoryExists(context.Parameters.OutputDirectory);
}
}
11 changes: 11 additions & 0 deletions build/Context/State/BuildState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Cake.Common.Tools.GitVersion;

public class BuildState : IssuesState
{
public GitVersion Version { get; set; }

public BuildState(BuildContext context)
: base(context, RepositoryInfoProviderType.CakeGit)
{
}
}
11 changes: 11 additions & 0 deletions build/Extensions/ServiceProviderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Cake.Core.Packaging;
using Cake.Frosting;

internal static class ServiceProviderExtensions
{
public static void InstallTool(this IServiceProvider provider, string packageName, string packageVersion)
{
var toolInstaller = (IToolInstaller)provider.GetService(typeof(IToolInstaller));
toolInstaller.Install(new PackageReference($"nuget:?package={packageName}&version={packageVersion}"));
}
}
18 changes: 18 additions & 0 deletions build/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Cake.Frosting;
using System.Reflection;

public static class Program
{
public static int Main(string[] args)
{
// renovate: depName=GitVersion.Tool
const string nuGetVersion = "6.1.0";

return new CakeHost()
.UseContext<BuildContext>()
.UseLifetime<BuildLifetime>()
.AddAssembly(Assembly.GetAssembly(typeof(IssuesTask)))
.InstallTool(new Uri($"dotnet:?package=GitVersion.Tool&version={nuGetVersion}"))
.Run(args);
}
}
32 changes: 32 additions & 0 deletions build/Tasks/Build/BuildSolutionTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Cake.Common.Tools.DotNet;
using Cake.Common.Tools.DotNet.Build;
using Cake.Common.Tools.DotNet.MSBuild;
using Cake.Frosting;

[TaskName("Build-Solution")]
[IsDependentOn(typeof(RestoreSolutionTask))]
[IsDependeeOf(typeof(ReadIssuesTask))]
public sealed class BuildSolutionTask : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
{
var msBuildLogFilePath = context.Paths.OutputLogsDirectoryPath.CombineWithFilePath("build.binlog");

var settings = new DotNetBuildSettings
{
NoRestore = true,
MSBuildSettings =
context.Parameters.Build.GetSettings()
.WithLogger(
"BinaryLogger," + context.Environment.ApplicationRoot.CombineWithFilePath("StructuredLogger.dll"),
"",
msBuildLogFilePath.FullPath)
.WithTarget("Rebuild"),
};

context.DotNetBuild(context.Paths.SolutionFilePath.FullPath, settings);

// Read issues
context.Parameters.InputFiles.AddMsBuildBinaryLogFilePath(msBuildLogFilePath);
}
}
7 changes: 7 additions & 0 deletions build/Tasks/Build/BuildTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using Cake.Frosting;

[TaskName("Build")]
[IsDependentOn(typeof(BuildSolutionTask))]
public sealed class BuildTask : FrostingTask
{
}
21 changes: 21 additions & 0 deletions build/Tasks/Build/RestoreSolutionTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Cake.Common.Build;
using Cake.Common.Tools.DotNet;
using Cake.Common.Tools.DotNet.Restore;
using Cake.Frosting;

[TaskName("Restore-Solution")]
public sealed class RestoreSolutionTask : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
{
// Enforce up to date lock files in CI builds.
// On local builds lock files will be updated if necessary.
var settings = new DotNetRestoreSettings
{
LockedMode = !context.BuildSystem().IsLocalBuild,
MSBuildSettings = context.Parameters.Build.GetSettings(),
};

context.DotNetRestore(context.Paths.SolutionFilePath.FullPath, settings);
}
}
Loading
Loading