Skip to content

Commit

Permalink
Tidy up.
Browse files Browse the repository at this point in the history
  • Loading branch information
CartBlanche committed Jun 19, 2024
1 parent d9ce989 commit 5bbcff1
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 40 deletions.
38 changes: 38 additions & 0 deletions Source/v2/Meadow.CLI/Commands/Current/App/AppTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,42 @@ internal static string SanitizeMeadowFilename(string fileName)

return meadowFileName!.Replace(Path.DirectorySeparatorChar, '/');
}

internal static async Task<int> RunProcessCommand(string command, string args, Action<string>? handleOutput = null, Action<string>? handleError = null, CancellationToken cancellationToken = default)
{
var processStartInfo = new ProcessStartInfo
{
FileName = command,
Arguments = args,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};

using (var process = new Process { StartInfo = processStartInfo })
{
process.Start();

var outputCompletion = ReadLinesAsync(process.StandardOutput, handleOutput, cancellationToken);
var errorCompletion = ReadLinesAsync(process.StandardError, handleError, cancellationToken);

await Task.WhenAll(outputCompletion, errorCompletion, process.WaitForExitAsync());

return process.ExitCode;
}
}

private static async Task ReadLinesAsync(StreamReader reader, Action<string>? handleLine, CancellationToken cancellationToken)
{
while (!reader.EndOfStream)
{
var line = await reader.ReadLineAsync(cancellationToken);
if (!string.IsNullOrWhiteSpace(line)
&& handleLine != null)
{
handleLine(line);
}
}
}
}
59 changes: 19 additions & 40 deletions Source/v2/Meadow.CLI/Commands/Current/UpdateCommand.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
using CliFx.Attributes;
using System.Reflection;
using CliFx.Attributes;
using Microsoft.Extensions.Logging;
using Mono.Cecil.Cil;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using static Meadow.CLI.Strings;

namespace Meadow.CLI.Commands.DeviceManagement;

[Command("update", Description = Update.Description)]
[Command("update", Description = Strings.Update.Description)]
public class UpdateCommand : BaseCommand<UpdateCommand>
{
const string MEADOW_CLI = "WildernessLabs.Meadow.CLI";
const string MEADOW_UPDATER = "Meadow.Updater";
const string CLIFX = "CliFx";
const string CLIFX = "CliFx";

[CommandOption("version", 'v', IsRequired = false)]
[CommandOption("version", 'v', IsRequired = false)]
public string? Version { get; set; }

public UpdateCommand(
ILoggerFactory loggerFactory)
public UpdateCommand(ILoggerFactory loggerFactory)
: base(loggerFactory)
{
}

protected override async ValueTask ExecuteCommand()
{
Logger.LogInformation(Update.Updating, MEADOW_CLI);
Logger.LogInformation(Strings.Update.Updating, MEADOW_CLI);

string toVersion;
if (!string.IsNullOrWhiteSpace(Version))
Expand All @@ -38,54 +33,37 @@ protected override async ValueTask ExecuteCommand()
toVersion = "vLatest";
}
;
Logger.LogInformation(Update.Instruction1, MEADOW_CLI, toVersion);
Logger.LogInformation(Update.Instruction2);
Logger.LogInformation(Strings.Update.Instruction1, MEADOW_CLI, toVersion);
Logger.LogInformation(Strings.Update.Instruction2);

// Path to the updater executable within the tool's output directory
string meadowUpdaterPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? string.Empty, $"{MEADOW_UPDATER}.dll");

// Ensure the updater executable exists

// Ensure the updater executable exists
if (!File.Exists(meadowUpdaterPath))
{
Logger.LogError(Update.UpdaterNotFound);
Logger.LogError(Strings.Update.UpdaterNotFound);
return;
}
}

// Copy all necessary files to a temporary location, so there aren't any access issues
// Copy all necessary files to a temporary location, so there aren't any access issues
string tempUpdaterDir = Path.Combine(Path.GetTempPath(), MEADOW_UPDATER);
if (!Directory.Exists(tempUpdaterDir))
{
Directory.CreateDirectory(tempUpdaterDir);
}
CopyMeadowUpdaterFiles(Path.GetDirectoryName(meadowUpdaterPath), tempUpdaterDir, MEADOW_UPDATER);

// Supporting files required in the temp directory
CopyMeadowUpdaterFiles(Path.GetDirectoryName(meadowUpdaterPath), tempUpdaterDir, CLIFX);
// Supporting files required in the temp directory
CopyMeadowUpdaterFiles(Path.GetDirectoryName(meadowUpdaterPath), tempUpdaterDir, CLIFX);

string commandArguments = $"update -t {MEADOW_CLI}";
string commandArguments = $"update -t {MEADOW_CLI}";
if (!string.IsNullOrWhiteSpace(Version))
{
commandArguments += $" -v {Version}";
}

RunCommand("dotnet", $"{Path.Combine(tempUpdaterDir, $"{MEADOW_UPDATER}.dll")} {commandArguments}");
}

internal static void RunCommand(string command, string arguments)
{
var processStartInfo = new ProcessStartInfo
{
FileName = command,
Arguments = arguments,
UseShellExecute = false,
CreateNoWindow = true
};

// Fire and forget as the updating happens in the called exe
using (var process = new Process { StartInfo = processStartInfo })
{
process.Start();
}
await AppTools.RunProcessCommand("dotnet", $"{Path.Combine(tempUpdaterDir, $"{MEADOW_UPDATER}.dll")} {commandArguments}", cancellationToken: CancellationToken);
}

internal static void CopyMeadowUpdaterFiles(string? sourceDirectory, string targetDirectory, string filesToCopy)
Expand All @@ -94,6 +72,7 @@ internal static void CopyMeadowUpdaterFiles(string? sourceDirectory, string targ
{
return;
}

var toolUpdaterFiles = Directory.GetFiles(sourceDirectory, $"{filesToCopy}*");
foreach (var file in toolUpdaterFiles)
{
Expand Down

0 comments on commit 5bbcff1

Please sign in to comment.