Skip to content

Commit

Permalink
Source Generator Foundations no longer uses it's own source generator
Browse files Browse the repository at this point in the history
This was causing a whole bunch of build issues and the complexity was not really needed. Now we just add loose source files to the build process
  • Loading branch information
ByronMayne committed Nov 17, 2023
1 parent 4db82ad commit d7a014e
Show file tree
Hide file tree
Showing 18 changed files with 162 additions and 176 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
<ItemGroup>
<ProjectReference Include="..\..\SourceGenerator.Foundations.Contracts\SourceGenerator.Foundations.Contracts.csproj" />
</ItemGroup>
<Import Project="$(MSBuildThisFileDirectory)..\..\SourceGenerator.Foundations\SourceGenerator.Embedding.props" />
<Import Project="..\..\SourceGenerator.Foundations.Shared\SourceGenerator.Foundations.Shared.projitems" Label="Shared" />
<Import Project="..\..\SourceGenerator.Foundations\SourceGenerator.Foundations.targets"/>
</Project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\SourceGenerator.Foundations\SourceGenerator.Foundations.props" />
<PropertyGroup>
<Nullable>enable</Nullable>
<LangVersion>9.0</LangVersion>
Expand All @@ -12,5 +13,9 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="Serilog" Version="3.1.1" />
</ItemGroup>
<Import Project="..\..\SourceGenerator.Foundations\SourceGenerator.Foundations.props" />
<ItemGroup>
<ProjectReference Include="..\..\Plugins\SourceGenerator.Foundations.Windows\SourceGenerator.Foundations.Windows.csproj" />
</ItemGroup>
<Import Project="..\..\SourceGenerator.Foundations.Shared\SourceGenerator.Foundations.Shared.projitems" Label="Shared" />
<Import Project="..\..\SourceGenerator.Foundations\SourceGenerator.Foundations.targets" />
</Project>
7 changes: 0 additions & 7 deletions src/Sandbox/ConsoleApp/ConsoleApp.csproj
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<None Include="..\.editorconfig" Link=".editorconfig" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ConsoleApp.SourceGenerator\ConsoleApp.SourceGenerator.csproj">
<OutputItemType>Analyzer</OutputItemType>
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
</ProjectReference>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ string assemblyVersion

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
AssemblyName windowsAssemblyName = new AssemblyName($"SourceGenerator.Foundations.Windows, Version={assemblyVersion}, Culture=neutral, PublicKeyToken=null");
AssemblyName windowsAssemblyName = new AssemblyName("SourceGenerator.Foundations.Windows");
environmentAssembly = Assembly.Load(windowsAssemblyName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@
using Microsoft.CodeAnalysis;
using Serilog;
using Serilog.Core;
using SGF.Reflection;
using System;
using System.Diagnostics;
using System.Runtime.ExceptionServices;

namespace SGF
{
/// <summary>
/// Used as a base class for creating your own source generator. This class provides some helper
/// methods and impoved debugging expereince.
/// </summary>
internal abstract class IncrementalGenerator : IIncrementalGenerator, IDisposable
public abstract class IncrementalGenerator : IIncrementalGenerator, IDisposable
{
protected static readonly AppDomain s_currentDomain;

Expand All @@ -31,7 +29,6 @@ internal abstract class IncrementalGenerator : IIncrementalGenerator, IDisposabl
static IncrementalGenerator()
{
s_currentDomain = AppDomain.CurrentDomain;
AssemblyResolver.Initialize();
}

/// <summary>
Expand All @@ -56,7 +53,13 @@ protected IncrementalGenerator(string? name)
/// Override to add logic for disposing this instance and free resources
/// </summary>
protected virtual void Dipose()
{}
{
if(Debugger.IsAttached && Environment.UserInteractive)
{
Console.WriteLine("Press any key to quit...");
Console.ReadKey();
}
}

/// <summary>
/// Attaches the debugger automtically if you are running from Visual Studio. You have the option
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace SGF
/// Middleware wrapper around a <see cref="IncrementalGeneratorInitializationContext"/> to allow for
/// wraping with exception handling and provide a better user expereince
/// </summary>
internal struct SgfInitializationContext
public struct SgfInitializationContext
{
private readonly Action<Exception> m_exceptionHandler;
private IncrementalGeneratorInitializationContext m_context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,14 @@
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
</ItemGroup>
<Import Project="..\SourceGenerator.Foundations\SourceGenerator.Embedding.props" />
<ItemGroup>
<Compile Update="IncrementalGenerator.cs">
<PackagePath>src/IncrementalGenerator.cs</PackagePath>
<Pack>True</Pack>
</Compile>
<Compile Update="SgfInitializationContext.cs">
<PackagePath>src/SgfInitializationContext.cs</PackagePath>
<Pack>True</Pack>
</Compile>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Collections.Generic;
using System.Reflection;

namespace SGF.Reflection
{
/// <summary>
/// Used to compare two <see cref="AssemblyName"/> to pull them out of the dictionary of types
/// </summary>
internal class AssemblyNameComparer : IEqualityComparer<AssemblyName>
{
public bool Equals(AssemblyName x, AssemblyName y)
{
return string.Equals(GetName(x), GetName(y));
}

public int GetHashCode(AssemblyName obj)
{
return GetName(obj).GetHashCode();
}

private static string GetName(AssemblyName assemblyName)
{
string name = assemblyName.Name;
int index = name.IndexOf(',');
return index <= 0
? name
: name.Substring(0, index);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,12 @@
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;

namespace SGF.Reflection
{
internal static class AssemblyResolver
{
/// <summary>
/// Used to compare two <see cref="AssemblyName"/> to pull them out of the dictionary of types
/// </summary>
private class AssemblyNameComparer : IEqualityComparer<AssemblyName>
{
public bool Equals(AssemblyName x, AssemblyName y)
{
return string.Equals(x.Name, y.Name);
}

public int GetHashCode(AssemblyName obj)
{
return obj.Name.GetHashCode();
}
}

private static readonly ConcurrentBag<Assembly> s_assembliesWithResources;
private static readonly Dictionary<AssemblyName, Assembly> s_loadedAssemblies;

Expand All @@ -36,6 +21,7 @@ static AssemblyResolver()
s_loadedAssemblies = new Dictionary<AssemblyName, Assembly>(new AssemblyNameComparer());
}

[ModuleInitializer]
internal static void Initialize()
{
// The assembly resolvers get added to multiple source generators
Expand Down Expand Up @@ -82,9 +68,21 @@ private static void AddAssembly(Assembly assembly)
}
s_loadedAssemblies.Add(assemblyName, assembly);

if (!assembly.IsDynamic && assembly.GetManifestResourceNames().Any(r => r.StartsWith(ResourceConfiguration.AssemblyResourcePrefix)))
if (assembly.IsDynamic) return;

string[] resources = assembly.GetManifestResourceNames()
.Where(r => r.StartsWith(ResourceConfiguration.AssemblyResourcePrefix))
.ToArray();

if (resources.Length == 0) return;

foreach (string resource in resources)
{
s_assembliesWithResources.Add(assembly);
Console.WriteLine($"Extracting: {resource}");
if (TryExtractingAssembly(assembly, resource, out Assembly? loadedAssembly))
{
AddAssembly(loadedAssembly!);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ namespace System.Runtime.CompilerServices
/// the problem for bootstrapping without user intervention
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public sealed class ModuleInitializerAttribute : Attribute { }
internal sealed class ModuleInitializerAttribute : Attribute { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<MSBuildAllProjects Condition="'$(MSBuildVersion)' == '' Or '$(MSBuildVersion)' &lt; '16.0'">$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
<HasSharedItems>true</HasSharedItems>
<SharedGUID>8af3630c-2bf5-4854-a45d-0074c2787964</SharedGUID>
</PropertyGroup>
<PropertyGroup Label="Configuration">
<Import_RootNamespace>SGF</Import_RootNamespace>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)Configuration\ResourceConfiguration.cs" Pack="True" PackagePath="sgf/src/Configuration\ResourceConfiguration.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Reflection\AssemblyNameComparer.cs" Pack="True" PackagePath="sgf/src/Reflection\AssemblyNameComparer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Reflection\AssemblyResolver.cs" Pack="True" PackagePath="sgf/src/Reflection\AssemblyResolver.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Runtime\ModuleInitializerAttribute.cs" Pack="True" PackagePath="sgf/src/Runtime\ModuleInitializerAttribute.cs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Label="Globals">
<ProjectGuid>8af3630c-2bf5-4854-a45d-0074c2787964</ProjectGuid>
<MinimumVisualStudioVersion>14.0</MinimumVisualStudioVersion>
</PropertyGroup>
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.Default.props" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.props" />
<PropertyGroup />
<Import Project="SourceGenerator.Foundations.Shared.projitems" Label="Shared" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.CSharp.targets" />
</Project>
30 changes: 30 additions & 0 deletions src/SourceGenerator.Foundations.sln
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SourceGenerator.Foundations
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Sandbox", "Sandbox", "{6118BF32-23BA-4D33-946E-F7E8A6F5D758}"
EndProject
Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "SourceGenerator.Foundations.Shared", "SourceGenerator.Foundations.Shared\SourceGenerator.Foundations.Shared.shproj", "{8AF3630C-2BF5-4854-A45D-0074C2787964}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleApp", "Sandbox\ConsoleApp\ConsoleApp.csproj", "{F4BA95B9-0353-44CA-9502-C74B532321B7}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleApp.SourceGenerator", "Sandbox\ConsoleApp.SourceGenerator\ConsoleApp.SourceGenerator.csproj", "{594AACB5-B550-46CF-B6E2-16EF826D655A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -51,14 +57,38 @@ Global
{DC8C5A1A-6269-4BA7-852A-D21CB0B2B5A0}.Release|Any CPU.Build.0 = Release|Any CPU
{DC8C5A1A-6269-4BA7-852A-D21CB0B2B5A0}.Release|x64.ActiveCfg = Release|Any CPU
{DC8C5A1A-6269-4BA7-852A-D21CB0B2B5A0}.Release|x64.Build.0 = Release|Any CPU
{F4BA95B9-0353-44CA-9502-C74B532321B7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F4BA95B9-0353-44CA-9502-C74B532321B7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F4BA95B9-0353-44CA-9502-C74B532321B7}.Debug|x64.ActiveCfg = Debug|Any CPU
{F4BA95B9-0353-44CA-9502-C74B532321B7}.Debug|x64.Build.0 = Debug|Any CPU
{F4BA95B9-0353-44CA-9502-C74B532321B7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F4BA95B9-0353-44CA-9502-C74B532321B7}.Release|Any CPU.Build.0 = Release|Any CPU
{F4BA95B9-0353-44CA-9502-C74B532321B7}.Release|x64.ActiveCfg = Release|Any CPU
{F4BA95B9-0353-44CA-9502-C74B532321B7}.Release|x64.Build.0 = Release|Any CPU
{594AACB5-B550-46CF-B6E2-16EF826D655A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{594AACB5-B550-46CF-B6E2-16EF826D655A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{594AACB5-B550-46CF-B6E2-16EF826D655A}.Debug|x64.ActiveCfg = Debug|x64
{594AACB5-B550-46CF-B6E2-16EF826D655A}.Debug|x64.Build.0 = Debug|x64
{594AACB5-B550-46CF-B6E2-16EF826D655A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{594AACB5-B550-46CF-B6E2-16EF826D655A}.Release|Any CPU.Build.0 = Release|Any CPU
{594AACB5-B550-46CF-B6E2-16EF826D655A}.Release|x64.ActiveCfg = Release|x64
{594AACB5-B550-46CF-B6E2-16EF826D655A}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{DC8C5A1A-6269-4BA7-852A-D21CB0B2B5A0} = {A651676B-FBDB-4710-B010-D05AF3A56084}
{F4BA95B9-0353-44CA-9502-C74B532321B7} = {6118BF32-23BA-4D33-946E-F7E8A6F5D758}
{594AACB5-B550-46CF-B6E2-16EF826D655A} = {6118BF32-23BA-4D33-946E-F7E8A6F5D758}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {EDB10920-970A-43F9-A2B3-7F1270DD477B}
EndGlobalSection
GlobalSection(SharedMSBuildProjectFiles) = preSolution
SourceGenerator.Foundations.Shared\SourceGenerator.Foundations.Shared.projitems*{0085a518-02b6-4218-b718-00c0f69b99ab}*SharedItemsImports = 5
SourceGenerator.Foundations.Shared\SourceGenerator.Foundations.Shared.projitems*{594aacb5-b550-46cf-b6e2-16ef826d655a}*SharedItemsImports = 5
SourceGenerator.Foundations.Shared\SourceGenerator.Foundations.Shared.projitems*{8af3630c-2bf5-4854-a45d-0074c2787964}*SharedItemsImports = 13
SourceGenerator.Foundations.Shared\SourceGenerator.Foundations.Shared.projitems*{dc8c5a1a-6269-4ba7-852a-d21cb0b2b5a0}*SharedItemsImports = 5
EndGlobalSection
EndGlobal

This file was deleted.

Loading

0 comments on commit d7a014e

Please sign in to comment.