Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deploy linked binaries if available + some cleanup #472

Merged
merged 7 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.4</PackageVersion>
<PackageVersion>2.0.5</PackageVersion>
<Platforms>AnyCPU</Platforms>
<PackageProjectUrl>http://developer.wildernesslabs.co/Meadow/Meadow.CLI/</PackageProjectUrl>
<RepositoryUrl>https://github.com/WildernessLabs/Meadow.CLI</RepositoryUrl>
Expand Down
61 changes: 44 additions & 17 deletions Source/v2/Meadow.Cli/AppManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Meadow.Hcom;
using Meadow.Linker;
using Meadow.Package;
using Meadow.Software;
using Microsoft.Extensions.Logging;
Expand All @@ -14,12 +15,12 @@ private static bool MatchingDllExists(string file)

private static bool IsPdb(string file)
{
return String.Compare(Path.GetExtension(file), ".pdb", StringComparison.OrdinalIgnoreCase) == 0;
return string.Compare(Path.GetExtension(file), ".pdb", StringComparison.OrdinalIgnoreCase) == 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is bool string.Equals(string, string, StringComparison) available here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah the CLI is a .NET 8 app so I get the fancy APIs ;)

}

private static bool IsXmlDoc(string file)
{
if (String.Compare(Path.GetExtension(file), ".xml", StringComparison.OrdinalIgnoreCase) == 0)
if (string.Compare(Path.GetExtension(file), ".xml", StringComparison.OrdinalIgnoreCase) == 0)
{
return MatchingDllExists(file);
}
Expand All @@ -36,27 +37,52 @@ public static async Task DeployApplication(
CancellationToken cancellationToken)
{
// TODO: add sub-folder support when HCOM supports it

var localFiles = new Dictionary<string, uint>();

// get a list of files to send
var dependencies = packageManager.GetDependencies(new FileInfo(Path.Combine(localBinaryDirectory, "App.dll")));
var dependencies = new List<string>();

var processedAppPath = localBinaryDirectory;

//check if there's a post link folder
if (Directory.Exists(Path.Combine(localBinaryDirectory, MeadowLinker.PostLinkDirectoryName)))
{
processedAppPath = Path.Combine(localBinaryDirectory, MeadowLinker.PostLinkDirectoryName);

//add all dlls from the postlink_bin folder to the dependencies
dependencies = Directory.EnumerateFiles(processedAppPath, "*.dll", SearchOption.TopDirectoryOnly).ToList();
dependencies.Remove(Path.Combine(processedAppPath, "App.dll"));

//add all pdbs from the postlink_bin folder to the dependencies if includePdbs is true
if (includePdbs)
{
dependencies.AddRange(Directory.EnumerateFiles(processedAppPath, "*.pdb", SearchOption.TopDirectoryOnly));
dependencies.Remove(Path.Combine(processedAppPath, "App.pdb"));
}
}
else
{
dependencies = packageManager.GetDependencies(new FileInfo(Path.Combine(processedAppPath, "App.dll")));
}
dependencies.Add(Path.Combine(localBinaryDirectory, "App.dll"));

if (includePdbs)
{
dependencies.Add(Path.Combine(localBinaryDirectory, "App.pdb"));
}

var binaries = Directory.EnumerateFiles(localBinaryDirectory, "*.*", SearchOption.TopDirectoryOnly)
.Where(s => new FileInfo(s).Extension != ".dll")
.Where(s => new FileInfo(s).Extension != ".pdb")
.Where(s => !s.Contains(".DS_Store"));
.Where(s => !s.Contains(".DS_Store")).ToList();
dependencies.AddRange(binaries);


logger?.LogInformation("Generating list of files to deploy...");

foreach (var file in dependencies)
{
// TODO: add any other filtering capability here

if (!includePdbs && IsPdb(file)) continue;
if (!includeXmlDocs && IsXmlDoc(file)) continue;
if (!includePdbs && IsPdb(file)) { continue; }
stevenkuhn marked this conversation as resolved.
Show resolved Hide resolved
if (!includeXmlDocs && IsXmlDoc(file)) { continue; }

// read the file data so we can generate a CRC
using FileStream fs = File.Open(file, FileMode.Open);
Expand All @@ -70,7 +96,7 @@ public static async Task DeployApplication(
localFiles.Add(file, crc);
}

if (localFiles.Count() == 0)
if (localFiles.Count == 0)
{
logger?.LogInformation($"No new files to deploy");
}
Expand All @@ -82,9 +108,9 @@ public static async Task DeployApplication(
var removeFiles = deviceFiles
.Select(f => Path.GetFileName(f.Name))
.Except(localFiles.Keys
.Select(f => Path.GetFileName(f)));
.Select(f => Path.GetFileName(f))).ToList();

if (!removeFiles.Any())
if (removeFiles.Count == 0)
{
logger?.LogInformation($"No files to delete");
}
Expand All @@ -103,9 +129,10 @@ public static async Task DeployApplication(

if (existing != null && existing.Crc != null)
{
if (uint.Parse(existing.Crc.Substring(2), System.Globalization.NumberStyles.HexNumber) == localFile.Value)
{
// exists and has a matching CRC, skip it
var crc = uint.Parse(existing.Crc.Substring(2), System.Globalization.NumberStyles.HexNumber);

if (crc == localFile.Value)
{ // exists and has a matching CRC, skip it
continue;
}
}
Expand All @@ -114,7 +141,7 @@ public static async Task DeployApplication(

if (!await connection.WriteFile(localFile.Key, null, cancellationToken))
{
logger?.LogWarning($"Error sending'{Path.GetFileName(localFile.Key)}'. Retrying.");
logger?.LogWarning($"Error sending'{Path.GetFileName(localFile.Key)}' - retrying");
await Task.Delay(100);
goto send_file;
}
Expand Down
2 changes: 2 additions & 0 deletions Source/v2/Meadow.Cli/Commands/Current/App/AppDeployCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ protected override async ValueTask ExecuteCommand()

if (isRuntimeEnabled)
{
await Task.Delay(1000);

// restore runtime state
Logger?.LogInformation("Enabling runtime...");

Expand Down
2 changes: 1 addition & 1 deletion Source/v2/Meadow.Cli/Commands/Current/App/AppRunCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class AppRunCommand : BaseDeviceCommand<AppRunCommand>
[CommandOption('c', Description = "The build configuration to compile", IsRequired = false)]
public string? Configuration { get; set; }

[CommandParameter(0, Name = "Path to folder containing the application to build.", IsRequired = false)]
[CommandParameter(0, Name = "Path to folder containing the application to build", IsRequired = false)]
public string? Path { get; init; }

public AppRunCommand(IPackageManager packageManager, MeadowConnectionManager connectionManager, ILoggerFactory loggerFactory)
Expand Down
3 changes: 1 addition & 2 deletions Source/v2/Meadow.Cli/Commands/Current/BaseCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ public async ValueTask ExecuteAsync(IConsole console)

await ExecuteCommand();
}
catch (CommandException ce)
catch (CommandException)
{
Logger?.LogError(ce.Message);
throw;
}
catch (Exception ex)
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,6 +6,6 @@ namespace Meadow.CLI
{
public static class Constants
{
public const string CLI_VERSION = "2.0.4.0";
public const string CLI_VERSION = "2.0.5.0";
}
}
4 changes: 2 additions & 2 deletions Source/v2/Meadow.Linker/Linker/MeadowLinker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public class MeadowLinker
private const string IL_LINKER_DLL = "illink.dll";
private const string MEADOW_LINK_XML = "meadow_link.xml";

private const string PostLinkDirectoryName = "postlink_bin";
private const string PreLinkDirectoryName = "prelink_bin";
public const string PostLinkDirectoryName = "postlink_bin";
public const string PreLinkDirectoryName = "prelink_bin";

readonly ILLinker _linker;
readonly ILogger? _logger;
Expand Down
6 changes: 2 additions & 4 deletions Source/v2/Meadow.Package/PackageManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
using Meadow.Software;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text.Json;
using YamlDotNet.Serialization;
Expand Down Expand Up @@ -230,10 +228,10 @@ private void CreateEntry(ZipArchive archive, string fromFile, string entryPath)

public static FileInfo[] GetAvailableBuiltConfigurations(string rootFolder, string appName = "App.dll")
{
// check if we were give path to a project file, not the folder of the project file.
// check if we were give path to a project file, not the folder of the project file
if (File.Exists(rootFolder))
{
rootFolder = Path.GetDirectoryName(rootFolder) ?? ""; // extreact the folder name or if invalid, use the current directory.
rootFolder = Path.GetDirectoryName(rootFolder) ?? ""; // extreact the folder name or if invalid, use the current directory
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small nit on "extreact"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice - I'll fix

}
if (!Directory.Exists(rootFolder)) { throw new DirectoryNotFoundException($"Directory not found '{rootFolder}'. Check path to project file."); }

Expand Down
Loading