Skip to content

Commit

Permalink
Merge pull request #357 from WildernessLabs/dominique-V2PortListRefactor
Browse files Browse the repository at this point in the history
Refactor to support port list without connection and with console int…
  • Loading branch information
ctacke committed Sep 28, 2023
2 parents 50a216b + 19c0387 commit 4f8d5c8
Show file tree
Hide file tree
Showing 18 changed files with 205 additions and 81 deletions.
16 changes: 9 additions & 7 deletions Source/v2/Meadow.Cli/Commands/Current/App/AppBuildCommand.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using CliFx.Attributes;
using CliFx.Infrastructure;
using Meadow.Cli;
using Meadow.Hcom;
using Microsoft.Extensions.Logging;

namespace Meadow.CLI.Commands.DeviceManagement;
Expand All @@ -15,13 +17,13 @@ public class AppBuildCommand : BaseCommand<AppBuildCommand>
[CommandParameter(0, Name = "Path to project file", IsRequired = false)]
public string? Path { get; set; } = default!;

public AppBuildCommand(IPackageManager packageManager, ISettingsManager settingsManager, ILoggerFactory loggerFactory)
: base(settingsManager, loggerFactory)
public AppBuildCommand(IPackageManager packageManager, ILoggerFactory loggerFactory)
: base(loggerFactory)
{
_packageManager = packageManager;
}

protected override async ValueTask ExecuteCommand(CancellationToken cancellationToken)
protected override async ValueTask ExecuteCommand(CancellationToken? cancellationToken)
{
string path = Path == null
? AppDomain.CurrentDomain.BaseDirectory
Expand All @@ -33,25 +35,25 @@ protected override async ValueTask ExecuteCommand(CancellationToken cancellation
// is it a valid directory?
if (!Directory.Exists(path))
{
Logger.LogError($"Invalid application path '{path}'");
Logger?.LogError($"Invalid application path '{path}'");
return;
}
}

if (Configuration == null) Configuration = "Release";

Logger.LogInformation($"Building {Configuration} configuration of {path}...");
Logger?.LogInformation($"Building {Configuration} configuration of {path}...");

// TODO: enable cancellation of this call
var success = _packageManager.BuildApplication(path, Configuration);

if (!success)
{
Logger.LogError($"Build failed!");
Logger?.LogError($"Build failed!");
}
else
{
Logger.LogError($"Build success.");
Logger?.LogError($"Build success.");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CliFx.Attributes;
using CliFx.Infrastructure;
using Meadow.Cli;
using Meadow.Hcom;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -96,4 +97,4 @@ protected override async ValueTask ExecuteCommand(IMeadowConnection connection,
await connection.RuntimeEnable(cancellationToken);
}
}
}
}
15 changes: 8 additions & 7 deletions Source/v2/Meadow.Cli/Commands/Current/App/AppTrimCommand.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CliFx.Attributes;
using CliFx.Infrastructure;
using Meadow.Cli;
using Microsoft.Extensions.Logging;

Expand All @@ -15,13 +16,13 @@ public class AppTrimCommand : BaseCommand<AppTrimCommand>
[CommandParameter(0, Name = "Path to project file", IsRequired = false)]
public string? Path { get; set; } = default!;

public AppTrimCommand(IPackageManager packageManager, ISettingsManager settingsManager, ILoggerFactory loggerFactory)
: base(settingsManager, loggerFactory)
public AppTrimCommand(IPackageManager packageManager, ILoggerFactory loggerFactory)
: base(loggerFactory)
{
_packageManager = packageManager;
}

