Skip to content

Commit cae78b9

Browse files
committed
testing
1 parent 2733298 commit cae78b9

File tree

5 files changed

+255
-206
lines changed

5 files changed

+255
-206
lines changed

sandbox/CliFrameworkBenchmark/CliFrameworkBenchmark.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<OutputType>Exe</OutputType>
55
<TargetFramework>net8.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
7+
<LangVersion>13</LangVersion>
78
<nullable>annotations</nullable>
89
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
910
<Configurations>Debug;Release</Configurations>

sandbox/GeneratorSandbox/GeneratorSandbox.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
55
<TargetFramework>net8.0</TargetFramework>
6-
<LangVersion>14</LangVersion>
6+
<LangVersion>13</LangVersion>
77

88
<ImplicitUsings>enable</ImplicitUsings>
99
<Nullable>disable</Nullable>

sandbox/GeneratorSandbox/Program.cs

Lines changed: 92 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
//var app = ConsoleApp.Create();
1515

1616

17-
args = ["--x", "10", "--y", "20", "-v", "--prefix-output", "takoyakix"];
17+
//args = ["--x", "10", "--y", "20", "-v", "--prefix-output", "takoyakix"];
1818

1919

2020
//// Enum.TryParse<Fruit>("", true,
@@ -59,60 +59,118 @@
5959

6060
//app.Run(args);
6161

62-
var app = ConsoleApp.Create();
62+
//var app = ConsoleApp.Create();
6363

6464

65-
app.ConfigureGlobalOptions((ref builder) =>
66-
{
67-
var verbose = builder.AddGlobalOption<bool>($"-v", "", true);
68-
var noColor = builder.AddGlobalOption<bool>("--no-color", "Don't colorize output.");
69-
var dryRun = builder.AddGlobalOption<bool>("--dry-run");
70-
var prefixOutput = builder.AddRequiredGlobalOption<string>("--prefix-output|-pp|-po", "Prefix output with level.");
65+
//app.ConfigureGlobalOptions((ref ConsoleApp.GlobalOptionsBuilder builder) =>
66+
//{
67+
// var verbose = builder.AddGlobalOption<bool>($"-v", "", true);
68+
// var noColor = builder.AddGlobalOption<bool>("--no-color", "Don't colorize output.");
69+
// var dryRun = builder.AddGlobalOption<bool>("--dry-run");
70+
// var prefixOutput = builder.AddRequiredGlobalOption<string>("--prefix-output|-pp|-po", "Prefix output with level.");
7171

72-
return new GlobalOptions(verbose, noColor, dryRun, prefixOutput);
73-
});
72+
// return new GlobalOptions(verbose, noColor, dryRun, prefixOutput);
73+
//});
7474

7575

76-
app.Add("", async (int x, int y, ConsoleAppContext context, CancellationToken cancellationToken) =>
77-
{
78-
Console.WriteLine("OK");
79-
await Task.Delay(TimeSpan.FromSeconds(1));
80-
Console.WriteLine(context.CommandName + ":" + (x, y));
81-
});
76+
//app.Add("", async (int x, int y, ConsoleAppContext context, CancellationToken cancellationToken) =>
77+
//{
78+
// Console.WriteLine("OK");
79+
// await Task.Delay(TimeSpan.FromSeconds(1));
80+
// Console.WriteLine(context.CommandName + ":" + (x, y));
81+
//});
8282

83-
app.Add("tako", (int x, int y, ConsoleAppContext context) =>
84-
{
85-
Console.WriteLine(context.CommandName);
86-
});
83+
//app.Add("tako", (int x, int y, ConsoleAppContext context) =>
84+
//{
85+
// Console.WriteLine(context.CommandName);
86+
//});
8787

88-
app.UseFilter<NopFilter>();
88+
//app.UseFilter<NopFilter>();
8989

90-
await app.RunAsync(args);
90+
//await app.RunAsync(args);
9191

92+
var builder = ConsoleApp.Create();
9293

94+
builder.UseFilter<NopFilter1>();
95+
builder.UseFilter<NopFilter2>();
9396

97+
builder.Add<MyClass>();
9498

95-
static T ParseArgumentEnum<T>(ref string[] args, int i)
96-
where T : struct, Enum
99+
await builder.RunAsync(args);
100+
101+
[ConsoleAppFilter<NopFilter3>]
102+
[ConsoleAppFilter<NopFilter4>]
103+
public class MyClass
97104
{
98-
if ((i + 1) < args.Length)
105+
[ConsoleAppFilter<NopFilter5>]
106+
[ConsoleAppFilter<NopFilter6>]
107+
public void Hello()
99108
{
100-
if (Enum.TryParse<T>(args[i + 1], out var value))
101-
{
102-
//RemoveRange(ref args, i, 2);
103-
return value;
104-
}
109+
Console.Write("abcde");
110+
}
111+
}
105112

