Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
chr233 committed Oct 14, 2024
1 parent d40a7d4 commit 180a4ef
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 104 deletions.
18 changes: 9 additions & 9 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
<ItemGroup>
<PackageVersion Include="ConfigureAwaitChecker.Analyzer" Version="5.0.0.1" />
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="7.0.13" />
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" />
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.Common" Version="4.11.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.11.0" />
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
<PackageVersion Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.1" />
<PackageVersion Include="Microsoft.Extensions.Http" Version="8.0.1" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="8.0.1" />
<PackageVersion Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
<PackageVersion Include="MySql.Data" Version="8.2.0" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
<PackageVersion Include="NLog" Version="5.3.3" />
<PackageVersion Include="NLog.Extensions.Logging" Version="5.3.12" />
<PackageVersion Include="NLog" Version="5.3.4" />
<PackageVersion Include="NLog.Extensions.Logging" Version="5.3.14" />
<PackageVersion Include="NLog.Web.AspNetCore" Version="5.3.5" />
<PackageVersion Include="Quartz" Version="3.13.0" />
<PackageVersion Include="Quartz.AspNetCore" Version="3.13.0" />
Expand All @@ -23,9 +23,9 @@
<PackageVersion Include="SkiaSharp.NativeAssets.macOS" Version="2.88.8" />
<PackageVersion Include="SkiaSharp.NativeAssets.Win32" Version="2.88.8" />
<PackageVersion Include="SqlSugar.IOC" Version="2.0.0" />
<PackageVersion Include="SqlSugarCore" Version="5.1.4.166" />
<PackageVersion Include="Swashbuckle.AspNetCore" Version="6.7.0" />
<PackageVersion Include="Swashbuckle.AspNetCore.Annotations" Version="6.7.0" />
<PackageVersion Include="SqlSugarCore" Version="5.1.4.169" />
<PackageVersion Include="Swashbuckle.AspNetCore" Version="6.8.1" />
<PackageVersion Include="Swashbuckle.AspNetCore.Annotations" Version="6.8.1" />
<PackageVersion Include="System.Text.Json" Version="8.0.1" />
<PackageVersion Include="Telegram.Bot" Version="19.0.0" />
</ItemGroup>
Expand Down
63 changes: 33 additions & 30 deletions XinjingdailyBot.Generator/AppServiceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,53 @@
namespace XinjingdailyBot.Generator;

