HavenDV / H.Generators.Extensions

A set of extensions to simplify the code of generators
MIT License
11 stars 3 forks source link

Project references support #15

Open valchetski opened 2 months ago

valchetski commented 2 months ago

I've tried to use your library as suggested in RicoSuter/NJsonSchema#1726. At first, amazing library đŸ™‚. It works like a charm for transient NuGet packages. However, I have one question and one issue with project references

Question

Does it support the automatic inclusion of referenced projects (also transient references) DLLs into the NuGet package?
For example, is it possible to avoid these lines and have these DLLs included automatically?

<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
<None Include="$(OutputPath)\NJsonSchema.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
<None Include="$(OutputPath)\NJsonSchema.Annotations.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
<None Include="$(OutputPath)\NJsonSchema.CodeGeneration.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
<None Include="$(OutputPath)\NJsonSchema.CodeGeneration.CSharp.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />

And avoid these for the case when Source Generator is used as project reference itself:

<PropertyGroup>
  <GetTargetPathDependsOn>
      $(GetTargetPathDependsOn);GetDependencyTargetPaths
  </GetTargetPathDependsOn>
</PropertyGroup>

<Target Name="GetDependencyTargetPaths">
  <ItemGroup>
      <TargetPathWithTargetPlatformMoniker Include="$(OutputPath)\NJsonSchema.dll" IncludeRuntimeDependency="false" />
      <TargetPathWithTargetPlatformMoniker Include="$(OutputPath)\NJsonSchema.Annotations.dll" IncludeRuntimeDependency="false" />
      <TargetPathWithTargetPlatformMoniker Include="$(OutputPath)\NJsonSchema.CodeGeneration.dll" IncludeRuntimeDependency="false" />
      <TargetPathWithTargetPlatformMoniker Include="$(OutputPath)\NJsonSchema.CodeGeneration.CSharp.dll" IncludeRuntimeDependency="false" />
  </ItemGroup>
</Target>

Issue

It appears that it somehow treats all project references as dependent NuGet packages.

Try to generate a NuGet package for the NJsonSchema.SourceGenerators.CSharp project from https://github.com/valchetski/NJsonSchema/tree/source-generator-h-generators Then try to install it and see that it gonna install all the packages. image

This is something I would like to disable. Or at least have it configurable.

If you do the same from https://github.com/valchetski/NJsonSchema/tree/source-generator branch (H.Generators.Extensions library is not installed there) then it'll install the bare minimum which is what I need: image Thanks in advance!

HavenDV commented 2 months ago

https://github.com/HavenDV/H.Generators.Extensions/blob/main/src/libs/H.Generators.Extensions/build/H.Generators.Extensions.props All the magic happens here. I'm not sure if you don't intersect with this file, I would try to remove GetDependencyTargetPaths/GetTargetPathDependsOn in your code and try to trust it completely. But I haven't tried testing this configuration when there is a non private package like Newtonsoft.Json here

I also tried releasing a new version of H.NSwag.Generator and it seems to still work correctly -

image

I also don't see any extra dependencies in the .nuspec file

HavenDV commented 2 months ago

The main problem with dependency code via PKG is transitive dependency version hell, because packages almost always use a non-latest version

valchetski commented 2 months ago

Getting this error after removal of GetDependencyTargetPaths/GetTargetPathDependsOn image Here is full build log:

