Skip to content

Commit ce69bcc

Browse files
authored
Merge pull request #257 from WeihanLi/dev
1.0.81
2 parents 1b5d724 + b19a6f7 commit ce69bcc

File tree

7 files changed

+164
-120
lines changed

7 files changed

+164
-120
lines changed

Directory.Packages.props

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
<PackageVersion Include="Microsoft.Extensions.Logging" Version="8.0.1" />
1717
</ItemGroup>
1818
<ItemGroup Condition="'$(TargetFramework)' == 'net9.0'">
19-
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="9.0.7" />
20-
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="9.0.7" />
21-
<PackageVersion Include="Microsoft.Extensions.Logging" Version="9.0.7" />
19+
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="9.0.8" />
20+
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="9.0.8" />
21+
<PackageVersion Include="Microsoft.Extensions.Logging" Version="9.0.8" />
2222
</ItemGroup>
2323
<ItemGroup Condition="'$(TargetFramework)' == 'net10.0'">
24-
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="10.0.0-preview.6.25358.103" />
25-
<PackageVersion Include="Microsoft.Extensions.Logging" Version="10.0.0-preview.6.25358.103" />
24+
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="10.0.0-preview.7.25380.108" />
25+
<PackageVersion Include="Microsoft.Extensions.Logging" Version="10.0.0-preview.7.25380.108" />
2626
</ItemGroup>
2727
<ItemGroup>
2828
<PackageVersion Include="Microsoft.CSharp" Version="4.7.0" />
@@ -39,9 +39,9 @@
3939
<PackageVersion Include="BenchmarkDotNet" Version="0.15.2" />
4040
</ItemGroup>
4141
<ItemGroup>
42-
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="9.0.7" />
43-
<PackageVersion Include="Microsoft.EntityFrameworkCore.InMemory" Version="9.0.7" />
44-
<PackageVersion Include="Microsoft.Extensions.ObjectPool" Version="9.0.7" />
42+
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="9.0.8" />
43+
<PackageVersion Include="Microsoft.EntityFrameworkCore.InMemory" Version="9.0.8" />
44+
<PackageVersion Include="Microsoft.Extensions.ObjectPool" Version="9.0.8" />
4545
<PackageVersion Include="Serilog.Sinks.Console" Version="6.0.0" />
4646
<PackageVersion Include="Microsoft.Data.SqlClient" Version="6.0.2" />
4747
<PackageVersion Include="Dapper" Version="2.1.66" />

WeihanLi.Common.sln.DotSettings

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=CA/@EntryIndexedValue">CA</s:String>
33
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=OS/@EntryIndexedValue">OS</s:String>
44
<s:Boolean x:Key="/Default/UserDictionary/Words/=Enricher/@EntryIndexedValue">True</s:Boolean>
5+
<s:Boolean x:Key="/Default/UserDictionary/Words/=sourcebranchname/@EntryIndexedValue">True</s:Boolean>
56
<s:Boolean x:Key="/Default/UserDictionary/Words/=Weihan/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

build/build.cs

Lines changed: 6 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
// Copyright (c) Weihan Li. All rights reserved.
22
// Licensed under the Apache license version 2.0 http://www.apache.org/licenses/LICENSE-2.0
33

4-
var target = CommandLineParser.Val(args, "target", "Default");
5-
var apiKey = CommandLineParser.Val(args, "apiKey");
6-
var stable = CommandLineParser.BooleanVal(args, "stable");
7-
var noPush = CommandLineParser.BooleanVal(args, "noPush");
8-
var branchName = EnvHelper.Val("BUILD_SOURCEBRANCHNAME", "local");
9-
104
var solutionPath = "./WeihanLi.Common.slnx";
115
string[] srcProjects = [
126
"./src/WeihanLi.Common/WeihanLi.Common.csproj",
@@ -15,110 +9,12 @@
159
];
1610
string[] testProjects = [ "./test/WeihanLi.Common.Test/WeihanLi.Common.Test.csproj" ];
1711

