Skip to content

Commit

Permalink
added commands to build and trim applications
Browse files Browse the repository at this point in the history
  • Loading branch information
ctacke committed Sep 22, 2023
1 parent 139ff11 commit 0b6f713
Show file tree
Hide file tree
Showing 19 changed files with 704 additions and 32 deletions.
115 changes: 115 additions & 0 deletions Source/v2/Meadow.Cli/Commands/Current/App/AppBuildCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
using CliFx.Attributes;
using Meadow.Cli;
using Microsoft.Extensions.Logging;

namespace Meadow.CLI.Commands.DeviceManagement;

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

[CommandOption('c', Description = "The build configuration to compile", IsRequired = false)]
public string? Configuration { get; set; }

[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)
{
_packageManager = packageManager;
}

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

// is the path a file?
if (!File.Exists(path))
{
// is it a valid directory?
if (!Directory.Exists(path))
{
Logger.LogError($"Invalid application path '{path}'");
return;
}
}

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

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!");
}
else
{
Logger.LogError($"Build success.");
}
}
}

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

[CommandOption('c', Description = "The build configuration to trim", IsRequired = false)]
public string? Configuration { get; set; }

[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)
{
_packageManager = packageManager;
}

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

// is the path a file?
FileInfo file;

if (!File.Exists(path))
{
// is it a valid directory?
if (!Directory.Exists(path))
{
Logger.LogError($"Invalid application path '{path}'");
return;
}

// it's a directory - we need to determine the latest build (they might have a Debug and Release config)
var candidates = PackageManager.GetAvailableBuiltConfigurations(path, "App.dll");

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

file = candidates.OrderByDescending(c => c.LastWriteTime).First();
}
else
{
file = new FileInfo(path);
}

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

await _packageManager.TrimApplication(file, false, null, cancellationToken);
}
}
16 changes: 16 additions & 0 deletions Source/v2/Meadow.Cli/Commands/Current/BaseFileCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Meadow.Cli;
using Meadow.Software;
using Microsoft.Extensions.Logging;

namespace Meadow.CLI.Commands.DeviceManagement;

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

