Skip to content

Commit

Permalink
Add Release build task
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreyAkinshin committed Jul 8, 2023
1 parent bf020e9 commit 3b361e3
Show file tree
Hide file tree
Showing 9 changed files with 367 additions and 47 deletions.
49 changes: 20 additions & 29 deletions build/BenchmarkDotNet.Build/BuildContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class BuildContext : FrostingContext
public DotNetVerbosity BuildVerbosity { get; set; } = DotNetVerbosity.Minimal;
public int Depth { get; set; }
public bool VersionStable { get; }
public string NextVersion { get; }
public bool PushMode { get; }

public DirectoryPath RootDirectory { get; }
public DirectoryPath BuildDirectory { get; }
Expand All @@ -34,6 +36,8 @@ public class BuildContext : FrostingContext
public FilePath SolutionFile { get; }
public FilePath TemplatesTestsProjectFile { get; }
public FilePathCollection AllPackableSrcProjects { get; }
public FilePath VersionsFile { get; }
public FilePath CommonPropsFile { get; }

public DotNetMSBuildSettings MsBuildSettingsRestore { get; }
public DotNetMSBuildSettings MsBuildSettingsBuild { get; }
Expand All @@ -45,9 +49,11 @@ public class BuildContext : FrostingContext

public VersionHistory VersionHistory { get; }

public GitRunner GitRunner { get; }
public UnitTestRunner UnitTestRunner { get; }
public DocumentationRunner DocumentationRunner { get; }
public BuildRunner BuildRunner { get; }
public ReleaseRunner ReleaseRunner { get; }

public BuildContext(ICakeContext context)
: base(context)
Expand All @@ -64,6 +70,9 @@ public BuildContext(ICakeContext context)
AllPackableSrcProjects = new FilePathCollection(context.GetFiles(RootDirectory.FullPath + "/src/**/*.csproj")
.Where(p => !p.FullPath.Contains("Disassembler")));

VersionsFile = BuildDirectory.CombineWithFilePath("versions.txt");
CommonPropsFile = BuildDirectory.CombineWithFilePath("common.props");

MsBuildSettingsRestore = new DotNetMSBuildSettings();
MsBuildSettingsBuild = new DotNetMSBuildSettings();
MsBuildSettingsPack = new DotNetMSBuildSettings();
Expand All @@ -78,6 +87,8 @@ public BuildContext(ICakeContext context)

Depth = -1;
VersionStable = false;
NextVersion = "";
PushMode = false;
if (context.Arguments.HasArgument("msbuild"))
{
var msBuildParameters = context.Arguments.GetArguments().First(it => it.Key == "msbuild").Value;
Expand Down Expand Up @@ -107,6 +118,12 @@ public BuildContext(ICakeContext context)

if (name.Equals("VersionStable", StringComparison.OrdinalIgnoreCase) && value != "")
VersionStable = true;

if (name.Equals("NextVersion", StringComparison.OrdinalIgnoreCase) && value != "")
NextVersion = value;

if (name.Equals("PushMode", StringComparison.OrdinalIgnoreCase) && value != "")
PushMode = true;
}
}
}
Expand All @@ -129,11 +146,13 @@ public BuildContext(ICakeContext context)
nuGetPackageNames.Sort();
NuGetPackageNames = nuGetPackageNames;

VersionHistory = new VersionHistory(this, BuildDirectory.CombineWithFilePath("versions.txt"));
VersionHistory = new VersionHistory(this, VersionsFile);

GitRunner = new GitRunner(this);
UnitTestRunner = new UnitTestRunner(this);
DocumentationRunner = new DocumentationRunner(this);
BuildRunner = new BuildRunner(this);
ReleaseRunner = new ReleaseRunner(this);
}

public void GenerateFile(FilePath filePath, StringBuilder content)
Expand All @@ -160,32 +179,4 @@ public void GenerateFile(FilePath filePath, string content)
}
}

public void Clone(DirectoryPath path, string repoUrl, string branchName)
{
this.Information($"[GitClone]");
this.Information($" Repo: {repoUrl}");
this.Information($" Branch: {branchName}");
this.Information($" Path: {path}");
var settings = new GitCloneSettings { Checkout = true, BranchName = branchName };
try
{
this.GitClone(repoUrl, path, settings);
this.Information(" Success");
}
catch (Exception e)
{
this.Error($" Failed to clone via API (Exception: {e.GetType().Name})'");
try
{
var gitArgs = $"clone -b {branchName} {repoUrl} {path}";
this.Information($" Trying to clone manually using 'git {gitArgs}'");
this.StartProcess("git", gitArgs);
this.Information(" Success");
}
catch (Exception e2)
{
throw new Exception($"Failed to clone {repoUrl} to {path} (branch: '{branchName})'", e2);
}
}
}
}
4 changes: 1 addition & 3 deletions build/BenchmarkDotNet.Build/ChangeLogBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ private async Task<string> Build()
if (string.IsNullOrEmpty(lastCommit))
lastCommit = $"v{currentVersion}";