[Generator]
internal sealed class AppServiceGenerator : ISourceGenerator
internal sealed class AppServiceGenerator : IIncrementalGenerator
{
const string InputFileName = "appService.json";
const string OutoutFileName = "GeneratedAppServiceExtensions.g.cs";

/// <inheritdoc/>
public void Initialize(GeneratorInitializationContext context)
public void Initialize(IncrementalGeneratorInitializationContext context)
{
// 无需处理
}

/// <inheritdoc/>
public void Execute(GeneratorExecutionContext context)
{
try
{
var fileText = context.AdditionalFiles.Where(static x => x.Path.EndsWith(InputFileName)).FirstOrDefault()
?? throw new FileNotFoundException("缺少配置文件, 请使用 scan_service.ps1 生成");
var additionalFiles = context.AdditionalTextsProvider
.Where(file => file.Path.EndsWith(InputFileName))
.Select((file, cancellationToken) => file.GetText(cancellationToken)?.ToString())
.Where(text => text is not null);

ProcessSettingsFile(fileText, context);
}
catch (Exception e)
{
context.ReportDiagnostic(
Diagnostic.Create(
"XJB_01",
nameof(AppServiceGenerator),
$"生成注入代码失败,{e.Message}",
defaultSeverity: DiagnosticSeverity.Error,
severity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
warningLevel: 0));
}
context.RegisterSourceOutput(additionalFiles, (context, fileText) => {
try
{
ProcessSettingsFile(fileText, context);
}
catch (Exception e)
{
context.ReportDiagnostic(
Diagnostic.Create(
"XJB_01",
nameof(AppServiceGenerator),
$"生成注入代码失败,{e.Message}",
defaultSeverity: DiagnosticSeverity.Error,
severity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
warningLevel: 0));
}
});
}

/// <summary>
/// 生成文件
/// </summary>
/// <param name="xmlFile"></param>
/// <param name="fileText"></param>
/// <param name="context"></param>
private void ProcessSettingsFile(AdditionalText xmlFile, GeneratorExecutionContext context)
private void ProcessSettingsFile(string? fileText, SourceProductionContext context)
{
var text = xmlFile.GetText(context.CancellationToken)?.ToString() ?? throw new FileLoadException("文件读取失败");
var json = JsonConvert.DeserializeObject<AppServiceData>(text) ?? throw new FileLoadException("文件读取失败");
if (string.IsNullOrEmpty(fileText))
{
Debug.WriteLine("文件为空");
return;
}

var json = JsonConvert.DeserializeObject<AppServiceData>(fileText) ?? throw new FileLoadException("文件读取失败");

var sb = new StringBuilder();
sb.AppendLine(Templates.AppServiceHeader);
Expand Down
65 changes: 32 additions & 33 deletions XinjingdailyBot.Generator/DbTableGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,55 +1,55 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
using Newtonsoft.Json;
using System.Diagnostics;
using System.Text;
using XinjingdailyBot.Generator.Data;

namespace XinjingdailyBot.Generator;

[Generator]
internal sealed class DbTableGenerator : ISourceGenerator
internal sealed class DbTableGenerator : IIncrementalGenerator
{
const string InputFileName = "dbtable.json";
const string OutoutFileName = "GeneratedDbTableExtensions.g.cs";

/// <inheritdoc/>
public void Initialize(GeneratorInitializationContext context)
public void Initialize(IncrementalGeneratorInitializationContext context)
{
// 无需处理
var additionalFiles = context.AdditionalTextsProvider
.Where(file => file.Path.EndsWith(InputFileName))
.Select((file, cancellationToken) => file.GetText(cancellationToken)?.ToString())
.Where(text => text != null);

context.RegisterSourceOutput(additionalFiles, (context, fileText) => {
try
{
ProcessSettingsFile(fileText, context);
}
catch (IOException e)
{
context.ReportDiagnostic(
Diagnostic.Create(
"XJB_03",
nameof(AppServiceGenerator),
$"生成注入代码失败,{e.Message}",
defaultSeverity: DiagnosticSeverity.Error,
severity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
warningLevel: 0));
}
});
}

/// <inheritdoc/>
public void Execute(GeneratorExecutionContext context)
private void ProcessSettingsFile(string? fileText, SourceProductionContext context)
{
try
if (string.IsNullOrEmpty(fileText))
{
var fileText = context.AdditionalFiles.Where(static x => x.Path.EndsWith(InputFileName)).FirstOrDefault() ?? throw new FileNotFoundException("缺少配置文件, 请使用 scan_service.ps1 生成");

ProcessSettingsFile(fileText, context);
}
catch (IOException e)
{
context.ReportDiagnostic(
Diagnostic.Create(
"XJB_03",
nameof(AppServiceGenerator),
$"生成注入代码失败,{e.Message}",
defaultSeverity: DiagnosticSeverity.Error,
severity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
warningLevel: 0));
Debug.WriteLine("文件为空");
return;
}
}

/// <summary>
/// 生成文件
/// </summary>
/// <param name="xmlFile"></param>
/// <param name="context"></param>
private void ProcessSettingsFile(AdditionalText xmlFile, GeneratorExecutionContext context)
{
var text = xmlFile.GetText(context.CancellationToken)?.ToString() ?? throw new FileLoadException("文件读取失败");
var json = JsonConvert.DeserializeObject<DbTableData>(text) ?? throw new FileLoadException("文件读取失败");
var json = JsonConvert.DeserializeObject<DbTableData>(fileText) ?? throw new FileLoadException("文件读取失败");

var sb = new StringBuilder();
sb.AppendLine(Templates.DbTableHeader);
Expand All @@ -68,10 +68,9 @@ private void ProcessSettingsFile(AdditionalText xmlFile, GeneratorExecutionConte
}
sb.AppendLine(Templates.DbTableFooter);

Console.WriteLine(sb.ToString());
Debug.WriteLine(sb.ToString());

context.AddSource(OutoutFileName, SourceText.From(sb.ToString(), Encoding.UTF8));
}

}

66 changes: 35 additions & 31 deletions XinjingdailyBot.Generator/ScheduleGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,55 +1,60 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
using Newtonsoft.Json;
using System.Diagnostics;
using System.Text;
using XinjingdailyBot.Generator.Data;

namespace XinjingdailyBot.Generator;

[Generator]
internal sealed class ScheduleGenerator : ISourceGenerator
internal sealed class ScheduleGenerator : IIncrementalGenerator
{
const string InputFileName = "schedule.json";
const string OutoutFileName = "GeneratedScheduleExtensions.g.cs";

/// <inheritdoc/>
public void Initialize(GeneratorInitializationContext context)
public void Initialize(IncrementalGeneratorInitializationContext context)
{
// 无需处理
}

/// <inheritdoc/>
public void Execute(GeneratorExecutionContext context)
{
try
{
var fileText = context.AdditionalFiles.Where(static x => x.Path.EndsWith(InputFileName)).FirstOrDefault() ?? throw new FileNotFoundException("缺少配置文件, 请使用 scan_service.ps1 生成");
var additionalFiles = context.AdditionalTextsProvider
.Where(file => file.Path.EndsWith(InputFileName))
.Select((file, cancellationToken) => file.GetText(cancellationToken)?.ToString())
.Where(text => text != null);

ProcessSettingsFile(fileText, context);
}
catch (IOException e)
{
context.ReportDiagnostic(
Diagnostic.Create(
"XJB_02",
nameof(AppServiceGenerator),
$"生成注入代码失败,{e.Message}",
defaultSeverity: DiagnosticSeverity.Error,
severity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
warningLevel: 0));
}
context.RegisterSourceOutput(additionalFiles, (context, fileText) => {
try
{
ProcessSettingsFile(fileText, context);
}
catch (IOException e)
{
context.ReportDiagnostic(
Diagnostic.Create(
"XJB_02",
nameof(AppServiceGenerator),
$"生成注入代码失败,{e.Message}",
defaultSeverity: DiagnosticSeverity.Error,
severity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
warningLevel: 0));
}
});
}

/// <summary>
/// 生成文件
/// </summary>
/// <param name="xmlFile"></param>
/// <param name="fileText"></param>
/// <param name="context"></param>
private void ProcessSettingsFile(AdditionalText xmlFile, GeneratorExecutionContext context)
private void ProcessSettingsFile(string? fileText, SourceProductionContext context)
{
var text = xmlFile.GetText(context.CancellationToken)?.ToString() ?? throw new FileLoadException("文件读取失败");
var json = JsonConvert.DeserializeObject<ScheduleData>(text) ?? throw new FileLoadException("文件读取失败");
if (string.IsNullOrEmpty(fileText))
{
Debug.WriteLine("文件为空");
return;
}

var json = JsonConvert.DeserializeObject<ScheduleData>(fileText) ?? throw new FileLoadException("文件读取失败");

var sb = new StringBuilder();
sb.AppendLine(Templates.ScheduleHeader);
Expand All @@ -71,10 +76,9 @@ private void ProcessSettingsFile(AdditionalText xmlFile, GeneratorExecutionConte
}
sb.AppendLine(Templates.ScheduleFooter);

Console.WriteLine(sb.ToString());
Debug.WriteLine(sb.ToString());

context.AddSource(OutoutFileName, SourceText.From(sb.ToString(), Encoding.UTF8));
}

}

1 change: 0 additions & 1 deletion XinjingdailyBot.Generator/XinjingdailyBot.Generator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
</ItemGroup>

<ItemGroup>
<!-- Package the generator in the analyzer directory of the nuget package -->
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
</ItemGroup>

Expand Down
Binary file modified XinjingdailyBot.WebAPI/Properties/appService.json
Binary file not shown.
Binary file modified XinjingdailyBot.WebAPI/Properties/dbtable.json
Binary file not shown.

0 comments on commit 180a4ef

Please sign in to comment.