Skip to content

Commit

Permalink
[build] Rework ReleaseTask
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreyAkinshin committed Jul 12, 2023
1 parent c780b3c commit 398ae65
Show file tree
Hide file tree
Showing 16 changed files with 160 additions and 118 deletions.
35 changes: 20 additions & 15 deletions build/BenchmarkDotNet.Build/BuildContext.cs
Expand Up @@ -5,6 +5,7 @@
using System.Text;
using BenchmarkDotNet.Build.Helpers;
using BenchmarkDotNet.Build.Meta;
using BenchmarkDotNet.Build.Options;
using BenchmarkDotNet.Build.Runners;
using Cake.Common;
using Cake.Common.Build;
Expand All @@ -23,9 +24,6 @@ public class BuildContext : FrostingContext
{
public string BuildConfiguration { get; set; } = "Release";
public DotNetVerbosity BuildVerbosity { get; set; } = DotNetVerbosity.Minimal;
public bool VersionStable { get; }
public string NextVersion { get; }
public bool PushMode { get; }

public DirectoryPath RootDirectory { get; }
public DirectoryPath BuildDirectory { get; }
Expand Down Expand Up @@ -83,9 +81,7 @@ public BuildContext(ICakeContext context)
MsBuildSettingsBuild.WithProperty("UseSharedCompilation", "false");
}

VersionStable = false;
NextVersion = "";
PushMode = false;

if (context.Arguments.HasArgument("msbuild"))
{
var msBuildParameters = context.Arguments.GetArguments().First(it => it.Key == "msbuild").Value;
Expand All @@ -109,19 +105,19 @@ public BuildContext(ICakeContext context)
if (parsedVerbosity != null)
BuildVerbosity = parsedVerbosity.Value;
}

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;
}
}
}

if (KnownOptions.Stable.Resolve(this))
{
const string name = "NoVersionSuffix";
const string value = "true";
MsBuildSettingsRestore.WithProperty(name, value);
MsBuildSettingsBuild.WithProperty(name, value);
MsBuildSettingsPack.WithProperty(name, value);
}

// NativeAOT build requires VS C++ tools to be added to $path via vcvars64.bat
// but once we do that, dotnet restore fails with:
// "Please specify a valid solution configuration using the Configuration and Platform properties"
Expand Down Expand Up @@ -177,4 +173,13 @@ public void GenerateFile(FilePath filePath, string content, bool reportNoChanges
}
}

public void RunOnlyInPushMode(Action action)
{
if (KnownOptions.Push.Resolve(this))
{
action();
}
else
this.Information(" Skip because PushMode is disabled");
}
}
2 changes: 1 addition & 1 deletion build/BenchmarkDotNet.Build/ChangeLogBuilder.cs
Expand Up @@ -60,7 +60,7 @@ private async Task<string> Build()
if (string.IsNullOrEmpty(lastCommit))
lastCommit = $"v{currentVersion}";

var client = GitHubCredentials.CreateClient();
var client = Utils.CreateGitHubClient();

if (currentVersion == "_")
{
Expand Down
12 changes: 7 additions & 5 deletions build/BenchmarkDotNet.Build/CommandLineParser.cs
Expand Up @@ -86,7 +86,7 @@ public class CommandLineParser

private readonly IOption[] baseOptions =
{
KnownOptions.Verbosity, KnownOptions.Exclusive, KnownOptions.Help
KnownOptions.Verbosity, KnownOptions.Exclusive, KnownOptions.Help, KnownOptions.Stable
};

private void PrintHelp()
Expand All @@ -108,7 +108,7 @@ private void PrintHelp()

WriteLine();

PrintExamples(GetTasks().SelectMany(task => task.HelpInfo.Examples));
PrintExamples(GetTasks().SelectMany(task => task.HelpInfo.Examples).ToList());

PrintOptions(baseOptions);

Expand Down Expand Up @@ -169,10 +169,10 @@ private void PrintTaskHelp(string taskName)
if (helpInfo.EnvironmentVariables.Any())
{
WriteHeader("Environment variables:");
foreach (var variable in helpInfo.EnvironmentVariables)
foreach (var envVar in helpInfo.EnvironmentVariables)
{
WritePrefix();
WriteOption(variable);
WriteOption(envVar.Name);
WriteLine();
}
}
Expand Down Expand Up @@ -237,8 +237,10 @@ int GetWidth(IOption option)
WriteLine();
}

