Skip to content

Commit

Permalink
Improved validation and user feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianstevens committed Jan 15, 2024
1 parent c4ee4d7 commit 51c98c2
Show file tree
Hide file tree
Showing 35 changed files with 174 additions and 219 deletions.
13 changes: 4 additions & 9 deletions Source/v2/Meadow.Cli/Commands/Current/App/AppBuildCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Meadow.CLI.Commands.DeviceManagement;

[Command("app build", Description = "Compiles a Meadow application")]
[Command("app build", Description = "Compile a Meadow application")]
public class AppBuildCommand : BaseCommand<AppBuildCommand>
{
private readonly IPackageManager _packageManager;
Expand All @@ -22,9 +22,7 @@ public AppBuildCommand(IPackageManager packageManager, ILoggerFactory loggerFact

protected override ValueTask ExecuteCommand()
{
string path = Path == null
? AppDomain.CurrentDomain.BaseDirectory
: Path;
string path = Path ?? AppDomain.CurrentDomain.BaseDirectory;

// is the path a file?
if (!File.Exists(path))
Expand All @@ -37,10 +35,7 @@ protected override ValueTask ExecuteCommand()
}
}

if (Configuration == null)
{
Configuration = "Release";
}
Configuration ??= "Release";

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

Expand All @@ -53,7 +48,7 @@ protected override ValueTask ExecuteCommand()
}
else
{
Logger?.LogError($"Build successful");
Logger?.LogInformation($"Build successful");
}
return ValueTask.CompletedTask;
}
Expand Down
24 changes: 10 additions & 14 deletions Source/v2/Meadow.Cli/Commands/Current/App/AppDebugCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Meadow.CLI.Commands.DeviceManagement;

