From 64391cc09f93f92ace05dde11bb00fc40e97d086 Mon Sep 17 00:00:00 2001 From: Chris Tacke Date: Sun, 11 Feb 2024 08:24:46 -0600 Subject: [PATCH] Change current directory. Improve a couple error returns --- Source/v2/Meadow.CLI/CommandErrors.cs | 2 ++ .../Commands/Current/App/AppBuildCommand.cs | 8 ++++---- .../Cloud/Package/CloudPackageCreateCommand.cs | 12 ++++++------ Source/v2/Meadow.Cli/Program.cs | 2 +- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/Source/v2/Meadow.CLI/CommandErrors.cs b/Source/v2/Meadow.CLI/CommandErrors.cs index 7b8094f1..20f2b598 100644 --- a/Source/v2/Meadow.CLI/CommandErrors.cs +++ b/Source/v2/Meadow.CLI/CommandErrors.cs @@ -5,5 +5,7 @@ public enum CommandErrors Success = 0, GeneralError = 1, UserCancelled = 2, + FileNotFound = 3, + DirectoryNotFound = 4, } } \ No newline at end of file diff --git a/Source/v2/Meadow.Cli/Commands/Current/App/AppBuildCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/App/AppBuildCommand.cs index 98324319..92612bd1 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/App/AppBuildCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/App/AppBuildCommand.cs @@ -1,4 +1,5 @@ using CliFx.Attributes; +using CliFx.Exceptions; using Meadow.Package; using Microsoft.Extensions.Logging; @@ -23,7 +24,7 @@ public AppBuildCommand(IPackageManager packageManager, ILoggerFactory loggerFact protected override ValueTask ExecuteCommand() { - string path = Path ?? AppDomain.CurrentDomain.BaseDirectory; + string path = Path ?? Directory.GetCurrentDirectory(); // is the path a file? if (!File.Exists(path)) @@ -31,8 +32,7 @@ protected override ValueTask ExecuteCommand() // is it a valid directory? if (!Directory.Exists(path)) { - Logger?.LogError($"Invalid application path '{path}'"); - return ValueTask.CompletedTask; + throw new CommandException($"Invalid application path '{path}'", (int)CommandErrors.FileNotFound); } } @@ -45,7 +45,7 @@ protected override ValueTask ExecuteCommand() if (!success) { - Logger?.LogError($"Build failed!"); + throw new CommandException($"Build failed.", (int)CommandErrors.GeneralError); } else { diff --git a/Source/v2/Meadow.Cli/Commands/Current/Cloud/Package/CloudPackageCreateCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/Cloud/Package/CloudPackageCreateCommand.cs index fb098a82..49da8c19 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/Cloud/Package/CloudPackageCreateCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/Cloud/Package/CloudPackageCreateCommand.cs @@ -1,4 +1,5 @@ using CliFx.Attributes; +using CliFx.Exceptions; using Meadow.Cloud.Client; using Meadow.Cloud.Client.Identity; using Meadow.Package; @@ -42,26 +43,25 @@ public CloudPackageCreateCommand( protected override async ValueTask ExecuteCommand() { - ProjectPath ??= AppDomain.CurrentDomain.BaseDirectory; + ProjectPath ??= Directory.GetCurrentDirectory(); ProjectPath = Path.GetFullPath(ProjectPath); if (!Directory.Exists(ProjectPath)) { - throw new DirectoryNotFoundException($"Directory not found '{ProjectPath}'. Check path to project file."); + throw new CommandException($"Directory not found '{ProjectPath}'. Check path to project file.", (int)CommandErrors.DirectoryNotFound); } // build Logger?.LogInformation($"Building {Configuration} version of application..."); if (!_packageManager.BuildApplication(ProjectPath, Configuration, true, CancellationToken)) { - return; + throw new CommandException($"Build failed.", (int)CommandErrors.GeneralError); } var candidates = PackageManager.GetAvailableBuiltConfigurations(ProjectPath, "App.dll"); if (candidates.Length == 0) { - Logger?.LogError($"Cannot find a compiled application at '{ProjectPath}'"); - return; + throw new CommandException($"Cannot find a compiled application at '{ProjectPath}'", (int)CommandErrors.FileNotFound); } var store = _fileManager.Firmware["Meadow F7"]; @@ -86,7 +86,7 @@ protected override async ValueTask ExecuteCommand() } else { - Logger?.LogError($"Package assembly failed."); + throw new CommandException($"Package assembly failed.", (int)CommandErrors.GeneralError); } } } \ No newline at end of file diff --git a/Source/v2/Meadow.Cli/Program.cs b/Source/v2/Meadow.Cli/Program.cs index b4a23ed7..0de0462a 100644 --- a/Source/v2/Meadow.Cli/Program.cs +++ b/Source/v2/Meadow.Cli/Program.cs @@ -74,7 +74,7 @@ public static async Task Main(string[] args) if (File.Exists("appsettings.json")) { var config = new ConfigurationBuilder() - .SetBasePath(AppDomain.CurrentDomain.BaseDirectory) + .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json") .Build();