private void PrintExamples(IEnumerable<Example> examples)
private void PrintExamples(IReadOnlyList<Example> examples)
{
if (!examples.Any())
return;
WriteHeader("Examples:");

foreach (var example in examples)
Expand Down
23 changes: 23 additions & 0 deletions build/BenchmarkDotNet.Build/EnvVar.cs
@@ -0,0 +1,23 @@
using System;

namespace BenchmarkDotNet.Build;

public class EnvVar
{
public static readonly EnvVar GitHubToken = new("GITHUB_TOKEN");
public static readonly EnvVar NuGetToken = new("NUGET_TOKEN");

public string Name { get; }

private EnvVar(string name) => Name = name;

public string? GetValue() => Environment.GetEnvironmentVariable(Name);

public void AssertHasValue()
{
if (string.IsNullOrEmpty(GetValue()))
throw new Exception($"Environment variable '{Name}' is not specified!");
}

public void SetEmpty() => Environment.SetEnvironmentVariable(Name, "");
}
10 changes: 5 additions & 5 deletions build/BenchmarkDotNet.Build/Example.cs
Expand Up @@ -15,19 +15,19 @@ public Example(string taskName)
TaskName = taskName;
}

public Example WithArgument(string name, string? value = null)
public Example WithMsBuildArgument(string name, string value)
{
arguments.Add(new Argument(name, value, false));
arguments.Add(new Argument(name, value, true));
return this;
}

public Example WithMsBuildArgument(string name, string value)
public Example WithArgument(BoolOption option)
{
arguments.Add(new Argument(name, value, true));
arguments.Add(new Argument(option.CommandLineName, null, false));
return this;
}

public Example WithArgument(IOption option, string? value = null)
public Example WithArgument(StringOption option, string value)
{
arguments.Add(new Argument(option.CommandLineName, value, false));
return this;
Expand Down
2 changes: 1 addition & 1 deletion build/BenchmarkDotNet.Build/HelpInfo.cs
Expand Up @@ -7,6 +7,6 @@ public class HelpInfo
{
public string Description { get; init; } = "";
public IOption[] Options { get; init; } = Array.Empty<IOption>();
public string[] EnvironmentVariables { get; init; } = Array.Empty<string>();
public EnvVar[] EnvironmentVariables { get; init; } = Array.Empty<EnvVar>();
public Example[] Examples { get; init; } = Array.Empty<Example>();
}
13 changes: 13 additions & 0 deletions build/BenchmarkDotNet.Build/Helpers/Utils.cs
@@ -1,7 +1,10 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using BenchmarkDotNet.Build.Options;
using Cake.Common.Diagnostics;
using Cake.Common.Tools.DotNet;
using Octokit;

namespace BenchmarkDotNet.Build.Helpers;

Expand Down Expand Up @@ -35,4 +38,14 @@ public static string GetOs()
};
return lookup.TryGetValue(verbosity, out var value) ? value : null;
}

public static GitHubClient CreateGitHubClient()
{
EnvVar.GitHubToken.AssertHasValue();

var client = new GitHubClient(new ProductHeaderValue("BenchmarkDotNet"));
var tokenAuth = new Credentials(EnvVar.GitHubToken.GetValue());
client.Credentials = tokenAuth;
return client;
}
}
20 changes: 0 additions & 20 deletions build/BenchmarkDotNet.Build/Meta/GitHubCredentials.cs

This file was deleted.

7 changes: 7 additions & 0 deletions build/BenchmarkDotNet.Build/Options/BoolOption.cs
Expand Up @@ -17,4 +17,11 @@ public override bool Resolve(BuildContext context)
return true;
return !value.Equals(false.ToString(), StringComparison.OrdinalIgnoreCase);
}

public void AssertTrue(BuildContext context)
{
var value = Resolve(context);
if (!value)
throw new Exception($"{CommandLineName} is not specified");
}
}
17 changes: 17 additions & 0 deletions build/BenchmarkDotNet.Build/Options/KnownOptions.cs
Expand Up @@ -34,4 +34,21 @@ public static class KnownOptions
Description = "Prints help information",
Aliases = new[] { "-h" }
};

public static readonly BoolOption Stable = new("--stable")
{
Description = "Removes VersionSuffix in MSBuild settings",
Aliases = new[] { "-s" }
};

public static readonly StringOption NextVersion = new("--next-version")
{
Description = "Specifies next version number",
Aliases = new[] { "-n" }
};