[Command("app debug", Description = "Debugs a running application")]
[Command("app debug", Description = "Debug a running application")]
public class AppDebugCommand : BaseDeviceCommand<AppDebugCommand>
{
// VS 2019 - 4024
Expand All @@ -14,8 +14,7 @@ public class AppDebugCommand : BaseDeviceCommand<AppDebugCommand>

public AppDebugCommand(MeadowConnectionManager connectionManager, ILoggerFactory loggerFactory)
: base(connectionManager, loggerFactory)
{
}
{ }

protected override async ValueTask ExecuteCommand()
{
Expand All @@ -27,20 +26,17 @@ protected override async ValueTask ExecuteCommand()
return;
}

if (connection != null)
connection.DeviceMessageReceived += (s, e) =>
{
connection.DeviceMessageReceived += (s, e) =>
{
Logger?.LogInformation(e.message);
};
Logger?.LogInformation(e.message);
};

using (var server = await connection.StartDebuggingSession(Port, Logger, CancellationToken))
using (var server = await connection.StartDebuggingSession(Port, Logger, CancellationToken))
{
if (Console != null)
{
if (Console != null)
{
Logger?.LogInformation("Debugging server started. Press Enter to exit");
await Console.Input.ReadLineAsync();
}
Logger?.LogInformation("Debugging server started - press Enter to exit");
await Console.Input.ReadLineAsync();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using CliFx.Attributes;
using Meadow.CLI;

using Microsoft.Extensions.Logging;

namespace Meadow.CLI.Commands.DeviceManagement;

[Command("app deploy", Description = "Deploys a built Meadow application to a target device")]
[Command("app deploy", Description = "Deploy a built Meadow application to a target device")]
public class AppDeployCommand : BaseDeviceCommand<AppDeployCommand>
{
private readonly IPackageManager _packageManager;
Expand Down
93 changes: 45 additions & 48 deletions Source/v2/Meadow.Cli/Commands/Current/App/AppRunCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,60 +35,57 @@ protected override async ValueTask ExecuteCommand()
return;
}

if (connection != null)
string path = Path == null
? AppDomain.CurrentDomain.BaseDirectory
: Path;

if (!Directory.Exists(path))
{
string path = Path == null
? AppDomain.CurrentDomain.BaseDirectory
: Path;

if (!Directory.Exists(path))
{
Logger?.LogError($"Target directory '{path}' not found.");
return;
}

var lastFile = string.Empty;

// in order to deploy, the runtime must be disabled
var wasRuntimeEnabled = await connection.IsRuntimeEnabled();
if (wasRuntimeEnabled)
{
Logger?.LogInformation("Disabling runtime...");

await connection.RuntimeDisable(CancellationToken);
}

if (!await BuildApplication(path, CancellationToken))
{
return;
}

if (!await TrimApplication(path, CancellationToken))
{
return;
}

// illink returns before all files are written - attempt a delay of 1s
await Task.Delay(1000);
Logger?.LogError($"Target directory '{path}' not found.");
return;
}

if (!await DeployApplication(connection, path, CancellationToken))
{
return;
}
var lastFile = string.Empty;

Logger?.LogInformation("Enabling the runtime...");
await connection.RuntimeEnable(CancellationToken);
// in order to deploy, the runtime must be disabled
var wasRuntimeEnabled = await connection.IsRuntimeEnabled();
if (wasRuntimeEnabled)
{
Logger?.LogInformation("Disabling runtime...");

Logger?.LogInformation("Listening for messages from Meadow...\n");
connection.DeviceMessageReceived += OnDeviceMessageReceived;
await connection.RuntimeDisable(CancellationToken);
}

while (!CancellationToken.IsCancellationRequested)
{
await Task.Delay(1000);
}
if (!await BuildApplication(path, CancellationToken))
{
return;
}

Logger?.LogInformation("Listen cancelled...");
if (!await TrimApplication(path, CancellationToken))
{
return;
}

// illink returns before all files are written - attempt a delay of 1s
await Task.Delay(1000);

if (!await DeployApplication(connection, path, CancellationToken))
{
return;
}

Logger?.LogInformation("Enabling the runtime...");
await connection.RuntimeEnable(CancellationToken);

Logger?.LogInformation("Listening for messages from Meadow...\n");
connection.DeviceMessageReceived += OnDeviceMessageReceived;

while (!CancellationToken.IsCancellationRequested)
{
await Task.Delay(1000);
}

Logger?.LogInformation("Listen cancelled...");
}

private Task<bool> BuildApplication(string path, CancellationToken cancellationToken)
Expand Down Expand Up @@ -170,4 +167,4 @@ private void OnDeviceMessageReceived(object? sender, (string message, string? so
Logger?.LogInformation($"{e.source}> {e.message.TrimEnd('\n', '\r')}");
}
}
}
}
6 changes: 2 additions & 4 deletions Source/v2/Meadow.Cli/Commands/Current/App/AppTrimCommand.cs
Original file line number Diff line number Diff line change
@@ -1,14 +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>
{
private IPackageManager _packageManager;
private readonly IPackageManager _packageManager;

[CommandOption('c', Description = "The build configuration to trim", IsRequired = false)]
public string? Configuration { get; set; }
Expand Down Expand Up @@ -61,4 +59,4 @@ protected override async ValueTask ExecuteCommand()

await _packageManager.TrimApplication(file, false, null, CancellationToken);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ public CloudLoginCommand(
CollectionService collectionService,
ILoggerFactory? loggerFactory)
: base(identityManager, userService, deviceService, collectionService, loggerFactory)
{
}
{ }

protected override async ValueTask ExecuteCommand()
{
if (Host == null) Host = DefaultHost;
Host ??= DefaultHost;

Logger?.LogInformation($"Logging into {Host}...");

Expand All @@ -37,4 +36,4 @@ protected override async ValueTask ExecuteCommand()
: "There was a problem retrieving your account information.");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ public CloudLogoutCommand(
CollectionService collectionService,
ILoggerFactory? loggerFactory)
: base(identityManager, userService, deviceService, collectionService, loggerFactory)
{
}
{ }

protected override async ValueTask ExecuteCommand()
protected override ValueTask ExecuteCommand()
{
Logger?.LogInformation($"Logging out of Meadow.Cloud...");

IdentityManager.Logout();

return ValueTask.CompletedTask;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ public CloudCollectionListCommand(
CollectionService collectionService,
ILoggerFactory? loggerFactory)
: base(identityManager, userService, deviceService, collectionService, loggerFactory)
{
}
{ }

protected override async ValueTask ExecuteCommand()
{
if (Host == null) Host = DefaultHost;
Host ??= DefaultHost;
var org = await ValidateOrg(Host, OrgId, CancellationToken);

if (org == null) return;
Expand All @@ -45,4 +44,4 @@ protected override async ValueTask ExecuteCommand()
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ namespace Meadow.CLI.Commands.DeviceManagement;

public class JsonDocumentBindingConverter : BindingConverter<JsonDocument>
{
public override JsonDocument Convert(string rawValue)
public override JsonDocument Convert(string? rawValue)
{
try
{
return JsonDocument.Parse(rawValue);
return JsonDocument.Parse(rawValue!);
}
catch (JsonException ex)
{
throw new CommandException($"Provided argument is not valid JSON: {ex.Message}", showHelp: false, innerException: ex);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Meadow.CLI.Commands.DeviceManagement;
public class CloudCommandPublishCommand : BaseCloudCommand<CloudCommandPublishCommand>
{
[CommandParameter(0, Description = "The name of the command", IsRequired = true, Name = "COMMAND_NAME")]
public string CommandName { get; set; }
public string CommandName { get; set; } = default!;

[CommandOption("collectionId", 'c', Description = "The target collection for publishing the command")]
public string? CollectionId { get; set; }
Expand Down Expand Up @@ -54,7 +54,7 @@ protected override async ValueTask ExecuteCommand()
throw new CommandException("Cannot specify both a collection ID (-c|--collectionId) and list of device IDs (-d|--deviceIds). Only one is allowed.", showHelp: true);
}

if (Host == null) Host = DefaultHost;
Host ??= DefaultHost;

var token = await IdentityManager.GetAccessToken(CancellationToken);
if (string.IsNullOrWhiteSpace(token))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ public CloudPackageCreateCommand(

protected override async ValueTask ExecuteCommand()
{
if (ProjectPath == null)
{
ProjectPath = AppDomain.CurrentDomain.BaseDirectory;
}
ProjectPath ??= AppDomain.CurrentDomain.BaseDirectory;

// build
Logger?.LogInformation($"Building {Configuration} version of application...");
Expand Down Expand Up @@ -85,6 +82,5 @@ protected override async ValueTask ExecuteCommand()
{
Logger?.LogError($"Package assembly failed.");
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Meadow.CLI.Commands.DeviceManagement;
[Command("cloud package list", Description = "Lists all Meadow Packages (MPAK)")]
public class CloudPackageListCommand : BaseCloudCommand<CloudPackageListCommand>
{
private PackageService _packageService;
private readonly PackageService _packageService;

[CommandOption("orgId", 'o', Description = "Optional organization ID", IsRequired = false)]
public string? OrgId { get; set; }
Expand All @@ -30,16 +30,16 @@ public CloudPackageListCommand(

protected override async ValueTask ExecuteCommand()
{
if (Host == null) Host = DefaultHost;
Host ??= DefaultHost;
var org = await ValidateOrg(Host, OrgId, CancellationToken);

if (org == null) return;
if (org == null) { return; }

var packages = await _packageService.GetOrgPackages(org.Id, Host, CancellationToken);

if (packages == null || packages.Count == 0)
{
Logger?.LogInformation("No packages found.");
Logger?.LogInformation("No packages found");
}
else
{
Expand All @@ -50,4 +50,4 @@ protected override async ValueTask ExecuteCommand()
}
}
}
}
}
Loading

0 comments on commit 51c98c2

Please sign in to comment.