Skip to content

Commit

Permalink
Check for and use linked binaries when deploying an app + minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianstevens committed Feb 14, 2024
1 parent 7fa5044 commit ce3d0aa
Showing 1 changed file with 44 additions and 17 deletions.
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;
}

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; }
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

0 comments on commit ce3d0aa

Please sign in to comment.