var client = new GitHubClient(new ProductHeaderValue(GitHubCredentials.ProductHeader));
var tokenAuth = new Credentials(GitHubCredentials.Token);
client.Credentials = tokenAuth;
var client = GitHubCredentials.CreateClient();

if (currentVersion == "_")
{
Expand Down
9 changes: 9 additions & 0 deletions build/BenchmarkDotNet.Build/Meta/GitHubCredentials.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Octokit;

namespace BenchmarkDotNet.Build.Meta;

Expand All @@ -8,4 +9,12 @@ public static class GitHubCredentials

public const string ProductHeader = "BenchmarkDotNet";
public static string? Token => Environment.GetEnvironmentVariable(TokenVariableName);

public static GitHubClient CreateClient()
{
var client = new GitHubClient(new ProductHeaderValue(ProductHeader));
var tokenAuth = new Credentials(Token);
client.Credentials = tokenAuth;
return client;
}
}
3 changes: 3 additions & 0 deletions build/BenchmarkDotNet.Build/Meta/Repo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ public static class Repo
public const string Name = "BenchmarkDotNet";
public const string HttpsUrlBase = $"https://github.com/{Owner}/{Name}";
public const string HttpsGitUrl = $"{HttpsUrlBase}.git";

public const string ChangelogDetailsBranch = "docs-changelog-details";
public const string DocsStableBranch = "docs-stable";
public const string MasterBranch = "master";
}
11 changes: 11 additions & 0 deletions build/BenchmarkDotNet.Build/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@ public class DocsBuildTask : FrostingTask<BuildContext>
public override void Run(BuildContext context) => context.DocumentationRunner.Build();
}

[TaskName("Release")]
[TaskDescription("Release new version")]
[IsDependentOn(typeof(BuildTask))]
[IsDependentOn(typeof(PackTask))]
[IsDependentOn(typeof(DocsUpdateTask))]
[IsDependentOn(typeof(DocsBuildTask))]
public class ReleaseTask : FrostingTask<BuildContext>
{
public override void Run(BuildContext context) => context.ReleaseRunner.Run();
}

[TaskName("FastTests")]
[TaskDescription("OBSOLETE: use 'UnitTests'")]
[IsDependentOn(typeof(UnitTestsTask))]
Expand Down
13 changes: 13 additions & 0 deletions build/BenchmarkDotNet.Build/Runners/BuildRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Cake.Common.Tools.DotNet.Pack;
using Cake.Common.Tools.DotNet.Restore;
using Cake.Core;
using Cake.Core.IO;

namespace BenchmarkDotNet.Build.Runners;

Expand Down Expand Up @@ -40,6 +41,18 @@ public void Build()
});
}

public void BuildProjectSilent(FilePath projectFile)
{
context.DotNetBuild(projectFile.FullPath, new DotNetBuildSettings
{
NoRestore = false,
DiagnosticOutput = false,
MSBuildSettings = context.MsBuildSettingsBuild,
Configuration = context.BuildConfiguration,
Verbosity = DotNetVerbosity.Quiet
});
}

