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

1.0.71 #224

Merged
merged 16 commits into from
Oct 10, 2024
Merged
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
3 changes: 3 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
<NoWarn>$(NoWarn);NU5048;CS1591;NETSDK1057</NoWarn>
<ArtifactsPath>$(MSBuildThisFileDirectory)artifacts</ArtifactsPath>
<ContinuousIntegrationBuild Condition="'$(CI)' == 'true' or '$(TF_BUILD)' == 'true' or '$(GITHUB_ACTIONS)' == 'true'">true</ContinuousIntegrationBuild>
<!-- NuGet Audit https://learn.microsoft.com/en-us/nuget/concepts/auditing-packages -->
<NuGetAudit>true</NuGetAudit>
<NuGetAuditMode>direct</NuGetAuditMode>
</PropertyGroup>
</Project>
14 changes: 7 additions & 7 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
<ExtensionPackageVersion Condition="'$(TargetFramework)' == 'netstandard2.0' OR '$(TargetFramework)' == 'netstandard2.1' OR '$(TargetFramework)' == 'net6.0'">6.0.0</ExtensionPackageVersion>
<ExtensionPackageVersion Condition="'$(TargetFramework)' == 'net7.0'">7.0.0</ExtensionPackageVersion>
<ExtensionPackageVersion Condition="'$(TargetFramework)' == 'net8.0'">8.0.0</ExtensionPackageVersion>
<ExtensionPackageVersion Condition="'$(TargetFramework)' == 'net9.0'">9.0.0-rc.1.24431.7</ExtensionPackageVersion>
<ExtensionPackageVersion Condition="'$(TargetFramework)' == 'net9.0'">9.0.0-rc.2.24473.5</ExtensionPackageVersion>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="$(ExtensionPackageVersion)" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="$(ExtensionPackageVersion)" />
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="8.0.1" />
</ItemGroup>
<ItemGroup>
<PackageVersion Include="Microsoft.CSharp" Version="4.7.0" />
Expand All @@ -25,15 +25,15 @@
<PackageVersion Include="Moq" Version="4.20.72" />
<PackageVersion Include="xunit" Version="2.9.2" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" />
<PackageVersion Include="Xunit.DependencyInjection" Version="8.7.1" />
<PackageVersion Include="Xunit.DependencyInjection.Logging" Version="8.1.0" />
<PackageVersion Include="Xunit.DependencyInjection" Version="9.3.0" />
<PackageVersion Include="Xunit.DependencyInjection.Logging" Version="9.0.0" />
<PackageVersion Include="coverlet.collector" Version="6.0.2" />
<PackageVersion Include="BenchmarkDotNet" Version="0.14.0" />
</ItemGroup>
<ItemGroup>
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="8.0.8" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.8" />
<PackageVersion Include="Microsoft.Extensions.ObjectPool" Version="8.0.8" />
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="8.0.10" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.10" />
<PackageVersion Include="Microsoft.Extensions.ObjectPool" Version="8.0.10" />
<PackageVersion Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageVersion Include="System.Data.SqlClient" Version="4.8.6" />
<PackageVersion Include="Dapper" Version="2.1.44" />
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

