Skip to content

Commit 398ae65

Browse files
[build] Rework ReleaseTask
1 parent c780b3c commit 398ae65

16 files changed

+160
-118
lines changed

build/BenchmarkDotNet.Build/BuildContext.cs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Text;
66
using BenchmarkDotNet.Build.Helpers;
77
using BenchmarkDotNet.Build.Meta;
8+
using BenchmarkDotNet.Build.Options;
89
using BenchmarkDotNet.Build.Runners;
910
using Cake.Common;
1011
using Cake.Common.Build;
@@ -23,9 +24,6 @@ public class BuildContext : FrostingContext
2324
{
2425
public string BuildConfiguration { get; set; } = "Release";
2526
public DotNetVerbosity BuildVerbosity { get; set; } = DotNetVerbosity.Minimal;
26-
public bool VersionStable { get; }
27-
public string NextVersion { get; }
28-
public bool PushMode { get; }
2927

3028
public DirectoryPath RootDirectory { get; }
3129
public DirectoryPath BuildDirectory { get; }
@@ -83,9 +81,7 @@ public BuildContext(ICakeContext context)
8381
MsBuildSettingsBuild.WithProperty("UseSharedCompilation", "false");
8482
}
8583

86-
VersionStable = false;
87-
NextVersion = "";
88-
PushMode = false;
84+
8985
if (context.Arguments.HasArgument("msbuild"))
9086
{
9187
var msBuildParameters = context.Arguments.GetArguments().First(it => it.Key == "msbuild").Value;
@@ -109,19 +105,19 @@ public BuildContext(ICakeContext context)
109105
if (parsedVerbosity != null)
110106
BuildVerbosity = parsedVerbosity.Value;
111107
}
112-
113-
if (name.Equals("VersionStable", StringComparison.OrdinalIgnoreCase) && value != "")
114-
VersionStable = true;
115-
116-
if (name.Equals("NextVersion", StringComparison.OrdinalIgnoreCase) && value != "")
117-
NextVersion = value;
118-
119-
if (name.Equals("PushMode", StringComparison.OrdinalIgnoreCase) && value != "")
120-
PushMode = true;
121108
}
122109
}
123110
}
124111

112+
if (KnownOptions.Stable.Resolve(this))
113+
{
114+
const string name = "NoVersionSuffix";
115+
const string value = "true";
116+
MsBuildSettingsRestore.WithProperty(name, value);
117+
MsBuildSettingsBuild.WithProperty(name, value);
118+
MsBuildSettingsPack.WithProperty(name, value);
119+
}
120+
125121
// NativeAOT build requires VS C++ tools to be added to $path via vcvars64.bat
126122
// but once we do that, dotnet restore fails with:
127123
// "Please specify a valid solution configuration using the Configuration and Platform properties"
@@ -177,4 +173,13 @@ public void GenerateFile(FilePath filePath, string content, bool reportNoChanges
177173
}
178174
}
179175

176+
public void RunOnlyInPushMode(Action action)
177+
{
178+
if (KnownOptions.Push.Resolve(this))
179+
{
180+
action();
181+
}
182+
else
183+
this.Information(" Skip because PushMode is disabled");
184+
}
180185
}

build/BenchmarkDotNet.Build/ChangeLogBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ private async Task<string> Build()
6060
if (string.IsNullOrEmpty(lastCommit))
6161
lastCommit = $"v{currentVersion}";
6262

63-
var client = GitHubCredentials.CreateClient();
63+
var client = Utils.CreateGitHubClient();
6464

6565
if (currentVersion == "_")
6666
{

build/BenchmarkDotNet.Build/CommandLineParser.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public class CommandLineParser
8686

8787
private readonly IOption[] baseOptions =
8888
{
89-
KnownOptions.Verbosity, KnownOptions.Exclusive, KnownOptions.Help
89+
KnownOptions.Verbosity, KnownOptions.Exclusive, KnownOptions.Help, KnownOptions.Stable
9090
};
9191

9292
private void PrintHelp()
@@ -108,7 +108,7 @@ private void PrintHelp()
108108

109109
WriteLine();
110110

111-
PrintExamples(GetTasks().SelectMany(task => task.HelpInfo.Examples));
111+
PrintExamples(GetTasks().SelectMany(task => task.HelpInfo.Examples).ToList());
112112

113113
PrintOptions(baseOptions);
114114

@@ -169,10 +169,10 @@ private void PrintTaskHelp(string taskName)
169169
if (helpInfo.EnvironmentVariables.Any())
170170
{
171171
WriteHeader("Environment variables:");
172-
foreach (var variable in helpInfo.EnvironmentVariables)
172+
foreach (var envVar in helpInfo.EnvironmentVariables)
173173
{
174174
WritePrefix();
175-
WriteOption(variable);
175+
WriteOption(envVar.Name);
176176
WriteLine();
177177
}
178178
}
@@ -237,8 +237,10 @@ int GetWidth(IOption option)
237237
WriteLine();
238238
}
239239