protected override async ValueTask ExecuteCommand(CancellationToken cancellationToken)
protected override async ValueTask ExecuteCommand(CancellationToken? cancellationToken)
{
string path = Path == null
? AppDomain.CurrentDomain.BaseDirectory
Expand All @@ -35,7 +36,7 @@ protected override async ValueTask ExecuteCommand(CancellationToken cancellation
// is it a valid directory?
if (!Directory.Exists(path))
{
Logger.LogError($"Invalid application path '{path}'");
Logger?.LogError($"Invalid application path '{path}'");
return;
}

Expand All @@ -44,7 +45,7 @@ protected override async ValueTask ExecuteCommand(CancellationToken cancellation

if (candidates.Length == 0)
{
Logger.LogError($"Cannot find a compiled application at '{path}'");
Logger?.LogError($"Cannot find a compiled application at '{path}'");
return;
}

Expand All @@ -56,8 +57,8 @@ protected override async ValueTask ExecuteCommand(CancellationToken cancellation
}

// if no configuration was provided, find the most recently built
Logger.LogInformation($"Trimming {file.FullName} (this may take a few seconds)...");
Logger?.LogInformation($"Trimming {file.FullName} (this may take a few seconds)...");

await _packageManager.TrimApplication(file, false, null, cancellationToken);
await _packageManager.TrimApplication(file, false, null, cancellationToken ?? default);
}
}
25 changes: 14 additions & 11 deletions Source/v2/Meadow.Cli/Commands/Current/BaseCommand.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
using CliFx;
using CliFx.Infrastructure;
using Meadow.Cli;
using Meadow.Hcom;
using Microsoft.Extensions.Logging;

namespace Meadow.CLI.Commands.DeviceManagement;

public abstract class BaseCommand<T> : ICommand
{
protected ILogger<T> Logger { get; }
protected ISettingsManager SettingsManager { get; }
protected ILogger<T>? Logger { get; }
protected ILoggerFactory? LoggerFactory { get; }
protected IConsole? Console { get; private set; }

public BaseCommand(ISettingsManager settingsManager, ILoggerFactory loggerFactory)
public BaseCommand(ILoggerFactory? loggerFactory)
{
Logger = loggerFactory.CreateLogger<T>();
SettingsManager = settingsManager;
LoggerFactory = loggerFactory;
Logger = loggerFactory?.CreateLogger<T>();
}

protected abstract ValueTask ExecuteCommand(CancellationToken cancellationToken);
protected abstract ValueTask ExecuteCommand(CancellationToken? cancellationToken);

public virtual async ValueTask ExecuteAsync(IConsole console)
{
var cancellationToken = console.RegisterCancellationHandler();
Console = console;
var cancellationToken = Console?.RegisterCancellationHandler();

try
{
await ExecuteCommand(cancellationToken);
if (cancellationToken!= null)
await ExecuteCommand(cancellationToken);
}
catch (Exception ex)
{
Logger.LogError(ex.Message);
Logger?.LogError(ex.Message);
}
}
}
}
2 changes: 1 addition & 1 deletion Source/v2/Meadow.Cli/Commands/Current/BaseFileCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Meadow.CLI.Commands.DeviceManagement;

public abstract class BaseFileCommand<T> : BaseCommand<T>
public abstract class BaseFileCommand<T> : BaseSettingsCommand<T>
{
protected FileManager FileManager { get; }

Expand Down
16 changes: 16 additions & 0 deletions Source/v2/Meadow.Cli/Commands/Current/BaseSettingsCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using CliFx.Infrastructure;
using Meadow.Cli;
using Microsoft.Extensions.Logging;

namespace Meadow.CLI.Commands.DeviceManagement;

public abstract class BaseSettingsCommand<T> : BaseCommand<T>
{
protected ISettingsManager SettingsManager { get; }

public BaseSettingsCommand(ISettingsManager settingsManager, ILoggerFactory? loggerFactory)
: base (loggerFactory)
{
SettingsManager = settingsManager;
}
}
35 changes: 16 additions & 19 deletions Source/v2/Meadow.Cli/Commands/Current/ConfigCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,64 +8,61 @@
namespace Meadow.CLI.Commands.DeviceManagement;

[Command("config", Description = "Read or modify the meadow CLI configuration")]
public class ConfigCommand : ICommand
public class ConfigCommand : BaseSettingsCommand<ConfigCommand>
{
private readonly ISettingsManager _settingsManager;
private readonly ILogger<DeviceInfoCommand>? _logger;

[CommandOption("list", IsRequired = false)]
public bool List { get; set; }

[CommandParameter(0, Name = "Settings", IsRequired = false)]
public string[] Settings { get; set; }
public string[]? Settings { get; set; }

public ConfigCommand(ISettingsManager settingsManager, ILoggerFactory? loggerFactory)
: base(settingsManager, loggerFactory)
{
_logger = loggerFactory?.CreateLogger<DeviceInfoCommand>();
_settingsManager = settingsManager;

}

public async ValueTask ExecuteAsync(IConsole console)
protected override async ValueTask ExecuteCommand(CancellationToken? cancellationToken)
{
if (List)
{
_logger?.LogInformation($"Current CLI configuration");
Logger?.LogInformation($"Current CLI configuration");

// display all current config
var settings = _settingsManager.GetPublicSettings();
var settings = SettingsManager.GetPublicSettings();
if (settings.Count == 0)
{
_logger?.LogInformation($" <no settings found>");
Logger?.LogInformation($" <no settings found>");
}
else
{
foreach (var kvp in _settingsManager.GetPublicSettings())
foreach (var kvp in SettingsManager.GetPublicSettings())
{
_logger?.LogInformation($" {kvp.Key} = {kvp.Value}");
Logger?.LogInformation($" {kvp.Key} = {kvp.Value}");
}
}
}
else
{
switch (Settings.Length)
switch (Settings?.Length)
{
case 0:
// not valid
throw new CommandException($"No setting provided");
case 1:
// erase a setting
_logger?.LogInformation($"Deleting Setting {Settings[0]}");
_settingsManager.DeleteSetting(Settings[0]);
Logger?.LogInformation($"{Environment.NewLine}Deleting Setting {Settings[0]}");
SettingsManager.DeleteSetting(Settings[0]);
break;
case 2:
// set a setting
_logger?.LogInformation($"Setting {Settings[0]}={Settings[1]}");
_settingsManager.SaveSetting(Settings[0], Settings[1]);
Logger?.LogInformation($"{Environment.NewLine}Setting {Settings[0]}={Settings[1]}");
SettingsManager.SaveSetting(Settings[0], Settings[1]);
break;
default:
// not valid;
throw new CommandException($"Too many parameters provided");
}
}
}
}
}
18 changes: 10 additions & 8 deletions Source/v2/Meadow.Cli/Commands/Current/Dfu/DfuInstallCommand.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using CliFx.Attributes;
using CliFx.Infrastructure;
using Meadow.Cli;
using Meadow.CLI.Core.Internals.Dfu;
using Meadow.Hcom;
using Meadow.Software;
using Microsoft.Extensions.Logging;
using System.Runtime.InteropServices;
Expand All @@ -9,7 +11,7 @@
namespace Meadow.CLI.Commands.DeviceManagement;

