From 65275b52a7ca8082946e18efd015baefeeba201c Mon Sep 17 00:00:00 2001 From: Caelan Sayler Date: Sun, 19 Oct 2025 16:22:18 +0100 Subject: [PATCH] Improve 097c1df by using provided locator instead of default in VelopackApp --- src/lib-csharp/Locators/DefaultProcessImpl.cs | 33 +++++++++---------- src/lib-csharp/Locators/IProcessImpl.cs | 25 ++++++++++++-- src/lib-csharp/VelopackApp.cs | 11 ++----- 3 files changed, 41 insertions(+), 28 deletions(-) diff --git a/src/lib-csharp/Locators/DefaultProcessImpl.cs b/src/lib-csharp/Locators/DefaultProcessImpl.cs index 996ae795..eec0972c 100644 --- a/src/lib-csharp/Locators/DefaultProcessImpl.cs +++ b/src/lib-csharp/Locators/DefaultProcessImpl.cs @@ -2,8 +2,8 @@ using System; using System.Collections.Generic; -using System.ComponentModel; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Runtime.InteropServices; using Velopack.Logging; @@ -29,7 +29,8 @@ namespace Velopack.Locators { _currentProcess ??= Process.GetCurrentProcess(); var fileName = _currentProcess.MainModule?.FileName ?? - throw new InvalidOperationException($"Could not determine process path, please construct {nameof(IVelopackLocator)} manually."); + throw new InvalidOperationException( + $"Could not determine process path, please construct {nameof(IVelopackLocator)} manually."); return Path.GetFullPath(fileName); } @@ -41,8 +42,7 @@ namespace Velopack.Locators public void StartProcess(string exePath, IEnumerable args, string? workDir, bool showWindow) { - var psi = new ProcessStartInfo() - { + var psi = new ProcessStartInfo() { CreateNoWindow = true, FileName = exePath, WorkingDirectory = workDir, @@ -55,27 +55,26 @@ namespace Velopack.Locators _logger.Info($"Process started with ID: {p.Id}"); - if (VelopackRuntimeInfo.IsWindows) - { - try - { + if (VelopackRuntimeInfo.IsWindows) { + try { // this is an attempt to work around a bug where the restarted app fails to come to foreground. if (!AllowSetForegroundWindow(p.Id)) - _logger.LogWarning("Failed to allow Update.exe to set foreground window."); - - } - catch (Exception ex) - { + _logger.LogWarning("Failed to allow Update.exe to set foreground window. (This is not generally concerning.)"); + } catch (Exception ex) { _logger.LogWarning(ex, "Error occurred while trying to allow Update.exe to set foreground window."); } } - - if (p.HasExited) - { + + if (p.HasExited) { _logger.Error($"Process {p.Id} has already exited."); } } - public void Exit(int exitCode) => Environment.Exit(exitCode); + public void Exit(int exitCode) + { + if (!VelopackRuntimeInfo.InUnitTestRunner) { + Environment.Exit(exitCode); + } + } } } \ No newline at end of file diff --git a/src/lib-csharp/Locators/IProcessImpl.cs b/src/lib-csharp/Locators/IProcessImpl.cs index 4f1e99c5..f00b977c 100644 --- a/src/lib-csharp/Locators/IProcessImpl.cs +++ b/src/lib-csharp/Locators/IProcessImpl.cs @@ -1,13 +1,32 @@ -#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member - -using System.Collections.Generic; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; namespace Velopack.Locators { + /// + /// Provides an abstraction for process-related operations, allowing for platform-specific implementations. + /// public interface IProcessImpl { + /// + /// Gets the full path to the current process executable. + /// + /// The full path to the current process executable. string GetCurrentProcessPath(); + + /// + /// Gets the process ID of the current process. + /// + /// The process ID of the current process. uint GetCurrentProcessId(); + + /// + /// Starts a new process with the specified executable path, arguments, and options. + /// + /// The path to the executable to start. + /// The command-line arguments to pass to the process. + /// The working directory for the new process. + /// Whether to show a window for the new process. void StartProcess(string exePath, IEnumerable args, string workDir, bool showWindow); /// diff --git a/src/lib-csharp/VelopackApp.cs b/src/lib-csharp/VelopackApp.cs index 51098f9b..631bb7c1 100644 --- a/src/lib-csharp/VelopackApp.cs +++ b/src/lib-csharp/VelopackApp.cs @@ -202,11 +202,11 @@ namespace Velopack var version = SemanticVersion.Parse(args[1]); hook(version); log.Info("Completed hook, exiting..."); - Exit(0); + locator.Process.Exit(0); return; } catch (Exception ex) { log.Error(ex, $"Error occurred executing user defined Velopack hook. ({args[0]})"); - Exit(-1); + locator.Process.Exit(-1); return; } } @@ -232,7 +232,7 @@ namespace Velopack if (!restarted && _autoApply) { log.Info("Auto apply is true, so restarting to apply update..."); UpdateExe.Apply(locator, latestLocal, false, locator.Process.GetCurrentProcessId(), true, args); - Exit(0); + locator.Process.Exit(0); } else { log.Info("Pre-condition failed, we will not restart to apply updates. (restarted: " + restarted + ", autoApply: " + _autoApply + ")"); } @@ -273,10 +273,5 @@ namespace Velopack } } } - - private static void Exit(int code) - { - VelopackLocator.GetCurrentOrCreateDefault().Process.Exit(code); - } } } \ No newline at end of file