Skip to content

Commit

Permalink
refactor: cleanup symbol condition
Browse files Browse the repository at this point in the history
  • Loading branch information
WeihanLi committed Dec 10, 2024
1 parent e6a55d1 commit 9864512
Show file tree
Hide file tree
Showing 18 changed files with 30 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/WeihanLi.Common/Event/EventBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected EventBase(string eventId)

// https://www.newtonsoft.com/json/help/html/JsonConstructorAttribute.htm
[JsonConstructor]
#if NET6_0_OR_GREATER
#if NET
[System.Text.Json.Serialization.JsonConstructor]
#endif
protected EventBase(string eventId, DateTimeOffset eventAt)
Expand Down
2 changes: 1 addition & 1 deletion src/WeihanLi.Common/Extensions/CollectionExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public static IEnumerable<T> GetRandomList<T>(this IList<T> list)
;
}

#if NET6_0_OR_GREATER
#if NET
// https://github.com/more-itertools/more-itertools/blob/master/more_itertools/more.py#L3149
//def set_partitions_helper(L, k):
//n = len(L)
Expand Down
4 changes: 2 additions & 2 deletions src/WeihanLi.Common/Extensions/CoreExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public static string ToBase64String(this byte[] inArray, int offset, int length,
return Convert.ToBase64String(inArray, offset, length, options);
}

