Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
badcel committed May 24, 2024
0 parents commit 1ad4795
Show file tree
Hide file tree
Showing 14 changed files with 544 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
- package-ecosystem: "nuget"
directory: "/src/"
schedule:
interval: "weekly"
86 changes: 86 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
name: 'CI'

on:
workflow_dispatch:
inputs:
publish_release:
description: If this build should publish nuget packages
required: true
type: boolean
version_suffix:
description: Suffix of the version number. Can be used to create a preview package.
required: false
type: string
push:
branches:
- main
paths-ignore:
- '**.md'
pull_request:

env:
configuration: Release
publish_release: ${{ github.event.inputs.publish_release }}
version_suffix: ${{ github.event.inputs.version_suffix }}
tester_framework: net8.0

jobs:
build:
name: Build
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ windows-latest, macos-latest, ubuntu-latest ]

steps:
- name: Checkout with submodules
uses: actions/checkout@v4
with:
submodules: 'true'

- name: Prepare .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: |
6.0.x
8.0.x
- name: Verify code format
if: matrix.os == 'ubuntu-latest'
run: dotnet format --verify-no-changes
working-directory: './src'

- name: Run unit tests ubuntu
if: matrix.os == 'ubuntu-latest'
env:
RuntimeIdentifier: unix
run: dotnet test -c ${{ env.configuration }}
working-directory: './src'

- name: Run unit tests macos
if: matrix.os == 'macos-latest'
env:
RuntimeIdentifier: unix
run: dotnet test -c ${{ env.configuration }}
working-directory: './src'

- name: Run unit tests windows
if: matrix.os == 'windows-latest'
env:
RuntimeIdentifier: windows
run: dotnet test -c ${{ env.configuration }}
working-directory: './src'

- name: Build solution
run: dotnet fsi build.fsx
working-directory: './src'

- name: Pack release version
if: env.publish_release == 'true' && matrix.os == 'ubuntu-latest'
run: dotnet pack WCharT.Net/WCharT.Net.csproj --no-build --nologo -c ${{ env.configuration }} --version-suffix "${{ env.version_suffix }}" -o ../Nuget
working-directory: './src'

- name: Publish to nuget org
if: env.publish_release == 'true' && matrix.os == 'ubuntu-latest'
run: dotnet nuget push "*.nupkg" -k ${{ secrets.NUGET_API_KEY }} -s nuget.org
working-directory: './Nuget'
26 changes: 26 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#Build results
[Dd]ebug/
[Rr]elease/
[Bb]in/
[Oo]bj/
*.Artifacts

*.nupkg

#.vscode/
global.json
coverage.*

*~

#meson

#Rider
*.idea

# Visual Studio
.vs/
*.DotSettings.user

