30 public static async Task<int>
InvokeAsync(
string[] args, CancellationToken cancellationToken =
default)
33 return await context.ExecuteAsync(cancellationToken);
42 internal static async Task<int> ExecuteInContext(
string[] args, CancellationToken cancellationToken =
default)
44 return await
new ImportTool().InvokeImplAsync(args, cancellationToken);
54 static Task<int> ExecuteAsync(ImportOptions options,
IDiagnosticHandler diagnostics, CancellationToken cancellationToken =
default)
56 return ExecuteImplAsync(options, _ => diagnostics, cancellationToken);
66 static Task<int> ExecuteImplAsync(ImportOptions options, Func<IServiceProvider, IDiagnosticHandler> diagnostics, CancellationToken cancellationToken)
68 var services =
new ServiceCollection();
69 services.AddToolsDiagnostics();
70 services.AddSingleton(options);
71 services.AddSingleton(diagnostics);
72 using var provider = services.BuildServiceProvider();
73 return ExecuteImplAsync(options, provider, cancellationToken);
82 static Task<int> ExecuteImplAsync(ImportOptions options, IServiceProvider services, CancellationToken cancellationToken)
85 throw new ArgumentNullException(nameof(services));
87 return Task.Run(() => ImportContext.Execute(options));
96 static Task ExecuteAsync(ImportOptions options, CancellationToken cancellationToken =
default)
99 throw new ArgumentNullException(nameof(options));
101 return ExecuteImplAsync(options, p => GetDiagnostics(p, options.Log), cancellationToken);
112 if (services is
null)
113 throw new ArgumentNullException(nameof(services));
114 if (
string.IsNullOrWhiteSpace(spec))
115 throw new ArgumentException($
"'{nameof(spec)}' cannot be null or whitespace.", nameof(spec));
134 async Task<int> InvokeImplAsync(
string[] args, CancellationToken cancellationToken)
137 throw new ArgumentNullException(nameof(args));
139 var stack =
new Stack<ImportArgLevel>();
142 foreach (var token
in new Parser().Parse(args).Tokens)
145 if (token.Value ==
"{")
153 if (token.Value ==
"}")
155 var parent = stack.Pop();
156 parent.Nested.Add(level);
162 level.Args.Add(token.Value);
166 var command =
new ImportCommand();
167 command.SetHandler(options => ExecuteAsync(options),
new ImportOptionsBinding(level.Nested.ToArray(),
null));
170 return await
new CommandLineBuilder(command)
173 .UseTypoCorrections()
175 .UseExceptionHandler()
176 .CancelOnProcessTermination()
177 .UseToolErrorExceptionHandler()
179 .InvokeAsync(level.Args.ToArray());