Skip to content

Commit

Permalink
Add BitzArt.CoreExtensions (#12)
Browse files Browse the repository at this point in the history
* Add BitzArt.CoreExtensions

* Set TargetFrameworks

* Add ImplicitUsings

* Update TaskExtensions
  • Loading branch information
ligowsky authored Oct 22, 2024
1 parent 4d634c8 commit 216acc7
Show file tree
Hide file tree
Showing 9 changed files with 301 additions and 1 deletion.
37 changes: 37 additions & 0 deletions .github/workflows/Publish CoreExtensions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Release CoreExtensions package

on:
repository_dispatch:
push:
tags:
- "CoreExtensions-v[0-9]+.[0-9]+.[0-9]+*"

env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NUGET_APIKEY: ${{ secrets.NUGET_APIKEY}}

jobs:

Release:
name: 'Release CoreExtensions package'
runs-on: ubuntu-latest
steps:

- name: Checkout
uses: actions/checkout@v2

- name: Verify commit
run: |
git fetch --no-tags --prune --depth=1 origin +refs/heads/*:refs/remotes/origin/*
git branch --remote --contains | grep origin/main
- name: Set version
run: echo "VERSION=${GITHUB_REF/refs\/tags\/CoreExtensions-v/}" >> $GITHUB_ENV

- name: Build
run: |
dotnet build src/Misc/BitzArt.CoreExtensions/BitzArt.CoreExtensions.csproj --configuration Release /p:Version=${VERSION}
dotnet pack src/Misc/BitzArt.CoreExtensions/BitzArt.CoreExtensions.csproj --configuration Release /p:Version=${VERSION} --no-build --output .
- name: Push
run: dotnet nuget push BitzArt.CoreExtensions.${VERSION}.nupkg --source https://api.nuget.org/v3/index.json --api-key ${NUGET_APIKEY}
30 changes: 30 additions & 0 deletions .github/workflows/Tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Tests

on:
push:
branches:
- main
tags-ignore:
- '*'
paths:
- "src/**"
- "tests/**"
- "Miscellaneous.sln"
- ".github/workflows/Tests.yml"

jobs:
tests:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Install dependencies
run: dotnet restore

- name: Build
run: dotnet build --configuration Release --no-restore

- name: Test
run: dotnet test --no-restore --verbosity normal
18 changes: 17 additions & 1 deletion Miscellaneous.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".github", ".github", "{2DA8
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{9545DA65-B29C-4A40-8010-3E339D138EE9}"
ProjectSection(SolutionItems) = preProject
.github\workflows\Publish CoreExtensions.yml = .github\workflows\Publish CoreExtensions.yml
.github\workflows\Publish EnumToMemberValue.yml = .github\workflows\Publish EnumToMemberValue.yml
.github\workflows\Publish FluentValidation.Extensions.yml = .github\workflows\Publish FluentValidation.Extensions.yml
.github\workflows\Publish Json.TypedObjects.yml = .github\workflows\Publish Json.TypedObjects.yml
.github\workflows\Publish LinqExtensions.yml = .github\workflows\Publish LinqExtensions.yml
.github\workflows\Publish MassTransit.ConsumerBase.yml = .github\workflows\Publish MassTransit.ConsumerBase.yml
.github\workflows\Publish Patcher.yml = .github\workflows\Publish Patcher.yml
.github\workflows\Tests.yml = .github\workflows\Tests.yml
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BitzArt.MassTransit.ConsumerBase", "src\MassTransit\BitzArt.MassTransit.ConsumerBase\BitzArt.MassTransit.ConsumerBase.csproj", "{1A828274-0B29-4892-8525-D9E3F79471EC}"
Expand Down Expand Up @@ -55,7 +57,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BitzArt.LinqExtensions.Batc
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BitzArt.LinqExtensions.Batching.Tests", "tests\BitzArt.LinqExtensions.Batching.Tests\BitzArt.LinqExtensions.Batching.Tests.csproj", "{6DBA7894-9852-4DF1-B9E0-09D60D311F6B}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{629938AD-8A1E-49C0-ABFB-724E564B0CCB}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sln", "sln", "{629938AD-8A1E-49C0-ABFB-724E564B0CCB}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
EndProjectSection
Expand All @@ -76,6 +78,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Linq", "Linq", "{BFE4986F-C
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Misc", "Misc", "{A5E6C844-8858-442E-8EAB-2CFD2E59B9EB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BitzArt.CoreExtensions", "src\Misc\BitzArt.CoreExtensions\BitzArt.CoreExtensions.csproj", "{350EF47C-407B-4509-AE6C-A86F17ED0C80}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BitzArt.CoreExtensions.Tests", "tests\Misc\BitzArt.CoreExtensions.Tests\BitzArt.CoreExtensions.Tests.csproj", "{039255F7-8BFF-4A5E-ABB6-602AF5F14AA5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -142,6 +148,14 @@ Global
{8F88487B-C78F-4695-B4F3-BF59CCDF6DF0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8F88487B-C78F-4695-B4F3-BF59CCDF6DF0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8F88487B-C78F-4695-B4F3-BF59CCDF6DF0}.Release|Any CPU.Build.0 = Release|Any CPU
{350EF47C-407B-4509-AE6C-A86F17ED0C80}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{350EF47C-407B-4509-AE6C-A86F17ED0C80}.Debug|Any CPU.Build.0 = Debug|Any CPU
{350EF47C-407B-4509-AE6C-A86F17ED0C80}.Release|Any CPU.ActiveCfg = Release|Any CPU
{350EF47C-407B-4509-AE6C-A86F17ED0C80}.Release|Any CPU.Build.0 = Release|Any CPU
{039255F7-8BFF-4A5E-ABB6-602AF5F14AA5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{039255F7-8BFF-4A5E-ABB6-602AF5F14AA5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{039255F7-8BFF-4A5E-ABB6-602AF5F14AA5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{039255F7-8BFF-4A5E-ABB6-602AF5F14AA5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -174,6 +188,8 @@ Global
{8F88487B-C78F-4695-B4F3-BF59CCDF6DF0} = {23E7F528-746A-4A42-8FB3-F89088395933}
{BFE4986F-CD69-4E42-8C6E-F8EE35491BBF} = {6C59FADC-BAEA-46B7-A69F-DC2AA0A124D6}
{A5E6C844-8858-442E-8EAB-2CFD2E59B9EB} = {6C59FADC-BAEA-46B7-A69F-DC2AA0A124D6}
{350EF47C-407B-4509-AE6C-A86F17ED0C80} = {A053045B-CB06-473C-A04E-D54A519B6BBD}
{039255F7-8BFF-4A5E-ABB6-602AF5F14AA5} = {A5E6C844-8858-442E-8EAB-2CFD2E59B9EB}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {49BCA0AC-45AF-4DE6-B691-30A0F2959200}
Expand Down
19 changes: 19 additions & 0 deletions src/Misc/BitzArt.CoreExtensions/BitzArt.CoreExtensions.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>BitzArt</RootNamespace>
<Nullable>enable</Nullable>
<GenerateDocumentationFile>true</GenerateDocumentationFile>

<PackageId>BitzArt.CoreExtensions</PackageId>
<Authors>BitzArt</Authors>
<Description>Base Class Library Extensions</Description>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/BitzArt/Miscellaneous</RepositoryUrl>
<PackageProjectUrl>https://github.com/BitzArt/Miscellaneous</PackageProjectUrl>
</PropertyGroup>

</Project>
37 changes: 37 additions & 0 deletions src/Misc/BitzArt.CoreExtensions/Enums/ComparisonType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
namespace BitzArt;

/// <summary>
/// Condition for comparing values.
/// </summary>
public enum ComparisonType : byte
{
/// <summary>
/// Represents an equality condition.
/// </summary>
Equal = 0,

/// <summary>
/// Represents an inequality condition.
/// </summary>
NotEqual = 1,

/// <summary>
/// Represents a 'greater than' condition.
/// </summary>
GreaterThan = 2,

/// <summary>
/// Represents a 'greater than or equal' condition.
/// </summary>
GreaterThanOrEqual = 3,

/// <summary>
/// Represents a 'less than' condition.
/// </summary>
LessThan = 4,

/// <summary>
/// Represents a 'less than or equal' condition.
/// </summary>
LessThanOrEqual = 5
}
39 changes: 39 additions & 0 deletions src/Misc/BitzArt.CoreExtensions/Enums/OrderDirection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace BitzArt;

/// <summary>
/// Order direction (ascending/descending).
/// </summary>
public enum OrderDirection : byte
{
/// <summary>
/// Ascending order direction.
/// </summary>
Ascending = 0,

/// <summary>
/// Descending order direction.
/// </summary>
Descending = 1
}

/// <summary>
/// Extensions to <see cref="OrderDirection"/>.
/// </summary>
public static class OrderDirectionExtensions
{
/// <summary>
/// Reverse the order direction.
/// </summary>
/// <param name="orderDirection"></param>
/// <returns></returns>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public static OrderDirection Reverse(this OrderDirection orderDirection)
{
return orderDirection switch
{
OrderDirection.Ascending => OrderDirection.Descending,
OrderDirection.Descending => OrderDirection.Ascending,
_ => throw new ArgumentOutOfRangeException(nameof(orderDirection), orderDirection, null)
};
}
}
51 changes: 51 additions & 0 deletions src/Misc/BitzArt.CoreExtensions/Extensions/TaskExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
namespace BitzArt;

/// <summary>
/// Extension methods for <see cref="Task"/>.
/// </summary>
public static class TaskExtensions
{
/// <summary>
/// Awaits the <paramref name="task"/> until it is completed or cancelled, while ignoring cancellation exceptions.
/// </summary>
/// <param name="task">The task to await.</param>
/// <param name="ignoreCancellation">Whether to ignore cancellation exceptions.</param>
/// <param name="byTaskStatus">Determines whether to check for task cancellation by task status (if <see langword="true"/>) or by <see cref="OperationCanceledException"/> (if <see langword="false"/>).</param>
public static Task IgnoreCancellation(this Task task, bool ignoreCancellation = true, bool byTaskStatus = true)
{
if (!ignoreCancellation) return task;

return byTaskStatus
? IgnoreCancellationByTaskCancelledAsync(task)
: IgnoreCancellationByExceptionAsync(task);
}

private static async Task IgnoreCancellationByTaskCancelledAsync(Task task)
{
try
{
await task;
}
catch
{
if (task.IsCanceled) return;
throw;
}
}

private static async Task IgnoreCancellationByExceptionAsync(Task task)
{
try
{
await task;
}
catch (OperationCanceledException)
{
return;
}
catch
{
throw;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\Misc\BitzArt.CoreExtensions\BitzArt.CoreExtensions.csproj" />
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>

</Project>
44 changes: 44 additions & 0 deletions tests/Misc/BitzArt.CoreExtensions.Tests/TaskExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
namespace BitzArt.CoreExtensions.Tests;

public class TaskExtensionsTests
{
[Fact]
public async Task IgnoreCancellation_WhenTrue_ShouldHandleExceptionByIgnoring()
{
var cts = new CancellationTokenSource();
cts.CancelAfter(10);

await Task.Delay(100, cts.Token).IgnoreCancellation(true);
}

[Fact]
public async Task IgnoreCancellation_WhenFalse_ShouldDoNothing()
{
var cts = new CancellationTokenSource();
cts.CancelAfter(10);

await Assert.ThrowsAnyAsync<OperationCanceledException>(()
=> Task.Delay(100, cts.Token).IgnoreCancellation(false));
}

[Fact]
public async Task IgnoreCancellation_WhenTrueAndInnerOperationCancelled_ShouldHandleExceptionByIgnoring()
{
await TestMethodAsync().IgnoreCancellation(true);
}

[Fact]
public async Task IgnoreCancellation_WhenFalseAndInnerOperationCancelled_ShouldDoNothing()
{
await Assert.ThrowsAnyAsync<OperationCanceledException>(()
=> TestMethodAsync().IgnoreCancellation(false));
}

private async Task TestMethodAsync()
{
var cts = new CancellationTokenSource();
cts.CancelAfter(10);

await Task.Delay(100, cts.Token);
}
}

0 comments on commit 216acc7

Please sign in to comment.