#Docs
_site/
21 changes: 21 additions & 0 deletions license.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Marcel Tiede

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
39 changes: 39 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# WCharT.Net
[![Build Status](https://img.shields.io/github/actions/workflow/status/badcel/WCharT.Net/ci.yml?branch=main)](https://github.com/badcel/WCharT.Net/actions/workflows/ci.yml)[![NuGet](https://img.shields.io/nuget/v/WCharT.Net)](https://www.nuget.org/packages/WCharT.Net/)[![License (MIT)](https://img.shields.io/github/license/badcel/WCharT.Net)](https://github.com/badcel/WCharT.Net/blob/main/license.txt)

Welcome to WCharT.Net a modern cross platform package to interop with WCharT data.

## Use
To work with WCharT data create a new instance of `WCharTString`:

```csharp
//Read data from a byte*
byte* pointer = NativeCal();
var data = new WCharTString(pointer).GetString();

//Create a buffer for a native library and read the data after it was filled
ReadOnlySpan<byte> data = new WCharTString(int bufferCharSize);
NativeCall(data);
var str = data.GetString();

//Pass a string to a native library
ReadOnlySpan<byte> data = new WCharTString(string str);
NativeCall(data);
```

## Build
To build the solution locally execute the following commands:

```sh
$ git clone https://github.com/badcel/WCharT.Net.git
$ cd WCharT.Net/src
$ dotnet fsi build.fsx
```

## Native AOT
If the application targets at least .NET 8 the nuget package allows applications to be published as _Native AOT_ as itself is AOT compatible.

## Licensing terms
HidApi.Net is licensed under the terms of the MIT-License. Please see the [license file][license] for further information.

[license]:https://github.com/badcel/WCharT.Net/blob/main/license.txt
9 changes: 9 additions & 0 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project>
<PropertyGroup>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<AnalysisMode>Recommended</AnalysisMode>
</PropertyGroup>
</Project>
77 changes: 77 additions & 0 deletions src/WCharT.Net.Tests/Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System.Runtime.InteropServices;
using System.Text;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace WCharT.Tests;

[TestClass]
public class Tests
{
[TestMethod]
public void TestEncoding()
{
var text = "Test";
var str = new WCharTString(text);

str.GetString().Should().Be(text);
}

[TestMethod]
public void TestUnicode()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
Assert.Inconclusive("Unicode is only supported on windows based operating systems");

#pragma warning disable CA1861
var data = new byte[6];
data[0] = 0;
data[1] = 1;
data[2] = 0;
data[3] = 1;
data[4] = 0;
data[5] = 0;

unsafe
{
fixed (byte* p = data)
{
var str = new WCharTString(p);
str.GetString().Should().Be(new string(new[] { '\u0100', '\u0100' }));
}
}
#pragma warning restore CA1861
}

[TestMethod]
public void TestUtf32()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
Assert.Inconclusive("Utf32 is only supported on unix based operating systems");

#pragma warning disable CA1861
var data = new byte[12];
data[0] = 0;
data[1] = 1;
data[2] = 0;
data[3] = 0;
data[4] = 0;
data[5] = 1;
data[6] = 0;
data[7] = 0;
data[8] = 0;
data[9] = 0;
data[10] = 0;
data[11] = 0;

unsafe
{
fixed (byte* p = data)
{
var str = new WCharTString(p);
str.GetString().Should().Be(new string(new[] { '\u0100', '\u0100' }));
}
}
#pragma warning restore CA1861
}
}
18 changes: 18 additions & 0 deletions src/WCharT.Net.Tests/WCharT.Net.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<IsPackable>false</IsPackable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<RootNamespace>WCHarT.Tests</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.2.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.2.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\WCharT.Net\WCharT.Net.csproj" />
</ItemGroup>
</Project>
53 changes: 53 additions & 0 deletions src/WCharT.Net/WCharT.Net.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<VersionPrefix>0.1.0</VersionPrefix>

<Authors>Marcel Tiede</Authors>
<Copyright>Marcel Tiede</Copyright>
<owners>badcel</owners>

<Description>A modern cross platform package to interop with WCharT data.</Description>
<PackageProjectUrl>https://github.com/badcel/WCharT</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>

<PackageReadmeFile>readme.md</PackageReadmeFile>
<PackageTags>wchart;native;interop;linux;windows;macos</PackageTags>
</PropertyGroup>

<PropertyGroup>
<RootNamespace>WCharT</RootNamespace>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>

<BUILD_FOR Condition="'$(RuntimeIdentifier)' != ''">$(RuntimeIdentifier.ToUpper())</BUILD_FOR>
<BUILD_FOR Condition="'$(RuntimeIdentifier)' == ''">UNIX</BUILD_FOR>
<DefineConstants>$(DefineConstants);BUILD_FOR_$(BUILD_FOR)</DefineConstants>

<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
<IncludeBuildOutput>false</IncludeBuildOutput>
<NoWarn>NU5131</NoWarn>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFramework)' == 'net8.0'">
<IsAotCompatible>true</IsAotCompatible>
</PropertyGroup>

<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
</PropertyGroup>

<ItemGroup>
<None Include="../../readme.md" Pack="true" PackagePath="/" Visible="false"/>
<None Include="obj/Release/net6.0/ref/WCharT.Net.dll" Pack="true" PackagePath="ref/net6.0" Visible="false"/>
<None Include="bin/Release/net6.0/unix/WCharT.Net.dll" Pack="true" PackagePath="runtimes/unix/lib/net6.0" Visible="false"/>
<None Include="bin/Release/net6.0/windows/WCharT.Net.dll" Pack="true" PackagePath="runtimes/windows/lib/net6.0" Visible="false"/>
<None Include="obj/Release/net8.0/ref/WCharT.Net.dll" Pack="true" PackagePath="ref/net8.0" Visible="false"/>
<None Include="bin/Release/net8.0/unix/WCharT.Net.dll" Pack="true" PackagePath="runtimes/unix/lib/net8.0" Visible="false"/>
<None Include="bin/Release/net8.0/windows/WCharT.Net.dll" Pack="true" PackagePath="runtimes/windows/lib/net8.0" Visible="false"/>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" Condition="'$(TargetFramework)' == 'net6.0' or '$(TargetFramework)' == 'net7.0'"/>
</ItemGroup>
</Project>
Loading

0 comments on commit 1ad4795

Please sign in to comment.