Skip to content

Commit

Permalink
Merge pull request #2 from CptWesley/sourcegenerator
Browse files Browse the repository at this point in the history
SourceGenerator Version
  • Loading branch information
MarkCiliaVincenti authored Nov 17, 2024
2 parents 882516f + c762b3e commit f4c0914
Show file tree
Hide file tree
Showing 12 changed files with 464 additions and 208 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net462;net8.0;net9.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<Using Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net9.0'))" Alias="Lock" Include="System.Threading.Lock" />
<Using Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net9.0'))" Alias="Lock" Include="Backport.System.Threading.Lock" />
<Using Alias="LockFactory" Include="Backport.System.Threading.LockFactory" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="FluentAssertions" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="System.Net.Http" />
<PackageReference Include="System.Text.RegularExpressions" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Backport.System.Threading.Lock\Backport.System.Threading.Lock.csproj" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Backport.System.Threading.Lock.SourceGenerator\Backport.System.Threading.Lock.SourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>

<ItemGroup>
<Compile Include="../Backport.System.Threading.Lock.PreNet5Tests/*.cs" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net8.0;net9.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="FluentAssertions" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="System.Net.Http" />
<PackageReference Include="System.Text.RegularExpressions" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Backport.System.Threading.Lock.SourceGenerator\Backport.System.Threading.Lock.SourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>

<ItemGroup>
<Compile Include="../Backport.System.Threading.Lock.Tests/*.cs" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<Authors>Mark Cilia Vincenti</Authors>
<RepositoryUrl>https://github.com/MarkCiliaVincenti/Backport.System.Threading.Lock.git</RepositoryUrl>
<PackageProjectUrl>https://github.com/MarkCiliaVincenti/Backport.System.Threading.Lock</PackageProjectUrl>
<Copyright>MIT</Copyright>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Version>2.0.7</Version>
<PackageIcon>logo.png</PackageIcon>
<PackageReleaseNotes>Final .NET 9.0 release.</PackageReleaseNotes>
<Description>A micro-library that backports/polyfills .NET 9.0+'s System.Threading.Lock to prior framework versions (from .NET Framework 3.5 up to .NET 8.0), providing as much backward compatibility as possible.</Description>
<Copyright>© 2024 Mark Cilia Vincenti</Copyright>
<PackageTags>System.Threading.Lock,lock,backport,polyfill,backward,compatible,compatibility,synchronization,synchronisation</PackageTags>
<RepositoryType>git</RepositoryType>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<AssemblyVersion>2.0.7.0</AssemblyVersion>
<FileVersion>2.0.7.0</FileVersion>
<PackageReadmeFile>README.md</PackageReadmeFile>
<IsPackable>true</IsPackable>
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<EnableNETAnalyzers>True</EnableNETAnalyzers>
<AnalysisLevel>latest</AnalysisLevel>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>Backport.System.Threading.Lock.SourceGenerator.snk</AssemblyOriginatorKeyFile>
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
<ImplicitUsings>false</ImplicitUsings>
</PropertyGroup>

<PropertyGroup Condition="$(DefineConstants.Contains(AOT_COMPATIBLE))">
<IsAotCompatible>true</IsAotCompatible>
<PublishAot>true</PublishAot>
</PropertyGroup>

<PropertyGroup Condition="$(DefineConstants.Contains(TRIMMING_COMPATIBLE))">
<PublishTrimmed>true</PublishTrimmed>
<TrimmerDefaultAction>link</TrimmerDefaultAction>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Optimize>true</Optimize>
</PropertyGroup>

<PropertyGroup>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
<DevelopmentDependency>true</DevelopmentDependency>
<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>

<ItemGroup>
<Compile Remove="Properties\PublishProfiles\**" />
<EmbeddedResource Remove="Properties\PublishProfiles\**" />
<None Remove="Properties\PublishProfiles\**" />
</ItemGroup>

<ItemGroup>
<None Include="..\README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
<None Include="..\logo.png">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
<None Include="..\CONTRIBUTORS.md">
<Pack>False</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="../Backport.System.Threading.Lock/Lock.cs" />
<EmbeddedResource Include="../Backport.System.Threading.Lock/LockFactory.cs" />
<EmbeddedResource Include="../Backport.System.Threading.Lock/PreNet5Lock.cs" />
<EmbeddedResource Include="../Backport.System.Threading.Lock/Properties/AssemblyInfo.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="DotNet.ReproducibleBuilds" PrivateAssets="All" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" PrivateAssets="All" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" PrivateAssets="All" />
</ItemGroup>
</Project>
Binary file not shown.
36 changes: 36 additions & 0 deletions Backport.System.Threading.Lock.SourceGenerator/Generator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Microsoft.CodeAnalysis;
using System.IO;
using System.Reflection;
using System.Text;

