IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
ImportTool.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.CommandLine;
4using System.CommandLine.Builder;
5using System.CommandLine.Parsing;
6using System.Threading;
7using System.Threading.Tasks;
8
12
13using Microsoft.Extensions.DependencyInjection;
14
15namespace IKVM.Tools.Importer
16{
17
21 public class ImportTool
22 {
23
30 public static async Task<int> InvokeAsync(string[] args, CancellationToken cancellationToken = default)
31 {
32 using var context = new ExecutionContext(args);
33 return await context.ExecuteAsync(cancellationToken);
34 }
35
42 internal static async Task<int> ExecuteInContext(string[] args, CancellationToken cancellationToken = default)
43 {
44 return await new ImportTool().InvokeImplAsync(args, cancellationToken);
45 }
46
54 static Task<int> ExecuteAsync(ImportOptions options, IDiagnosticHandler diagnostics, CancellationToken cancellationToken = default)
55 {
56 return ExecuteImplAsync(options, _ => diagnostics, cancellationToken);
57 }
58
66 static Task<int> ExecuteImplAsync(ImportOptions options, Func<IServiceProvider, IDiagnosticHandler> diagnostics, CancellationToken cancellationToken)
67 {
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);
74 }
75
82 static Task<int> ExecuteImplAsync(ImportOptions options, IServiceProvider services, CancellationToken cancellationToken)
83 {
84 if (services is null)
85 throw new ArgumentNullException(nameof(services));
86
87 return Task.Run(() => ImportContext.Execute(options));
88 }
89
96 static Task ExecuteAsync(ImportOptions options, CancellationToken cancellationToken = default)
97 {
98 if (options is null)
99 throw new ArgumentNullException(nameof(options));
100
101 return ExecuteImplAsync(options, p => GetDiagnostics(p, options.Log), cancellationToken);
102 }
103
110 static FormattedDiagnosticHandler GetDiagnostics(IServiceProvider services, string spec)
111 {
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));
116
117 return ActivatorUtilities.CreateInstance<FormattedDiagnosticHandler>(services, spec);
118 }
119
123 public ImportTool()
124 {
125
126 }
127
134 async Task<int> InvokeImplAsync(string[] args, CancellationToken cancellationToken)
135 {
136 if (args is null)
137 throw new ArgumentNullException(nameof(args));
138
139 var stack = new Stack<ImportArgLevel>();
140 var level = new ImportArgLevel(0);
141
142 foreach (var token in new Parser().Parse(args).Tokens)
143 {
144 // open new level, set as current
145 if (token.Value == "{")
146 {
147 stack.Push(level);
148 level = new ImportArgLevel(level.Depth + 1);
149 continue;
150 }
151
152 // close current level, parent becomes current
153 if (token.Value == "}")
154 {
155 var parent = stack.Pop();
156 parent.Nested.Add(level);
157 level = parent;
158 continue;
159 }
160
161 // add arg to current level
162 level.Args.Add(token.Value);
163 }
164
165 // root command binds the first level, and accepts the nested levels
166 var command = new ImportCommand();
167 command.SetHandler(options => ExecuteAsync(options), new ImportOptionsBinding(level.Nested.ToArray(), null));
168
169 // execute command
170 return await new CommandLineBuilder(command)
171 .UseHelp()
172 .UseParseDirective()
173 .UseTypoCorrections()
174 .UseToolParseError()
175 .UseExceptionHandler()
176 .CancelOnProcessTermination()
177 .UseToolErrorExceptionHandler()
178 .Build()
179 .InvokeAsync(level.Args.ToArray());
180 }
181
182 }
183
184}
IDiagnosticHandler implementation that outputs based on a format specification.
Communicates command line arguments across either an AppDomain or AssemblyLoadContext boundary....
Main entry point for the application.
Definition ImportTool.cs:22
ImportTool()
Initializes a new instance.
static async Task< int > InvokeAsync(string[] args, CancellationToken cancellationToken=default)
Executes the importer against a set of command line options.
Definition ImportTool.cs:30
Exposes methods to accept diagnostic invocations.