106-
//ThrowArgumentParseFailed(args[i], args[i + 1]);
113+
internal class NopFilter1(ConsoleAppFilter next)
114+
: ConsoleAppFilter(next)
115+
{
116+
public override Task InvokeAsync(ConsoleAppContext context, CancellationToken cancellationToken)
117+
{
118+
Console.Write(1);
119+
return Next.InvokeAsync(context, cancellationToken);
107120
}
108-
else
121+
}
122+
123+
internal class NopFilter2(ConsoleAppFilter next)
124+
: ConsoleAppFilter(next)
125+
{
126+
public override Task InvokeAsync(ConsoleAppContext context, CancellationToken cancellationToken)
109127
{
110-
// ThrowArgumentParseFailed(args[i], "");
128+
Console.Write(2);
129+
return Next.InvokeAsync(context, cancellationToken);
111130
}
131+
}
132+
133+
internal class NopFilter3(ConsoleAppFilter next)
134+
: ConsoleAppFilter(next)
135+
{
136+
public override Task InvokeAsync(ConsoleAppContext context, CancellationToken cancellationToken)
137+
{
138+
Console.Write(3);
139+
return Next.InvokeAsync(context, cancellationToken);
140+
}
141+
}
142+
143+
internal class NopFilter4(ConsoleAppFilter next)
144+
: ConsoleAppFilter(next)
145+
{
146+
public override Task InvokeAsync(ConsoleAppContext context, CancellationToken cancellationToken)
147+
{
148+
Console.Write(4);
149+
return Next.InvokeAsync(context, cancellationToken);
150+
}
151+
}
152+
153+
internal class NopFilter5(ConsoleAppFilter next)
154+
: ConsoleAppFilter(next)
155+
{
156+
public override Task InvokeAsync(ConsoleAppContext context, CancellationToken cancellationToken)
157+
{
158+
Console.Write(5);
159+
return Next.InvokeAsync(context, cancellationToken);
160+
}
161+
}
112162

113-
return default;
163+
internal class NopFilter6(ConsoleAppFilter next)
164+
: ConsoleAppFilter(next)
165+
{
166+
public override Task InvokeAsync(ConsoleAppContext context, CancellationToken cancellationToken)
167+
{
168+
Console.Write(6);
169+
return Next.InvokeAsync(context, cancellationToken);
170+
}
114171
}
115172

173+
116174
public record GlobalOptions(bool Verbose, bool NoColor, bool DryRun, string PrefixOutput);
117175

118176

src/ConsoleAppFramework/Emitter.cs