public void Pack()
{
context.CleanDirectory(context.ArtifactsDirectory);
Expand Down
37 changes: 22 additions & 15 deletions build/BenchmarkDotNet.Build/Runners/DocumentationRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public class DocumentationRunner
{
private readonly BuildContext context;

private readonly DirectoryPath changelogDirectory;
private readonly DirectoryPath changelogSrcDirectory;
public DirectoryPath ChangelogDirectory { get; }
public DirectoryPath ChangelogSrcDirectory { get; }
private readonly DirectoryPath changelogDetailsDirectory;
private readonly DirectoryPath docsGeneratedDirectory;

Expand All @@ -34,19 +34,19 @@ public DocumentationRunner(BuildContext context)
this.context = context;

var docsDirectory = context.RootDirectory.Combine("docs");
changelogDirectory = docsDirectory.Combine("changelog");
changelogSrcDirectory = docsDirectory.Combine("_changelog");
changelogDetailsDirectory = changelogSrcDirectory.Combine("details");
ChangelogDirectory = docsDirectory.Combine("changelog");
ChangelogSrcDirectory = docsDirectory.Combine("_changelog");
changelogDetailsDirectory = ChangelogSrcDirectory.Combine("details");
docsGeneratedDirectory = docsDirectory.Combine("_site");

redirectFile = docsDirectory.Combine("_redirects").CombineWithFilePath("_redirects");
docfxJsonFile = docsDirectory.CombineWithFilePath("docfx.json");
readmeFile = context.RootDirectory.CombineWithFilePath("README.md");
rootIndexFile = docsDirectory.CombineWithFilePath("index.md");
changelogIndexFile = changelogDirectory.CombineWithFilePath("index.md");
changelogFullFile = changelogDirectory.CombineWithFilePath("full.md");
changelogTocFile = changelogDirectory.CombineWithFilePath("toc.yml");
lastFooterFile = changelogSrcDirectory.Combine("footer")
changelogIndexFile = ChangelogDirectory.CombineWithFilePath("index.md");
changelogFullFile = ChangelogDirectory.CombineWithFilePath("full.md");
changelogTocFile = ChangelogDirectory.CombineWithFilePath("toc.yml");
lastFooterFile = ChangelogSrcDirectory.Combine("footer")
.CombineWithFilePath("v" + context.VersionHistory.CurrentVersion + ".md");
}

Expand Down Expand Up @@ -132,6 +132,10 @@ private void GenerateIndexMd()
private void GenerateChangelogToc()
{
var content = new StringBuilder();

content.AppendLine($"- name: {context.VersionHistory.CurrentVersion}");
content.AppendLine($" href: {context.VersionHistory.CurrentVersion}.md");

foreach (var version in context.VersionHistory.StableVersions.Reverse())
{
content.AppendLine($"- name: {version}");
Expand All @@ -153,6 +157,8 @@ private void GenerateChangelogFull()
content.AppendLine("");
content.AppendLine("# Full ChangeLog");
content.AppendLine("");
content.AppendLine(
$"[!include[{context.VersionHistory.CurrentVersion}]({context.VersionHistory.CurrentVersion}.md)]");
foreach (var version in context.VersionHistory.StableVersions.Reverse())
content.AppendLine($"[!include[{version}]({version}.md)]");

Expand All @@ -168,6 +174,7 @@ private void GenerateChangelogIndex()
content.AppendLine("");
content.AppendLine("# ChangeLog");
content.AppendLine("");
content.AppendLine($"* @changelog.{context.VersionHistory.CurrentVersion}");
foreach (var version in context.VersionHistory.StableVersions.Reverse())
content.AppendLine($"* @changelog.{version}");
content.AppendLine("* @changelog.full");
Expand All @@ -178,10 +185,10 @@ private void GenerateChangelogIndex()
private void DocfxChangelogGenerate(string version)
{
EnsureChangelogDetailsExist();
var header = changelogSrcDirectory.Combine("header").CombineWithFilePath(version + ".md");
var footer = changelogSrcDirectory.Combine("footer").CombineWithFilePath(version + ".md");
var details = changelogSrcDirectory.Combine("details").CombineWithFilePath(version + ".md");
var release = changelogDirectory.CombineWithFilePath(version + ".md");
var header = ChangelogSrcDirectory.Combine("header").CombineWithFilePath(version + ".md");
var footer = ChangelogSrcDirectory.Combine("footer").CombineWithFilePath(version + ".md");
var details = ChangelogSrcDirectory.Combine("details").CombineWithFilePath(version + ".md");
var release = ChangelogDirectory.CombineWithFilePath(version + ".md");

var content = new StringBuilder();
content.AppendLine("---");
Expand Down Expand Up @@ -224,7 +231,7 @@ private void EnsureChangelogDetailsExist(bool forceClean = false)
new DeleteDirectorySettings { Force = true, Recursive = true });

if (!context.DirectoryExists(changelogDetailsDirectory))
context.Clone(changelogDetailsDirectory, Repo.HttpsGitUrl, Repo.ChangelogDetailsBranch);
context.GitRunner.Clone(changelogDetailsDirectory, Repo.HttpsGitUrl, Repo.ChangelogDetailsBranch);
}

private void DocfxChangelogDownload(string version, string versionPrevious, string lastCommit = "")
Expand Down Expand Up @@ -273,7 +280,7 @@ private void UpdateLastFooter()
var version = context.VersionHistory.CurrentVersion;
var previousVersion = context.VersionHistory.StableVersions.Last();
var date = context.VersionStable
? DateTime.Now.ToString("MMMM dd, yyyy", CultureInfo.InvariantCulture)
? DateTime.Now.ToString("MMMM dd, yyyy", CultureInfo.InvariantCulture)
: "TBA";

var content = new StringBuilder();
Expand Down
Loading

0 comments on commit 3b361e3

Please sign in to comment.