From cec50297e707c2d9ea32f0a27d78a268b25a29a3 Mon Sep 17 00:00:00 2001 From: Robert Date: Wed, 5 May 2021 12:58:20 +0200 Subject: [PATCH] Plugins - Added ExecuteFileAction --- .../PrerequisiteAction/ExecuteFileAction.cs | 88 +++++++++++++++++++ src/Artemis.UI/Utilities/ProcessUtilities.cs | 54 ++++-------- 2 files changed, 104 insertions(+), 38 deletions(-) create mode 100644 src/Artemis.Core/Plugins/Prerequisites/PrerequisiteAction/ExecuteFileAction.cs diff --git a/src/Artemis.Core/Plugins/Prerequisites/PrerequisiteAction/ExecuteFileAction.cs b/src/Artemis.Core/Plugins/Prerequisites/PrerequisiteAction/ExecuteFileAction.cs new file mode 100644 index 000000000..491e28f04 --- /dev/null +++ b/src/Artemis.Core/Plugins/Prerequisites/PrerequisiteAction/ExecuteFileAction.cs @@ -0,0 +1,88 @@ +using System; +using System.Diagnostics; +using System.Threading; +using System.Threading.Tasks; + +namespace Artemis.Core +{ + /// + /// Represents a plugin prerequisite action that executes a file + /// + public class ExecuteFileAction : PluginPrerequisiteAction + { + /// + /// Creates a new instance of + /// + /// The name of the action + /// The target file to execute + /// A set of command-line arguments to use when starting the application + /// A boolean indicating whether the action should wait for the process to exit + public ExecuteFileAction(string name, string fileName, string? arguments = null, bool waitForExit = true) : base(name) + { + FileName = fileName ?? throw new ArgumentNullException(nameof(fileName)); + Arguments = arguments; + WaitForExit = waitForExit; + } + + /// + /// Gets the target file to execute + /// + public string FileName { get; } + + /// + /// Gets a set of command-line arguments to use when starting the application + /// + public string? Arguments { get; } + + /// + /// Gets a boolean indicating whether the action should wait for the process to exit + /// + public bool WaitForExit { get; } + + /// + public override async Task Execute(CancellationToken cancellationToken) + { + if (WaitForExit) + { + Status = $"Running {FileName} and waiting for exit.."; + ShowProgressBar = true; + ProgressIndeterminate = true; + + int result = await RunProcessAsync(FileName, Arguments); + + Status = $"{FileName} exited with code {result}"; + } + else + { + Status = $"Running {FileName}"; + Process process = new() + { + StartInfo = {FileName = FileName, Arguments = Arguments!}, + EnableRaisingEvents = true + }; + process.Start(); + } + } + + private static Task RunProcessAsync(string fileName, string? arguments) + { + TaskCompletionSource tcs = new(); + + Process process = new() + { + StartInfo = {FileName = fileName, Arguments = arguments!}, + EnableRaisingEvents = true + }; + + process.Exited += (_, _) => + { + tcs.SetResult(process.ExitCode); + process.Dispose(); + }; + + process.Start(); + + return tcs.Task; + } + } +} \ No newline at end of file diff --git a/src/Artemis.UI/Utilities/ProcessUtilities.cs b/src/Artemis.UI/Utilities/ProcessUtilities.cs index dc418ace1..58043c5d9 100644 --- a/src/Artemis.UI/Utilities/ProcessUtilities.cs +++ b/src/Artemis.UI/Utilities/ProcessUtilities.cs @@ -2,33 +2,11 @@ using System.Diagnostics; using System.IO; using System.Runtime.InteropServices; -using System.Threading.Tasks; namespace Artemis.UI.Utilities { public static class ProcessUtilities { - public static Task RunProcessAsync(string fileName, string arguments) - { - TaskCompletionSource tcs = new(); - - Process process = new() - { - StartInfo = {FileName = fileName, Arguments = arguments}, - EnableRaisingEvents = true - }; - - process.Exited += (sender, args) => - { - tcs.SetResult(process.ExitCode); - process.Dispose(); - }; - - process.Start(); - - return tcs.Task; - } - public static Process RunAsDesktopUser(string fileName, string arguments, bool hideWindow) { if (string.IsNullOrWhiteSpace(fileName)) @@ -198,24 +176,24 @@ namespace Artemis.UI.Utilities [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] private struct STARTUPINFO { - public int cb; - public string lpReserved; - public string lpDesktop; - public string lpTitle; - public int dwX; - public int dwY; - public int dwXSize; - public int dwYSize; - public int dwXCountChars; - public int dwYCountChars; - public int dwFillAttribute; + public readonly int cb; + public readonly string lpReserved; + public readonly string lpDesktop; + public readonly string lpTitle; + public readonly int dwX; + public readonly int dwY; + public readonly int dwXSize; + public readonly int dwYSize; + public readonly int dwXCountChars; + public readonly int dwYCountChars; + public readonly int dwFillAttribute; public int dwFlags; public short wShowWindow; - public short cbReserved2; - public IntPtr lpReserved2; - public IntPtr hStdInput; - public IntPtr hStdOutput; - public IntPtr hStdError; + public readonly short cbReserved2; + public readonly IntPtr lpReserved2; + public readonly IntPtr hStdInput; + public readonly IntPtr hStdOutput; + public readonly IntPtr hStdError; } [DllImport("kernel32.dll", ExactSpelling = true)]