Skip to content

Commit

Permalink
1.3.5 released
Browse files Browse the repository at this point in the history
  • Loading branch information
vipwan committed Aug 29, 2024
1 parent 5e9eb1d commit 0640de3
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net7.0;net8.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net6.0;net8.0;net9.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<PackageVersion>1.3.3</PackageVersion>
<PackageVersion>1.3.5</PackageVersion>
<PackageProjectUrl>https://github.com/vipwan/Biwen.AutoClassGen</PackageProjectUrl>
<Authors>万雅虎</Authors>
<RepositoryUrl>https://github.com/vipwan/Biwen.AutoClassGen</RepositoryUrl>
Expand All @@ -12,7 +12,7 @@
<Copyright>MIT</Copyright>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageTags>Roslyn,SourceGenerator,DTO,Mapper,Decorator,QuickApi</PackageTags>
<PackageReleaseNotes>AutoInject</PackageReleaseNotes>
<PackageReleaseNotes>built-in decorate extension</PackageReleaseNotes>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>SA1402,1573,1591,1712</NoWarn>
<Configuration Condition="'$(Configuration)' == ''">Debug</Configuration>
Expand All @@ -30,9 +30,7 @@
<PackageReference Include="Biwen.AutoClassGen" Version="1.3.2.3" PrivateAssets="contentfiles;analyzers" />
</ItemGroup>-->


<ItemGroup>
<PackageReference Include="Scrutor" Version="4.2.2" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
8 changes: 4 additions & 4 deletions Biwen.AutoClassGen.Gen/Biwen.AutoClassGen.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
<AnalysisLevel>6.0-all</AnalysisLevel>
<Authors>万雅虎</Authors>
<PackageVersion>1.3.3</PackageVersion>
<PackageVersion>1.3.5</PackageVersion>
</PropertyGroup>

<PropertyGroup>
Expand All @@ -24,7 +24,7 @@
<Copyright>MIT</Copyright>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageTags>Roslyn,SourceGenerator,QuickApi</PackageTags>
<PackageReleaseNotes>AutoInject support NET8 keyed service</PackageReleaseNotes>
<PackageReleaseNotes>built-in decorate extension</PackageReleaseNotes>
<PackageProjectUrl>https://github.com/vipwan/Biwen.AutoClassGen</PackageProjectUrl>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>1573,1591,1712,SA1309</NoWarn>
Expand All @@ -41,8 +41,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.11.0-3.final" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.11.0-3.final" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.12.0-1.final" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.12.0-1.final" PrivateAssets="all" />

<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556">
<PrivateAssets>all</PrivateAssets>
Expand Down
88 changes: 87 additions & 1 deletion Biwen.AutoClassGen.Gen/SourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -698,8 +698,14 @@ private static void HandleAnnotatedNodesDecor(Compilation compilation, Immutable
envStringBuilder.AppendLine("#pragma warning disable");
//envStringBuilder.AppendLine("namespace Microsoft.Extensions.DependencyInjection");
//envStringBuilder.AppendLine("{");
envStringBuilder.AppendLine("public static partial class AutoDecorExtension");
envStringBuilder.AppendLine("public static partial class AutoDecorExtensions");
envStringBuilder.AppendLine("{");

//append decorate method:

envStringBuilder.AppendLine(DecorateString);
envStringBuilder.AppendLine();

envStringBuilder.AppendLine("/// <summary>");
envStringBuilder.AppendLine("/// AddAutoDecor");
envStringBuilder.AppendLine("/// </summary>");
Expand All @@ -725,6 +731,86 @@ private static void HandleAnnotatedNodesDecor(Compilation compilation, Immutable

#region Template

/// <summary>
/// decorate method
/// </summary>
private const string DecorateString = """
/// <summary>
/// 装饰服务,<typeparamref name="TImpl"/>必须有一个构造函数接受<typeparamref name="TService"/>类型的参数
/// </summary>
/// <param name="services"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public static bool Decorate<TService, TImpl>(this IServiceCollection services, params object[] parameters)
{
var existingService = services.SingleOrDefault(s => s.ServiceType == typeof(TService));
if (existingService is null)
throw new ArgumentException($"No service of type {typeof(TService).Name} found.");
ServiceDescriptor? newService;
if (existingService.ImplementationType is not null)
{
newService = new ServiceDescriptor(existingService.ServiceType,
sp =>
{
TService inner =
(TService)ActivatorUtilities.CreateInstance(sp, existingService.ImplementationType);
if (inner is null)
throw new Exception(
$"Unable to instantiate decorated type via implementation type {existingService.ImplementationType.Name}.");
var parameters2 = new object[parameters.Length + 1];
Array.Copy(parameters, 0, parameters2, 1, parameters.Length);
parameters2[0] = inner;
return ActivatorUtilities.CreateInstance<TImpl>(sp, parameters2)!;
},
existingService.Lifetime);
}
else if (existingService.ImplementationInstance is not null)
{
newService = new ServiceDescriptor(existingService.ServiceType,
sp =>
{
TService inner = (TService)existingService.ImplementationInstance;
return ActivatorUtilities.CreateInstance<TImpl>(sp, inner, parameters)!;
},
existingService.Lifetime);
}
else if (existingService.ImplementationFactory is not null)
{
newService = new ServiceDescriptor(existingService.ServiceType,
sp =>
{
TService inner = (TService)existingService.ImplementationFactory(sp);
if (inner is null)
throw new Exception(
"Unable to instantiate decorated type via implementation factory.");
return ActivatorUtilities.CreateInstance<TImpl>(sp, inner, parameters)!;
},
existingService.Lifetime);
}
else
{
throw new Exception(
"Unable to instantiate decorated type.");
}
services.Remove(existingService);
services.Add(newService);
return true;
}
""";



private const string MapperTemplate = $@"
namespace $namespace
{{
Expand Down

0 comments on commit 0640de3

Please sign in to comment.