Skip to content

Commit 3957e22

Browse files
committed
feat(SG): Added CLI generation to source generators.
1 parent dc7e19f commit 3957e22

File tree

4 files changed

+82
-5
lines changed

4 files changed

+82
-5
lines changed

src/libs/AutoSDK.SourceGenerators/AutoSDK.SourceGenerators.props

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@
9090
<!-- Generates full sdk(all methods/models) -->
9191
<!-- true/false. Default: true -->
9292
<CompilerVisibleProperty Include="AutoSDK_GenerateSdk"/>
93+
94+
<!-- Generates CLI -->
95+
<!-- true/false. Default: false -->
96+
<CompilerVisibleProperty Include="AutoSDK_GenerateCli"/>
9397
</ItemGroup>
9498

9599
<ItemGroup>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System.Collections.Immutable;
2+
using AutoSDK.Generation;
3+
using Microsoft.CodeAnalysis;
4+
5+
namespace AutoSDK.SourceGenerators;
6+
7+
[Generator]
8+
public class CliGenerator : IIncrementalGenerator
9+
{
10+
#region Constants
11+
12+
private const string Id = "CLIG";
13+
14+
#endregion
15+
16+
#region Methods
17+
18+
public void Initialize(IncrementalGeneratorInitializationContext context)
19+
{
20+
var settings = context.DetectSettings();
21+
22+
var data = context.AdditionalTextsProvider
23+
.Combine(context.AnalyzerConfigOptionsProvider)
24+
.Where(static pair =>
25+
pair.Right.GetOption(pair.Left, "OpenApiSpecification", prefix: "AutoSDK")?.ToUpperInvariant() == "TRUE")
26+
.Select((pair, cancellationToken) => (
27+
GetContent(pair.Left, cancellationToken),
28+
pair.Right.GetSettings(prefix: "AutoSDK", additionalText: pair.Left)))
29+
.Combine(settings)
30+
.Where(x => x.Right.GenerateCli)
31+
.SelectAndReportExceptions(Data.Prepare, context, Id);
32+
33+
data
34+
.SelectMany(static (x, _) => x.Methods.GroupBy(x => x.Tag))
35+
.SelectAndReportExceptions((x, c) => Sources.TagCommand(x.Key, x.ToImmutableArray(), c)
36+
.AsFileWithName(), context, Id)
37+
.AddSource(context);
38+
data
39+
.SelectMany(static (x, _) => x.Methods)
40+
.SelectAndReportExceptions((x, c) => Sources.Command(x, c)
41+
.AsFileWithName(), context, Id)
42+
.AddSource(context);
43+
data
44+
.Select(static (x, _) => x.Tags)
45+
.SelectAndReportExceptions((x, c) => Sources.MainCommand(x, c)
46+
.AsFileWithName(), context, Id)
47+
.AddSource(context);
48+
data
49+
.Select(static (x, _) => (x.Methods, x.Tags))
50+
.SelectAndReportExceptions((x, c) => Sources.AddCommands(x.Methods, x.Tags, c)
51+
.AsFileWithName(), context, Id)
52+
.AddSource(context);
53+
}
54+
55+
private static string GetContent(AdditionalText additionalText, CancellationToken cancellationToken = default)
56+
{
57+
return additionalText.Path.StartsWith("http", StringComparison.OrdinalIgnoreCase)
58+
? Task.Run(() => new HttpClient().GetStringAsync(new Uri(additionalText.Path)), cancellationToken).Result
59+
: additionalText.GetText(cancellationToken)?.ToString() ?? string.Empty;
60+
}
61+
62+
#endregion
63+
}

src/libs/AutoSDK.SourceGenerators/OptionsExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public static Settings GetSettings(
7575

7676
GenerateSdk: options.GetBoolGlobalOption(nameof(Settings.GenerateSdk), prefix, defaultValue: Settings.Default.GenerateSdk),
7777
FromCli: false,
78-
GenerateCli: false);
78+
GenerateCli: options.GetBoolGlobalOption(nameof(Settings.GenerateCli), prefix, defaultValue: Settings.Default.GenerateCli));
7979

8080
string? GetOptionFromAdditionalText(string name)
8181
{

src/libs/AutoSDK.SourceGenerators/SdkGenerator.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using AutoSDK.Generation;
2+
using AutoSDK.Models;
23
using Microsoft.CodeAnalysis;
4+
using Data = AutoSDK.Generation.Data;
35

46
namespace AutoSDK.SourceGenerators;
57

@@ -18,19 +20,27 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
1820
{
1921
var settings = context.DetectSettings();
2022
settings
21-
.SelectAndReportExceptions((x, c) => Sources.Polyfills(x, c)
23+
.SelectAndReportExceptions((x, c) => x.GenerateSdk
24+
? Sources.Polyfills(x, c)
25+
: FileWithName.Empty
2226
.AsFileWithName(), context, Id)
2327
.AddSource(context);
2428
settings
25-
.SelectAndReportExceptions((x, c) => Sources.Exceptions(x, c)
29+
.SelectAndReportExceptions((x, c) => x.GenerateSdk
30+
? Sources.Exceptions(x, c)
31+
: FileWithName.Empty
2632
.AsFileWithName(), context, Id)
2733
.AddSource(context);
2834
settings
29-
.SelectAndReportExceptions((x, c) => Sources.PathBuilder(x, c)
35+
.SelectAndReportExceptions((x, c) => x.GenerateSdk
36+
? Sources.PathBuilder(x, c)
37+
: FileWithName.Empty
3038
.AsFileWithName(), context, Id)
3139
.AddSource(context);
3240
settings
33-
.SelectAndReportExceptions((x, c) => Sources.UnixTimestampJsonConverter(x, c)
41+
.SelectAndReportExceptions((x, c) => x.GenerateSdk
42+
? Sources.UnixTimestampJsonConverter(x, c)
43+
: FileWithName.Empty
3444
.AsFileWithName(), context, Id)
3545
.AddSource(context);
3646

0 commit comments

Comments
 (0)