Skip to content

Commit 3cdaac8

Browse files
committed
Remove additoinal complexity from older dynamic files
Since we don't emit customized versions of the APIs with [Obsolete], we can just add the files via MSBuild and things just work.
1 parent 3dbc259 commit 3cdaac8

10 files changed

+46
-144
lines changed

src/CodeAnalysis.Tests/AddServicesAnalyzerTests.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ public static void Main()
4343
{
4444
Sources =
4545
{
46-
StaticGenerator.AddServicesExtension,
47-
StaticGenerator.ServiceAttribute,
48-
StaticGenerator.ServiceAttributeT,
46+
ThisAssembly.Resources.AddServicesNoReflectionExtension.Text,
47+
ThisAssembly.Resources.ServiceAttribute.Text,
48+
ThisAssembly.Resources.ServiceAttribute_1.Text
4949
},
5050
ReferenceAssemblies = new ReferenceAssemblies(
5151
"net8.0",
@@ -89,9 +89,9 @@ public static void Main()
8989
{
9090
Sources =
9191
{
92-
StaticGenerator.AddServicesExtension,
93-
StaticGenerator.ServiceAttribute,
94-
StaticGenerator.ServiceAttributeT,
92+
ThisAssembly.Resources.AddServicesNoReflectionExtension.Text,
93+
ThisAssembly.Resources.ServiceAttribute.Text,
94+
ThisAssembly.Resources.ServiceAttribute_1.Text
9595
},
9696
ReferenceAssemblies = new ReferenceAssemblies(
9797
"net8.0",
@@ -130,9 +130,9 @@ public static void Main()
130130
{
131131
Sources =
132132
{
133-
StaticGenerator.AddServicesExtension,
134-
StaticGenerator.ServiceAttribute,
135-
StaticGenerator.ServiceAttributeT,
133+
ThisAssembly.Resources.AddServicesNoReflectionExtension.Text,
134+
ThisAssembly.Resources.ServiceAttribute.Text,
135+
ThisAssembly.Resources.ServiceAttribute_1.Text
136136
},
137137
ReferenceAssemblies = new ReferenceAssemblies(
138138
"net8.0",
@@ -182,9 +182,9 @@ public static void Main()
182182
{
183183
Sources =
184184
{
185-
StaticGenerator.AddServicesExtension,
186-
StaticGenerator.ServiceAttribute,
187-
StaticGenerator.ServiceAttributeT,
185+
ThisAssembly.Resources.AddServicesNoReflectionExtension.Text,
186+
ThisAssembly.Resources.ServiceAttribute.Text,
187+
ThisAssembly.Resources.ServiceAttribute_1.Text
188188
},
189189
ReferenceAssemblies = new ReferenceAssemblies(
190190
"net8.0",

src/CodeAnalysis.Tests/CodeAnalysis.Tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,8 @@
2121
<ProjectReference Include="..\DependencyInjection\DependencyInjection.csproj" AdditionalProperties="ILRepack=true" />
2222
</ItemGroup>
2323

24+
<ItemGroup>
25+
<EmbeddedCode Include="..\DependencyInjection\compile\*.cs" />
26+
</ItemGroup>
27+
2428
</Project>

src/CodeAnalysis.Tests/ConventionAnalyzerTests.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ public static void Main()
4545
{
4646
Sources =
4747
{
48-
StaticGenerator.AddServicesExtension,
49-
StaticGenerator.ServiceAttribute,
50-
StaticGenerator.ServiceAttributeT,
48+
ThisAssembly.Resources.AddServicesNoReflectionExtension.Text,
49+
ThisAssembly.Resources.ServiceAttribute.Text,
50+
ThisAssembly.Resources.ServiceAttribute_1.Text
5151
},
5252
ReferenceAssemblies = new ReferenceAssemblies(
5353
"net8.0",
@@ -89,9 +89,9 @@ public static void Main()
8989
{
9090
Sources =
9191
{
92-
StaticGenerator.AddServicesExtension,
93-
StaticGenerator.ServiceAttribute,
94-
StaticGenerator.ServiceAttributeT,
92+
ThisAssembly.Resources.AddServicesNoReflectionExtension.Text,
93+
ThisAssembly.Resources.ServiceAttribute.Text,
94+
ThisAssembly.Resources.ServiceAttribute_1.Text
9595
},
9696
ReferenceAssemblies = new ReferenceAssemblies(
9797
"net8.0",
@@ -136,9 +136,9 @@ public static void Main()
136136
{
137137
Sources =
138138
{
139-
StaticGenerator.AddServicesExtension,
140-
StaticGenerator.ServiceAttribute,
141-
StaticGenerator.ServiceAttributeT,
139+
ThisAssembly.Resources.AddServicesNoReflectionExtension.Text,
140+
ThisAssembly.Resources.ServiceAttribute.Text,
141+
ThisAssembly.Resources.ServiceAttribute_1.Text
142142
},
143143
ReferenceAssemblies = new ReferenceAssemblies(
144144
"net8.0",
@@ -192,9 +192,9 @@ public static void Main()
192192
},
193193
Sources =
194194
{
195-
StaticGenerator.AddServicesExtension,
196-
StaticGenerator.ServiceAttribute,
197-
StaticGenerator.ServiceAttributeT,
195+
ThisAssembly.Resources.AddServicesNoReflectionExtension.Text,
196+
ThisAssembly.Resources.ServiceAttribute.Text,
197+
ThisAssembly.Resources.ServiceAttribute_1.Text
198198
},
199199
ReferenceAssemblies = new ReferenceAssemblies(
200200
"net8.0",
@@ -252,9 +252,9 @@ public static void Main()
252252
},
253253
Sources =
254254
{
255-
StaticGenerator.AddServicesExtension,
256-
StaticGenerator.ServiceAttribute,
257-
StaticGenerator.ServiceAttributeT,
255+
ThisAssembly.Resources.AddServicesNoReflectionExtension.Text,
256+
ThisAssembly.Resources.ServiceAttribute.Text,
257+
ThisAssembly.Resources.ServiceAttribute_1.Text
258258
},
259259
ReferenceAssemblies = new ReferenceAssemblies(
260260
"net8.0",

src/DependencyInjection/CodeAnalysisExtensions.cs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,6 @@
33

44
static class CodeAnalysisExtensions
55
{
6-
/// <summary>
7-
/// Whether the current process is running in an IDE, either
8-
/// <see cref="IsVisualStudio"/> or <see cref="IsRider"/>.
9-
/// </summary>
10-
public static bool IsEditor => IsVisualStudio || IsRider;
11-
12-
/// <summary>
13-
/// Whether the current process is running as part of an active Visual Studio instance.
14-
/// </summary>
15-
public static bool IsVisualStudio =>
16-
Environment.GetEnvironmentVariable("ServiceHubLogSessionKey") != null ||
17-
Environment.GetEnvironmentVariable("VSAPPIDNAME") != null;
18-
19-
/// <summary>
20-
/// Whether the current process is running as part of an active Rider instance.
21-
/// </summary>
22-
public static bool IsRider =>
23-
Environment.GetEnvironmentVariable("RESHARPER_FUS_SESSION") != null ||
24-
Environment.GetEnvironmentVariable("IDEA_INITIAL_DIRECTORY") != null;
25-
266
/// <summary>
277
/// Gets whether the current build is a design-time build.
288
/// </summary>

src/DependencyInjection/DependencyInjection.csproj

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,10 @@
3434
<None Update="Devlooped.Extensions.DependencyInjection.props" CopyToOutputDirectory="PreserveNewest" PackFolder="build\netstandard2.0" />
3535
<None Update="Devlooped.Extensions.DependencyInjection.targets" CopyToOutputDirectory="PreserveNewest" PackFolder="build\netstandard2.0" />
3636
<None Include="compile\*.cs" CopyToOutputDirectory="PreserveNewest" PackFolder="build\netstandard2.0" />
37-
38-
<EmbeddedCode Include="compile\*.cs" />
39-
37+
4038
<ILRepackInclude Include="Microsoft.Bcl.HashCode" />
4139
</ItemGroup>
4240

43-
<Target Name="CopyEmbeddedCode" Inputs="@(EmbeddedCode)" Outputs="@(EmbeddedCode -> '$(IntermediateOutputPath)%(Filename).txt')">
44-
<Copy SourceFiles="@(EmbeddedCode)" DestinationFiles="@(EmbeddedCode -> '$(IntermediateOutputPath)%(Filename).txt')" SkipUnchangedFiles="true" />
45-
</Target>
46-
47-
<Target Name="AddEmbeddedResources" DependsOnTargets="CopyEmbeddedCode" BeforeTargets="SplitResourcesByCulture">
48-
<ItemGroup>
49-
<EmbeddedResource Include="@(EmbeddedCode -> '$(IntermediateOutputPath)%(Filename).txt')" Link="%(EmbeddedCode.Filename).txt" Type="Non-Resx" />
50-
</ItemGroup>
51-
</Target>
52-
5341
<Target Name="PokePackageVersion" BeforeTargets="GetPackageContents" DependsOnTargets="CopyFilesToOutputDirectory" Condition="'$(dotnet-nugetize)' == '' and Exists('$(OutputPath)\Devlooped.Extensions.DependencyInjection.props')">
5442
<XmlPoke XmlInputPath="$(OutputPath)\Devlooped.Extensions.DependencyInjection.props" Query="/Project/PropertyGroup/DevloopedExtensionsDependencyInjectionVersion" Value="$(PackageVersion)" />
5543
</Target>

src/DependencyInjection/Devlooped.Extensions.DependencyInjection.props

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
<DevloopedExtensionsDependencyInjectionVersion>42.42.42</DevloopedExtensionsDependencyInjectionVersion>
55
<AddServiceAttribute Condition="'$(AddServiceAttribute)' == ''">true</AddServiceAttribute>
66
<AddServicesExtension Condition="'$(AddServicesExtension)' == ''">true</AddServicesExtension>
7-
8-
<IsVisualStudio Condition="'$(ServiceHubLogSessionKey)' != '' or '$(VSAPPIDNAME)' != ''">true</IsVisualStudio>
9-
<IsRider Condition="'$(RESHARPER_FUS_SESSION)' != '' or '$(IDEA_INITIAL_DIRECTORY)' != ''"></IsRider>
10-
<IsEditor Condition="'$(IsVisualStudio)' == 'true' or '$(IsRider)' == 'true'">true</IsEditor>
117
</PropertyGroup>
128

139
</Project>

src/DependencyInjection/Devlooped.Extensions.DependencyInjection.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<CompilerVisibleProperty Include="DesignTimeBuild" />
1313
</ItemGroup>
1414

15-
<ItemGroup Condition="'$(IsEditor)' != 'true'">
15+
<ItemGroup>
1616
<Compile Include="$(MSBuildThisFileDirectory)compile\ServiceAttribute*.cs"
1717
Visible="false"
1818
PackageId="Devlooped.Extensions.DependencyInjection"

src/DependencyInjection/IncrementalGenerator.cs

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -60,29 +60,7 @@ record ServiceRegistration(int Lifetime, TypeSyntax? AssignableTo, string? FullN
6060

6161
public void Initialize(IncrementalGeneratorInitializationContext context)
6262
{
63-
var compilation = context.CompilationProvider.Select((compilation, _) =>
64-
{
65-
// Add missing types as needed since we depend on the static generator potentially and can't
66-
// rely on its sources being added.
67-
var parse = (CSharpParseOptions)compilation.SyntaxTrees.FirstOrDefault().Options;
68-
69-
if (compilation.GetTypeByMetadataName("Microsoft.Extensions.DependencyInjection.AddServicesNoReflectionExtension") is null)
70-
{
71-
compilation = compilation.AddSyntaxTrees(
72-
CSharpSyntaxTree.ParseText(ThisAssembly.Resources.AddServicesNoReflectionExtension.Text, parse));
73-
}
74-
75-
if (compilation.GetTypeByMetadataName("Microsoft.Extensions.DependencyInjection.ServiceAttribute") is null)
76-
{
77-
compilation = compilation.AddSyntaxTrees(
78-
CSharpSyntaxTree.ParseText(ThisAssembly.Resources.ServiceAttribute.Text, parse),
79-
CSharpSyntaxTree.ParseText(ThisAssembly.Resources.ServiceAttribute_1.Text, parse));
80-
}
81-
82-
return compilation;
83-
});
84-
85-
var types = compilation.Combine(context.AnalyzerConfigOptionsProvider).SelectMany((x, c) =>
63+
var types = context.CompilationProvider.Combine(context.AnalyzerConfigOptionsProvider).SelectMany((x, c) =>
8664
{
8765
(var compilation, var options) = x;
8866

@@ -214,7 +192,7 @@ bool IsExport(AttributeData attr)
214192
.Collect();
215193

216194
// Project matching service types to register with the given lifetime.
217-
var conventionServices = types.Combine(methodInvocations.Combine(compilation)).SelectMany((pair, cancellationToken) =>
195+
var conventionServices = types.Combine(methodInvocations.Combine(context.CompilationProvider)).SelectMany((pair, cancellationToken) =>
218196
{
219197
var (typeSymbol, (registrations, compilation)) = pair;
220198
var results = ImmutableArray.CreateBuilder<ServiceSymbol>();
@@ -242,7 +220,7 @@ bool IsExport(AttributeData attr)
242220
.SelectMany((tuple, _) => ImmutableArray.CreateRange([tuple.Item1, tuple.Item2]))
243221
.SelectMany((items, _) => items.Distinct().ToImmutableArray());
244222

245-
RegisterServicesOutput(context, finalServices, compilation);
223+
RegisterServicesOutput(context, finalServices, context.CompilationProvider);
246224
}
247225

248226
void RegisterServicesOutput(IncrementalGeneratorInitializationContext context, IncrementalValuesProvider<ServiceSymbol> services, IncrementalValueProvider<Compilation> compilation)
@@ -314,24 +292,7 @@ void ReportInconsistencies(SourceProductionContext context, ImmutableArray<Servi
314292
// target first (no args and wrong method name), in the predicate, before moving on to semantic analyis here.
315293

316294
var options = (CSharpParseOptions)invocation.SyntaxTree.Options;
317-
318295
var compilation = semanticModel.Compilation;
319-
320-
// Add missing types as needed since we depend on the static generator potentially and can't
321-
// rely on its sources being added.
322-
if (compilation.GetTypeByMetadataName("Microsoft.Extensions.DependencyInjection.AddServicesNoReflectionExtension") is null)
323-
{
324-
compilation = compilation.AddSyntaxTrees(
325-
CSharpSyntaxTree.ParseText(ThisAssembly.Resources.AddServicesNoReflectionExtension.Text, options));
326-
}
327-
328-
if (compilation.GetTypeByMetadataName("Microsoft.Extensions.DependencyInjection.ServiceAttribute") is null)
329-
{
330-
compilation = compilation.AddSyntaxTrees(
331-
CSharpSyntaxTree.ParseText(ThisAssembly.Resources.ServiceAttribute.Text, options),
332-
CSharpSyntaxTree.ParseText(ThisAssembly.Resources.ServiceAttribute_1.Text, options));
333-
}
334-
335296
var model = compilation.GetSemanticModel(invocation.SyntaxTree);
336297

337298
var symbolInfo = model.GetSymbolInfo(invocation);

src/DependencyInjection/StaticGenerator.cs

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/Directory.targets

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
<Project InitialTargets="SetLocalVersion">
22

3+
<Target Name="CopyEmbeddedCode" Inputs="@(EmbeddedCode)" Outputs="@(EmbeddedCode -> '$(IntermediateOutputPath)%(Filename).txt')">
4+
<Copy SourceFiles="@(EmbeddedCode)" DestinationFiles="@(EmbeddedCode -> '$(IntermediateOutputPath)%(Filename).txt')" SkipUnchangedFiles="true" />
5+
</Target>
6+
7+
<Target Name="AddEmbeddedResources" DependsOnTargets="CopyEmbeddedCode" BeforeTargets="SplitResourcesByCulture">
8+
<ItemGroup>
9+
<EmbeddedResource Include="@(EmbeddedCode -> '$(IntermediateOutputPath)%(Filename).txt')" Link="%(EmbeddedCode.Filename).txt" Type="Non-Resx" />
10+
</ItemGroup>
11+
</Target>
12+
313
<Target Name="SetLocalVersion" Condition="!$(CI)">
414
<GetVersion>
515
<Output TaskParameter="Version" PropertyName="Version" />

0 commit comments

Comments
 (0)