Skip to content

Commit

Permalink
Refactor to support port list without connection and with console int…
Browse files Browse the repository at this point in the history
…eraction.
  • Loading branch information
CartBlanche committed Sep 28, 2023
1 parent 126d446 commit ed648f8
Show file tree
Hide file tree
Showing 15 changed files with 131 additions and 69 deletions.
14 changes: 8 additions & 6 deletions Source/v2/Meadow.Cli/Commands/Current/App/AppBuildCommand.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using CliFx.Attributes;
using CliFx.Infrastructure;
using Meadow.Cli;
using Meadow.Hcom;
using Microsoft.Extensions.Logging;

namespace Meadow.CLI.Commands.DeviceManagement;

[Command("app build", Description = "Compiles a Meadow application")]
public class AppBuildCommand : BaseCommand<AppBuildCommand>
public class AppBuildCommand : BaseSettingsCommand<AppBuildCommand>
{
private IPackageManager _packageManager;

Expand All @@ -21,7 +23,7 @@ public AppBuildCommand(IPackageManager packageManager, ISettingsManager settings
_packageManager = packageManager;
}

protected override async ValueTask ExecuteCommand(CancellationToken cancellationToken)
protected override async ValueTask ExecuteCommand(IConsole console, 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 of {path}...");
Logger?.LogInformation($"Building {Configuration} configuration of 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 @@ -54,4 +55,4 @@ protected override async ValueTask ExecuteCommand(IMeadowConnection connection,
Logger.LogError($"Build success.");
}
}
}
}
11 changes: 6 additions & 5 deletions Source/v2/Meadow.Cli/Commands/Current/App/AppTrimCommand.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using CliFx.Attributes;
using CliFx.Infrastructure;
using Meadow.Cli;
using Microsoft.Extensions.Logging;

namespace Meadow.CLI.Commands.DeviceManagement;

[Command("app trim", Description = "Runs an already-compiled Meadow application through reference trimming")]
public class AppTrimCommand : BaseCommand<AppTrimCommand>
public class AppTrimCommand : BaseSettingsCommand<AppTrimCommand>
{
private IPackageManager _packageManager;

Expand All @@ -21,7 +22,7 @@ public AppTrimCommand(IPackageManager packageManager, ISettingsManager settingsM
_packageManager = packageManager;
}

protected override async ValueTask ExecuteCommand(CancellationToken cancellationToken)
protected override async ValueTask ExecuteCommand(IConsole console, 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,7 +57,7 @@ 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);
}
Expand Down
20 changes: 10 additions & 10 deletions Source/v2/Meadow.Cli/Commands/Current/BaseCommand.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
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; }

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(IConsole console, CancellationToken cancellationToken);

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

try
{
await ExecuteCommand(cancellationToken);
await ExecuteCommand(console, 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
14 changes: 14 additions & 0 deletions Source/v2/Meadow.Cli/Commands/Current/BaseSettingsCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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;
}
}
32 changes: 14 additions & 18 deletions Source/v2/Meadow.Cli/Commands/Current/ConfigCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,36 @@
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 ConfigCommand(ISettingsManager settingsManager, ILoggerFactory? loggerFactory)
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(IConsole console, 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}");
}
}
}
Expand All @@ -54,18 +50,18 @@ public async ValueTask ExecuteAsync(IConsole console)
throw new CommandException($"No setting provided");
case 1:
// erase a setting
_logger?.LogInformation($"Deleting Setting {Settings[0]}");
_settingsManager.DeleteSetting(Settings[0]);
Logger?.LogInformation($"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($"Setting {Settings[0]}={Settings[1]}");
SettingsManager.SaveSetting(Settings[0], Settings[1]);
break;
default:
// not valid;
throw new CommandException($"Too many parameters provided");
}
}
}
}
}
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(IConsole console, CancellationToken cancellationToken)
{
if (Version == null)
{
Expand Down Expand Up @@ -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(IConsole console, 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(IConsole console, CancellationToken cancellationToken)
{
await FileManager.Refresh();

Expand All @@ -30,4 +32,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 @@ -19,7 +21,7 @@ public FirmwareDownloadCommand(FileManager fileManager, ISettingsManager setting
[CommandParameter(0, Name = "Version number to download", IsRequired = false)]
public string? Version { get; set; } = default!;

protected override async ValueTask ExecuteCommand(CancellationToken cancellationToken)
protected override async ValueTask ExecuteCommand(IConsole console, CancellationToken cancellationToken)
{
await FileManager.Refresh();

Expand Down Expand Up @@ -81,4 +83,4 @@ private void OnDownloadProgress(object? sender, long e)
{
Console.Write($"Retrieved {e} bytes... \r");
}
}
}
Loading

0 comments on commit ed648f8

Please sign in to comment.