From 4c7159dddb6c60299486de97fdd9e7bf8a397903 Mon Sep 17 00:00:00 2001 From: Daniel Cazzulino Date: Sun, 22 Dec 2024 20:35:06 -0300 Subject: [PATCH] Drastically simply packing and namespace management The ability to customize the namespace of the struct id types seemed a bit of a corner case and it was introducing non-trivial complexity in scenarios involving transitive project references (analyzers are transitive, as well as buildTransitive targets, but analyzer options, contentFiles -with or without compile action- are not). This was leading to hack over hack for no serious gain. So even if we keep the CodeTemplate behavior intact should we figure out how to properly handle changing the namespace, this change removes the copying and content updating on the static files, which are now simply included via nuget's contentFiles feature plus buildAction=Compile. This provides the behavior we're looking for: - Interfaces and types required for struct ids are only added to the project referencing the package - using and codegen for struct ids is supported in projects referencing the "core" one: analyzers are already transitive, we just make the templates transitive too via targets (since we can't via contentFiles). --- readme.md | 3 - src/StructId.Analyzer/CodeTemplate.cs | 3 + src/StructId.Analyzer/KnownTypes.cs | 4 +- src/StructId.FunctionalTests/NoNs.cs | 3 +- .../StructId.FunctionalTests.csproj | 1 - .../UlidEntityFramework.cs | 2 +- .../StructId.Package.msbuildproj | 5 +- src/StructId.Package/StructId.props | 3 - src/StructId.Package/StructId.targets | 78 ++++--------------- src/StructId.Tests/StructId.Tests.csproj | 2 - 10 files changed, 25 insertions(+), 79 deletions(-) delete mode 100644 src/StructId.Package/StructId.props diff --git a/readme.md b/readme.md index c4d62ca..e936fc4 100644 --- a/readme.md +++ b/readme.md @@ -41,9 +41,6 @@ The package is a [development dependency](https://github.com/NuGet/Home/wiki/Dev meaning it will not add any run-time dependencies to your project (or package if you publish one that uses struct ids). -The default target namespace for the included types will match the `RootNamespace` of the -project, but can be customized by setting the `StructIdNamespace` property. - You can simply declare a new ID type by implementing `IStructId`: ```csharp diff --git a/src/StructId.Analyzer/CodeTemplate.cs b/src/StructId.Analyzer/CodeTemplate.cs index 1a3ee38..1518193 100644 --- a/src/StructId.Analyzer/CodeTemplate.cs +++ b/src/StructId.Analyzer/CodeTemplate.cs @@ -53,6 +53,9 @@ public static SyntaxNode Apply(this SyntaxNode node, INamedTypeSymbol structId) var tid = iface.TypeArguments.FirstOrDefault()?.ToFullName() ?? "string"; var corens = iface.ContainingNamespace.ToFullName(); + if (string.IsNullOrEmpty(corens)) + corens = nameof(StructId); + var targetNamespace = structId.ContainingNamespace != null && !structId.ContainingNamespace.IsGlobalNamespace ? structId.ContainingNamespace.ToDisplayString() : null; diff --git a/src/StructId.Analyzer/KnownTypes.cs b/src/StructId.Analyzer/KnownTypes.cs index 2b92880..cb2cea3 100644 --- a/src/StructId.Analyzer/KnownTypes.cs +++ b/src/StructId.Analyzer/KnownTypes.cs @@ -20,13 +20,13 @@ public record KnownTypes(Compilation Compilation) /// StructId.IStructId /// public INamedTypeSymbol? IStructId { get; } = Compilation - .GetAllTypes(true) + .GetAllTypes(includeReferenced: true) .FirstOrDefault(x => x.MetadataName == "IStructId" && x.IsGeneratedByStructId()); /// /// StructId.IStructId{T} /// public INamedTypeSymbol? IStructIdT { get; } = Compilation - .GetAllTypes(true) + .GetAllTypes(includeReferenced: true) .FirstOrDefault(x => x.MetadataName == "IStructId`1" && x.IsGeneratedByStructId()); } \ No newline at end of file diff --git a/src/StructId.FunctionalTests/NoNs.cs b/src/StructId.FunctionalTests/NoNs.cs index a0c3559..23c956b 100644 --- a/src/StructId.FunctionalTests/NoNs.cs +++ b/src/StructId.FunctionalTests/NoNs.cs @@ -1,4 +1,5 @@ -using StructId.Functional; +using StructId; +using StructId.Functional; // Showcases that types don't need to have a namespace public partial record struct NoNsId : IStructId; diff --git a/src/StructId.FunctionalTests/StructId.FunctionalTests.csproj b/src/StructId.FunctionalTests/StructId.FunctionalTests.csproj index 3c2a8dd..205fc8c 100644 --- a/src/StructId.FunctionalTests/StructId.FunctionalTests.csproj +++ b/src/StructId.FunctionalTests/StructId.FunctionalTests.csproj @@ -1,5 +1,4 @@  - net8.0 diff --git a/src/StructId.FunctionalTests/UlidEntityFramework.cs b/src/StructId.FunctionalTests/UlidEntityFramework.cs index 7e94b3b..e136bdd 100644 --- a/src/StructId.FunctionalTests/UlidEntityFramework.cs +++ b/src/StructId.FunctionalTests/UlidEntityFramework.cs @@ -2,7 +2,7 @@ #nullable enable using Microsoft.EntityFrameworkCore.Storage.ValueConversion; -using StructId.Functional; +using StructId; [TStructId] file partial record struct TSelf(Ulid Value) : INewable diff --git a/src/StructId.Package/StructId.Package.msbuildproj b/src/StructId.Package/StructId.Package.msbuildproj index 25a661d..cd7cd65 100644 --- a/src/StructId.Package/StructId.Package.msbuildproj +++ b/src/StructId.Package/StructId.Package.msbuildproj @@ -5,7 +5,8 @@ true Stronly typed ids using readonly record structs and modern C# features. dotnet record struct typed id - build + buildTransitive + true @@ -15,7 +16,7 @@ - + diff --git a/src/StructId.Package/StructId.props b/src/StructId.Package/StructId.props deleted file mode 100644 index 4de98b5..0000000 --- a/src/StructId.Package/StructId.props +++ /dev/null @@ -1,3 +0,0 @@ - - - \ No newline at end of file diff --git a/src/StructId.Package/StructId.targets b/src/StructId.Package/StructId.targets index 3e8a234..3b4a04e 100644 --- a/src/StructId.Package/StructId.targets +++ b/src/StructId.Package/StructId.targets @@ -1,50 +1,21 @@ - - $(RootNamespace) - StructId - $([MSBuild]::StableStringHash($(StructIdNamespace)))\ - - - - + + + false + StructId\%(Filename)%(Extension) + + + + - - - - $(IntermediateOutputPath)%(Link) - - - - - - - - - %(FullPath) - - - - - - - $([System.IO.File]::ReadAllText(%(StructId.FullPath))) - $(StructIdContent.Replace('using StructId', 'using $(StructIdNamespace)').Replace('namespace StructId', 'namespace $(StructIdNamespace)')) - - - - $([MSBuild]::Unescape($(StructIdContent))) - - - - - - - - - + false @@ -62,29 +33,8 @@ - + - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/StructId.Tests/StructId.Tests.csproj b/src/StructId.Tests/StructId.Tests.csproj index 756b75c..dd5faaa 100644 --- a/src/StructId.Tests/StructId.Tests.csproj +++ b/src/StructId.Tests/StructId.Tests.csproj @@ -1,7 +1,5 @@  - - net8.0 enable