Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.NET 8 Migration #1103

Merged
merged 17 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Snap.Hutao/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ dotnet_naming_style.pascal_case.capitalization = pascal_case
dotnet_diagnostic.SA1629.severity = none
dotnet_diagnostic.SA1642.severity = none

dotnet_diagnostic.IDE0005.severity = warning
dotnet_diagnostic.IDE0060.severity = none
dotnet_diagnostic.IDE0290.severity = none

# SA1208: System using directives should be placed before other using directives
dotnet_diagnostic.SA1208.severity = none
Expand Down Expand Up @@ -322,6 +324,8 @@ dotnet_diagnostic.CA2227.severity = suggestion

# CA2251: 使用 “string.Equals”
dotnet_diagnostic.CA2251.severity = suggestion
csharp_style_prefer_primary_constructors = true:suggestion
dotnet_diagnostic.SA1010.severity = none

[*.vb]
#### 命名样式 ####
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ public InjectionAttribute(InjectAs injectAs)
public InjectionAttribute(InjectAs injectAs, Type interfaceType)
{
}

public object Key { get; set; }
}
""";
context.AddSource("Snap.Hutao.Core.DependencyInjection.Annotation.Attributes.g.cs", coreDependencyInjectionAnnotations);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license.

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Snap.Hutao.SourceGeneration.Primitive;
using System.Collections.Generic;
Expand Down Expand Up @@ -68,7 +69,8 @@ private static void GenerateDependencyPropertyImplementation(SourceProductionCon

string propertyName = (string)arguments[0].Value!;
string propertyType = arguments[1].Value!.ToString();
string defaultValue = GetLiteralString(arguments.ElementAtOrDefault(2)) ?? "default";
string defaultValue = arguments.ElementAtOrDefault(2).ToCSharpString() ?? "default";
defaultValue = defaultValue == "null" ? "default" : defaultValue;
string propertyChangedCallback = arguments.ElementAtOrDefault(3) is { IsNull: false } arg3 ? $", {arg3.Value}" : string.Empty;

string code;
Expand Down Expand Up @@ -125,25 +127,4 @@ partial class {{owner}}
production.AddSource($"{normalizedClassName}.{propertyName}.g.cs", code);
}
}

private static string? GetLiteralString(TypedConstant typedConstant)
{
if (typedConstant.IsNull)
{
return default;
}

if (typedConstant.Value is bool boolValue)
{
return boolValue ? "true" : "false";
}

string result = typedConstant.Value!.ToString();
if (string.IsNullOrEmpty(result))
{
return default;
}

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ private static void GenerateAddHttpClientsImplementation(SourceProductionContext
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.

using Snap.Hutao.Web.Hoyolab.DynamicSecret;
using System.Net.Http;

namespace Snap.Hutao.Core.DependencyInjection;
Expand All @@ -86,7 +85,7 @@ public static partial IServiceCollection AddHttpClients(this IServiceCollection

private static void FillUpWithAddHttpClient(StringBuilder sourceBuilder, SourceProductionContext production, ImmutableArray<GeneratorSyntaxContext2> contexts)
{
List<string> lines = new();
List<string> lines = [];
StringBuilder lineBuilder = new();

foreach (GeneratorSyntaxContext2 context in contexts.DistinctBy(c => c.Symbol.ToDisplayString()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static partial IServiceCollection AddInjections(this IServiceCollection s

private static void FillUpWithAddServices(StringBuilder sourceBuilder, SourceProductionContext production, ImmutableArray<GeneratorSyntaxContext2> contexts)
{
List<string> lines = new();
List<string> lines = [];
StringBuilder lineBuilder = new();

foreach (GeneratorSyntaxContext2 context in contexts.DistinctBy(c => c.Symbol.ToDisplayString()))
Expand All @@ -92,17 +92,29 @@ private static void FillUpWithAddServices(StringBuilder sourceBuilder, SourcePro
ImmutableArray<TypedConstant> arguments = injectionInfo.ConstructorArguments;

string injectAsName = arguments[0].ToCSharpString();
switch (injectAsName)

bool hasKey = injectionInfo.TryGetNamedArgumentValue("Key", out TypedConstant key);

switch (injectAsName, hasKey)
{
case InjectAsSingletonName:
case (InjectAsSingletonName, false):
lineBuilder.Append(" services.AddSingleton<");
break;
case InjectAsTransientName:
case (InjectAsSingletonName, true):
lineBuilder.Append(" services.AddKeyedSingleton<");
break;
case (InjectAsTransientName, false):
lineBuilder.Append(" services.AddTransient<");
break;
case InjectAsScopedName:
case (InjectAsTransientName, true):
lineBuilder.Append(" services.AddKeyedTransient<");
break;
case (InjectAsScopedName, false):
lineBuilder.Append(" services.AddScoped<");
break;
case (InjectAsScopedName, true):
lineBuilder.Append(" services.AddKeyedScoped<");
break;
default:
production.ReportDiagnostic(Diagnostic.Create(invalidInjectionDescriptor, context.Context.Node.GetLocation(), injectAsName));
break;
Expand All @@ -113,7 +125,14 @@ private static void FillUpWithAddServices(StringBuilder sourceBuilder, SourcePro
lineBuilder.Append($"{arguments[1].Value}, ");
}

lineBuilder.Append($"{context.Symbol.ToDisplayString()}>();");
if (hasKey)
{
lineBuilder.Append($"{context.Symbol.ToDisplayString()}>({key.ToCSharpString()});");
}
else
{
lineBuilder.Append($"{context.Symbol.ToDisplayString()}>();");
}

lines.Add(lineBuilder.ToString());
}
Expand Down
10 changes: 5 additions & 5 deletions src/Snap.Hutao/Snap.Hutao.SourceGeneration/JsonParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public static class JsonParser
public static T? FromJson<T>(this string json)
{
// Initialize, if needed, the ThreadStatic variables
propertyInfoCache ??= new Dictionary<Type, Dictionary<string, PropertyInfo>>();
fieldInfoCache ??= new Dictionary<Type, Dictionary<string, FieldInfo>>();
stringBuilder ??= new StringBuilder();
splitArrayPool ??= new Stack<List<string>>();
propertyInfoCache ??= [];
fieldInfoCache ??= [];
stringBuilder ??= new();
splitArrayPool ??= [];

// Remove all whitespace not within strings to make parsing simpler
stringBuilder.Length = 0;
Expand Down Expand Up @@ -99,7 +99,7 @@ private static int AppendUntilStringEnd(bool appendEscapeCharacter, int startIdx
// Splits { <value>:<value>, <value>:<value> } and [ <value>, <value> ] into a list of <value> strings
private static List<string> Split(string json)
{
List<string> splitArray = splitArrayPool!.Count > 0 ? splitArrayPool.Pop() : new List<string>();
List<string> splitArray = splitArrayPool!.Count > 0 ? splitArrayPool.Pop() : [];
splitArray.Clear();
if (json.Length == 2)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using Microsoft.CodeAnalysis;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Snap.Hutao.SourceGeneration.Primitive;
Expand All @@ -13,4 +14,19 @@ public static bool HasNamedArgumentWith<TValue>(this AttributeData data, string
{
return data.NamedArguments.Any(a => a.Key == key && predicate((TValue)a.Value.Value!));
}

public static bool TryGetNamedArgumentValue(this AttributeData data, string key, out TypedConstant value)
{
foreach (KeyValuePair<string, TypedConstant> pair in data.NamedArguments)
{
if (pair.Key == key)
{
value = pair.Value;
return true;
}
}

value = default;
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ private static IEnumerable<TSource> DistinctByIterator<TSource, TKey>(IEnumerabl

if (enumerator.MoveNext())
{
HashSet<TKey> set = new();
HashSet<TKey> set = [];

do
{
Expand Down
Loading