[Command("dfu install", Description = "Deploys a built Meadow application to a target device")]
public class DfuInstallCommand : BaseCommand<AppDeployCommand>
public class DfuInstallCommand : BaseSettingsCommand<AppDeployCommand>
{
public const string DefaultVersion = "0.11";

Expand All @@ -27,7 +29,7 @@ public DfuInstallCommand(ISettingsManager settingsManager, ILoggerFactory logger
{
}

protected override async ValueTask ExecuteCommand(CancellationToken cancellationToken)
protected override async ValueTask ExecuteCommand(CancellationToken? cancellationToken)
{
if (Version == null)
{
Expand All @@ -41,28 +43,28 @@ protected override async ValueTask ExecuteCommand(CancellationToken cancellation
// valid
break;
default:
Logger.LogError("Only versions 0.10 and 0.11 are supported.");
Logger?.LogError("Only versions 0.10 and 0.11 are supported.");
return;
}

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
if (IsAdministrator())
{
await DfuUtils.InstallDfuUtil(FileManager.WildernessTempFolderPath, Version, cancellationToken);
await DfuUtils.InstallDfuUtil(FileManager.WildernessTempFolderPath, Version, cancellationToken ?? default);
}
else
{
Logger.LogError("To install DFU on Windows, you'll need to re-run the command from as an Administrator");
Logger?.LogError("To install DFU on Windows, you'll need to re-run the command from as an Administrator");
}
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
Logger.LogWarning("To install DFU on macOS, run: brew install dfu-util");
Logger?.LogWarning("To install DFU on macOS, run: brew install dfu-util");
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
Logger.LogWarning(
Logger?.LogWarning(
"To install DFU on Linux, use the package manager to install the dfu-util package");
}
}
Expand All @@ -80,4 +82,4 @@ private static bool IsAdministrator()
return false;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using CliFx.Attributes;
using CliFx.Infrastructure;
using Meadow.Cli;
using Meadow.Hcom;
using Meadow.Software;
using Microsoft.Extensions.Logging;

Expand All @@ -16,7 +18,7 @@ public FirmwareDefaultCommand(FileManager fileManager, ISettingsManager settings
[CommandParameter(0, Name = "Version number to use as default", IsRequired = true)]
public string Version { get; set; } = default!;

protected override async ValueTask ExecuteCommand(CancellationToken cancellationToken)
protected override async ValueTask ExecuteCommand(CancellationToken? cancellationToken)
{
await FileManager.Refresh();

Expand All @@ -32,4 +34,4 @@ protected override async ValueTask ExecuteCommand(CancellationToken cancellation

Logger?.LogInformation($"Done.");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using CliFx.Attributes;
using CliFx.Infrastructure;
using Meadow.Cli;
using Meadow.Hcom;
using Meadow.Software;
using Microsoft.Extensions.Logging;

Expand All @@ -16,7 +18,7 @@ public FirmwareDeleteCommand(FileManager fileManager, ISettingsManager settingsM
[CommandParameter(0, Name = "Version number to delete", IsRequired = true)]
public string Version { get; set; } = default!;

protected override async ValueTask ExecuteCommand(CancellationToken cancellationToken)
protected override async ValueTask ExecuteCommand(CancellationToken? cancellationToken)
{
await FileManager.Refresh();

Expand All @@ -30,4 +32,4 @@ protected override async ValueTask ExecuteCommand(CancellationToken cancellation

Logger?.LogInformation($"Done.");
}
}
}
Loading

0 comments on commit 4f8d5c8

Please sign in to comment.