MarkCiliaVincenti / Backport.System.Threading.Lock

A micro-library that backports/polyfills .NET 9.0+'s System.Threading.Lock to prior versions (from .NET Standard 2.0 up to .NET 8.0), providing as much similar functionality as possible.
https://www.nuget.org/packages/Backport.System.Threading.Lock
MIT License
11 stars 2 forks source link

SourceGenerator Version #2

Open CptWesley opened 2 days ago

CptWesley commented 2 days ago

Closes #1

Note that the project files now contain quite some duplication. I personally prefer organizing things in dedicated .props files, but I did not want to mess with your setup right now.

codecov[bot] commented 1 day ago

Welcome to Codecov :tada:

Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests.

Thanks for integrating Codecov - We've got you covered :open_umbrella:

MarkCiliaVincenti commented 1 day ago

Thank you @CptWesley! I will try to review it today. It will take some time because I've never worked on source generators before. I will run some tests with it, like implementing it in a project and try breaking it :)

PS: do you not sleep?!

MarkCiliaVincenti commented 1 day ago

Also, not sure if this can be done or not, but would it be possible to have just one project which can be used either as is or as a source generator rather than 2 NuGet packages?

CptWesley commented 1 day ago

Haha, I like to get straight to programming the moment I wake up in the morning, hence my early contribution.

I'm not entirely sure if it's possible to keep it as a single assembly, but inside the nuget package it is included as an analyzer. That's what this bit takes care of:

<PropertyGroup>
  <IncludeSymbols>false</IncludeSymbols>
  <IncludeBuildOutput>false</IncludeBuildOutput>
</PropertyGroup>

<ItemGroup Label="DependencyPackaging">
  <None Include="$(OutputPath)/netstandard2.0/$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
</ItemGroup>

It ensures that the assembly itself is not included, but it copies the assembly over to the analyzer/dotnet/cs output directory, which ensures it is registered as a roslyn analyzer project (source code generators are technically roslyn analyzers). So perhaps it would be possible to pack both the regular Backport.System.Threading.Lock dll and the Backport.System.Threading.Lock.SourceGenerator as an analyzer in the same .nupkg file. Then you would just need some way to flag whether or not it should produce the source files.

You can retrieve some dependency information during the analyzer step from the compilation unit. Perhaps it is possible to find out if the package is included in the output. If it is not included, inject the source code, otherwise don't and just use the runtime dependency.

But this is also delving into code generator territory that I have not ventured into myself... 😅

MarkCiliaVincenti commented 1 day ago

Excellent @CptWesley, I will take a look as soon as I find some time.

In the meantime, do you know if it's possible to also ensure net9.0 is being targeted? If it's a <TargetFramework>net8.0</TargetFramework> it would change it to <TargetFramework**s**>net8.0;net9.0</TargetFramework**s**> and <TargetFramework**s**>netstandard2.0;net8.0</TargetFramework**s**> would change it to <TargetFramework**s**>netstandard2.0;net8.0;net9.0</TargetFramework**s**>

CptWesley commented 1 day ago

@MarkCiliaVincenti I'm not entirely sure if I'm following what you are asking

MarkCiliaVincenti commented 1 day ago

@MarkCiliaVincenti I'm not entirely sure if I'm following what you are asking

What I would like, if possible, that if a project that uses this source generator is not targeting net9, it modifies the source of that project forcing it to multi target net9.

CptWesley commented 1 day ago

@MarkCiliaVincenti I don't think that is possible with roslyn analyzers