Skip to content

Commit

Permalink
Merge pull request #552 from WildernessLabs/pack_fix
Browse files Browse the repository at this point in the history
Updated cloud package command to include non-assembly files + cleanup
  • Loading branch information
jorgedevs authored Apr 19, 2024
2 parents 323b9f2 + 6fb5c75 commit 2603425
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ namespace Meadow.CLI.Commands.DeviceManagement;
public class CloudPackageCreateCommand : BaseCommand<CloudPackageCreateCommand>
{
[CommandParameter(0, Description = "Path to project file", IsRequired = false)]
public string? ProjectPath { get; set; }
public string? ProjectPath { get; init; }

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

[CommandOption("name", 'n', Description = "Name of the mpak file to be created", IsRequired = false)]
public string? MpakName { get; init; }

[CommandOption("filter", 'f', Description = "Glob pattern to filter files. ex ('app.dll', 'app*','{app.dll,meadow.dll}')",
IsRequired = false)]
public string Filter { get; init; } = "*";
public string Filter { get; init; } = "**/*";

[CommandOption("osVersion", 'v', Description = "Target OS version for the app", IsRequired = false)]
public string? OsVersion { get; init; } = default!;
Expand All @@ -39,40 +39,26 @@ public CloudPackageCreateCommand(

protected override async ValueTask ExecuteCommand()
{
ProjectPath ??= Directory.GetCurrentDirectory();
ProjectPath = Path.GetFullPath(ProjectPath);
if (!Directory.Exists(ProjectPath))
{
throw new CommandException($"Directory not found '{ProjectPath}'. Check path to project file.", CommandExitCode.DirectoryNotFound);
}
var store = _fileManager.Firmware["Meadow F7"];
await ValidateFirmwarePackage(store);

// build
Logger.LogInformation(string.Format(Strings.BuildingSpecifiedConfiguration, Configuration));
if (!_packageManager.BuildApplication(ProjectPath, Configuration, true, CancellationToken))
{
throw new CommandException(Strings.BuildFailed);
}
var osVersion = OsVersion ?? store!.DefaultPackage!.Version;

var candidates = PackageManager.GetAvailableBuiltConfigurations(ProjectPath, "App.dll");
var projectPath = ProjectPath ?? AppTools.ValidateAndSanitizeAppPath(ProjectPath);

if (candidates.Length == 0)
{
throw new CommandException($"Cannot find a compiled application at '{ProjectPath}'", CommandExitCode.FileNotFound);
}
BuildApp(projectPath);

var store = _fileManager.Firmware["Meadow F7"];
await store.Refresh();
var osVersion = OsVersion ?? store?.DefaultPackage?.Version ?? "unknown";
var buildPath = GetAppBuildPath(projectPath);

var file = candidates.OrderByDescending(c => c.LastWriteTime).First();
// trim
Logger.LogInformation(string.Format(Strings.TrimmingApplicationForSpecifiedVersion, osVersion));
await _packageManager.TrimApplication(file, cancellationToken: CancellationToken);
await AppTools.TrimApplication(projectPath, _packageManager, Configuration, null, Logger, Console, CancellationToken);
Logger.LogInformation(string.Format(Strings.TrimmedApplicationForSpecifiedVersion, osVersion));

// package
var packageDir = Path.Combine(file.Directory?.FullName ?? string.Empty, PackageManager.PackageOutputDirectoryName);
//TODO - properly manage shared paths
var postlinkDir = Path.Combine(file.Directory?.FullName ?? string.Empty, PackageManager.PostLinkDirectoryName);
var packageDir = Path.Combine(buildPath, PackageManager.PackageOutputDirectoryName);
var postlinkDir = Path.Combine(buildPath, PackageManager.PostLinkDirectoryName);

//copy non-assembly files to the postlink directory
CopyContentFiles(buildPath, postlinkDir);

Logger.LogInformation(Strings.AssemblingCloudPackage);
var packagePath = await _packageManager.AssemblePackage(postlinkDir, packageDir, osVersion, MpakName, Filter, true, CancellationToken);
Expand All @@ -86,4 +72,93 @@ protected override async ValueTask ExecuteCommand()
throw new CommandException(Strings.PackageAssemblyFailed);
}
}

private async Task ValidateFirmwarePackage(IFirmwarePackageCollection? collection)
{
await _fileManager.Refresh();

// for now we only support F7
if (collection == null || collection.Count() == 0)
{
throw new CommandException(Strings.NoFirmwarePackagesFound, CommandExitCode.GeneralError);
}

if (collection.DefaultPackage == null)
{
throw new CommandException(Strings.NoDefaultFirmwarePackageSet, CommandExitCode.GeneralError);
}
}

private void BuildApp(string path)
{
Configuration ??= "Release";

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

var success = _packageManager.BuildApplication(path, Configuration);

if (!success)
{
throw new CommandException("Build failed", CommandExitCode.GeneralError);
}
else
{
Logger?.LogInformation($"Build successful");
}
}

private string GetAppBuildPath(string path)
{
var candidates = PackageManager.GetAvailableBuiltConfigurations(path, "App.dll");

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

var file = candidates.OrderByDescending(c => c.LastWriteTime).First();

//get the directory of the file
return Path.GetDirectoryName(file.FullName);
}

/// <summary>
/// Copy all files that are not assemblies in the source directory to the target directory
// and ignore the prelink, postlink, and package output directories if they're in the source directory
/// </summary>
/// <param name="sourceDir">Source, the compiled output folder</param>
/// <param name="targetDir">The target folder</param>
private void CopyContentFiles(string sourceDir, string targetDir)
{
var sourceFiles = Directory.GetFiles(sourceDir, "*.*", SearchOption.AllDirectories);

foreach (var sourceFile in sourceFiles)
{
var relativePath = Path.GetRelativePath(sourceDir, sourceFile);
var targetFile = Path.Combine(targetDir, relativePath);
var targetDirectory = Path.GetDirectoryName(targetFile);

if (sourceFile.Contains(PackageManager.PostLinkDirectoryName) ||
sourceFile.Contains(PackageManager.PreLinkDirectoryName) ||
sourceFile.Contains(PackageManager.PackageOutputDirectoryName))
{
continue;
}

if (targetDirectory != null && !Directory.Exists(targetDirectory))
{
Directory.CreateDirectory(targetDirectory);
}

if (Path.GetExtension(sourceFile) == ".dll" ||
Path.GetExtension(sourceFile) == ".exe" ||
Path.GetExtension(sourceFile) == ".pdb")
{
continue;
}

File.Copy(sourceFile, targetFile, true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ protected override async ValueTask ExecuteCommand()
if (result.isSuccess)
{
Logger?.LogInformation(Strings.ProvisioningSucceeded);
Logger?.LogInformation($"Device ID: {provisioningID}");
}
else
{
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.34.0</PackageVersion>
<PackageVersion>2.0.35.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.34.0";
public const string CLI_VERSION = "2.0.35.0";
}
2 changes: 1 addition & 1 deletion Source/v2/Meadow.Cli/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@
},
"Cloud package create": {
"commandName": "Project",
"commandLineArgs": "cloud package create F:\\temp\\MeadowApplication1"
"commandLineArgs": "cloud package create H:\\WL\\Blinky\\Blinky\\BlinkyCs"
},
"Cloud package upload": {
"commandName": "Project",
Expand Down
3 changes: 2 additions & 1 deletion Source/v2/Meadow.Cli/Strings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ public static class Strings
public const string BuildingSpecifiedConfiguration = "Building {0} configuration of application...";
public const string BuildFailed = "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...";
public const string PackageAssemblyFailed = "Package assembly failed";
public const string PackageAvailableAtSpecifiedPath = "Done. Package is available at {0}";
public const string PackageAvailableAtSpecifiedPath = "Package is available at {0}";
public const string NoCompiledApplicationFound = "No compiled application found";
public const string DfuDeviceDetected = "DFU Device Detected";
public const string UsingDfuToWriteOs = "using DFU to write OS";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Meadow.Package;

public partial class PackageManager
{
private const string PreLinkDirectoryName = "prelink_bin";
public const string PreLinkDirectoryName = "prelink_bin";
public const string PostLinkDirectoryName = "postlink_bin";
public const string PackageOutputDirectoryName = "mpak";

Expand Down
5 changes: 3 additions & 2 deletions Source/v2/Meadow.Tooling.Core/Package/PackageManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public Task<string> AssemblePackage(string contentSourceFolder,
string outputFolder,
string osVersion,
string? mpakName = null,
string filter = "*",
string filter = "**/*",
bool overwrite = false,
CancellationToken? cancellationToken = null)
{
Expand Down Expand Up @@ -204,7 +204,8 @@ public Task<string> AssemblePackage(string contentSourceFolder,

foreach (var fPath in appFiles)
{
CreateEntry(archive, Path.Combine(contentSourceFolder, fPath), Path.Combine("app", Path.GetFileName(fPath)));
var destination = Path.Combine("app", fPath);
CreateEntry(archive, Path.Combine(contentSourceFolder, fPath), destination);
}

// write a metadata file info.json in the mpak
Expand Down

0 comments on commit 2603425

Please sign in to comment.