-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fork process proxy for using workloads tools #283
Changes from all commits
549c9c9
a2cbb39
6a41eb1
5621ada
0099ec6
ce0da93
bf063fe
0808a4c
0bc6fc5
6728fb2
bfeeb4d
4de3aa6
c26c338
4cf7104
8a55fac
f0b7362
2584e81
153ce37
ad2f610
2c2fecc
e17e19c
bb705ed
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -14,10 +14,9 @@ public override void WriteJson(JsonWriter writer, WorkloadKey value, JsonSeriali | |||||||||||||
public override WorkloadKey ReadJson(JsonReader reader, Type objectType, WorkloadKey existingValue, bool hasExistingValue, | ||||||||||||||
JsonSerializer serializer) | ||||||||||||||
{ | ||||||||||||||
if (hasExistingValue) | ||||||||||||||
throw new NotSupportedException(); | ||||||||||||||
|
||||||||||||||
return new((string)reader.Value); | ||||||||||||||
if (reader.Value is not string str) | ||||||||||||||
throw new InvalidOperationException(); | ||||||||||||||
return new(str); | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
public class PackageKeyContactConverter : JsonConverter<PackageKey> | ||||||||||||||
|
@@ -28,9 +27,9 @@ public override void WriteJson(JsonWriter writer, PackageKey value, JsonSerializ | |||||||||||||
public override PackageKey ReadJson(JsonReader reader, Type objectType, PackageKey existingValue, bool hasExistingValue, | ||||||||||||||
JsonSerializer serializer) | ||||||||||||||
{ | ||||||||||||||
if (hasExistingValue) | ||||||||||||||
throw new NotSupportedException(); | ||||||||||||||
return new((string)reader.Value); | ||||||||||||||
if (reader.Value is not string str) | ||||||||||||||
throw new InvalidOperationException(); | ||||||||||||||
return new(str); | ||||||||||||||
Comment on lines
+30
to
+32
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Improve exception message for The - throw new InvalidOperationException();
+ throw new InvalidOperationException("Expected a string value for PackageKey."); Committable suggestion
Suggested change
|
||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
public class PlatformKeyContactConverter : JsonConverter<PlatformKey> | ||||||||||||||
|
@@ -41,9 +40,9 @@ public override void WriteJson(JsonWriter writer, PlatformKey value, JsonSeriali | |||||||||||||
public override PlatformKey ReadJson(JsonReader reader, Type objectType, PlatformKey existingValue, bool hasExistingValue, | ||||||||||||||
JsonSerializer serializer) | ||||||||||||||
{ | ||||||||||||||
if (hasExistingValue) | ||||||||||||||
throw new NotSupportedException(); | ||||||||||||||
return new((string)reader.Value); | ||||||||||||||
if (reader.Value is not string str) | ||||||||||||||
throw new InvalidOperationException(); | ||||||||||||||
return new(str); | ||||||||||||||
Comment on lines
+43
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Improve exception message for The - throw new InvalidOperationException();
+ throw new InvalidOperationException("Expected a string value for PlatformKey."); Committable suggestion
Suggested change
|
||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
public class PackageKindKeyContactConverter : JsonConverter<PackageKindKey> | ||||||||||||||
|
@@ -54,9 +53,9 @@ public override void WriteJson(JsonWriter writer, PackageKindKey value, JsonSeri | |||||||||||||
public override PackageKindKey ReadJson(JsonReader reader, Type objectType, PackageKindKey existingValue, bool hasExistingValue, | ||||||||||||||
JsonSerializer serializer) | ||||||||||||||
{ | ||||||||||||||
if (hasExistingValue) | ||||||||||||||
throw new NotSupportedException(); | ||||||||||||||
return new((string)reader.Value); | ||||||||||||||
if (reader.Value is not string str) | ||||||||||||||
throw new InvalidOperationException(); | ||||||||||||||
return new(str); | ||||||||||||||
Comment on lines
+56
to
+58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Improve exception message for The - throw new InvalidOperationException();
+ throw new InvalidOperationException("Expected a string value for PackageKindKey."); Committable suggestion
Suggested change
|
||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
public class WorkloadPackageBaseConverter : JsonConverter<List<IWorkloadPackageBase>> | ||||||||||||||
|
@@ -123,6 +122,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist | |||||||||||||
|
||||||||||||||
foreach (var property in jsonObject.Properties()) | ||||||||||||||
{ | ||||||||||||||
property.Value["name"] = property.Name; | ||||||||||||||
var workload = property.Value.ToObject<Workload>(serializer); | ||||||||||||||
workloads[new WorkloadKey(property.Name)] = workload; | ||||||||||||||
} | ||||||||||||||
|
@@ -179,6 +179,40 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s | |||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
public class DictionaryWithPackageKeyConverter<T> : JsonConverter | ||||||||||||||
{ | ||||||||||||||
public override bool CanConvert(Type objectType) | ||||||||||||||
=> typeof(Dictionary<PackageKey, T>).IsAssignableFrom(objectType); | ||||||||||||||
|
||||||||||||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||||||||||||||
{ | ||||||||||||||
var packages = new Dictionary<PackageKey, T>(); | ||||||||||||||
var jsonObject = JObject.Load(reader); | ||||||||||||||
|
||||||||||||||
foreach (var property in jsonObject.Properties()) | ||||||||||||||
{ | ||||||||||||||
var package = property.Value.ToObject<T>(serializer); | ||||||||||||||
packages[new PackageKey(property.Name)] = package; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
return packages; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | ||||||||||||||
{ | ||||||||||||||
var packages = (Dictionary<PackageKey, WorkloadPackage>)value; | ||||||||||||||
writer.WriteStartObject(); | ||||||||||||||
|
||||||||||||||
foreach (var kvp in packages) | ||||||||||||||
{ | ||||||||||||||
writer.WritePropertyName(kvp.Key.key); | ||||||||||||||
serializer.Serialize(writer, kvp.Value); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
writer.WriteEndObject(); | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
public class WorkloadPackageConverter : JsonConverter | ||||||||||||||
{ | ||||||||||||||
public override bool CanConvert(Type objectType) | ||||||||||||||
|
@@ -191,6 +225,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist | |||||||||||||
|
||||||||||||||
foreach (var property in jsonObject.Properties()) | ||||||||||||||
{ | ||||||||||||||
property.Value["name"] = property.Name; | ||||||||||||||
var package = property.Value.ToObject<WorkloadPackage>(serializer); | ||||||||||||||
packages[new PackageKey(property.Name)] = package; | ||||||||||||||
} | ||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
namespace vein.cli; | ||
|
||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
using Spectre.Console.Cli; | ||
using ITypeResolver = Spectre.Console.Cli.ITypeResolver; | ||
|
||
public static class SpectreConsoleHostBuilderExtensions | ||
{ | ||
public static IHostBuilder UseSpectreConsole(this IHostBuilder builder, Action<IConfigurator> configureCommandApp) | ||
{ | ||
builder = builder ?? throw new ArgumentNullException(nameof(builder)); | ||
|
||
builder.ConfigureServices((_, collection) => | ||
{ | ||
var command = new CommandApp(new TypeRegistrar(collection)); | ||
command.Configure(configureCommandApp); | ||
collection.AddSingleton<ICommandApp>(command); | ||
collection.AddHostedService<SpectreConsoleWorker>(); | ||
} | ||
); | ||
|
||
return builder; | ||
} | ||
|
||
public static IHostBuilder UseSpectreConsole<TDefaultCommand>(this IHostBuilder builder, | ||
Action<IConfigurator>? configureCommandApp = null) | ||
where TDefaultCommand : class, ICommand | ||
{ | ||
builder = builder ?? throw new ArgumentNullException(nameof(builder)); | ||
|
||
builder.ConfigureServices((_, collection) => | ||
{ | ||
var command = new CommandApp<TDefaultCommand>(new TypeRegistrar(collection)); | ||
if (configureCommandApp != null) | ||
{ | ||
command.Configure(configureCommandApp); | ||
} | ||
|
||
collection.AddSingleton<ICommandApp>(command); | ||
collection.AddHostedService<SpectreConsoleWorker>(); | ||
} | ||
); | ||
|
||
return builder; | ||
} | ||
} | ||
public sealed class TypeResolver(IServiceProvider serviceProvider) : ITypeResolver, IDisposable | ||
{ | ||
private readonly IServiceProvider _serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider)); | ||
|
||
public void Dispose() | ||
{ | ||
if (_serviceProvider is IDisposable disposable) | ||
{ | ||
disposable.Dispose(); | ||
} | ||
} | ||
|
||
public object? Resolve(Type? type) | ||
{ | ||
if (type == null) | ||
{ | ||
return null; | ||
} | ||
|
||
return _serviceProvider.GetService(type) ?? Activator.CreateInstance(type); | ||
} | ||
} | ||
public sealed class TypeRegistrar(IServiceCollection builder) : ITypeRegistrar | ||
{ | ||
public ITypeResolver Build() => new TypeResolver(builder.BuildServiceProvider()); | ||
|
||
public void Register(Type service, Type implementation) => builder.AddSingleton(service, implementation); | ||
|
||
public void RegisterInstance(Type service, object implementation) => builder.AddSingleton(service, implementation); | ||
|
||
public void RegisterLazy(Type service, Func<object> func) | ||
{ | ||
if (func is null) | ||
throw new ArgumentNullException(nameof(func)); | ||
|
||
builder.AddSingleton(service, _ => func()); | ||
} | ||
} | ||
|
||
public class SpectreConsoleWorker( | ||
ILogger<SpectreConsoleWorker> logger, | ||
ICommandApp commandApp, | ||
IHostApplicationLifetime hostLifetime) | ||
: IHostedService | ||
{ | ||
private int _exitCode; | ||
|
||
public Task StartAsync(CancellationToken cancellationToken) => | ||
Task.Factory.StartNew(async () => { | ||
try | ||
{ | ||
var args = GetArgs(); | ||
await Task.Delay(100, cancellationToken); | ||
_exitCode = await commandApp.RunAsync(args); | ||
} | ||
catch (Exception ex) | ||
{ | ||
logger.LogError(ex, "An unexpected error occurred"); | ||
_exitCode = 1; | ||
} | ||
finally | ||
{ | ||
hostLifetime.StopApplication(); | ||
} | ||
}, cancellationToken); | ||
|
||
public Task StopAsync(CancellationToken cancellationToken) | ||
{ | ||
Environment.ExitCode = _exitCode; | ||
return Task.CompletedTask; | ||
} | ||
|
||
private static string[] GetArgs() => Environment.GetCommandLineArgs().Skip(1).Where(x => !x.StartsWith("+")).ToArray(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
namespace vein.compiler.shared; | ||
|
||
using System.ComponentModel; | ||
using System.Diagnostics.CodeAnalysis; | ||
using Spectre.Console.Cli; | ||
|
||
[ExcludeFromCodeCoverage] | ||
public class CompileSettings : CommandSettings, IProjectSettingProvider | ||
{ | ||
[Description("Path to vproj file")] | ||
[CommandArgument(0, "[PROJECT]")] | ||
public string Project { get; set; } | ||
|
||
[Description("Display exported types table")] | ||
[CommandOption("--print-result-types")] | ||
public bool PrintResultType { get; set; } | ||
|
||
[Description("Display exported types table")] | ||
[CommandOption("--disable-optimization|-O")] | ||
public bool DisableOptimization { get; set; } | ||
|
||
[Description("Compile into single file")] | ||
[CommandOption("--single-file|-s")] | ||
public bool HasSingleFile { get; set; } | ||
|
||
[Description("Wait to attach debbugger (ONLY DEBUG COMPILER)")] | ||
[CommandOption("--sys-debugger")] | ||
public bool IsNeedDebuggerAttach { get; set; } | ||
[Description("Enable stacktrace printing when error.")] | ||
[CommandOption("--sys-stack-trace")] | ||
public bool DisplayStacktraceGenerator { get; set; } | ||
|
||
[Description("Generate shard package.")] | ||
[CommandOption("--gen-shard")] | ||
public bool GeneratePackageOutput { get; set; } | ||
|
||
[Description("Ignore cache.")] | ||
[CommandOption("--ignore-cache")] | ||
public bool IgnoreCache { get; set; } | ||
|
||
[Description("Override generated version")] | ||
[CommandOption("--override-version", IsHidden = true)] | ||
public string OverrideVersion { get; set; } | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Improve exception message for
WorkloadKeyContactConverter
.The
InvalidOperationException
thrown whenreader.Value
is not a string lacks a descriptive message. Providing a message can help with debugging.Committable suggestion