From 86761da7e60ddbc7474a7325eec9cd9c012b1e17 Mon Sep 17 00:00:00 2001 From: Cj <161484149+CJ-SPT@users.noreply.github.com> Date: Thu, 20 Jun 2024 20:38:58 -0400 Subject: [PATCH] Command improvements --- ReCodeItCLI/Commands/BuildCommand.cs | 78 +++++++++++++++++++++++----- 1 file changed, 66 insertions(+), 12 deletions(-) diff --git a/ReCodeItCLI/Commands/BuildCommand.cs b/ReCodeItCLI/Commands/BuildCommand.cs index 0671225..2fb1d1f 100644 --- a/ReCodeItCLI/Commands/BuildCommand.cs +++ b/ReCodeItCLI/Commands/BuildCommand.cs @@ -7,29 +7,82 @@ using ReCodeItLib.Utils; namespace ReCodeIt.Commands; -[Command("Build", Description = "Build your project and get a dll output for the original assembly. You dont need to provide a path if the last project you built is the one you want to target")] +[Command("Build", Description = "Build your project and get a dll output for the original assembly. You dont need to provide a path if the last project you built is the one you want to target, or you are running this command from inside a directory where a project file exists.")] public class BuildCommand : ICommand { private ReCodeItCrossCompiler CrossCompiler { get; set; } - [CommandParameter(0, IsRequired = false, Description = "the location of your project file")] + [CommandParameter(0, IsRequired = false, Description = "the location of your project file (ReCodeItProj.json)")] public string ProjectJsonPath { get; init; } public async ValueTask ExecuteAsync(IConsole console) { - if (ProjectJsonPath is not null && ProjectJsonPath != string.Empty) - { - CrossCompiler = new(); - ProjectManager.LoadProject(ProjectJsonPath); - CrossCompiler.StartCrossCompile(); + var isLocal = await UseLocalProject(console); - return; + if (isLocal) { return; } + + var isRemote = await UseRemoteProject(console); + + if (isRemote) { return; } + + await UseLastLoadedProject(console); + } + + private async Task UseLocalProject(IConsole console) + { + var jsonPath = Path.Combine(Directory.GetCurrentDirectory(), "ReCodeItProj.json"); + + if (File.Exists(jsonPath)) + { + Logger.Log("Found a project file in the current directory, loading it", ConsoleColor.Yellow); + + CrossCompiler = new(); + + DataProvider.LoadAppSettings(); + DataProvider.IsCli = true; + + ProjectManager.LoadProject(jsonPath); + await CrossCompiler.StartCrossCompile(); + + return true; } - console.Output.WriteLine(RegistryHelper.GetRegistryValue("LastLoadedProject")); + return false; + } + private async Task UseRemoteProject(IConsole console) + { + if (ProjectJsonPath is not null && ProjectJsonPath != string.Empty) + { + if (!File.Exists(ProjectJsonPath)) + { + console.Output.WriteLine("The project file you provided does not exist"); + return false; + } + + CrossCompiler = new(); + + DataProvider.LoadAppSettings(); + DataProvider.IsCli = true; + + ProjectManager.LoadProject(ProjectJsonPath); + await CrossCompiler.StartCrossCompile(); + + return true; + } + + return false; + } + + private async Task UseLastLoadedProject(IConsole console) + { if (RegistryHelper.GetRegistryValue("LastLoadedProject") != null) { + string currentDirectory = Directory.GetCurrentDirectory(); + + console.Output.WriteLine($"Project: {RegistryHelper.GetRegistryValue("LastLoadedProject")}"); + console.Output.WriteLine($"Working Dir: {currentDirectory}"); + CrossCompiler = new(); DataProvider.LoadAppSettings(); @@ -37,15 +90,16 @@ public class BuildCommand : ICommand ProjectManager.LoadProject(RegistryHelper.GetRegistryValue("LastLoadedProject"), true); - if (!Validate(console)) { return; } + if (!Validate(console)) { return false; } await CrossCompiler.StartCrossCompile(); DataProvider.SaveAppSettings(); - return; + + return true; } - return; + return false; } private bool Validate(IConsole console)