namespace Backport.System.Threading.Lock.SourceGenerator;

[Generator]
internal sealed class Generator : IIncrementalGenerator
{
private static readonly Assembly Assembly = typeof(Generator).Assembly;

public void Initialize(IncrementalGeneratorInitializationContext context)
=> context.RegisterPostInitializationOutput(Generate);

private static void Generate(IncrementalGeneratorPostInitializationContext context)
{
foreach (var resource in Assembly.GetManifestResourceNames())
{
Generate(context, resource);
}
}

private static void Generate(IncrementalGeneratorPostInitializationContext context, string resourceName)
{
using var stream = Assembly.GetManifestResourceStream(resourceName);
using var streamReader = new StreamReader(stream);
var text = streamReader.ReadToEnd();

var sb = new StringBuilder();
sb.AppendLine("#define SOURCE_GENERATOR");
sb.AppendLine(text);

context.AddSource(resourceName, sb.ToString());
}
}
18 changes: 18 additions & 0 deletions Backport.System.Threading.Lock.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Backport.System.Threading.Lock.PreNet5Tests", "Backport.System.Threading.Lock.PreNet5Tests\Backport.System.Threading.Lock.PreNet5Tests.csproj", "{768B4ACE-4D32-4103-AB50-FE9B7945A496}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Backport.System.Threading.Lock.SourceGenerator", "Backport.System.Threading.Lock.SourceGenerator\Backport.System.Threading.Lock.SourceGenerator.csproj", "{E94F628E-BFAC-4061-B45C-CC94A7B31E60}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Backport.System.Threading.Lock.SourceGenerator.Tests", "Backport.System.Threading.Lock.SourceGenerator.Tests\Backport.System.Threading.Lock.SourceGenerator.Tests.csproj", "{C4D3E64B-981F-4BDC-B429-063AFB17F6DD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Backport.System.Threading.Lock.SourceGenerator.PreNet5Tests", "Backport.System.Threading.Lock.SourceGenerator.PreNet5Tests\Backport.System.Threading.Lock.SourceGenerator.PreNet5Tests.csproj", "{F0FF2CD2-E1C5-4349-A6EA-A178772B4A37}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -39,6 +45,18 @@ Global
{768B4ACE-4D32-4103-AB50-FE9B7945A496}.Debug|Any CPU.Build.0 = Debug|Any CPU
{768B4ACE-4D32-4103-AB50-FE9B7945A496}.Release|Any CPU.ActiveCfg = Release|Any CPU
{768B4ACE-4D32-4103-AB50-FE9B7945A496}.Release|Any CPU.Build.0 = Release|Any CPU
{E94F628E-BFAC-4061-B45C-CC94A7B31E60}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E94F628E-BFAC-4061-B45C-CC94A7B31E60}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E94F628E-BFAC-4061-B45C-CC94A7B31E60}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E94F628E-BFAC-4061-B45C-CC94A7B31E60}.Release|Any CPU.Build.0 = Release|Any CPU
{C4D3E64B-981F-4BDC-B429-063AFB17F6DD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C4D3E64B-981F-4BDC-B429-063AFB17F6DD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C4D3E64B-981F-4BDC-B429-063AFB17F6DD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C4D3E64B-981F-4BDC-B429-063AFB17F6DD}.Release|Any CPU.Build.0 = Release|Any CPU
{F0FF2CD2-E1C5-4349-A6EA-A178772B4A37}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F0FF2CD2-E1C5-4349-A6EA-A178772B4A37}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F0FF2CD2-E1C5-4349-A6EA-A178772B4A37}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F0FF2CD2-E1C5-4349-A6EA-A178772B4A37}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>Backport.System.Threading.Lock.snk</AssemblyOriginatorKeyFile>
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
<ImplicitUsings>false</ImplicitUsings>
</PropertyGroup>

<PropertyGroup Condition="$(DefineConstants.Contains(AOT_COMPATIBLE))">
Expand Down
Loading

0 comments on commit f4c0914

Please sign in to comment.