Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 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
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PackageVersion Include="FluentAssertions" Version="6.12.0" />
<PackageVersion Include="Gress" Version="2.1.1" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageVersion Include="PolyShim" Version="2.8.2" />
<PackageVersion Include="PolyShim" Version="2.9.0" />
<PackageVersion Include="xunit" Version="2.9.2" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" />
</ItemGroup>
Expand Down
6 changes: 6 additions & 0 deletions PowerKit/Extensions/AssemblyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
using System.Reflection;
using System.Resources;
using System.Text;
#if NET40_OR_GREATER || NETSTANDARD || NET
using System.Threading;
using System.Threading.Tasks;
#endif
Comment thread
Tyrrrz marked this conversation as resolved.
Outdated

namespace PowerKit.Extensions;

Expand Down Expand Up @@ -44,6 +46,7 @@ public string GetManifestResourceString(string resourceName, Encoding encoding)
public string GetManifestResourceString(string resourceName) =>
assembly.GetManifestResourceString(resourceName, Encoding.UTF8);

#if NET40_OR_GREATER || NETSTANDARD || NET
/// <summary>
/// Reads the specified manifest resource as a string using the specified encoding asynchronously.
/// Throws <see cref="MissingManifestResourceException" /> if the resource is not found.
Expand Down Expand Up @@ -75,6 +78,7 @@ public async Task<string> GetManifestResourceStringAsync(
await assembly
.GetManifestResourceStringAsync(resourceName, Encoding.UTF8, cancellationToken)
.ConfigureAwait(false);
#endif

/// <summary>
/// Extracts the specified manifest resource to a file at the given path.
Expand All @@ -93,6 +97,7 @@ public void ExtractManifestResource(string resourceName, string filePath)
destination.Flush();
}

#if NET40_OR_GREATER || NETSTANDARD || NET
/// <summary>
/// Extracts the specified manifest resource to a file at the given path asynchronously.
/// Throws <see cref="MissingManifestResourceException" /> if the resource is not found.
Expand Down Expand Up @@ -121,5 +126,6 @@ public async Task ExtractManifestResourceAsync(
await source.CopyToAsync(destination, 81920, cancellationToken).ConfigureAwait(false);
await destination.FlushAsync(cancellationToken).ConfigureAwait(false);
}
#endif
}
}
2 changes: 2 additions & 0 deletions PowerKit/Extensions/AsyncEnumerableExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if NET40_OR_GREATER || NETSTANDARD || NET
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -84,3 +85,4 @@ var item in source
public ValueTaskAwaiter<List<T>> GetAwaiter() => source.ToListAsync().GetAwaiter();
}
}
#endif
6 changes: 6 additions & 0 deletions PowerKit/Extensions/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,12 @@ public IEnumerable<string> WhereNotNullOrWhiteSpace()
/// </summary>
public T? LastOrNull()
{
#if NET40_OR_GREATER || NETSTANDARD || NET
if (source is IReadOnlyList<T> list)
{
return list.Count > 0 ? list[list.Count - 1] : null;
}
#endif

var last = default(T?);

Expand All @@ -124,7 +126,11 @@ public IEnumerable<string> WhereNotNullOrWhiteSpace()
/// </summary>
public T? ElementAtOrNull(int index)
{
#if NET40_OR_GREATER || NETSTANDARD || NET
var list = source as IReadOnlyList<T> ?? source.ToArray();
#else
var list = source as IList<T> ?? source.ToArray();
#endif
return index >= 0 && index < list.Count ? list[index] : null;
}
}
Expand Down
5 changes: 5 additions & 0 deletions PowerKit/Extensions/ExceptionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ internal static class ExceptionExtensions
/// Returns a flat list containing the exception itself and all of its
/// nested inner exceptions, recursively unwrapping <see cref="AggregateException" /> instances.
/// </summary>
#if NET40_OR_GREATER || NETSTANDARD || NET
public IReadOnlyList<Exception> GetSelfAndDescendants()
#else
public IList<Exception> GetSelfAndDescendants()
#endif
Comment thread
Tyrrrz marked this conversation as resolved.
{
static void PopulateDescendants(Exception ex, ICollection<Exception> result)
{
Expand All @@ -36,3 +40,4 @@ static void PopulateDescendants(Exception ex, ICollection<Exception> result)
}
}
}

2 changes: 2 additions & 0 deletions PowerKit/Extensions/FileExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public static byte[] ReadAllBytes(string path, long offset, int length)
return buffer;
}

#if NET40_OR_GREATER || NETSTANDARD || NET
/// <summary>
/// Reads all bytes from the specified file starting at the given offset asynchronously.
/// </summary>
Comment thread
Tyrrrz marked this conversation as resolved.
Expand Down Expand Up @@ -136,5 +137,6 @@ public static async Task<byte[]> ReadAllBytesAsync(

return buffer;
}
#endif
}
}
2 changes: 2 additions & 0 deletions PowerKit/Extensions/StreamExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if NET40_OR_GREATER || NETSTANDARD || NET

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrap the methods, nto the whole file. We might add sync extensions later. @copilot

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 74a003f — the #if NET40_OR_GREATER || NETSTANDARD || NET guard is now inside the extension(Stream source) block wrapping the methods, with using directives unconditional at the top of the file.

using System;
using System.Buffers;
using System.IO;
Expand Down Expand Up @@ -99,3 +100,4 @@ await source
}
}
}
#endif
2 changes: 2 additions & 0 deletions PowerKit/Extensions/TextReaderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if NET40_OR_GREATER || NETSTANDARD || NET

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrap the methods, nto the whole file. We might add sync extensions later. @copilot

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 74a003f — same approach as StreamExtensions.cs: #if NET40_OR_GREATER || NETSTANDARD || NET wraps the ReadLinesAsync method inside the extension(TextReader reader) block, with using directives unconditional at the top.

using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -25,3 +26,4 @@ await reader.ReadLineAsync(cancellationToken).ConfigureAwait(false) is { } line
}
}
}
#endif
2 changes: 1 addition & 1 deletion PowerKit/PowerKit.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>netstandard2.0;net35;net10.0</TargetFrameworks>
Comment thread
Tyrrrz marked this conversation as resolved.
<IsPackable>true</IsPackable>
</PropertyGroup>

Expand Down