From 9aabdd40dcb0f0d5e5172cd181d502218f269424 Mon Sep 17 00:00:00 2001 From: Yoshi Askharoun Date: Thu, 22 Feb 2024 08:27:49 -0600 Subject: [PATCH] Use Spectre.Console.Cli --- UIXC/Commands/CompileCommand.cs | 96 +++++++++++++++++++++++++++++++++ UIXC/Program.cs | 74 ++++--------------------- UIXC/UIXC.csproj | 1 + 3 files changed, 108 insertions(+), 63 deletions(-) create mode 100644 UIXC/Commands/CompileCommand.cs diff --git a/UIXC/Commands/CompileCommand.cs b/UIXC/Commands/CompileCommand.cs new file mode 100644 index 0000000..1b3478d --- /dev/null +++ b/UIXC/Commands/CompileCommand.cs @@ -0,0 +1,96 @@ +using Errata; +using Microsoft.Iris; +using Microsoft.Iris.Asm; +using Microsoft.Iris.Debug; +using Microsoft.Iris.Markup; +using Microsoft.Iris.Session; +using Spectre.Console; +using Spectre.Console.Cli; +using System.ComponentModel; + +namespace UIXC.Commands; + +public class CompileCommand : Command +{ + public override int Execute(CommandContext context, Settings settings) + { + if (settings.Compilands.Length <= 0) + { + Diagnostic.Error("Missing arguments: must specify a file to compile. Accepts XML and Asm source."); + //AnsiConsole.MarkupLine("[red][/]"); + return -1; + } + + // TODO: Support multiple compilands + string sourcePath = settings.Compilands[0]; + CompilerInput compiland = new() + { + SourceFileName = sourcePath, + OutputFileName = Path.Combine(Environment.CurrentDirectory, Path.ChangeExtension(sourcePath, ".uib")) + }; + CompilerInput[] compilands = [compiland]; + + // Configure error and warning messages + var report = new Report(new IrisSourceRepository(compilands, default)); + ErrorManager.OnErrors += (errors) => + { + foreach (ErrorRecord error in errors) + { + var l = Math.Max(0, error.Line); + var c = Math.Max(0, error.Column); + var msg = error.Message; + var ctx = error.Context; + + const string compilerError = "Iris error"; + var diagnostic = error.Warning ? Diagnostic.Warning(compilerError) : Diagnostic.Error(compilerError); + + report.AddDiagnostic(diagnostic + .WithCode("UIX0000") + .WithLabel(new(ctx, new Location(l, c), msg)) + ); + } + }; + TraceSettings.Current.SetCategoryLevel(TraceCategory.Markup, byte.MaxValue); + TraceSettings.Current.SetCategoryLevel(TraceCategory.MarkupCompiler, byte.MaxValue); + TraceSettings.Current.OnWriteLine += (line) => + { + AnsiConsole.MarkupLineInterpolated($"[grey]{line}[/]"); + }; + + MarkupSystem.Startup(true); + Assembler.RegisterLoader(); + + // TODO: Support shared binary data tables + var success = MarkupCompiler.Compile(compilands, default); + + if (!success) + { + AnsiConsole.MarkupLine("[red]Compilation failed.[/]"); + return -1; + } + else + { + AnsiConsole.MarkupLine("[green]Compilation finished.[/]"); + return 0; + } + } + + public sealed class Settings : CommandSettings + { + [Description("The source files to compile.")] + [CommandArgument(0, "[compilands]")] + public required string[] Compilands { get; init; } + + [Description("Directories to search for imported files in.")] + [CommandOption("-I ")] + public required string[] IncludeDirectories { get; init; } + + [Description("A shared binary data table to compile with.")] + [CommandOption("-t|--dataTable")] + public string? DataTable { get; init; } + + [CommandOption("--verbose")] + [DefaultValue(false)] + public bool Verbose { get; init; } + } +} diff --git a/UIXC/Program.cs b/UIXC/Program.cs index 70faa0b..424e50c 100644 --- a/UIXC/Program.cs +++ b/UIXC/Program.cs @@ -1,10 +1,5 @@ -using Errata; -using Microsoft.Iris; -using Microsoft.Iris.Asm; -using Microsoft.Iris.Debug; -using Microsoft.Iris.Markup; -using Microsoft.Iris.Session; -using Spectre.Console; +using Spectre.Console.Cli; +using UIXC.Commands; namespace UIXC; @@ -12,63 +7,16 @@ internal class Program { static int Main(string[] args) { - if (args.Length == 0) + var app = new CommandApp(); + + app.Configure(config => { - AnsiConsole.MarkupLine("[red]Missing arguments: must specify a file to compile. Accepts XML and Asm source.[/]"); - return -1; - } +#if DEBUG + config.PropagateExceptions(); + config.ValidateExamples(); +#endif + }); - // TODO: Use Spectre.Console.Cli - string sourcePath = args[0]; - CompilerInput compiland = new() - { - SourceFileName = sourcePath, - OutputFileName = Path.Combine(Environment.CurrentDirectory, Path.ChangeExtension(sourcePath, ".uib")) - }; - CompilerInput[] compilands = [compiland]; - - // Configure error and warning messages - var report = new Report(new IrisSourceRepository(compilands, default)); - ErrorManager.OnErrors += (errors) => - { - foreach (ErrorRecord error in errors) - { - var l = Math.Max(0, error.Line); - var c = Math.Max(0, error.Column); - var msg = error.Message; - var ctx = error.Context; - - const string compilerError = "Iris error"; - var diagnostic = error.Warning ? Diagnostic.Warning(compilerError) : Diagnostic.Error(compilerError); - - report.AddDiagnostic(diagnostic - .WithCode("UIX0000") - .WithLabel(new(ctx, new Location(l, c), msg)) - ); - } - }; - TraceSettings.Current.SetCategoryLevel(TraceCategory.Markup, byte.MaxValue); - TraceSettings.Current.SetCategoryLevel(TraceCategory.MarkupCompiler, byte.MaxValue); - TraceSettings.Current.OnWriteLine += (line) => - { - AnsiConsole.MarkupLineInterpolated($"[grey]{line}[/]"); - }; - - MarkupSystem.Startup(true); - Assembler.RegisterLoader(); - - // TODO: Support shared binary data tables - var success = MarkupCompiler.Compile(compilands, default); - - if (!success) - { - AnsiConsole.MarkupLine("[red]Compilation failed.[/]"); - return -1; - } - else - { - AnsiConsole.MarkupLine("[green]Compilation finished.[/]"); - return 0; - } + return app.Run(args); } } diff --git a/UIXC/UIXC.csproj b/UIXC/UIXC.csproj index c0dbb0d..3452dc1 100644 --- a/UIXC/UIXC.csproj +++ b/UIXC/UIXC.csproj @@ -10,6 +10,7 @@ +