18-
await new BuildProcessBuilder()
19-
.WithSetup(() =>
20-
{
21-
// cleanup artifacts
22-
if (Directory.Exists("./artifacts/packages"))
23-
Directory.Delete("./artifacts/packages", true);
24-
25-
// args
26-
Console.WriteLine("Arguments");
27-
Console.WriteLine($" {args.StringJoin(" ")}");
28-
})
29-
.WithTaskExecuting(task => Console.WriteLine($@"===== Task {task.Name} {task.Description} executing ======"))
30-
.WithTaskExecuted(task => Console.WriteLine($@"===== Task {task.Name} {task.Description} executed ======"))
31-
.WithTask("hello", b => b.WithExecution(async () =>
32-
{
33-
Console.WriteLine("Hello dotnet-exec build");
34-
await ExecuteCommandAsync("dotnet-exec info");
35-
}))
36-
.WithTask("build", b =>
12+
await DotNetPackageBuildProcess
13+
.Create(options =>
3714
{
38-
b.WithDescription("dotnet build")
39-
.WithExecution(() => ExecuteCommandAsync($"dotnet build {solutionPath}"))
40-
;
15+
options.SolutionPath = solutionPath;
16+
options.SrcProjects = srcProjects;
17+
options.TestProjects = testProjects;
4118
})
42-
.WithTask("test", b =>
43-
{
44-
b.WithDescription("dotnet test")
45-
.WithDependency("build")
46-
.WithExecution(async () =>
47-
{
48-
foreach (var project in testProjects)
49-
{
50-
await ExecuteCommandAsync($"dotnet test --collect:\"XPlat Code Coverage;Format=cobertura,opencover;ExcludeByAttribute=ExcludeFromCodeCoverage,Obsolete,GeneratedCode,CompilerGeneratedAttribute\" {project}");
51-
}
52-
})
53-
;
54-
})
55-
.WithTask("pack", b => b.WithDescription("dotnet pack")
56-
.WithDependency("test")
57-
.WithExecution(async () =>
58-
{
59-
if (stable || branchName == "master")
60-
{
61-
foreach (var project in srcProjects)
62-
{
63-
await ExecuteCommandAsync($"dotnet pack {project} -o ./artifacts/packages");
64-
}
65-
}
66-
else
67-
{
68-
var suffix = $"preview-{DateTime.UtcNow:yyyyMMdd-HHmmss}";
69-
foreach (var project in srcProjects)
70-
{
71-
await ExecuteCommandAsync($"dotnet pack {project} -o ./artifacts/packages --version-suffix {suffix}");
72-
}
73-
}
74-
75-
if (noPush)
76-
{
77-
Console.WriteLine("Skip push there's noPush specified");
78-
return;
79-
}
80-
81-
if (string.IsNullOrEmpty(apiKey))
82-
{
83-
// try to get apiKey from environment variable
84-
apiKey = Environment.GetEnvironmentVariable("NuGet__ApiKey");
85-
86-
if (string.IsNullOrEmpty(apiKey))
87-
{
88-
Console.WriteLine("Skip push since there's no apiKey found");
89-
return;
90-
}
91-
}
92-
93-
if (branchName != "master" && branchName != "preview")
94-
{
95-
Console.WriteLine($"Skip push since branch name {branchName} not support push packages");
96-
return;
97-
}
19+
.ExecuteAsync(args);
9820

99-
// push nuget packages
100-
foreach (var file in Directory.GetFiles("./artifacts/packages/", "*.nupkg"))
101-
{
102-
await ExecuteCommandAsync($"dotnet nuget push {file} -k {apiKey} --skip-duplicate", [new("$NuGet__ApiKey", apiKey)]);
103-
}
104-
}))
105-
.WithTask("Default", b => b.WithDependency("hello").WithDependency("pack"))
106-
.Build()
107-
.ExecuteAsync(target, ApplicationHelper.ExitToken);
108-
109-
async Task ExecuteCommandAsync(string commandText, KeyValuePair<string, string>[]? replacements = null)
110-
{
111-
var commandTextWithReplacements = commandText;
112-
if (replacements is { Length: > 0})
113-
{
114-
foreach (var item in replacements)
115-
{
116-
commandTextWithReplacements = commandTextWithReplacements.Replace(item.Value, item.Key);
117-
}
118-
}
119-
Console.WriteLine($"Executing command: \n {commandTextWithReplacements}");
120-
Console.WriteLine();
121-
var result = await CommandExecutor.ExecuteCommandAndOutputAsync(commandText);
122-
result.EnsureSuccessExitCode();
123-
Console.WriteLine();
124-
}

build/version.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<PropertyGroup>
33
<VersionMajor>1</VersionMajor>
44
<VersionMinor>0</VersionMinor>
5-
<VersionPatch>80</VersionPatch>
5+
<VersionPatch>81</VersionPatch>
66
<VersionPrefix>$(VersionMajor).$(VersionMinor).$(VersionPatch)</VersionPrefix>
77
</PropertyGroup>
88
</Project>

src/WeihanLi.Common/Helpers/BuildProcess.cs

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Weihan Li. All rights reserved.
22
// Licensed under the Apache license.
33

4+
using System.Diagnostics;
45
using WeihanLi.Extensions;
56

67
namespace WeihanLi.Common.Helpers;
@@ -206,3 +207,148 @@ internal BuildTask Build()
206207
return buildTask;
207208
}
208209
}
210+
211+
public sealed class DotNetBuildProcessOptions
212+
{
213+
public string? SolutionPath { get; set; }
214+
public string[]? SrcProjects { get; set; }
215+
public string[]? TestProjects { get; set; }
216+
public Func<string?> FallbackNuGetApiKeyFunc { get; set; } = () => EnvHelper.Val("NuGet__ApiKey");
217+
public string ArtifactsPath { get; set; } = "./artifacts/dist";
218+
public Func<string?> BranchFunc { get; set; } = () => EnvHelper.Val("BUILD_SOURCEBRANCHNAME", EnvHelper.Val("GITHUB_REF_NAME"));
219+
public bool AllowLocalPush { get; set; }
220+
}
221+
222+
public sealed class DotNetPackageBuildProcess
223+
{
224+
private readonly BuildProcess _buildProcess;
225+
private string? _apiKey, _branch;
226+
private bool _stable, _noPush;
227+
private DotNetPackageBuildProcess(DotNetBuildProcessOptions options)
228+
{
229+
_branch = options.BranchFunc();
230+
_buildProcess = new BuildProcessBuilder()
231+
.WithSetup(() =>
232+
{
233+
// cleanup artifacts
234+
if (Directory.Exists(options.ArtifactsPath))
235+
Directory.Delete(options.ArtifactsPath, true);
236+
237+
// args
238+
Console.WriteLine(@"Executing command line:");
239+
Console.WriteLine($@" {Environment.CommandLine}");
240+
})
241+
.WithTaskExecuting(task => Console.WriteLine($@"===== Task {task.Name} {task.Description} executing ======"))
242+
.WithTaskExecuted(task => Console.WriteLine($@"===== Task {task.Name} {task.Description} executed ======"))
243+
.WithTask("build", b =>
244+
{
245+
b.WithDescription("dotnet build").WithExecution(() =>
246+
CommandExecutor.ExecuteCommandAndOutput($"dotnet build {options.SolutionPath}")
247+
.EnsureSuccessExitCode()
248+
);
249+
})
250+
.WithTask("test", b =>
251+
{
252+
b.WithDescription("dotnet test")
253+
.WithDependency("build")
254+
.WithExecution(() =>
255+
{
256+
foreach (var project in options.TestProjects ?? [])
257+
{
258+
CommandExecutor.ExecuteCommandAndOutput(
259+
$"dotnet test --collect:\"XPlat Code Coverage;Format=cobertura,opencover;ExcludeByAttribute=ExcludeFromCodeCoverage,Obsolete,GeneratedCode,CompilerGeneratedAttribute\" {project}"
260+
).EnsureSuccessExitCode();
261+
}
262+
})
263+
;
264+
})
265+
.WithTask("pack", b => b.WithDescription("dotnet pack")
266+
.WithDependency("test")
267+
.WithExecution(() =>
268+
{
269+
if (options.SrcProjects is not { Length: > 0})
270+
return;
271+
272+
if (_stable)
273+
{
274+
foreach (var project in options.SrcProjects ?? [])
275+
{
276+
CommandExecutor.ExecuteCommandAndOutput(
277+
$"dotnet pack {project} -o {options.ArtifactsPath}"
278+
)
279+
.EnsureSuccessExitCode();
280+
}
281+
}
282+
else
283+
{
284+
var suffix = $"preview-{DateTime.UtcNow:yyyyMMdd-HHmmss}";
285+
foreach (var project in options.SrcProjects ?? [])
286+
{
287+
CommandExecutor.ExecuteCommandAndOutput(
288+
$"dotnet pack {project} -o {options.ArtifactsPath} --version-suffix {suffix}"
289+
).EnsureSuccessExitCode();
290+
}
291+
}
292+
293+
if (_noPush)
294+
{
295+
Console.WriteLine(@"Skip push there's noPush specified");
296+
return;
297+
}
298+
299+
if (!_stable && _branch != "preview" && (!options.AllowLocalPush && _branch is "local"))
300+
{
301+
Console.WriteLine($@"Skip push since branch [{_branch}] not supported to push packages");
302+
return;
303+
}
304+
305+
if (string.IsNullOrEmpty(_apiKey))
306+
{
307+
// try to get apiKey from environment variable
308+
_apiKey = options.FallbackNuGetApiKeyFunc();
309+
310+
if (string.IsNullOrEmpty(_apiKey))
311+
{
312+
Console.WriteLine(@"Skip push since there's no apiKey found");
313+
return;
314+
}
315+
}
316+
317+
318+
// push nuget packages
319+
foreach (var file in Directory.GetFiles(options.ArtifactsPath, "*.nupkg"))
320+
{
321+
var commandText = $"dotnet nuget push {file} -k {_apiKey} --skip-duplicate";
322+
CommandExecutor.ExecuteCommandAndOutput(commandText).EnsureSuccessExitCode();
323+
}
324+
}))
325+
.WithTask("Default", b => b.WithDependency("pack"))
326+
.Build();
327+
}
328+
329+
public async Task ExecuteAsync(string[] args, CancellationToken cancellation = default)
330+
{
331+
_apiKey = CommandLineParser.Val(args, "apiKey");
332+
_stable = CommandLineParser.BooleanVal(args, "stable");
333+
_noPush = CommandLineParser.BooleanVal(args, "noPush");
334+
if (string.IsNullOrEmpty(_branch))
335+
{
336+
_branch = CommandLineParser.Val(args, "branch", "local");
337+
}
338+
Debug.Assert(_branch is not null);
339+
if (!_stable)
340+
{
341+
_stable = _branch is "main" or "master" ||
342+
_branch?.StartsWith("release/", StringComparison.OrdinalIgnoreCase) == true;
343+
}
344+
var target = CommandLineParser.Val(args, "target", "Default");
345+
await _buildProcess.ExecuteAsync(target, cancellation);
346+
}
347+
348+
public static DotNetPackageBuildProcess Create(Action<DotNetBuildProcessOptions> configure)
349+
{
350+
var options = new DotNetBuildProcessOptions();
351+
configure(options);
352+
return new DotNetPackageBuildProcess(options);
353+
}
354+
}

src/WeihanLi.Common/Helpers/InvokeHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public static class InvokeHelper
1010
{
1111
static InvokeHelper()
1212
{
13-
OnInvokeException = ex => Debug.WriteLine(ex);
13+
OnInvokeException = ex => ConsoleHelper.WriteLineWithColor(ex.ToString(), ConsoleColor.DarkRed);
1414

1515
#region Exit event register
1616

src/WeihanLi.Common/Http/NoProxyHttpClientHandler.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ public NoProxyHttpClientHandler()
88
UseProxy = false;
99
UseCookies = false;
1010
AllowAutoRedirect = false;
11+
CheckCertificateRevocationList = false;
1112
}
1213
}

0 commit comments

Comments
 (0)