Lines changed: 87 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -65,120 +65,120 @@ public void EmitRun(SourceBuilder sb, CommandWithId commandWithId, bool isRunAsy
6565
// method signature
6666
using (sb.BeginBlock($"{accessibility} {unsafeCode}{returnType} {methodName}(string[] args{commandDepthEscapeIndex}{commandMethodType}{filterCancellationToken})"))
6767
{
68-
if (emitForBuilder)
69-
{
70-
sb.AppendLine("var commandArgs = (escapeIndex == -1) ? args.AsSpan(commandDepth) : args.AsSpan(commandDepth, escapeIndex - commandDepth);");
71-
}
72-
else
68+
using (command.HasFilter ? sb.Nop : sb.BeginBlock("try"))
7369
{
74-
if (hasConsoleAppContext)
70+
if (emitForBuilder)
7571
{
76-
sb.AppendLine("var escapeIndex = args.AsSpan().IndexOf(\"--\");");
77-
sb.AppendLine("var commandArgs = (escapeIndex == -1) ? args.AsSpan() : args.AsSpan(0, escapeIndex);");
72+
sb.AppendLine("var commandArgs = (escapeIndex == -1) ? args.AsSpan(commandDepth) : args.AsSpan(commandDepth, escapeIndex - commandDepth);");
7873
}
7974
else
8075
{
81-
sb.AppendLine("var commandArgs = args.AsSpan();");
76+
if (hasConsoleAppContext)
77+
{
78+
sb.AppendLine("var escapeIndex = args.AsSpan().IndexOf(\"--\");");
79+
sb.AppendLine("var commandArgs = (escapeIndex == -1) ? args.AsSpan() : args.AsSpan(0, escapeIndex);");
80+
}
81+
else
82+
{
83+
sb.AppendLine("var commandArgs = args.AsSpan();");
84+
}
8285
}
83-
}
8486

85-
sb.AppendLine($"if (TryShowHelpOrVersion(commandArgs, {requiredParsableParameterCount}, {commandWithId.Id})) return;");
86-
sb.AppendLine();
87-
88-
// prepare argument variables
89-
if (hasCancellationToken)
90-
{
91-
sb.AppendLine($"using var posixSignalHandler = PosixSignalHandler.Register(Timeout, {cancellationTokenName});");
87+
sb.AppendLine($"if (TryShowHelpOrVersion(commandArgs, {requiredParsableParameterCount}, {commandWithId.Id})) return;");
9288
sb.AppendLine();
93-
cancellationTokenName = "posixSignalHandler.Token";
94-
}
9589

96-
if (hasConsoleAppContext)
97-
{
98-
if (emitForBuilder)
90+
// prepare argument variables
91+
if (hasCancellationToken)
9992
{
100-
sb.AppendLine("ConsoleAppContext context;");
101-
using (sb.BeginBlock("if (configureGlobalOptions == null)"))
102-
{
103-
sb.AppendLine($"context = new ConsoleAppContext(\"{command.Name}\", args, args, null, null, commandDepth, escapeIndex);");
104-
}
105-
using (sb.BeginBlock("else"))
106-
{
107-
sb.AppendLine("var builder = new GlobalOptionsBuilder(commandArgs);");
108-
sb.AppendLine("var globalOptions = configureGlobalOptions(ref builder);");
109-
sb.AppendLine($"context = new ConsoleAppContext(\"{command.Name}\", args, args, null, globalOptions, commandDepth, escapeIndex);");
110-
sb.AppendLine("commandArgs = builder.RemainingArgs;");
111-
}
112-
sb.AppendLine("BuildAndSetServiceProvider(context);");
93+
sb.AppendLine($"using var posixSignalHandler = PosixSignalHandler.Register(Timeout, {cancellationTokenName});");
94+
sb.AppendLine();
95+
cancellationTokenName = "posixSignalHandler.Token";
96+
}
11397

114-
if (dllReference != null && dllReference.Value.HasHost)
98+
if (hasConsoleAppContext)
99+
{
100+
if (emitForBuilder)
115101
{
116-
sb.AppendLine("host = ConsoleApp.ServiceProvider?.GetService(typeof(Microsoft.Extensions.Hosting.IHost)) as Microsoft.Extensions.Hosting.IHost;");
117-
using (sb.BeginBlock("if (startHost && host != null)"))
102+
sb.AppendLine("ConsoleAppContext context;");
103+
using (sb.BeginBlock("if (configureGlobalOptions == null)"))
118104
{
119-
if (isRunAsync)
120-
{
121-
sb.AppendLine($"await host.StartAsync({cancellationTokenName});");
122-
}
123-
else
105+
sb.AppendLine($"context = new ConsoleAppContext(\"{command.Name}\", args, args, null, null, commandDepth, escapeIndex);");
106+
}
107+
using (sb.BeginBlock("else"))
108+
{
109+
sb.AppendLine("var builder = new GlobalOptionsBuilder(commandArgs);");
110+
sb.AppendLine("var globalOptions = configureGlobalOptions(ref builder);");
111+
sb.AppendLine($"context = new ConsoleAppContext(\"{command.Name}\", args, args, null, globalOptions, commandDepth, escapeIndex);");
112+
sb.AppendLine("commandArgs = builder.RemainingArgs;");
113+
}
114+
sb.AppendLine("BuildAndSetServiceProvider(context);");
115+
116+
if (dllReference != null && dllReference.Value.HasHost)
117+
{
118+
sb.AppendLine("host = ConsoleApp.ServiceProvider?.GetService(typeof(Microsoft.Extensions.Hosting.IHost)) as Microsoft.Extensions.Hosting.IHost;");
119+
using (sb.BeginBlock("if (startHost && host != null)"))
124120
{
125-
sb.AppendLine($"host.StartAsync({cancellationTokenName}).GetAwaiter().GetResult();");
121+
if (isRunAsync)
122+
{
123+
sb.AppendLine($"await host.StartAsync({cancellationTokenName});");
124+
}
125+
else
126+
{
127+
sb.AppendLine($"host.StartAsync({cancellationTokenName}).GetAwaiter().GetResult();");
128+
}
126129
}
127130
}
128131
}
132+
else
133+
{
134+
sb.AppendLine($"var context = new ConsoleAppContext(\"{command.Name}\", args, args, null, null, 0, escapeIndex);");
135+
}
136+
sb.AppendLine();
129137
}
130-
else
131-
{
132-
sb.AppendLine($"var context = new ConsoleAppContext(\"{command.Name}\", args, args, null, null, 0, escapeIndex);");
133-
}
134-
sb.AppendLine();
135-
}
136138

137-
for (var i = 0; i < command.Parameters.Length; i++)
138-
{
139-
var parameter = command.Parameters[i];
140-
if (parameter.IsParsable)
139+
for (var i = 0; i < command.Parameters.Length; i++)
141140
{
142-
var defaultValue = parameter.IsParams ? $"({parameter.ToTypeDisplayString()})[]"
143-
: parameter.HasDefaultValue ? parameter.DefaultValueToString()
144-
: $"default({parameter.Type.ToFullyQualifiedFormatDisplayString()})";
145-
sb.AppendLine($"var arg{i} = {defaultValue};");
146-
if (parameter.RequireCheckArgumentParsed)
141+
var parameter = command.Parameters[i];
142+
if (parameter.IsParsable)
147143
{
148-
sb.AppendLine($"var arg{i}Parsed = false;");
144+
var defaultValue = parameter.IsParams ? $"({parameter.ToTypeDisplayString()})[]"
145+
: parameter.HasDefaultValue ? parameter.DefaultValueToString()
146+
: $"default({parameter.Type.ToFullyQualifiedFormatDisplayString()})";
147+
sb.AppendLine($"var arg{i} = {defaultValue};");
148+
if (parameter.RequireCheckArgumentParsed)
149+
{
150+
sb.AppendLine($"var arg{i}Parsed = false;");
151+
}
149152
}
150-
}
151-
else if (parameter.IsCancellationToken)
152-
{
153-
if (command.HasFilter)
153+
else if (parameter.IsCancellationToken)
154154
{
155-
sb.AppendLine($"var arg{i} = cancellationToken;");
155+
if (command.HasFilter)
156+
{
157+
sb.AppendLine($"var arg{i} = cancellationToken;");
158+
}
159+
else
160+
{
161+
sb.AppendLine($"var arg{i} = posixSignalHandler.Token;");
162+
}
156163
}
157-
else
164+
else if (parameter.IsConsoleAppContext)
158165
{
159-
sb.AppendLine($"var arg{i} = posixSignalHandler.Token;");
166+
sb.AppendLine($"var arg{i} = context;");
167+
}
168+
else if (parameter.IsFromServices)
169+
{
170+
var type = parameter.Type.ToFullyQualifiedFormatDisplayString();
171+
sb.AppendLine($"var arg{i} = ({type})ServiceProvider!.GetService(typeof({type}))!;");
172+
}
173+
else if (parameter.IsFromKeyedServices)
174+
{
175+
var type = parameter.Type.ToFullyQualifiedFormatDisplayString();
176+
var line = $"var arg{i} = ({type})((Microsoft.Extensions.DependencyInjection.IKeyedServiceProvider)ServiceProvider).GetKeyedService(typeof({type}), {parameter.GetFormattedKeyedServiceKey()})!;";
177+
sb.AppendLine(line);
160178
}
161179
}
162-
else if (parameter.IsConsoleAppContext)
163-
{
164-
sb.AppendLine($"var arg{i} = context;");
165-
}
166-
else if (parameter.IsFromServices)
167-
{
168-
var type = parameter.Type.ToFullyQualifiedFormatDisplayString();
169-
sb.AppendLine($"var arg{i} = ({type})ServiceProvider!.GetService(typeof({type}))!;");
170-
}
171-
else if (parameter.IsFromKeyedServices)
172-
{
173-
var type = parameter.Type.ToFullyQualifiedFormatDisplayString();
174-
var line = $"var arg{i} = ({type})((Microsoft.Extensions.DependencyInjection.IKeyedServiceProvider)ServiceProvider).GetKeyedService(typeof({type}), {parameter.GetFormattedKeyedServiceKey()})!;";
175-
sb.AppendLine(line);
176-
}
177-
}
178-
sb.AppendLineIfExists(command.Parameters.AsSpan());
180+
sb.AppendLineIfExists(command.Parameters.AsSpan());
179181

180-
using (command.HasFilter ? sb.Nop : sb.BeginBlock("try"))
181-
{
182182
if (hasArgument)
183183
{
184184
sb.AppendLine("var argumentPosition = 0;");
@@ -904,7 +904,7 @@ public void EmitConfigure(SourceBuilder sb, DllReference dllReference)
904904
using (sb.BeginBlock("partial void StartHostAsyncIfNeeded(CancellationToken cancellationToken, ref Task task)"))
905905
{
906906
sb.AppendLine("Microsoft.Extensions.Hosting.IHost? host = ConsoleApp.ServiceProvider?.GetService(typeof(Microsoft.Extensions.Hosting.IHost)) as Microsoft.Extensions.Hosting.IHost;");
907-
using(sb.BeginBlock("if (startHost && host != null)"))
907+
using (sb.BeginBlock("if (startHost && host != null)"))
908908
{
909909
sb.AppendLine("task = host.StartAsync(cancellationToken);");
910910
}

0 commit comments

Comments
 (0)