[![WeihanLi.Common Latest Stable](https://img.shields.io/nuget/v/WeihanLi.Common.svg)](https://www.nuget.org/packages/WeihanLi.Common/)

[![WeihanLi.Common Latest](https://img.shields.io/nuget/vpre/WeihanLi.Common)](https://www.nuget.org/packages/WeihanLi.Common/absoluteLatest)
[![WeihanLi.Common Latest Preview](https://img.shields.io/nuget/vpre/WeihanLi.Common)](https://www.nuget.org/packages/WeihanLi.Common/absoluteLatest)

[![Azure Pipelines Build Status](https://weihanli.visualstudio.com/Pipelines/_apis/build/status/WeihanLi.WeihanLi.Common?branchName=master)](https://weihanli.visualstudio.com/Pipelines/_build/latest?definitionId=16&branchName=master)

Expand Down
14 changes: 9 additions & 5 deletions build/build.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Copyright (c) 2022-2023 Weihan Li. All rights reserved.
// Licensed under the Apache license version 2.0 http://www.apache.org/licenses/LICENSE-2.0

var target = CommandLineParser.Val("target", args, "Default");
var apiKey = CommandLineParser.Val("apiKey", args);
var stable = CommandLineParser.BooleanVal("stable", args);
var noPush = CommandLineParser.BooleanVal("noPush", args);
var target = CommandLineParser.Val(args, "target", "Default");
var apiKey = CommandLineParser.Val(args, "apiKey");
var stable = CommandLineParser.BooleanVal(args, "stable");
var noPush = CommandLineParser.BooleanVal(args, "noPush");
var branchName = EnvHelper.Val("BUILD_SOURCEBRANCHNAME", "local");

var solutionPath = "./WeihanLi.Common.sln";
Expand All @@ -28,7 +28,11 @@
})
.WithTaskExecuting(task => Console.WriteLine($@"===== Task {task.Name} {task.Description} executing ======"))
.WithTaskExecuted(task => Console.WriteLine($@"===== Task {task.Name} {task.Description} executed ======"))
.WithTask("hello", b => b.WithExecution(() => Console.WriteLine("Hello dotnet-exec build")))
.WithTask("hello", b => b.WithExecution(async () =>
{
Console.WriteLine("Hello dotnet-exec build");
await ExecuteCommandAsync("dotnet-exec info");
}))
.WithTask("build", b =>
{
b.WithDescription("dotnet build")
Expand Down
2 changes: 1 addition & 1 deletion build/version.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<VersionMajor>1</VersionMajor>
<VersionMinor>0</VersionMinor>
<VersionPatch>70</VersionPatch>
<VersionPatch>71</VersionPatch>
<VersionPrefix>$(VersionMajor).$(VersionMinor).$(VersionPatch)</VersionPrefix>
</PropertyGroup>
</Project>
4 changes: 3 additions & 1 deletion samples/DotNetCoreSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

Console.WriteLine("----------DotNetCoreSample----------");

InvokeHelper.OnInvokeException = ex => ConsoleHelper.ErrorWriteWithColor(ex.ToString(), ConsoleColor.DarkRed);
InvokeHelper.OnInvokeException = ex => ConsoleHelper.ErrorWriteLineWithColor(ex.ToString(), ConsoleColor.DarkRed);

// ServiceDecoratorTest.MainTest();

Expand Down Expand Up @@ -345,6 +345,8 @@

// InvokeHelper.TryInvoke(CommandExecutorTest.MainTest);

// InvokeHelper.TryInvoke(() => throw null, 3);

await InvokeHelper.TryInvokeAsync(TemplatingSample.MainTest);

ConsoleHelper.ReadKeyWithPrompt("Press any key to exit");
Expand Down
26 changes: 17 additions & 9 deletions src/WeihanLi.Common/Helpers/ApplicationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,16 @@ public static LibraryInfo GetLibraryInfo(Assembly assembly)
.FirstOrDefault(x => nameof(LibraryInfo.RepositoryUrl).Equals(x.Key))?.Value ?? string.Empty;
if (assemblyInformation is not null)
{
var informationalVersionSplit = assemblyInformation.InformationalVersion.Split('+');
var informationalVersionSplit = assemblyInformation.InformationalVersion
#if NET
.Split('+', 2)
#else
.Split('+')
#endif
;
return new LibraryInfo()
{
VersionWithHash = assemblyInformation.InformationalVersion,
LibraryFullVersion = assemblyInformation.InformationalVersion,
LibraryVersion = informationalVersionSplit[0],
LibraryHash = informationalVersionSplit.Length > 1 ? informationalVersionSplit[1] : string.Empty,
RepositoryUrl = repositoryUrl
Expand Down Expand Up @@ -167,10 +173,7 @@ private static RuntimeInfo GetRuntimeInfo()
IsInKubernetes = IsInKubernetesCluster(),
KubernetesNamespace = GetKubernetesNamespace(),

LibraryVersion = libInfo.LibraryVersion,
LibraryHash = libInfo.LibraryHash,
VersionWithHash = libInfo.VersionWithHash,
RepositoryUrl = libInfo.RepositoryUrl,
LibraryInfo = libInfo,
};
return runtimeInfo;
}
Expand Down Expand Up @@ -236,14 +239,14 @@ private static bool IsInKubernetesCluster()

public class LibraryInfo
{
private string? _versionWithHash;
private readonly string? _fullVersion;
public required string LibraryVersion { get; init; }
public required string LibraryHash { get; init; }
public string LibraryFullVersion { get => _fullVersion ?? LibraryVersion; init => _fullVersion = value; }
public required string RepositoryUrl { get; init; }
public string VersionWithHash { get => _versionWithHash ?? LibraryVersion; init => _versionWithHash = value; }
}

public class RuntimeInfo : LibraryInfo
public class RuntimeInfo
{
public required string Version { get; init; }
public required string FrameworkDescription { get; init; }
Expand Down Expand Up @@ -282,4 +285,9 @@ public class RuntimeInfo : LibraryInfo
/// Kubernetes namespace when running in a Kubernetes cluster
/// </summary>
public string? KubernetesNamespace { get; init; }

/// <summary>
/// Runtime library info
/// </summary>
public required LibraryInfo LibraryInfo { get; init; }
}
19 changes: 19 additions & 0 deletions src/WeihanLi.Common/Helpers/CommandLineParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,32 @@ public static IEnumerable<string> ParseLine(string line, LineParseOptions? optio
return GetValueInternal(args ?? Environment.GetCommandLineArgs(), optionName) ?? defaultValue;
}

/// <summary>
/// Get argument value from arguments
/// </summary>
/// <param name="args">arguments</param>
/// <param name="defaultValue">default argument value when not found</param>
/// <param name="optionName">argument name to get value</param>
/// <returns>argument value</returns>
[return: NotNullIfNotNull(nameof(defaultValue))]
public static string? Val(string[] args, string optionName, string? defaultValue = null)
{
return GetValueInternal(args, optionName) ?? defaultValue;
}
public static bool BooleanVal(string optionName, string[]? args = null, bool defaultValue = default)
{
return GetValueInternal(args ?? Environment.GetCommandLineArgs(), optionName).ToBoolean(defaultValue);
}

public static bool BooleanVal(string[] args, string optionName, bool defaultValue = default)
{
return GetValueInternal(args, optionName).ToBoolean(defaultValue);
}

private static string? GetValueInternal(string[] args, string argumentName)
{
Guard.NotNull(args);
Guard.NotNullOrEmpty(argumentName);
for (var i = 0; i < args.Length; i++)
{
if (args[i] == $"--{argumentName}" || args[i] == $"-{argumentName}")
Expand Down
88 changes: 67 additions & 21 deletions src/WeihanLi.Common/Helpers/InvokeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public static async Task<double> ProfileAsync<T1, T2, T3>(Func<T1, T2, T3, Task>

public static Action<Exception>? OnInvokeException { get; set; }

private static readonly Lock _exitLock = new();
private static readonly Lock ExitLock = new();
private static volatile bool _exited;
private static readonly Lazy<CancellationTokenSource> LazyCancellationTokenSource = new();
private static void InvokeExitHandler(object? sender, EventArgs? args)
Expand All @@ -116,7 +116,7 @@ private static void InvokeExitHandler(object? sender, EventArgs? args)
// posixSignalContext.Cancel = true;
// }
// #endif
lock (_exitLock)
lock (ExitLock)
{
if (_exited) return;
Debug.WriteLine("exiting...");
Expand All @@ -130,105 +130,148 @@ private static void InvokeExitHandler(object? sender, EventArgs? args)
}
}

[Obsolete("Please use ApplicationHelper.ExitToken instead", true)]
public static CancellationToken GetExitToken() => GetExitTokenInternal();

internal static CancellationToken GetExitTokenInternal() => LazyCancellationTokenSource.Value.Token;

public static void TryInvoke(Action action)
public static void TryInvoke(Action action, int? maxRetryCount = null)
{
Guard.NotNull(action, nameof(action));
Guard.NotNull(action);

var maxRetry = maxRetryCount.GetValueOrDefault();
invoke:

try
{
action();
}
catch (Exception ex)
{
OnInvokeException?.Invoke(ex);
if (maxRetry-- > 0)
goto invoke;
}
}

public static void TryInvoke<T>(Action<T> action, T t)
public static void TryInvoke<T>(Action<T> action, T t, int? maxRetryCount = null)
{
Guard.NotNull(action, nameof(action));
Guard.NotNull(action);

var maxRetry = maxRetryCount.GetValueOrDefault();
invoke:

try
{
action(t);
}
catch (Exception ex)
{
OnInvokeException?.Invoke(ex);
if (maxRetry-- > 0)
goto invoke;
}
}

public static void TryInvoke<T1, T2>(Action<T1, T2> action, T1 t1, T2 t2)
public static void TryInvoke<T1, T2>(Action<T1, T2> action, T1 t1, T2 t2, int? maxRetryCount = null)
{
Guard.NotNull(action, nameof(action));
Guard.NotNull(action);

var maxRetry = maxRetryCount.GetValueOrDefault();
invoke:

try
{
action(t1, t2);
}
catch (Exception ex)
{
OnInvokeException?.Invoke(ex);
if (maxRetry -- > 0)
goto invoke;
}
}

public static async Task TryInvokeAsync<T1, T2>(Func<T1, T2, Task> func, T1 t1, T2 t2)
public static async Task TryInvokeAsync<T1, T2>(Func<T1, T2, Task> func, T1 t1, T2 t2, int? maxRetryCount = null)
{
Guard.NotNull(func, nameof(func));
Guard.NotNull(func);

var maxRetry = maxRetryCount.GetValueOrDefault();
invoke:

try
{
await func(t1, t2);
}
catch (Exception ex)
{
OnInvokeException?.Invoke(ex);
if (maxRetry -- > 0)
goto invoke;
}
}

public static void TryInvoke<T1, T2, T3>(Action<T1, T2, T3> action, T1 t1, T2 t2, T3 t3)
public static void TryInvoke<T1, T2, T3>(Action<T1, T2, T3> action, T1 t1, T2 t2, T3 t3, int? maxRetryCount = null)
{
Guard.NotNull(action, nameof(action));
Guard.NotNull(action);

var maxRetry = maxRetryCount.GetValueOrDefault();
invoke:

try
{
action(t1, t2, t3);
}
catch (Exception ex)
{
OnInvokeException?.Invoke(ex);
if (maxRetry -- > 0)
goto invoke;
}
}

public static async Task TryInvokeAsync(Func<Task> func)
public static async Task TryInvokeAsync(Func<Task> func, int? maxRetryCount = null)
{
Guard.NotNull(func, nameof(func));
Guard.NotNull(func);

var maxRetry = maxRetryCount.GetValueOrDefault();
invoke:


try
{
await func();
}
catch (Exception ex)
{
OnInvokeException?.Invoke(ex);
if (maxRetry -- > 0)
goto invoke;
}
}

public static async Task TryInvokeAsync<T>(Func<T, Task> func, T t)
public static async Task TryInvokeAsync<T>(Func<T, Task> func, T t, int? maxRetryCount = null)
{
Guard.NotNull(func, nameof(func));
Guard.NotNull(func);

var maxRetry = maxRetryCount.GetValueOrDefault();
invoke:

try
{
await func(t);
}
catch (Exception ex)
{
OnInvokeException?.Invoke(ex);
if (maxRetry -- > 0)
goto invoke;
}
}

public static async Task TryInvokeAsync<T1, T2, T3>(Func<T1, T2, T3, Task> func, T1 t1, T2 t2, T3 t3)
public static async Task TryInvokeAsync<T1, T2, T3>(Func<T1, T2, T3, Task> func, T1 t1, T2 t2, T3 t3, int? maxRetryCount = null)
{
Guard.NotNull(func, nameof(func));
Guard.NotNull(func);

var maxRetry = maxRetryCount.GetValueOrDefault();
invoke:

try
{
Expand All @@ -237,6 +280,9 @@ public static async Task TryInvokeAsync<T1, T2, T3>(Func<T1, T2, T3, Task> func,
catch (Exception ex)
{
OnInvokeException?.Invoke(ex);

if (maxRetry -- > 0)
goto invoke;
}
}

Expand Down
Loading
Loading