Skip to content

Commit

Permalink
Merge pull request #555 from WildernessLabs/trim_fix
Browse files Browse the repository at this point in the history
Trim assemeblies / path fix
  • Loading branch information
ctacke authored Apr 30, 2024
2 parents cdd0fa4 + 04fe790 commit b05d434
Show file tree
Hide file tree
Showing 16 changed files with 95 additions and 45 deletions.
3 changes: 2 additions & 1 deletion Source/v2/Meadow.CLI/Commands/Current/App/AppTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ internal static async Task DisableRuntimeIfEnabled(IMeadowConnection connection,

internal static async Task<bool> TrimApplication(string path,
IPackageManager packageManager,
string osVersion,
string? configuration,
IEnumerable<string>? noLinkAssemblies,
ILogger? logger,
Expand Down Expand Up @@ -86,7 +87,7 @@ internal static async Task<bool> TrimApplication(string path,
logger?.LogInformation($"Skippping assemblies: {string.Join(", ", noLinkAssemblies)}");
}

await packageManager.TrimApplication(file, false, noLinkAssemblies, cancellationToken);
await packageManager.TrimApplication(file, osVersion, false, noLinkAssemblies, cancellationToken);
cts.Cancel();

// illink returns before all files are written - attempt a delay of 1s
Expand Down
15 changes: 11 additions & 4 deletions Source/v2/Meadow.Cli/Commands/Current/App/AppDeployCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,16 @@ protected override async ValueTask ExecuteCommand()

await AppTools.DisableRuntimeIfEnabled(connection, Logger, CancellationToken);

if (!await DeployApplication(connection, file.FullName, CancellationToken))
var deviceInfo = await connection.GetDeviceInfo();

if (deviceInfo == null || deviceInfo.OsVersion == null)
{
throw new CommandException(Strings.UnableToGetDeviceInfo, CommandExitCode.GeneralError);
}

if (!await DeployApplication(connection, deviceInfo.OsVersion, file.FullName, CancellationToken))
{
throw new CommandException("Application deploy failed", CommandExitCode.GeneralError);
throw new CommandException(Strings.AppDeployFailed, CommandExitCode.GeneralError);
}
}

Expand Down Expand Up @@ -79,7 +86,7 @@ private FileInfo GetMeadowAppFile(string path)
return file;
}