public static readonly BoolOption Push = new("--push")
{
Description = "When specified, the task actually perform push to GitHub and nuget.org"
};
}
10 changes: 10 additions & 0 deletions build/BenchmarkDotNet.Build/Options/StringOption.cs
@@ -1,3 +1,5 @@
using System;

namespace BenchmarkDotNet.Build.Options;

public class StringOption : Option<string>
Expand All @@ -16,4 +18,12 @@ public override string Resolve(BuildContext context)
return "";
return value.Trim();
}

public string AssertHasValue(BuildContext context)
{
var value = Resolve(context);
if (string.IsNullOrWhiteSpace(value))
throw new Exception($"{CommandLineName} is not specified");
return value;
}
}
16 changes: 10 additions & 6 deletions build/BenchmarkDotNet.Build/Program.cs
@@ -1,4 +1,3 @@
using BenchmarkDotNet.Build.Meta;
using BenchmarkDotNet.Build.Options;
using Cake.Common;
using Cake.Frosting;
Expand Down Expand Up @@ -129,7 +128,8 @@ public HelpInfo GetHelp()
{
new Example(Name)
.WithMsBuildArgument("VersionPrefix", "0.1.1729")
.WithMsBuildArgument("VersionSuffix", "preview")
.WithMsBuildArgument("VersionSuffix", "preview"),
new Example(Name).WithArgument(KnownOptions.Stable)
}
};
}
Expand All @@ -147,7 +147,7 @@ public HelpInfo GetHelp()
return new HelpInfo
{
Options = new IOption[] { KnownOptions.DocsPreview, KnownOptions.DocsDepth },
EnvironmentVariables = new[] { GitHubCredentials.TokenVariableName },
EnvironmentVariables = new[] { EnvVar.GitHubToken },
Examples = new[]
{
new Example(Name)
Expand Down Expand Up @@ -210,10 +210,14 @@ public class ReleaseTask : FrostingTask<BuildContext>, IHelpProvider

public HelpInfo GetHelp() => new()
{
EnvironmentVariables = new[]
Options = new IOption[] { KnownOptions.NextVersion, KnownOptions.Push },
EnvironmentVariables = new[] { EnvVar.GitHubToken, EnvVar.NuGetToken },
Examples = new[]
{
GitHubCredentials.TokenVariableName,
"NUGET_TOKEN"
new Example(Name)
.WithArgument(KnownOptions.Stable)
.WithArgument(KnownOptions.NextVersion, "v0.1.1729")
.WithArgument(KnownOptions.Push)
}
};
}
7 changes: 3 additions & 4 deletions build/BenchmarkDotNet.Build/Runners/DocumentationRunner.cs
Expand Up @@ -57,14 +57,13 @@ public DocumentationRunner(BuildContext context)

public void Update()
{
EnvVar.GitHubToken.AssertHasValue();

ReadmeUpdater.Run(context);
UpdateLastFooter();

EnsureChangelogDetailsExist();

if (string.IsNullOrEmpty(GitHubCredentials.Token))
throw new Exception($"Environment variable '{GitHubCredentials.TokenVariableName}' is not specified!");

var history = context.VersionHistory;
var stableVersionCount = history.StableVersions.Length;

Expand Down Expand Up @@ -294,7 +293,7 @@ private void UpdateLastFooter()
{
var version = context.VersionHistory.CurrentVersion;
var previousVersion = context.VersionHistory.StableVersions.Last();
var date = context.VersionStable
var date = KnownOptions.Stable.Resolve(context)
? DateTime.Now.ToString("MMMM dd, yyyy", CultureInfo.InvariantCulture)
: "TBA";

Expand Down
6 changes: 2 additions & 4 deletions build/BenchmarkDotNet.Build/Runners/GitRunner.cs
Expand Up @@ -63,13 +63,11 @@ public void Push(string target, bool force = false)
context.Information("[GitPush]");
context.Information($" Target: {target}");
context.Information($" Force: {force}");
if (context.PushMode)
context.RunOnlyInPushMode(() =>
{
var forceFlag = force ? " --force" : "";
RunCommand($"push origin {target}{forceFlag}");
}
else
context.Information(" Skip because PushMode is disabled");
});
}

private void RunCommand(string commandLineArgs) => RunCommand(null, commandLineArgs);
Expand Down

0 comments on commit 398ae65

Please sign in to comment.