Skip to content

Commit 1d2a861

Browse files
committed
Updated the host generation. Namespace collision avoidance for multiple project use. Also no longer emits when zero items are found in a project.
1 parent efd597d commit 1d2a861

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

EnumSourceGenerator/EnumSourceGenerator.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
<ItemGroup>
1414
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="4.14.0">
15-
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
15+
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
1616
<PrivateAssets>all</PrivateAssets>
1717
</PackageReference>
1818
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.14.0" />

EnumSourceGenerator/HostGenerator.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,17 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
2929
})
3030
.Where(s => s is not null)!;
3131

32+
33+
3234
var compilationAndClasses = context.CompilationProvider
3335
.Combine(classSymbols.Collect());
3436

3537
context.RegisterSourceOutput(compilationAndClasses, (spc, source) =>
3638
{
39+
if( source.Right.IsEmpty) return;
40+
41+
var projectName = source.Left.AssemblyName ?? "GeneratedProject";
42+
3743
var (compilation, classes) = source;
3844
var services = classes.OfType<(INamedTypeSymbol symbol, AttributeData attr)>().Distinct().ToList();
3945

@@ -44,7 +50,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
4450
sb.AppendLine("using System.Collections.Generic;");
4551
sb.AppendLine("using System.Reflection;");
4652
sb.AppendLine();
47-
sb.AppendLine("namespace SimpleInjection.Injection;");
53+
sb.AppendLine($"namespace {projectName}.Genenerated;");
4854
sb.AppendLine("public sealed class Host : IDisposable");
4955
sb.AppendLine("{");
5056
sb.AppendLine(" private readonly Dictionary<Type, object> _singletonInstances = new();");
@@ -64,10 +70,10 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
6470

6571
// Build dependency graph and sort services
6672
var serviceInfos = BuildServiceInfos(services, compilation);
67-
var sortedServices = TopologicalSort(serviceInfos);
73+
6874

6975
// Generate baked factories in dependency order
70-
foreach (var serviceInfo in sortedServices)
76+
foreach (var serviceInfo in TopologicalSort(serviceInfos))
7177
{
7278
GenerateFactory(sb, serviceInfo, compilation);
7379
}
@@ -82,7 +88,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
8288

8389
sb.AppendLine("}");
8490

85-
spc.AddSource("Host.Configure.g.cs", SourceText.From(sb.ToString(), Encoding.UTF8));
91+
spc.AddSource($"{projectName}.Host.Configure.g.cs", SourceText.From(sb.ToString(), Encoding.UTF8));
8692
});
8793
}
8894

SimpleInjection/SimpleInjection.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
1010
<PackageId>SimpleInjection</PackageId>
11-
<Version>0.9.8.2</Version>
11+
<Version>0.9.8.3</Version>
1212
<Authors>Derek Gooding</Authors>
1313
<Company>Derek Gooding</Company>
1414
<Description>

0 commit comments

Comments
 (0)