IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
IkvmToolLauncher.cs
Go to the documentation of this file.
1using System;
2using System.IO;
3using System.Runtime.InteropServices;
4using System.Text;
5using System.Text.Json;
6using System.Threading;
7using System.Threading.Tasks;
8
10
11namespace IKVM.Tools.Runner
12{
13
14 public abstract class IkvmToolLauncher
15 {
16
17 readonly string toolName;
18 readonly string toolPath;
19 readonly IIkvmToolDiagnosticEventListener listener;
20
27 public IkvmToolLauncher(string toolName, string toolPath, IIkvmToolDiagnosticEventListener listener)
28 {
29 this.toolName = toolName ?? throw new ArgumentNullException(nameof(toolName));
30 this.toolPath = toolPath ?? throw new ArgumentNullException(nameof(toolPath));
31 this.listener = listener;
32
33 if (Directory.Exists(toolPath) == false)
34 throw new DirectoryNotFoundException($"Could not locate tool path {toolPath}.");
35 }
36
42 protected ValueTask LogEventAsync(in IkvmToolDiagnosticEvent @event, CancellationToken cancellationToken)
43 {
44 return listener.ReceiveAsync(@event, cancellationToken);
45 }
46
54 protected ValueTask LogEventAsync(IkvmToolDiagnosticEventLevel level, string message, string[] args, CancellationToken cancellationToken)
55 {
56 return LogEventAsync(new IkvmToolDiagnosticEvent(-1, level, message, args), cancellationToken);
57 }
58
65 protected ValueTask ParseAndLogEventAsync(string line, CancellationToken cancellationToken)
66 {
67 return LogEventAsync(ParseEvent(line), cancellationToken);
68 }
69
75 protected IkvmToolDiagnosticEvent ParseEvent(string line)
76 {
77 try
78 {
79 var buffer = Encoding.UTF8.GetBytes(line);
80 var reader = new Utf8JsonReader(buffer);
81 if (reader.Read() == false)
82 throw new JsonException();
83
84 return IkvmToolDiagnosticEvent.ReadJson(ref reader);
85 }
86 catch (JsonException)
87 {
88 return new IkvmToolDiagnosticEvent(0, IkvmToolDiagnosticEventLevel.Fatal, "Unable to parse tool output: {0}", [line]);
89 }
90 }
91
98 public string? GetToolExe()
99 {
100 foreach (var name in (Span<string>)[toolName + ".exe", toolName])
101 if (File.Exists(Path.Combine(toolPath, name)))
102 return Path.Combine(toolPath, name);
103
104 return null;
105 }
106
107 }
108
109}
IkvmToolLauncher(string toolName, string toolPath, IIkvmToolDiagnosticEventListener listener)
Initializes a new instance.
string? GetToolExe()
Gets the path to executable for the given environment.
IkvmToolDiagnosticEvent ParseEvent(string line)
Parse a line of JSON tool output into a diagnostic event.
ValueTask LogEventAsync(IkvmToolDiagnosticEventLevel level, string message, string[] args, CancellationToken cancellationToken)
Logs an anonymous event if a listener is provided.
ValueTask ParseAndLogEventAsync(string line, CancellationToken cancellationToken)
Parses the line and logs it.
ValueTask LogEventAsync(in IkvmToolDiagnosticEvent @event, CancellationToken cancellationToken)
Logs an event if a listener is provided.
readonly record struct IkvmToolDiagnosticEvent(int Id, IkvmToolDiagnosticEventLevel Level, string Message, object?[] Args, IkvmToolDiagnosticEventLocation Location=default)
Describes an event emitted from a tool.
IkvmToolDiagnosticEventLevel
Describes the level of diagnostic event a tool could emit.