@@ -478,6 +478,73 @@ public void Add<T>(string commandPath) { }
478478 [System.Diagnostics.Conditional("DEBUG")]
479479 public void UseFilter<T>() where T : ConsoleAppFilter { }
480480
481+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
482+ partial void AddCore(string commandName, Delegate command);
483+
484+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
485+ partial void RunCore(string[] args);
486+
487+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
488+ partial void RunAsyncCore(string[] args, ref Task result);
489+
490+ partial void BuildAndSetServiceProvider();
491+
492+ static partial void ShowHelp(int helpId);
493+
494+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
495+ static bool TryShowHelpOrVersion(ReadOnlySpan<string> args, int requiredParameterCount, int helpId)
496+ {
497+ if (args.Length == 0)
498+ {
499+ if (requiredParameterCount == 0) return false;
500+
501+ ShowHelp(helpId);
502+ return true;
503+ }
504+
505+ if (args.Length == 1)
506+ {
507+ switch (args[0])
508+ {
509+ case "--version":
510+ ShowVersion();
511+ return true;
512+ case "-h":
513+ case "--help":
514+ ShowHelp(helpId);
515+ return true;
516+ default:
517+ break;
518+ }
519+ }
520+
521+ return false;
522+ }
523+ }
524+ }
525+ """ ;
526+
527+ public const string ConsoleAppBuilderRunStandard = """
528+ // <auto-generated/>
529+ #nullable enable
530+ #pragma warning disable
531+
532+ namespace ConsoleAppFramework;
533+
534+ using System;
535+ using System.Text;
536+ using System.Reflection;
537+ using System.Threading;
538+ using System.Threading.Tasks;
539+ using System.Runtime.InteropServices;
540+ using System.Runtime.CompilerServices;
541+ using System.Diagnostics.CodeAnalysis;
542+ using System.ComponentModel.DataAnnotations;
543+
544+ internal static partial class ConsoleApp
545+ {
546+ internal partial class ConsoleAppBuilder
547+ {
481548 public void Run(string[] args)
482549 {
483550 BuildAndSetServiceProvider();
@@ -518,48 +585,76 @@ public async Task RunAsync(string[] args)
518585 }
519586 }
520587 }
588+ }
589+ }
590+ """ ;
521591
522- [MethodImpl(MethodImplOptions.AggressiveInlining)]
523- partial void AddCore(string commandName, Delegate command);
524-
525- [MethodImpl(MethodImplOptions.AggressiveInlining)]
526- partial void RunCore(string[] args);
527-
528- [MethodImpl(MethodImplOptions.AggressiveInlining)]
529- partial void RunAsyncCore(string[] args, ref Task result);
592+ public const string ConsoleAppBuilderRunWithHost = """
593+ // <auto-generated/>
594+ #nullable enable
595+ #pragma warning disable
530596
531- partial void BuildAndSetServiceProvider() ;
597+ namespace ConsoleAppFramework ;
532598
533- static partial void ShowHelp(int helpId);
599+ using System;
600+ using System.Text;
601+ using System.Reflection;
602+ using System.Threading;
603+ using System.Threading.Tasks;
604+ using System.Runtime.InteropServices;
605+ using System.Runtime.CompilerServices;
606+ using System.Diagnostics.CodeAnalysis;
607+ using System.ComponentModel.DataAnnotations;
534608
535- [MethodImpl(MethodImplOptions.AggressiveInlining)]
536- static bool TryShowHelpOrVersion(ReadOnlySpan<string> args, int requiredParameterCount, int helpId)
609+ internal static partial class ConsoleApp
610+ {
611+ internal partial class ConsoleAppBuilder
612+ {
613+ public void Run(string[] args)
537614 {
538- if (args.Length == 0)
615+ BuildAndSetServiceProvider();
616+ Microsoft.Extensions.Hosting.IHost? host = ConsoleApp.ServiceProvider?.GetService(typeof(Microsoft.Extensions.Hosting.IHost)) as Microsoft.Extensions.Hosting.IHost;
617+ try
539618 {
540- if (requiredParameterCount == 0) return false;
541-
542- ShowHelp(helpId);
543- return true;
619+ host?.StartAsync().GetAwaiter().GetResult();
620+ RunCore(args);
544621 }
545-
546- if (args.Length == 1)
622+ finally
547623 {
548- switch (args[0])
624+ host?.StopAsync().GetAwaiter().GetResult();
625+ if (ServiceProvider is IDisposable d)
549626 {
550- case "--version":
551- ShowVersion();
552- return true;
553- case "-h":
554- case "--help":
555- ShowHelp(helpId);
556- return true;
557- default:
558- break;
627+ d.Dispose();
559628 }
560629 }
630+ }
561631
562- return false;
632+ public async Task RunAsync(string[] args)
633+ {
634+ BuildAndSetServiceProvider();
635+ Microsoft.Extensions.Hosting.IHost? host = ConsoleApp.ServiceProvider?.GetService(typeof(Microsoft.Extensions.Hosting.IHost)) as Microsoft.Extensions.Hosting.IHost;
636+ try
637+ {
638+ await host?.StartAsync();
639+ Task? task = null;
640+ RunAsyncCore(args, ref task!);
641+ if (task != null)
642+ {
643+ await task;
644+ }
645+ }
646+ finally
647+ {
648+ await host?.StopAsync();
649+ if (ServiceProvider is IAsyncDisposable ad)
650+ {
651+ await ad.DisposeAsync();
652+ }
653+ else if (ServiceProvider is IDisposable d)
654+ {
655+ d.Dispose();
656+ }
657+ }
563658 }
564659 }
565660}
0 commit comments