Skip to content

Commit

Permalink
Add guard class.
Browse files Browse the repository at this point in the history
  • Loading branch information
olivegamestudio committed Aug 19, 2024
0 parents commit 8741aa1
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 0 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# This workflow will build a .NET project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net

name: .NET

on:
push:
branches: [ "main", "develop" ]
pull_request:
branches: [ "main", "develop" ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
filter: tree:0

- name: Get tags
run: git fetch --tags origin

- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x

- name: Restore dependencies
run: dotnet restore

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

- name: Test
run: dotnet test --configuration Release -p:CollectCoverage=true

- name: Publish Nuget
run: |
dotnet nuget push '**/*.nupkg' -k ${NUGET} -s https://api.nuget.org/v3/index.json --skip-duplicate --no-symbols
env:
NUGET: ${{ secrets.NUGET }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.vs
bin
obj
24 changes: 24 additions & 0 deletions Utility.Guard.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# 17
VisualStudioVersion = 17.10.35027.167
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Utility.Guard", "Utility.Guard\Utility.Guard.csproj", "{1DF6CF00-3907-40A3-BA69-B32788F37F4C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{1DF6CF00-3907-40A3-BA69-B32788F37F4C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1DF6CF00-3907-40A3-BA69-B32788F37F4C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1DF6CF00-3907-40A3-BA69-B32788F37F4C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1DF6CF00-3907-40A3-BA69-B32788F37F4C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7D25D4C6-E7A0-4490-8BDE-9076CA666008}
EndGlobalSection
EndGlobal
46 changes: 46 additions & 0 deletions Utility.Guard/Guard.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Linq.Expressions;

namespace Utility;

public static class Guard
{
public static T ThrowIfNull<T>(T item)
{
if (item is null)
{
throw new InvalidOperationException();
}

return item;
}

public static T ThrowIfNotNull<T>(T item)
{
if (item is not null)
{
throw new InvalidOperationException();
}

return item;
}

public static int ThrowIfIntegerNotInRange(int item, int start, int end)
{
if (item >= start && item <= end)
{
throw new InvalidOperationException();
}

return item;
}

public static string ThrowIfStringNullOrEmpty(string value)
{
if (string.IsNullOrEmpty(value))
{
throw new InvalidOperationException();
}

return value;
}
}
22 changes: 22 additions & 0 deletions Utility.Guard/Utility.Guard.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard20</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Authors>Simon Jefferies</Authors>
<Company>Simon Jefferies</Company>
<Description></Description>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MinVer" Version="*">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>

0 comments on commit 8741aa1

Please sign in to comment.