Build started at 18:03...
1>------ Skipped Build: Project: _build, Configuration: Debug Any CPU ------
1>Project not selected to build for this solution configuration 
2>------ Build started: Project: NJsonSchema.SourceGenerators.CSharp, Configuration: Debug Any CPU ------
2>Added generation time reference: C:\Users\alexvolchetsky\.nuget\packages\fluid.core\2.9.0\lib\netstandard2.0\Fluid.dll
2>Added generation time reference: C:\Users\alexvolchetsky\.nuget\packages\h.generators.extensions\1.22.0\lib\netstandard2.0\H.Generators.Extensions.dll
2>Added generation time reference: C:\Users\alexvolchetsky\.nuget\packages\microsoft.extensions.fileproviders.abstractions\8.0.0\lib\netstandard2.0\Microsoft.Extensions.FileProviders.Abstractions.dll
2>Added generation time reference: C:\Users\alexvolchetsky\.nuget\packages\microsoft.extensions.primitives\8.0.0\lib\netstandard2.0\Microsoft.Extensions.Primitives.dll
2>Added generation time reference: C:\Users\alexvolchetsky\.nuget\packages\namotion.reflection\3.1.1\lib\netstandard2.0\Namotion.Reflection.dll
2>Added generation time reference: C:\Users\alexvolchetsky\.nuget\packages\newtonsoft.json\13.0.3\lib\netstandard2.0\Newtonsoft.Json.dll
2>Added generation time reference: C:\Users\alexvolchetsky\.nuget\packages\parlot\0.0.25\lib\netstandard2.0\Parlot.dll
2>Added generation time reference: C:\Users\alexvolchetsky\.nuget\packages\system.text.encodings.web\8.0.0\lib\netstandard2.0\System.Text.Encodings.Web.dll
2>Added generation time reference: C:\Users\alexvolchetsky\.nuget\packages\system.text.json\8.0.3\lib\netstandard2.0\System.Text.Json.dll
2>Added generation time reference: C:\Users\alexvolchetsky\.nuget\packages\timezoneconverter\6.1.0\lib\netstandard2.0\TimeZoneConverter.dll
2>NJsonSchema.SourceGenerators.CSharp -> D:\personal\njsonschema\artifacts\bin\NJsonSchema.SourceGenerators.CSharp\debug\NJsonSchema.SourceGenerators.CSharp.dll
3>------ Build started: Project: NJsonSchema.SourceGenerators.CSharp.Tests, Configuration: Debug Any CPU ------
4>------ Build started: Project: NJsonSchema.Demo, Configuration: Debug Any CPU ------
3>NJsonSchema.SourceGenerators.CSharp.Tests -> D:\personal\njsonschema\artifacts\bin\NJsonSchema.SourceGenerators.CSharp.Tests\debug\NJsonSchema.SourceGenerators.CSharp.Tests.dll
4>CSC : error CS8785: Generator 'JsonSchemaSourceGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'FileNotFoundException' with message 'Could not load file or assembly 'NJsonSchema.CodeGeneration.CSharp, Version=11.0.0.0, Culture=neutral, PublicKeyToken=c2f9c3bdfae56102' or one of its dependencies. The system cannot find the file specified.'.
4>Done building project "NJsonSchema.Demo.csproj" -- FAILED.
========== Build: 2 succeeded, 1 failed, 15 up-to-date, 1 skipped ==========
========== Build completed at 18:03 and took 14.423 seconds ==========
HavenDV commented 2 months ago

Add this also

    <None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />

H.NSwag.Generator.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <NoWarn>$(NoWarn);CA1031</NoWarn>
  </PropertyGroup>

  <PropertyGroup Label="NuGet">
    <NoWarn>$(NoWarn);NU5128</NoWarn>
    <DevelopmentDependency>true</DevelopmentDependency>
    <IncludeBuildOutput>false</IncludeBuildOutput>
    <IncludeSymbols>false</IncludeSymbols>
    <Description>C# 9 Source Generator library for NSwag.</Description>
    <PackageTags>nswag, generator, source generator, csharp 9</PackageTags>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="NSwag.CodeGeneration.CSharp" Version="14.1.0" PrivateAssets="all" />
    <PackageReference Include="NSwag.Core.Yaml" Version="14.1.0" PrivateAssets="all" />
    <PackageReference Include="NJsonSchema.CodeGeneration.CSharp" Version="11.0.2" PrivateAssets="all" />

    <None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
    <None Include="$(AssemblyName).props" Pack="true" PackagePath="build" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.3.1" PrivateAssets="all" />
    <PackageReference Include="H.Generators.Extensions" Version="1.22.0" PrivateAssets="all" />
  </ItemGroup>

</Project>
valchetski commented 2 months ago

It was already there https://github.com/valchetski/NJsonSchema/blob/a213136fad1e41c519bd2762d81767574bbacc83/src/NJsonSchema.SourceGenerators.CSharp/NJsonSchema.SourceGenerators.CSharp.csproj#L30.

I've pushed the latest changes after the removal of GetDependencyTargetPaths/GetTargetPathDependsOn to https://github.com/valchetski/NJsonSchema/tree/source-generator-h-generators. It'll fail during compilation with the same error mentioned above.