/* * SPDX-License-Identifier: AGPL-3.0-or-later * Copyright (C) 2025 Sergej Görzen * This file is part of xAPI4Unity. */ #if UNITY_EDITOR using System; using System.Diagnostics; using Debug = UnityEngine.Debug; namespace xAPI4Unity.Editor { /// /// Provides helper methods for executing external processes (e.g., shell commands). /// public static class ProcessHelper { /// /// Executes a command-line process with specified arguments and options. /// /// The name or path of the executable to run. /// The command-line arguments to pass to the executable. /// Indicates whether to display the shell window during execution. /// The working directory to execute the process in. Defaults to the current directory. /// An optional callback to configure the before starting it. /// The instance after execution. public static Process Cmd(string app, string arguments, bool showShell = false, string workingDirectory = default, Action configure = null) { // Log the command execution for debugging purposes Debug.Log($"[xAPI4Unity] Executed `> {app} {arguments}` in directory '{workingDirectory}'"); // Set up the process start information var startInfo = new ProcessStartInfo(app) { Arguments = arguments, UseShellExecute = showShell, RedirectStandardOutput = !showShell, // Redirect output if not showing the shell CreateNoWindow = !showShell, // Avoid creating a window if shell is not shown WorkingDirectory = workingDirectory ?? Environment.CurrentDirectory // Default to current directory if not specified }; // Create the process with the specified start information var process = new Process() { StartInfo = startInfo }; // Apply any additional configuration provided configure?.Invoke(process); // Start the process process.Start(); // Wait for the process to exit if the shell is hidden process.WaitForExit(); // Begin reading standard output if shell is not displayed if (!showShell) process.BeginOutputReadLine(); return process; } } } #endif