Skip to content

Commit 1596f2d

Browse files
authored
updated source generator to netstandard2.0 (#352)
1 parent dc91f76 commit 1596f2d

File tree

5 files changed

+35
-22
lines changed

5 files changed

+35
-22
lines changed

src/TickerQ.SourceGenerator/TickerQ.SourceGenerator.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3+
<TargetFramework>netstandard2.0</TargetFramework>
4+
<LangVersion>8.0</LangVersion>
35
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
46
<AssemblyName>TickerQ.SourceGenerator</AssemblyName>
57
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
@@ -11,5 +13,6 @@
1113
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1214
</PackageReference>
1315
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="[4.10.0,)"/>
16+
<PackageReference Include="System.Memory" Version="4.5.5" />
1417
</ItemGroup>
1518
</Project>

src/TickerQ.SourceGenerator/TickerQIncrementalSourceGenerator.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -317,14 +317,14 @@ private static HashSet<string> DetectTypeNameConflicts(List<string> fullTypeName
317317
continue;
318318

319319
var simpleName = fullTypeName.Contains('.') ?
320-
fullTypeName[(fullTypeName.LastIndexOf('.') + 1)..] :
320+
fullTypeName.Substring(fullTypeName.LastIndexOf('.') + 1) :
321321
fullTypeName;
322322

323323
simpleNameCounts[simpleName] = simpleNameCounts.TryGetValue(simpleName, out var count) ? count + 1 : 1;
324324
}
325325

326326
// Return simple names that have conflicts (count > 1)
327-
return [..simpleNameCounts.Where(kv => kv.Value > 1).Select(kv => kv.Key)];
327+
return new HashSet<string>(simpleNameCounts.Where(kv => kv.Value > 1).Select(kv => kv.Key));
328328
}
329329

330330

@@ -712,10 +712,10 @@ private static string GenerateServiceResolution(
712712
{
713713
var name = attr.AttributeClass?.Name;
714714
var fullName = attr.AttributeClass?.ToDisplayString();
715-
return name == "FromKeyedServicesAttribute" ||
715+
return name == SourceGeneratorConstants.FromKeyedServicesAttributeName ||
716716
name == "FromKeyedServices" ||
717717
fullName == "Microsoft.Extensions.DependencyInjection.FromKeyedServicesAttribute" ||
718-
fullName?.EndsWith("FromKeyedServicesAttribute") == true;
718+
fullName?.EndsWith(SourceGeneratorConstants.FromKeyedServicesAttributeName) == true;
719719
});
720720

721721
if (keyedServiceAttribute != null)

src/TickerQ.SourceGenerator/Utilities/SourceGeneratorConstants.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23

34
namespace TickerQ.SourceGenerator.Utilities
45
{
@@ -30,17 +31,17 @@ internal static class SourceGeneratorConstants
3031
public const int InitialStringBuilderCapacity = 8192; // Pre-allocate reasonable capacity
3132

3233
public static readonly string[] CommonNamespaces =
33-
[
34+
{
3435
"System",
3536
"System.Collections.Generic",
3637
"System.Threading",
3738
"System.Threading.Tasks",
3839
"Microsoft.Extensions.DependencyInjection"
39-
];
40+
};
4041

4142
// Pre-computed common variable names for performance (static readonly for better memory usage)
42-
public static readonly System.Collections.Generic.HashSet<string> CommonVariableNames =
43-
new(StringComparer.Ordinal)
43+
public static readonly HashSet<string> CommonVariableNames =
44+
new HashSet<string>(StringComparer.Ordinal)
4445
{
4546
"context", "service", "serviceProvider", "tickerFunctionDelegateDict",
4647
"cancellationToken", "genericContext", "requestTypes", "args",

src/TickerQ.SourceGenerator/Utilities/SourceGeneratorUtilities.cs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -103,18 +103,27 @@ public static string GetServiceKey(AttributeData keyedServiceAttribute)
103103
/// </summary>
104104
private static string FormatServiceKeyValue(object value, ITypeSymbol type)
105105
{
106-
return value switch
106+
switch (value)
107107
{
108-
double d => d.ToString("G", System.Globalization.CultureInfo.InvariantCulture) + "D",
109-
float f => f.ToString("G", System.Globalization.CultureInfo.InvariantCulture) + "F",
110-
decimal m => m.ToString("G", System.Globalization.CultureInfo.InvariantCulture) + "M",
111-
long l => l.ToString(System.Globalization.CultureInfo.InvariantCulture) + "L",
112-
uint ui => ui.ToString(System.Globalization.CultureInfo.InvariantCulture) + "U",
113-
ulong ul => ul.ToString(System.Globalization.CultureInfo.InvariantCulture) + "UL",
114-
char c => $"'{c}'",
115-
bool b => b ? "true" : "false",
116-
_ => value.ToString(),
117-
};
108+
case double d:
109+
return d.ToString("G", System.Globalization.CultureInfo.InvariantCulture) + "D";
110+
case float f:
111+
return f.ToString("G", System.Globalization.CultureInfo.InvariantCulture) + "F";
112+
case decimal m:
113+
return m.ToString("G", System.Globalization.CultureInfo.InvariantCulture) + "M";
114+
case long l:
115+
return l.ToString(System.Globalization.CultureInfo.InvariantCulture) + "L";
116+
case uint ui:
117+
return ui.ToString(System.Globalization.CultureInfo.InvariantCulture) + "U";
118+
case ulong ul:
119+
return ul.ToString(System.Globalization.CultureInfo.InvariantCulture) + "UL";
120+
case char c:
121+
return $"'{c}'";
122+
case bool b:
123+
return b ? "true" : "false";
124+
default:
125+
return value.ToString();
126+
}
118127
}
119128

120129
/// <summary>
@@ -158,7 +167,7 @@ private static string GenerateTypeAlias(string fullTypeName, HashSet<string> use
158167
{
159168
// Extract the simple type name from the end
160169
var parts = fullTypeName.Split('.');
161-
var simpleName = parts[^1]; // Last part
170+
var simpleName = parts[parts.Length - 1]; // Last part
162171

163172
// If the simple name is unique, use it
164173
if (!usedAliases.Contains(simpleName))
@@ -169,7 +178,7 @@ private static string GenerateTypeAlias(string fullTypeName, HashSet<string> use
169178
// If not unique, try using just the parent namespace first (cleaner)
170179
if (parts.Length > 1)
171180
{
172-
var parentName = parts[^2];
181+
var parentName = parts[parts.Length - 2];
173182
var parentAlias = $"{parentName}Alias";
174183
if (!usedAliases.Contains(parentAlias))
175184
{

src/TickerQ/TickerQ.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
</Target>
2222

2323
<ItemGroup>
24-
<None Include="..\TickerQ.SourceGenerator\bin\$(Configuration)\net9.0\TickerQ.SourceGenerator.dll"
24+
<None Include="..\TickerQ.SourceGenerator\bin\$(Configuration)\netstandard2.0\TickerQ.SourceGenerator.dll"
2525
Pack="true"
2626
PackagePath="analyzers/dotnet/cs" />
2727
</ItemGroup>

0 commit comments

Comments
 (0)