Use base compiler command

This commit is contained in:
Yoshi Askharoun
2024-07-07 22:40:19 -05:00
parent 1d5243b8bf
commit eb3f233047
4 changed files with 78 additions and 69 deletions
+13 -41
View File
@@ -1,16 +1,14 @@
using Errata;
using Microsoft.Iris;
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<CompileCommand.Settings>
public class CompileCommand : CompilerCommandBase<CompileCommand.Settings>
{
public override int Execute(CommandContext context, Settings settings)
{
@@ -31,46 +29,18 @@ public class CompileCommand : Command<CompileCommand.Settings>
};
}
CompilerInput dataTableInput = new();
CompilerInput? dataTableInput = null;
if (settings.DataTable is not null)
{
dataTableInput.SourceFileName = settings.DataTable;
dataTableInput.OutputFileName = SourceToCompiledPath(settings, settings.DataTable);
dataTableInput = new()
{
SourceFileName = settings.DataTable,
OutputFileName = SourceToCompiledPath(settings, settings.DataTable)
};
}
// Configure error and warning messages
var report = new Report(new IrisSourceRepository(compilands, default));
ErrorManager.OnErrors += (errors) =>
{
foreach (ErrorRecord error in errors)
{
var l = error.Line;
var c = error.Column;
var msg = error.Message;
var ctx = error.Context;
var diagnostic = error.Warning
? Diagnostic.Warning(msg)
: Diagnostic.Error(msg);
if (ctx is not null)
{
if (error.Line >= 0 && error.Column >= 0)
{
var label = new Label(ctx, new Location(l, c), "")
.WithColor(error.Warning ? Color.Yellow : Color.Red);
diagnostic.Labels.Add(label);
}
else
{
diagnostic.Note = ctx;
}
}
report.AddDiagnostic(diagnostic);
}
};
BeginErrorReporting(new IrisSourceRepository(compilands, dataTableInput));
TraceSettings.Current.SetCategoryLevel(TraceCategory.Markup, byte.MaxValue);
TraceSettings.Current.SetCategoryLevel(TraceCategory.MarkupCompiler, byte.MaxValue);
TraceSettings.Current.OnWriteLine += (line) =>
@@ -81,9 +51,11 @@ public class CompileCommand : Command<CompileCommand.Settings>
MarkupSystem.Startup(true);
Assembler.RegisterLoader();
var success = MarkupCompiler.Compile(compilands, dataTableInput);
System.Runtime.Loader.AssemblyLoadContext.Default.LoadFromAssemblyPath(@"C:\Program Files\Zune\UIXcontrols.dll");
var success = MarkupCompiler.Compile(compilands, dataTableInput ?? default);
report.Render(AnsiConsole.Console);
StopErrorReporting();
Report?.Render(AnsiConsole.Console);
AnsiConsole.WriteLine();
AnsiConsole.WriteLine();
+55
View File
@@ -0,0 +1,55 @@
using Errata;
using Microsoft.Iris.Session;
using Spectre.Console;
using Spectre.Console.Cli;
using System.Collections;
namespace UIXC.Commands;
public abstract class CompilerCommandBase<TSettings> : Command<TSettings> where TSettings : CompilerSettings
{
public Report? Report { get; private set; }
protected void BeginErrorReporting(IrisSourceRepository repo)
{
Report = new Report(repo);
ErrorManager.OnErrors += OnError;
}
private void OnError(IList errors)
{
foreach (ErrorRecord error in errors)
{
var l = error.Line;
var c = error.Column;
var msg = error.Message;
var ctx = error.Context;
var diagnostic = error.Warning
? Diagnostic.Warning(msg)
: Diagnostic.Error(msg);
if (ctx is not null)
{
if (error.Line >= 0 && error.Column >= 0)
{
var label = new Label(ctx, new Location(l, c), "")
.WithColor(error.Warning ? Color.Yellow : Color.Red);
diagnostic.Labels.Add(label);
}
else
{
diagnostic.Note = ctx;
}
}
Report?.AddDiagnostic(diagnostic);
}
}
protected void StopErrorReporting()
{
ErrorManager.OnErrors -= OnError;
}
}
+2 -11
View File
@@ -10,7 +10,7 @@ using System.Runtime.Loader;
namespace UIXC.Commands;
public class DecompileCommand : Command<DecompileCommand.Settings>
public class DecompileCommand : CompilerCommandBase<DecompileCommand.Settings>
{
public override int Execute(CommandContext context, Settings settings)
{
@@ -32,16 +32,7 @@ public class DecompileCommand : Command<DecompileCommand.Settings>
};
}
ErrorManager.OnErrors += (errors) =>
{
foreach (ErrorRecord error in errors)
{
var (color, type) = error.Warning
? ("yellow", "WARN") : ("red", "ERROR");
AnsiConsole.MarkupLineInterpolated($"[{color}]{type}: {error.Message}[/]");
}
};
BeginErrorReporting(new IrisSourceRepository(settings.Inputs));
var searchPaths = settings.IncludeDirectories.Prepend(Environment.CurrentDirectory).ToArray();
+8 -17
View File
@@ -1,29 +1,20 @@
using Errata;
using Microsoft.Iris;
using System.Diagnostics.CodeAnalysis;
namespace UIXC;
internal class IrisSourceRepository : ISourceRepository
public class IrisSourceRepository(IEnumerable<string?> compilands) : ISourceRepository
{
private readonly Dictionary<string, Source?> _sourceCache;
private readonly Dictionary<string, Source?> _sourceCache = compilands
.Where(c => c is not null).Cast<string>()
.Where(c => !c.Contains(Uri.SchemeDelimiter))
.Select(c => (c, (Source?)null))
.ToDictionary();
public IrisSourceRepository(CompilerInput[] compilands, CompilerInput? sharedBinaryDataTable)
: this(compilands.Select(c => c.SourceFileName).AsEnumerable().Append(sharedBinaryDataTable.HasValue ? sharedBinaryDataTable.Value.SourceFileName : null))
{
_sourceCache = new(compilands.Length + 1);
foreach (var compiland in compilands)
{
var id = compiland.SourceFileName;
// We can only read source from local files
if (id.Contains(Uri.SchemeDelimiter))
continue;
_sourceCache.Add(id, null);
}
if (sharedBinaryDataTable?.SourceFileName is not null)
_sourceCache.Add(sharedBinaryDataTable.Value.SourceFileName, null);
}
public bool TryGet(string uri, [NotNullWhen(true)] out Source? source)