Skip to content

Commit

Permalink
chore: 更新 Directory.Build.props 文件中的 Version 属性为 1.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
239573049 committed Oct 21, 2024
1 parent ff4582f commit 5022969
Show file tree
Hide file tree
Showing 10 changed files with 16 additions and 52 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>

<PropertyGroup>
<Version>1.0.2</Version>
<Version>1.0.3</Version>
<Company>AIDotNet</Company>
<Authors>Token</Authors>
<Description>自动依赖注入,利用源生成器实现自动依赖注入,并且不使用反射进行实现功能。</Description>
Expand Down
2 changes: 1 addition & 1 deletion Gnarly.sln
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
Directory.Build.props = Directory.Build.props
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test", "src\Test\Test.csproj", "{B241F5DD-1A66-4E36-9B98-C7D4A5D5A1A8}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Gnarly.Application", "src\Gnarly.Application\Gnarly.Application.csproj", "{B241F5DD-1A66-4E36-9B98-C7D4A5D5A1A8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<RootNamespace>Test</RootNamespace>
</PropertyGroup>

<ItemGroup>
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class TestService : ITestService, ISingletonDependency
public async Task SendMessageAsync()
{
Console.WriteLine("Hello, Gnarly!");

await Task.CompletedTask;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Gnarly.Data/Gnarly.Data.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<LangVersion>latest</LangVersion>
</PropertyGroup>

</Project>
1 change: 0 additions & 1 deletion src/Gnarly.Data/ITransientDependency.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@
/// </summary>
public interface ITransientDependency
{

}
}
2 changes: 1 addition & 1 deletion src/Gnarly.WebApi/Gnarly.WebApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<ItemGroup>
<ProjectReference Include="..\Gnarly\Gnarly.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\Gnarly.Data\Gnarly.Data.csproj" />
<ProjectReference Include="..\Test\Test.csproj" />
<ProjectReference Include="..\Gnarly.Application\Gnarly.Application.csproj" />
</ItemGroup>


Expand Down
50 changes: 6 additions & 44 deletions src/Gnarly/ScanService.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace Gnarly;

Expand All @@ -15,45 +13,8 @@ public class ScanService
private static INamedTypeSymbol iSingletonDependencySymbol;
private static INamedTypeSymbol iTransientDependencySymbol;

/// <summary>
/// 扫描程序集中的所有类,找到实现了<see cref="IScopeDependency"/>、<see cref="ISingletonDependency"/>、<see cref="ITransientDependency"/>接口的类ConditionalExpressionSyntax conditionalExpression;
/// </summary>
/// <param name="compilation"></param>
/// <returns></returns>
public static IEnumerable<(int type, INamedTypeSymbol namedTypeSymbol)> ScanForDependencyTypes(Compilation compilation)
{
iScopeDependencySymbol ??= compilation.GetTypeByMetadataName("Gnarly.Data.IScopeDependency");
iSingletonDependencySymbol ??= compilation.GetTypeByMetadataName("Gnarly.Data.ISingletonDependency");
iTransientDependencySymbol ??= compilation.GetTypeByMetadataName("Gnarly.Data.ITransientDependency");

foreach (var syntaxTree in compilation.SyntaxTrees)
{
var semanticModel = compilation.GetSemanticModel(syntaxTree);
var root = syntaxTree.GetRoot();

var classes = root.DescendantNodes().OfType<ClassDeclarationSyntax>();

foreach (var @class in classes)
{
if (semanticModel.GetDeclaredSymbol(@class) is INamedTypeSymbol symbol)
{
if (symbol.AllInterfaces.Contains(iScopeDependencySymbol))
{
yield return (1, symbol);
}
else if (symbol.AllInterfaces.Contains(iSingletonDependencySymbol))
{
yield return (2, symbol);
}
else if (symbol.AllInterfaces.Contains(iTransientDependencySymbol))
{
yield return (3, symbol);
}
}
}
}
}
public static void ScanAndCollect(Compilation compilationContext, List<string> scopeMethods, List<string> singletonMethods, List<string> transientMethods)
public static void ScanAndCollect(Compilation compilationContext, List<string> scopeMethods,
List<string> singletonMethods, List<string> transientMethods)
{
var visitedAssemblies = new HashSet<IAssemblySymbol>(SymbolEqualityComparer.Default);

Expand All @@ -63,7 +24,8 @@ public static void ScanAndCollect(Compilation compilationContext, List<string> s

void ScanAssembly(IAssemblySymbol assemblySymbol)
{
if (visitedAssemblies.Contains(assemblySymbol) || assemblySymbol.Name.StartsWith("Microsoft") || assemblySymbol.Name.StartsWith("System"))
if (visitedAssemblies.Contains(assemblySymbol) || assemblySymbol.Name.StartsWith("Microsoft") ||
assemblySymbol.Name.StartsWith("System"))
{
return;
}
Expand Down Expand Up @@ -97,7 +59,7 @@ void ScanType(INamedTypeSymbol namedTypeSymbol)

if (registrationAttribute != null &&
registrationAttribute.ConstructorArguments.FirstOrDefault().Value as INamedTypeSymbol is
{ } registrationType && registrationType.TypeKind == TypeKind.Interface)
{ } registrationType && registrationType.TypeKind == TypeKind.Interface)
{
switch (type)
{
Expand Down
8 changes: 4 additions & 4 deletions src/Gnarly/ServiceRegistrationGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private void Execute(Compilation compilation, SourceProductionContext context)
var transientRegistrations = string.Join("\n", transientMethods.Select(m => $"services.AddTransient<{m}>();"));

// 生成源代码
string source = $@"// 这是由SourceGenerator自动生成的代码
string source = $@"// Gnarly自动生成的源代码,请勿手动修改
using System;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -45,9 +45,9 @@ public static class GnarlyExtensions
/// </summary>
public static IServiceCollection AddAutoGnarly(this IServiceCollection services)
{{
{scopeRegistrations}
{singletonRegistrations}
{transientRegistrations}
{scopeRegistrations.Trim()}
{singletonRegistrations.Trim()}
{transientRegistrations.Trim()}
return services;
}}
}}
Expand Down

0 comments on commit 5022969

Please sign in to comment.