240-
private void PrintExamples(IEnumerable<Example> examples)
240+
private void PrintExamples(IReadOnlyList<Example> examples)
241241
{
242+
if (!examples.Any())
243+
return;
242244
WriteHeader("Examples:");
243245

244246
foreach (var example in examples)

build/BenchmarkDotNet.Build/EnvVar.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
3+
namespace BenchmarkDotNet.Build;
4+
5+
public class EnvVar
6+
{
7+
public static readonly EnvVar GitHubToken = new("GITHUB_TOKEN");
8+
public static readonly EnvVar NuGetToken = new("NUGET_TOKEN");
9+
10+
public string Name { get; }
11+
12+
private EnvVar(string name) => Name = name;
13+
14+
public string? GetValue() => Environment.GetEnvironmentVariable(Name);
15+
16+
public void AssertHasValue()
17+
{
18+
if (string.IsNullOrEmpty(GetValue()))
19+
throw new Exception($"Environment variable '{Name}' is not specified!");
20+
}
21+
22+
public void SetEmpty() => Environment.SetEnvironmentVariable(Name, "");
23+
}

build/BenchmarkDotNet.Build/Example.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ public Example(string taskName)
1515
TaskName = taskName;
1616
}
1717

18-
public Example WithArgument(string name, string? value = null)
18+
public Example WithMsBuildArgument(string name, string value)
1919
{
20-
arguments.Add(new Argument(name, value, false));
20+
arguments.Add(new Argument(name, value, true));
2121
return this;
2222
}
2323

24-
public Example WithMsBuildArgument(string name, string value)
24+
public Example WithArgument(BoolOption option)
2525
{
26-
arguments.Add(new Argument(name, value, true));
26+
arguments.Add(new Argument(option.CommandLineName, null, false));
2727
return this;
2828
}
2929

30-
public Example WithArgument(IOption option, string? value = null)
30+
public Example WithArgument(StringOption option, string value)
3131
{
3232
arguments.Add(new Argument(option.CommandLineName, value, false));
3333
return this;

build/BenchmarkDotNet.Build/HelpInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ public class HelpInfo
77
{
88
public string Description { get; init; } = "";
99
public IOption[] Options { get; init; } = Array.Empty<IOption>();
10-
public string[] EnvironmentVariables { get; init; } = Array.Empty<string>();
10+
public EnvVar[] EnvironmentVariables { get; init; } = Array.Empty<EnvVar>();
1111
public Example[] Examples { get; init; } = Array.Empty<Example>();
1212
}

build/BenchmarkDotNet.Build/Helpers/Utils.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Runtime.InteropServices;
4+
using BenchmarkDotNet.Build.Options;
5+
using Cake.Common.Diagnostics;
46
using Cake.Common.Tools.DotNet;
7+
using Octokit;
58

69
namespace BenchmarkDotNet.Build.Helpers;
710

@@ -35,4 +38,14 @@ public static string GetOs()
3538
};
3639
return lookup.TryGetValue(verbosity, out var value) ? value : null;
3740
}
41+
42+
public static GitHubClient CreateGitHubClient()
43+
{
44+
EnvVar.GitHubToken.AssertHasValue();
45+
46+
var client = new GitHubClient(new ProductHeaderValue("BenchmarkDotNet"));
47+
var tokenAuth = new Credentials(EnvVar.GitHubToken.GetValue());
48+
client.Credentials = tokenAuth;
49+
return client;
50+
}
3851
}

build/BenchmarkDotNet.Build/Meta/GitHubCredentials.cs

Lines changed: 0 additions & 20 deletions
This file was deleted.

build/BenchmarkDotNet.Build/Options/BoolOption.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,11 @@ public override bool Resolve(BuildContext context)
1717
return true;
1818
return !value.Equals(false.ToString(), StringComparison.OrdinalIgnoreCase);
1919
}
20+
21+
public void AssertTrue(BuildContext context)
22+
{
23+
var value = Resolve(context);
24+
if (!value)
25+
throw new Exception($"{CommandLineName} is not specified");
26+
}
2027
}

build/BenchmarkDotNet.Build/Options/KnownOptions.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,21 @@ public static class KnownOptions
3434
Description = "Prints help information",
3535
Aliases = new[] { "-h" }
3636
};
37+
38+
public static readonly BoolOption Stable = new("--stable")
39+
{
40+
Description = "Removes VersionSuffix in MSBuild settings",
41+
Aliases = new[] { "-s" }
42+
};
43+
44+
public static readonly StringOption NextVersion = new("--next-version")
45+
{
46+
Description = "Specifies next version number",
47+
Aliases = new[] { "-n" }
48+
};
49+
50+
public static readonly BoolOption Push = new("--push")
51+
{
52+
Description = "When specified, the task actually perform push to GitHub and nuget.org"
53+
};
3754
}

0 commit comments

Comments
 (0)