Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ base128:
bouncycastle:
just msbuild/BouncyCastle/

generatorequals:
just msbuild/Generator.Equals/

newtonsoftjson:
just msbuild/Newtonsoft.Json/

Expand Down
2 changes: 2 additions & 0 deletions msbuild/Generator.Equals/Generator.Equals.Runtime.dll.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 59 additions & 0 deletions msbuild/Generator.Equals/Generator.Equals.dll.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions msbuild/Generator.Equals/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Generator.Equals

The Generator.Equals library from nuget!

## Oddities of this package

The generator has two parts: the roslyn source generator part, and the runtime
library. The source generator runs only at build time and also has a dependency
on the runtime library. The runtime library runs both at build time and at
regular runtime.

The runtime lib uses [`System.HashCode`][System.HashCode] which is included in
netstandard2.1, but requires the `Microsoft.Bcl.HashCode` package when
targetting netstandard2.0 (i.e. it is present in netstandard2.0 but
"package-provided").

Because unity already supports netstandard2.1, it was preferable to build the
runtime lib against netstandard2.1 instead of vendoring
`Microsoft.Bcl.HashCode`. However, roslyn source generators [*must* be built
against netstandard2.0][roslyn-requires-netstandard20]...

To work around this, and avoid needing to vendor `Microsoft.Bcl.HashCode`, we
build the package twice: Once we build *both* the source generator and the
runtime against netstandard2.0, and the second time we build *only* the runtime
against netstandard2.1. To accomplish the second build, we `git apply` a patch
to enable netstandard2.1 and remove the unneded dependency, and then `dotnet
build`.

Finally to get the source generator to work in Unity, we follow the [official
docs][source-generator-unity-docs], in particular we disable all target
platforms and give it an asset label of `RoslynAnalyzer`, but only for the
source generator. The runtime dll doesn't get this label and has support for
all platforms.

[System.HashCode]: https://learn.microsoft.com/en-us/dotnet/api/system.hashcode?view=net-9.0
[roslyn-requires-netstandard20]: https://github.com/dotnet/roslyn/issues/45162
[source-generator-unity-docs]: https://docs.unity3d.com/Manual/install-existing-analyzer.html
22 changes: 22 additions & 0 deletions msbuild/Generator.Equals/build-runtime-for-netstandard21.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
diff --git a/Generator.Equals.Runtime/Generator.Equals.Runtime.csproj b/Generator.Equals.Runtime/Generator.Equals.Runtime.csproj
index b2b1595..d161784 100644
--- a/Generator.Equals.Runtime/Generator.Equals.Runtime.csproj
+++ b/Generator.Equals.Runtime/Generator.Equals.Runtime.csproj
@@ -1,14 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
- <TargetFramework>netstandard2.0</TargetFramework>
+ <TargetFramework>netstandard2.1</TargetFramework>
<LangVersion>9.0</LangVersion>
<Nullable>enable</Nullable>
<RootNamespace>Generator.Equals</RootNamespace>
</PropertyGroup>

<ItemGroup>
- <PackageReference Include="Microsoft.Bcl.HashCode" Version="1.1.1" />
+ <!--Not needed on 2.1-->
+ <!--<PackageReference Include="Microsoft.Bcl.HashCode" Version="1.1.1" />-->
</ItemGroup>

</Project>
35 changes: 35 additions & 0 deletions msbuild/Generator.Equals/justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
git_repo := "https://github.com/diegofrata/Generator.Equals.git"
git_ref := "3.2.0"

out_dir := invocation_directory() / "build"
working_dir := `mktemp -d`
repo_dir := working_dir / "repo"
dotnet_artifact_dir := working_dir / "dotnet_artifacts"
upm_contents_dir := working_dir / "upm_contents"

default: assemble_upm

# Clean build outputs
clean:
rm -rf {{working_dir}}

clone:
git clone --single-branch --branch {{git_ref}} {{git_repo}} {{repo_dir}}

build_both: clone
cd {{repo_dir}} && dotnet build {{"Generator.Equals" / "Generator.Equals.csproj"}} --configuration Release -o {{dotnet_artifact_dir}} --framework netstandard2.0

patch_runtime_for_netstandard21: clone
cd {{repo_dir}} && git apply {{justfile_dir() / "build-runtime-for-netstandard21.patch"}}

build_only_runtime: clone build_both patch_runtime_for_netstandard21
cd {{repo_dir}} && dotnet build {{"Generator.Equals.Runtime" / "Generator.Equals.Runtime.csproj"}} --configuration Release -o {{dotnet_artifact_dir}} --framework netstandard2.1

assemble_upm: clone build_both build_only_runtime && clean
mkdir -p {{upm_contents_dir}}
cp {{dotnet_artifact_dir / "Generator.Equals.dll"}} {{upm_contents_dir}}
cp {{dotnet_artifact_dir / "Generator.Equals.Runtime.dll"}} {{upm_contents_dir}}
cp package.json {{upm_contents_dir}}
cp *.meta {{upm_contents_dir}}
mkdir -p {{out_dir}}
npm pack --pack-destination {{out_dir}} {{upm_contents_dir}}
9 changes: 9 additions & 0 deletions msbuild/Generator.Equals/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "org.basisvr.generator.equals",
"version": "3.2.0",
"displayName": "Generator.Equals",
"description": "A source code generator for automatically implementing IEquatable<T> using only attributes.",
"documentationUrl": "https://github.com/diegofrata/Generator.Equals",
"publishConfig": { "registry": "https://npm.pkg.github.com/@BasisVR" },
"repository":"https://github.com/BasisVR/UpmBuilds"
}
7 changes: 7 additions & 0 deletions msbuild/Generator.Equals/package.json.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.