public BaseFileCommand(FileManager fileManager, ISettingsManager settingsManager, ILoggerFactory loggerFactory)
: base(settingsManager, loggerFactory)
{
FileManager = fileManager;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
namespace Meadow.CLI.Commands.DeviceManagement;

[Command("firmware default", Description = "Sets the current default firmware package")]
public class FirmwareDefaultCommand : BaseCommand<FirmwareDefaultCommand>
public class FirmwareDefaultCommand : BaseFileCommand<FirmwareDefaultCommand>
{
public FirmwareDefaultCommand(ISettingsManager settingsManager, ILoggerFactory loggerFactory)
: base(settingsManager, loggerFactory)
public FirmwareDefaultCommand(FileManager fileManager, ISettingsManager settingsManager, ILoggerFactory loggerFactory)
: base(fileManager, settingsManager, loggerFactory)
{
}

Expand All @@ -18,13 +18,11 @@ public FirmwareDefaultCommand(ISettingsManager settingsManager, ILoggerFactory l

protected override async ValueTask ExecuteCommand(CancellationToken cancellationToken)
{
var manager = new FileManager();

await manager.Refresh();
await FileManager.Refresh();

// for now we only support F7
// TODO: add switch and support for other platforms
var collection = manager.Firmware["Meadow F7"];
var collection = FileManager.Firmware["Meadow F7"];

var existing = collection.FirstOrDefault(p => p.Version == Version);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
namespace Meadow.CLI.Commands.DeviceManagement;

[Command("firmware delete", Description = "Delete a local firmware package")]
public class FirmwareDeleteCommand : BaseCommand<FirmwareDeleteCommand>
public class FirmwareDeleteCommand : BaseFileCommand<FirmwareDeleteCommand>
{
public FirmwareDeleteCommand(ISettingsManager settingsManager, ILoggerFactory loggerFactory)
: base(settingsManager, loggerFactory)
public FirmwareDeleteCommand(FileManager fileManager, ISettingsManager settingsManager, ILoggerFactory loggerFactory)
: base(fileManager, settingsManager, loggerFactory)
{
}

Expand All @@ -18,13 +18,11 @@ public FirmwareDeleteCommand(ISettingsManager settingsManager, ILoggerFactory lo

protected override async ValueTask ExecuteCommand(CancellationToken cancellationToken)
{
var manager = new FileManager();

await manager.Refresh();
await FileManager.Refresh();

// for now we only support F7
// TODO: add switch and support for other platforms
var collection = manager.Firmware["Meadow F7"];
var collection = FileManager.Firmware["Meadow F7"];

Logger?.LogInformation($"Deleting firmware '{Version}'...");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
namespace Meadow.CLI.Commands.DeviceManagement;

[Command("firmware download", Description = "Download a firmware package")]
public class FirmwareDownloadCommand : BaseCommand<FirmwareDownloadCommand>
public class FirmwareDownloadCommand : BaseFileCommand<FirmwareDownloadCommand>
{
public FirmwareDownloadCommand(ISettingsManager settingsManager, ILoggerFactory loggerFactory)
: base(settingsManager, loggerFactory)
public FirmwareDownloadCommand(FileManager fileManager, ISettingsManager settingsManager, ILoggerFactory loggerFactory)
: base(fileManager, settingsManager, loggerFactory)
{
}

Expand All @@ -21,13 +21,11 @@ public FirmwareDownloadCommand(ISettingsManager settingsManager, ILoggerFactory

protected override async ValueTask ExecuteCommand(CancellationToken cancellationToken)
{
var manager = new FileManager();

await manager.Refresh();
await FileManager.Refresh();

// for now we only support F7
// TODO: add switch and support for other platforms
var collection = manager.Firmware["Meadow F7"];
var collection = FileManager.Firmware["Meadow F7"];

if (Version == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,26 @@ public class FirmwareListCommand : ICommand
[CommandOption("verbose", 'v', IsRequired = false)]
public bool Verbose { get; set; }

public FirmwareListCommand(ISettingsManager settingsManager, ILoggerFactory? loggerFactory)
private FileManager FileManager { get; }

public FirmwareListCommand(FileManager fileManager, ISettingsManager settingsManager, ILoggerFactory? loggerFactory)
{
FileManager = fileManager;
_settingsManager = settingsManager;
_logger = loggerFactory?.CreateLogger<DeviceInfoCommand>();
}

public async ValueTask ExecuteAsync(IConsole console)
{

var manager = new FileManager();

await manager.Refresh();
await FileManager.Refresh();

if (Verbose)
{
await DisplayVerboseResults(manager);
await DisplayVerboseResults(FileManager);
}
else
{
await DisplayTerseResults(manager);
await DisplayTerseResults(FileManager);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ public class FirmwareWriteCommand : BaseDeviceCommand<FirmwareWriteCommand>
[CommandParameter(0, Name = "Files to write", IsRequired = false)]
public FirmwareType[]? Files { get; set; } = default!;

public FirmwareWriteCommand(MeadowConnectionManager connectionManager, ILoggerFactory loggerFactory)
private FileManager FileManager { get; }

public FirmwareWriteCommand(FileManager fileManager, MeadowConnectionManager connectionManager, ILoggerFactory loggerFactory)
: base(connectionManager, loggerFactory)
{
FileManager = fileManager;
}

public override async ValueTask ExecuteAsync(IConsole console)
Expand Down Expand Up @@ -94,10 +97,9 @@ public override async ValueTask ExecuteAsync(IConsole console)

private async Task<FirmwarePackage?> GetSelectedPackage()
{
var manager = new FileManager();
await manager.Refresh();
await FileManager.Refresh();

var collection = manager.Firmware["Meadow F7"];
var collection = FileManager.Firmware["Meadow F7"];
FirmwarePackage package;

if (Version != null)
Expand Down
18 changes: 18 additions & 0 deletions Source/v2/Meadow.Cli/Meadow.Cli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<AssemblyName>meadow</AssemblyName>
<LangVersion>latest</LangVersion>
<Copyright>Copyright 2020-2022 Wilderness Labs</Copyright>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand All @@ -37,6 +38,8 @@
<PackageReference Include="System.Configuration.ConfigurationManager" Version="5.0.0" />
<PackageReference Include="LibUsbDotNet" Version="3.0.102-alpha" />
<PackageReference Include="System.Management" Version="7.0.2" />
<PackageReference Include="YamlDotNet" Version="13.4.0" />
<PackageReference Include="Mono.Cecil" Version="0.11.3" />
</ItemGroup>

<ItemGroup>
Expand All @@ -48,4 +51,19 @@
<None Include="..\icon.png" Pack="true" PackagePath="" />
</ItemGroup>

<ItemGroup>
<None Update="lib\illink.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="lib\illink.runtimeconfig.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="lib\meadow_link.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="lib\Mono.Cecil.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Loading

0 comments on commit 0b6f713

Please sign in to comment.