From cbc4d6149f50e7bf721b8cdd5da17ade87a66702 Mon Sep 17 00:00:00 2001 From: Yoshi Askharoun Date: Mon, 30 Dec 2024 03:43:09 -0600 Subject: [PATCH] Remove LoadFromAssemblyPath hack from compiler command --- UIXC/Commands/CompileCommand.cs | 21 ++++++---- UIXC/Commands/CompilerCommandBase.cs | 62 ++++++++++++++++++++++++++++ UIXC/Commands/DecompileCommand.cs | 52 +++-------------------- 3 files changed, 81 insertions(+), 54 deletions(-) diff --git a/UIXC/Commands/CompileCommand.cs b/UIXC/Commands/CompileCommand.cs index fd64cc1..67d461a 100644 --- a/UIXC/Commands/CompileCommand.cs +++ b/UIXC/Commands/CompileCommand.cs @@ -41,18 +41,25 @@ public class CompileCommand : CompilerCommandBase // Configure error and warning messages BeginErrorReporting(new IrisSourceRepository(compilands, dataTableInput)); - TraceSettings.Current.SetCategoryLevel(TraceCategory.Markup, byte.MaxValue); - TraceSettings.Current.SetCategoryLevel(TraceCategory.MarkupCompiler, byte.MaxValue); - TraceSettings.Current.SetCategoryLevel(TraceCategory.Tool, byte.MaxValue); - TraceSettings.Current.OnWriteLine += (line) => + + if (settings.Verbose) { - AnsiConsole.MarkupLineInterpolated($"[grey]{line}[/]"); - }; + TraceSettings.Current.SetCategoryLevel(TraceCategory.Markup, byte.MaxValue); + TraceSettings.Current.SetCategoryLevel(TraceCategory.MarkupCompiler, byte.MaxValue); + TraceSettings.Current.SetCategoryLevel(TraceCategory.Tool, byte.MaxValue); + TraceSettings.Current.OnWriteLine += (line) => + { + AnsiConsole.MarkupLineInterpolated($"[grey]{line}[/]"); + }; + } MarkupSystem.Startup(true); Assembler.RegisterLoader(); - System.Runtime.Loader.AssemblyLoadContext.Default.LoadFromAssemblyPath(@"C:\Program Files\Zune\UIXcontrols.dll"); + var loadAssembliesResult = LoadAssemblies(settings); + if (loadAssembliesResult < 0) + return loadAssembliesResult; + var success = MarkupCompiler.Compile(compilands, dataTableInput ?? default); StopErrorReporting(); diff --git a/UIXC/Commands/CompilerCommandBase.cs b/UIXC/Commands/CompilerCommandBase.cs index 2816462..ded3d61 100644 --- a/UIXC/Commands/CompilerCommandBase.cs +++ b/UIXC/Commands/CompilerCommandBase.cs @@ -3,11 +3,15 @@ using Microsoft.Iris.Session; using Spectre.Console; using Spectre.Console.Cli; using System.Collections; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.Loader; namespace UIXC.Commands; public abstract class CompilerCommandBase : Command where TSettings : CompilerSettings { + private string[]? _searchPaths; + public Report? Report { get; private set; } protected void BeginErrorReporting(IrisSourceRepository repo) @@ -52,4 +56,62 @@ public abstract class CompilerCommandBase : Command where { ErrorManager.OnErrors -= OnError; } + + protected static string ResolvePath(string givenPath, ICollection searchPaths) + { + if (!TryResolvePath(givenPath, searchPaths, out var assemblyPath)) + throw new FileNotFoundException(null, givenPath); + return assemblyPath; + } + + protected static bool TryResolvePath(string givenPath, ICollection searchPaths, [NotNullWhen(true)] out string? absolutePath) + { + if (Path.IsPathFullyQualified(givenPath)) + { + absolutePath = givenPath; + return Path.Exists(absolutePath); + } + + absolutePath = null; + if (searchPaths.Count == 0) + return false; + + foreach (var searchPath in searchPaths) + { + absolutePath = Path.GetFullPath(givenPath, searchPath); + if (Path.Exists(absolutePath)) + return true; + } + + return false; + } + + [MemberNotNull(nameof(_searchPaths))] + protected string[] GetSearchPaths(TSettings settings) + { + return _searchPaths ??= settings.IncludeDirectories.Prepend(Environment.CurrentDirectory).ToArray(); + } + + [MemberNotNull(nameof(_searchPaths))] + protected int LoadAssemblies(TSettings settings) + { + GetSearchPaths(settings); + + foreach (var givenPath in settings.Assemblies ?? []) + { + try + { + var assemblyPath = ResolvePath(givenPath, _searchPaths); + _ = AssemblyLoadContext.Default.LoadFromAssemblyPath(assemblyPath); + } + catch (Exception ex) + { + AnsiConsole.WriteException(ex); + if (!AnsiConsole.Confirm("Do you want to continue?", false)) + return -1; + } + } + + return 0; + } } diff --git a/UIXC/Commands/DecompileCommand.cs b/UIXC/Commands/DecompileCommand.cs index 3931b2c..c23bf94 100644 --- a/UIXC/Commands/DecompileCommand.cs +++ b/UIXC/Commands/DecompileCommand.cs @@ -34,22 +34,9 @@ public class DecompileCommand : CompilerCommandBase BeginErrorReporting(new IrisSourceRepository(settings.Inputs)); - var searchPaths = settings.IncludeDirectories.Prepend(Environment.CurrentDirectory).ToArray(); - - foreach (var givenPath in settings.Assemblies ?? []) - { - try - { - var assemblyPath = ResolvePath(givenPath, searchPaths); - _ = AssemblyLoadContext.Default.LoadFromAssemblyPath(assemblyPath); - } - catch (Exception ex) - { - AnsiConsole.WriteException(ex); - if (!AnsiConsole.Confirm("Do you want to continue?", false)) - return -1; - } - } + var loadAssembliesResult = LoadAssemblies(settings); + if (loadAssembliesResult < 0) + return loadAssembliesResult; foreach (var redirectOption in settings.ImportRedirects ?? []) { @@ -66,7 +53,7 @@ public class DecompileCommand : CompilerCommandBase { try { - if (!TryResolvePath(input, searchPaths, out var inputPath)) + if (!TryResolvePath(input, GetSearchPaths(settings), out var inputPath)) inputPath = input; if (!inputPath.Contains("://")) @@ -118,41 +105,12 @@ public class DecompileCommand : CompilerCommandBase var fileName = Path.GetFileName(inputFilePath);//.Replace('!', '/'); var outputFile = Path.Combine(settings.OutputDir, Path.ChangeExtension(fileName, settings.Language.GetExtension())); - var outputDir = Path.GetDirectoryName(outputFile); + var outputDir = Path.GetDirectoryName(outputFile)!; Directory.CreateDirectory(outputDir); return outputFile; } - private static string ResolvePath(string givenPath, ICollection searchPaths) - { - if (!TryResolvePath(givenPath, searchPaths, out var assemblyPath)) - throw new FileNotFoundException(null, givenPath); - return assemblyPath; - } - - private static bool TryResolvePath(string givenPath, ICollection searchPaths, [NotNullWhen(true)] out string? absolutePath) - { - if (Path.IsPathFullyQualified(givenPath)) - { - absolutePath = givenPath; - return Path.Exists(absolutePath); - } - - absolutePath = null; - if (searchPaths.Count == 0) - return false; - - foreach (var searchPath in searchPaths) - { - absolutePath = Path.GetFullPath(givenPath, searchPath); - if (Path.Exists(absolutePath)) - return true; - } - - return false; - } - public sealed class Settings : CompilerSettings { [Description("The UIB files to decompile.")]