#if NET5_0_OR_GREATER
#if NET
public static string ToHexString(this ReadOnlySpan<byte> bytes, bool isLowerCase = false)
{
#if NET9_0_OR_GREATER
Expand Down Expand Up @@ -1616,7 +1616,7 @@ public static byte[] HexStringToBytes(this string hexString)
if (string.IsNullOrEmpty(hexString))
return [];

#if NET6_0_OR_GREATER
#if NET
return Convert.FromHexString(hexString);
#else
var charArray = hexString.ToCharArray();
Expand Down
2 changes: 1 addition & 1 deletion src/WeihanLi.Common/Extensions/DataExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ private static string GetParameterName(string originName)
[typeof(TimeSpan)] = DbType.Time,
[typeof(byte[])] = DbType.Binary,
[typeof(object)] = DbType.Object,
#if NET6_0_OR_GREATER
#if NET
[typeof(DateOnly)] = DbType.Date,
[typeof(TimeOnly)] = DbType.Time,
#endif
Expand Down
6 changes: 3 additions & 3 deletions src/WeihanLi.Common/Extensions/HttpClientExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public static async Task<TResponse?> ReadJsonResponseAsync<TResponse>
{
Guard.NotNull(response);
responseAction?.Invoke(response);
#if NET6_0_OR_GREATER
#if NET
var responseText = await response.Content.ReadAsStringAsync(cancellationToken);
#else
var responseText = await response.Content.ReadAsStringAsync();
Expand All @@ -109,15 +109,15 @@ public static async Task<TResponse?> ReadJsonResponseAsync<TResponse>
requestAction?.Invoke(requestMessage);
using var response = await httpClient.SendAsync(requestMessage, cancellationToken);
responseAction?.Invoke(response);
#if NET6_0_OR_GREATER
#if NET
var responseText = await response.Content.ReadAsStringAsync(cancellationToken);
#else
var responseText = await response.Content.ReadAsStringAsync();
#endif
return JsonConvert.DeserializeObject<TResponse>(responseText);
}

#if NET6_0_OR_GREATER
#if NET
/// <summary>
/// PatchAsJsonAsync
/// </summary>
Expand Down
5 changes: 2 additions & 3 deletions src/WeihanLi.Common/Extensions/ProcessExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public static ProcessStartInfo WithEnv(this ProcessStartInfo processStartInfo, s
return processStartInfo;
}

#if NET6_0_OR_GREATER
#else
#if NETSTANDARD
public static Task WaitForExitAsync(this Process process, CancellationToken cancellationToken = default)
{
Guard.NotNull(process);
Expand Down Expand Up @@ -285,7 +284,7 @@ public static async Task<Process> ExecuteProcessAsync(this ProcessStartInfo psi,
public static bool TryKill(this Process process, bool entireProcessTree = true)
{
return
#if NET6_0_OR_GREATER
#if NET
process.Try(x => x.Kill(entireProcessTree))
#else
process.Try(x => x.Kill())
Expand Down
2 changes: 1 addition & 1 deletion src/WeihanLi.Common/Extensions/TaskExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static Task AsTask(this CancellationToken cancellationToken)

public static Task WhenAllSafely(this IEnumerable<Task> tasks, Action<Exception>? onException = null) => Task.WhenAll(tasks.Select(async t =>
{
#if NET8_0_OR_GREATER
#if NET
await t.ConfigureAwait(ConfigureAwaitOptions.SuppressThrowing);
#else
try
Expand Down
2 changes: 1 addition & 1 deletion src/WeihanLi.Common/Extensions/TypeExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static class TypeExtension
typeof(char),
typeof(string),// IsPrimitive:False

#if NET6_0_OR_GREATER
#if NET
typeof(DateOnly),
typeof(TimeOnly),
#endif
Expand Down
6 changes: 3 additions & 3 deletions src/WeihanLi.Common/Guard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static T NotNull<T>([NotNull] T? t,
[CallerArgumentExpression(nameof(t))]
string? paramName = default)
{
#if NET6_0_OR_GREATER
#if NET
ArgumentNullException.ThrowIfNull(t, paramName);
#else
if (t is null)
Expand All @@ -26,7 +26,7 @@ public static string NotNullOrEmpty([NotNull] string? str,
[CallerArgumentExpression(nameof(str))]
string? paramName = null)
{
#if NET7_0_OR_GREATER
#if NET
ArgumentException.ThrowIfNullOrEmpty(str, paramName);
#else
NotNull(str, paramName);
Expand All @@ -42,7 +42,7 @@ public static string NotNullOrEmpty([NotNull] string? str,
public static string NotNullOrWhiteSpace([NotNull] string? str,
[CallerArgumentExpression(nameof(str))] string? paramName = null)
{
#if NET8_0_OR_GREATER
#if NET
ArgumentException.ThrowIfNullOrWhiteSpace(str, paramName);
#else
NotNull(str, paramName);
Expand Down
10 changes: 5 additions & 5 deletions src/WeihanLi.Common/Helpers/ApplicationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static LibraryInfo GetLibraryInfo(Assembly assembly)
if (!string.IsNullOrEmpty(environmentOverride) && Directory.Exists(environmentOverride))
{
var execFileName =
#if NET6_0_OR_GREATER
#if NET
OperatingSystem.IsWindows()
#else
RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
Expand Down Expand Up @@ -105,7 +105,7 @@ public static string GetDotnetDirectory()

if (string.IsNullOrWhiteSpace(dotnetExe))
{
#if NET6_0_OR_GREATER
#if NET
dotnetExe = Environment.ProcessPath;
#else
dotnetExe = System.Diagnostics.Process.GetCurrentProcess().MainModule?.FileName;
Expand Down Expand Up @@ -141,15 +141,15 @@ public static string GetDotnetDirectory()
private static RuntimeInfo GetRuntimeInfo()
{
var libInfo = GetLibraryInfo(typeof(object).Assembly);
#if NET6_0_OR_GREATER
#if NET
#else
var currentProcess = System.Diagnostics.Process.GetCurrentProcess();
#endif
var runtimeInfo = new RuntimeInfo()
{
Version = Environment.Version.ToString(),

#if NET6_0_OR_GREATER
#if NET
ProcessId = Environment.ProcessId,
ProcessPath = Environment.ProcessPath ?? string.Empty,
RuntimeIdentifier = RuntimeInformation.RuntimeIdentifier,
Expand Down Expand Up @@ -257,7 +257,7 @@ public class RuntimeInfo
public required string MachineName { get; init; }
public required string UserName { get; init; }

#if NET6_0_OR_GREATER
#if NET
public required string RuntimeIdentifier { get; init; }
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/WeihanLi.Common/Helpers/Hosting/AppHostBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public AppHost Build()
throw new InvalidOperationException("The AppHost had been created");
}
_hostBuilt = true;
#if NET7_0_OR_GREATER
#if NET
_serviceCollection.MakeReadOnly();
#endif
var services = Services.BuildServiceProvider();
Expand Down
2 changes: 1 addition & 1 deletion src/WeihanLi.Common/Helpers/Hosting/BackgroundService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public virtual async Task StopAsync(CancellationToken cancellationToken)
try
{
// Signal cancellation to the executing method
#if NET8_0_OR_GREATER
#if NET
await _stoppingCts!.CancelAsync();
#else
_stoppingCts!.Cancel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System.Diagnostics;
using System.Diagnostics.Metrics;

#if NET6_0_OR_GREATER
#if NET

namespace WeihanLi.Common.Helpers.Hosting;

Expand Down
2 changes: 1 addition & 1 deletion src/WeihanLi.Common/Helpers/HttpHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static bool IsWellKnownContentHeader(string header)
private static readonly Lazy<HttpClient> SharedHttpClient = new(() => new(new NoProxyHttpClientHandler()
{
UseCookies = false,
#if NET6_0_OR_GREATER
#if NET
AutomaticDecompression = DecompressionMethods.All
#else
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
Expand Down
4 changes: 2 additions & 2 deletions src/WeihanLi.Common/Helpers/InvokeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ static InvokeHelper()
#if NETCOREAPP
System.Runtime.Loader.AssemblyLoadContext.Default.Unloading += ctx => InvokeExitHandler(ctx, null);
#endif
#if NET6_0_OR_GREATER
#if NET
// https://github.com/dotnet/runtime/blob/940b332ad04e58862febe019788a5b21e266ea10/src/libraries/Microsoft.Extensions.Hosting/src/Internal/ConsoleLifetime.netcoreapp.cs
PosixSignalRegistration.Create(PosixSignal.SIGINT, ctx => InvokeExitHandler(ctx, null));
PosixSignalRegistration.Create(PosixSignal.SIGQUIT, ctx => InvokeExitHandler(ctx, null));
Expand Down Expand Up @@ -110,7 +110,7 @@ private static void InvokeExitHandler(object? sender, EventArgs? args)
// {
// consoleCancelEventArgs.Cancel = true;
// }
// #if NET6_0_OR_GREATER
// #if NET
// if (sender is PosixSignalContext posixSignalContext)
// {
// posixSignalContext.Cancel = true;
Expand Down
2 changes: 1 addition & 1 deletion src/WeihanLi.Common/Helpers/PipelineBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static IAsyncPipelineBuilder<TContext> CreateAsync<TContext>(Func<TContex
public static IValueAsyncPipelineBuilder<TContext> CreateValueAsync<TContext>()
{
return new ValueAsyncPipelineBuilder<TContext>(c =>
#if NET6_0_OR_GREATER
#if NET
ValueTask.CompletedTask
#else
default
Expand Down
4 changes: 2 additions & 2 deletions src/WeihanLi.Common/Helpers/ProfilerHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static ProfilerStopper StartNew(this IProfiler profiler, Action<TimeSpan>
/// <param name="startTimestamp">startTimestamp, get by Stopwatch.GetTimestamp()</param>
/// <returns>elapsed time</returns>
public static TimeSpan GetElapsedTime(long startTimestamp) =>
#if NET7_0_OR_GREATER
#if NET
Stopwatch.GetElapsedTime(startTimestamp)
#else
GetElapsedTime(startTimestamp, Stopwatch.GetTimestamp())
Expand All @@ -64,7 +64,7 @@ public static TimeSpan GetElapsedTime(long startTimestamp) =>
/// <returns>elapsed time</returns>
public static TimeSpan GetElapsedTime(long startTimestamp, long endTimestamp)
{
#if NET7_0_OR_GREATER
#if NET
return Stopwatch.GetElapsedTime(startTimestamp, endTimestamp);
#else
var ticks = (long)((endTimestamp - startTimestamp) * TicksPerTimestamp);
Expand Down
2 changes: 1 addition & 1 deletion src/WeihanLi.Common/Helpers/SecurityHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static class SecurityHelper
'F'
];

#if NET6_0_OR_GREATER
#if NET
public static Random Random => Random.Shared;
#else

Expand Down

0 comments on commit 9864512

Please sign in to comment.