private async Task<bool> DeployApplication(IMeadowConnection connection, string path, CancellationToken GetAvailableBuiltConfigurations)
private async Task<bool> DeployApplication(IMeadowConnection connection, string osVersion, string path, CancellationToken GetAvailableBuiltConfigurations)
{
connection.FileWriteProgress += OnFileWriteProgress;

Expand All @@ -95,7 +102,7 @@ private async Task<bool> DeployApplication(IMeadowConnection connection, string

Logger?.LogInformation($"Deploying app from {file.DirectoryName}...");

await AppManager.DeployApplication(_packageManager, connection, file.DirectoryName!, true, false, Logger, GetAvailableBuiltConfigurations);
await AppManager.DeployApplication(_packageManager, connection, osVersion, file.DirectoryName!, true, false, Logger, GetAvailableBuiltConfigurations);

connection.FileWriteProgress -= OnFileWriteProgress;

Expand Down
26 changes: 20 additions & 6 deletions Source/v2/Meadow.Cli/Commands/Current/App/AppRunCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,33 @@ protected override async ValueTask ExecuteCommand()

var connection = await GetCurrentConnection();

var deviceInfo = await connection.GetDeviceInfo();

if (deviceInfo == null || deviceInfo.OsVersion == null)
{
throw new CommandException(Strings.UnableToGetDeviceInfo, CommandExitCode.GeneralError);
}

var lastFile = string.Empty;

// in order to deploy, the runtime must be disabled
await AppTools.DisableRuntimeIfEnabled(connection, Logger, CancellationToken);

Logger?.LogInformation($"Building {Configuration} configuration of {path}...");
Logger?.LogInformation($"Building {Configuration} configuration of {path} for Meadow v{deviceInfo.OsVersion}...");

if (!_packageManager.BuildApplication(path, Configuration))
{
throw new CommandException("Application build failed", CommandExitCode.GeneralError);
throw new CommandException(Strings.AppBuildFailed, CommandExitCode.GeneralError);
}

if (!await AppTools.TrimApplication(path, _packageManager, Configuration, NoLink, Logger, Console, CancellationToken))
if (!await AppTools.TrimApplication(path, _packageManager, deviceInfo.OsVersion, Configuration, NoLink, Logger, Console, CancellationToken))
{
throw new CommandException("Application trimming failed", CommandExitCode.GeneralError);
throw new CommandException(Strings.AppTrimFailed, CommandExitCode.GeneralError);
}

if (!await DeployApplication(connection, path, CancellationToken))
{
throw new CommandException("Application deploy failed", CommandExitCode.GeneralError);
throw new CommandException(Strings.AppDeployFailed, CommandExitCode.GeneralError);
}

Logger?.LogInformation($"{Strings.EnablingRuntime}...");
Expand All @@ -97,6 +104,13 @@ private async Task<bool> DeployApplication(IMeadowConnection connection, string
{
connection.FileWriteProgress += OnFileWriteProgress;

var deviceInfo = await connection.GetDeviceInfo();

if (deviceInfo == null || deviceInfo.OsVersion == null)
{
throw new CommandException(Strings.UnableToGetDeviceInfo, CommandExitCode.GeneralError);
}

var candidates = PackageManager.GetAvailableBuiltConfigurations(path, "App.dll");

if (candidates.Length == 0)
Expand All @@ -109,7 +123,7 @@ private async Task<bool> DeployApplication(IMeadowConnection connection, string

Logger?.LogInformation($"Deploying app from {file.DirectoryName}...");

await AppManager.DeployApplication(_packageManager, connection, file.DirectoryName!, true, false, Logger, cancellationToken);
await AppManager.DeployApplication(_packageManager, connection, deviceInfo.OsVersion, file.DirectoryName!, true, false, Logger, cancellationToken);

connection.FileWriteProgress -= OnFileWriteProgress;

Expand Down
20 changes: 16 additions & 4 deletions Source/v2/Meadow.Cli/Commands/Current/App/AppTrimCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace Meadow.CLI.Commands.DeviceManagement;

[Command("app trim", Description = "Trim a pre-compiled Meadow application")]
public class AppTrimCommand : BaseCommand<AppTrimCommand>
public class AppTrimCommand : BaseDeviceCommand<AppTrimCommand>
{
private readonly IPackageManager _packageManager;

Expand All @@ -21,8 +21,8 @@ public class AppTrimCommand : BaseCommand<AppTrimCommand>

readonly FileManager _fileManager;

public AppTrimCommand(FileManager fileManager, IPackageManager packageManager, ILoggerFactory loggerFactory)
: base(loggerFactory)
public AppTrimCommand(FileManager fileManager, IPackageManager packageManager, MeadowConnectionManager connectionManager, ILoggerFactory loggerFactory)
: base(connectionManager, loggerFactory)
{
_packageManager = packageManager;
_fileManager = fileManager;
Expand Down Expand Up @@ -57,7 +57,19 @@ protected override async ValueTask ExecuteCommand()
}
}

await AppTools.TrimApplication(path, _packageManager, Configuration, NoLink, Logger, Console, CancellationToken);
var connection = await GetCurrentConnection();

await AppTools.DisableRuntimeIfEnabled(connection, Logger, CancellationToken);

var deviceInfo = await connection.GetDeviceInfo();

if (deviceInfo == null || deviceInfo.OsVersion == null)
{
throw new CommandException(Strings.UnableToGetDeviceInfo, CommandExitCode.GeneralError);
}

Logger.LogInformation($"Preparing to trim using v{deviceInfo.OsVersion} assemblies...");
await AppTools.TrimApplication(path, _packageManager, deviceInfo.OsVersion, Configuration, NoLink, Logger, Console, CancellationToken);
Logger.LogInformation("Application trimmed successfully");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected override async ValueTask ExecuteCommand()

var buildPath = GetAppBuildPath(projectPath);

await AppTools.TrimApplication(projectPath, _packageManager, Configuration, null, Logger, Console, CancellationToken);
await AppTools.TrimApplication(projectPath, _packageManager, osVersion, Configuration, null, Logger, Console, CancellationToken);
Logger.LogInformation(string.Format(Strings.TrimmedApplicationForSpecifiedVersion, osVersion));

// package
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private async Task DisplayVerboseResults(FileManager manager)
Logger?.LogInformation($" {name}");
var collection = manager.Firmware[name];

foreach (var package in collection.OrderByDescending(p => new Version(p.Version)))
foreach (var package in collection.OrderBy(p => new Version(p.Version)))
{
if (package == collection.DefaultPackage)
{
Expand Down Expand Up @@ -88,7 +88,7 @@ private async Task DisplayTerseResults(FileManager manager)
Logger?.LogInformation($" {name}");
var collection = manager.Firmware[name];

foreach (var package in collection.OrderByDescending(p => new Version(p.Version)))
foreach (var package in collection.OrderBy(p => new Version(p.Version)))
{
if (package == collection.DefaultPackage)
{
Expand Down
2 changes: 1 addition & 1 deletion Source/v2/Meadow.Cli/Meadow.CLI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<Authors>Wilderness Labs, Inc</Authors>
<Company>Wilderness Labs, Inc</Company>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageVersion>2.0.35.0</PackageVersion>
<PackageVersion>2.0.37.0</PackageVersion>
<Platforms>AnyCPU</Platforms>
<PackageProjectUrl>http://developer.wildernesslabs.co/Meadow/Meadow.CLI/</PackageProjectUrl>
<RepositoryUrl>https://github.com/WildernessLabs/Meadow.CLI</RepositoryUrl>
Expand Down
2 changes: 1 addition & 1 deletion Source/v2/Meadow.Cli/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ namespace Meadow.CLI;

public static class Constants
{
public const string CLI_VERSION = "2.0.35.0";
public const string CLI_VERSION = "2.0.37.0";
}
4 changes: 2 additions & 2 deletions Source/v2/Meadow.Cli/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,11 @@
},
"App Build": {
"commandName": "Project",
"commandLineArgs": "app build H:\\AS\\BlockPuzzleGenerator\\src\\MeadowBoulderScape\\"
"commandLineArgs": "app build H:\\WL\\Blinky\\Blinky\\BlinkyCs"
},
"App Build Debug": {
"commandName": "Project",
"commandLineArgs": "app build F:\\temp\\MeadowApplication1 -c Debug"
"commandLineArgs": "app build H:\\WL\\Blinky\\Blinky\\BlinkyCs -c Debug"
},
"App Trim": {
"commandName": "Project",
Expand Down
5 changes: 4 additions & 1 deletion Source/v2/Meadow.Cli/Strings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static class Strings
public const string ProvisioningSucceeded = "Device provisioned successfully";
public const string ProvisioningFailed = "Failed to provision device: {0}";
public const string BuildingSpecifiedConfiguration = "Building {0} configuration of application...";
public const string BuildFailed = "Build failed";
public const string AppBuildFailed = "Application build failed";
public const string TrimmingApplicationForSpecifiedVersion = "Trimming application for OS version {0}...";
public const string TrimmedApplicationForSpecifiedVersion = "Trimmed application with OS version {0}";
public const string AssemblingCloudPackage = "Assembling the MPAK...";
Expand Down Expand Up @@ -82,4 +82,7 @@ public static class Telemetry

public const string AskToParticipate = "Would you like to participate?";
}

public const string AppDeployFailed = "Application deploy failed";
public const string AppTrimFailed = "Application trimming failed";
}
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@ public Task Refresh()
return Task.CompletedTask;
}

public FirmwarePackage? GetLocalPackage(string osVersion)
{
return _f7Packages.FirstOrDefault(p => p.Version == osVersion);
}

public IEnumerator<FirmwarePackage> GetEnumerator()
{
return _f7Packages.GetEnumerator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public interface IFirmwarePackageCollection : IEnumerable<FirmwarePackage>
Task<string?> UpdateAvailable();
Task<bool> IsVersionAvailableForDownload(string version);
Task<bool> RetrievePackage(string version, bool overwrite = false);
FirmwarePackage? GetLocalPackage(string version);
FirmwarePackage this[int index] { get; }
FirmwarePackage? this[string version] { get; }
string PackageFileRoot { get; }
Expand Down
3 changes: 2 additions & 1 deletion Source/v2/Meadow.Tooling.Core/App/AppManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ private static bool IsXmlDoc(string file)
public static async Task DeployApplication(
IPackageManager packageManager,
IMeadowConnection connection,
string osVersion,
string localBinaryDirectory,
bool includePdbs,
bool includeXmlDocs,
Expand Down Expand Up @@ -67,7 +68,7 @@ public static async Task DeployApplication(
}
else
{
dependencies = packageManager.GetDependencies(new FileInfo(Path.Combine(processedAppPath, "App.dll")));
dependencies = packageManager.GetDependencies(new FileInfo(Path.Combine(processedAppPath, "App.dll")), osVersion);
}
dependencies.Add(Path.Combine(localBinaryDirectory, "App.dll"));

Expand Down
3 changes: 2 additions & 1 deletion Source/v2/Meadow.Tooling.Core/Package/IPackageManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Meadow.Package;

public interface IPackageManager
{
List<string> GetDependencies(FileInfo file);
List<string> GetDependencies(FileInfo file, string? osVerion);

bool BuildApplication(
string projectFilePath,
Expand All @@ -17,6 +17,7 @@ bool BuildApplication(

Task TrimApplication(
FileInfo applicationFilePath,
string osVerion,
bool includePdbs = false,
IEnumerable<string>? noLink = null,
CancellationToken? cancellationToken = null);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.IO;

namespace Meadow.Package;
Expand All @@ -9,28 +10,33 @@ public partial class PackageManager
public const string PostLinkDirectoryName = "postlink_bin";
public const string PackageOutputDirectoryName = "mpak";

private string _meadowAssembliesPath = string.Empty;

public List<string> GetDependencies(FileInfo file)
public List<string> GetDependencies(FileInfo file, string? osVerion)
{
return _meadowLinker.MapDependencies(file);
var linker = new Linker.MeadowLinker(GetAssemblyPathForOS(osVerion));
return linker.MapDependencies(file);
}

private string GetMeadowAssembliesPath()
private string GetAssemblyPathForOS(string? osVersion)
{
if (string.IsNullOrWhiteSpace(_meadowAssembliesPath))
{ // for now we only support F7
// TODO: add switch and support for other platforms
var store = _fileManager.Firmware["Meadow F7"];
if (store != null)
if (string.IsNullOrWhiteSpace(osVersion))
{
osVersion = _fileManager?.Firmware["Meadow F7"]?.DefaultPackage?.Version;
}

var store = _fileManager?.Firmware["Meadow F7"];
if (store != null)
{
store.Refresh();

var package = store.GetLocalPackage(osVersion!);

if (package == null)
{
store.Refresh();
if (store.DefaultPackage != null && store.DefaultPackage.BclFolder != null)
{
_meadowAssembliesPath = store.DefaultPackage.GetFullyQualifiedPath(store.DefaultPackage.BclFolder);
}
throw new Exception($"No firmware package found for Meadow F7 with version {osVersion}");
}
return package.GetFullyQualifiedPath(package.BclFolder);
}
return _meadowAssembliesPath;

throw new Exception("No firmware package(s) found for Meadow F7");
}
}
5 changes: 2 additions & 3 deletions Source/v2/Meadow.Tooling.Core/Package/PackageManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@ public partial class PackageManager : IPackageManager
public const string BuildOptionsFileName = "app.build.yaml";

private readonly FileManager _fileManager;
private readonly MeadowLinker _meadowLinker;

public PackageManager(FileManager fileManager)
{
_fileManager = fileManager;
_meadowLinker = new MeadowLinker(GetMeadowAssembliesPath(), null);
}

private bool CleanApplication(string projectFilePath, string configuration = "Release", CancellationToken? cancellationToken = null)
Expand Down Expand Up @@ -129,6 +127,7 @@ public bool BuildApplication(string projectFilePath, string configuration = "Rel

public Task TrimApplication(
FileInfo applicationFilePath,
string osVersion,
bool includePdbs = false,
IEnumerable<string>? noLink = null,
CancellationToken? cancellationToken = null)
Expand Down Expand Up @@ -164,7 +163,7 @@ public Task TrimApplication(
}
}

var linker = new MeadowLinker(GetMeadowAssembliesPath());
var linker = new MeadowLinker(GetAssemblyPathForOS(osVersion));

return linker.Trim(applicationFilePath, includePdbs, noLink);
}
Expand Down

0 comments on commit b05d434

Please sign in to comment.