Skip to content

Instantly share code, notes, and snippets.

@kezzyhko
Last active February 17, 2025 17:27
Show Gist options
  • Select an option

  • Save kezzyhko/705f1a8fe2667bd5c3f1ed0bf4cbc1ef to your computer and use it in GitHub Desktop.

Select an option

Save kezzyhko/705f1a8fe2667bd5c3f1ed0bf4cbc1ef to your computer and use it in GitHub Desktop.
Testing MedallionShell in Unity
#if UNITY_EDITOR
using System;
using UnityEditor;
using Medallion.Shell;
using UnityEditor.Compilation;
using UnityEngine;
public static class MedallionShellTest
{
private const string Prefix = "Shell Test/";
private const int Priority = -9999;
private static int MyProcessId
{
get => SessionState.GetInt(Prefix + "ProcessId", -1);
set => SessionState.SetInt(Prefix + "ProcessId", value);
}
private static Command _myCommand;
private static readonly Shell MyShell = new(options =>
{
options.StartInfo(startInfo =>
{
startInfo.CreateNoWindow = false;
startInfo.RedirectStandardInput = false;
startInfo.RedirectStandardOutput = false;
startInfo.RedirectStandardError = false;
startInfo.UseShellExecute = true;
});
options.ThrowOnError();
options.DisposeOnExit(false);
});
[MenuItem(Prefix + "Run shell", false, Priority + 10)]
private static void Run()
{
_myCommand = MyShell.Run("cmd", "/c", "ping", "-t", "google.com");
Debug.Log($"Run | Success | {_myCommand.ProcessId}");
MyProcessId = _myCommand.ProcessId;
}
[MenuItem(Prefix + "/Domain Reload", false, Priority + 20)]
private static void ReloadDomain()
{
CompilationPipeline.RequestScriptCompilation();
}
[MenuItem(Prefix + "/Attach", false, Priority + 30)]
private static void Attach()
{
var isSuccess = Command.TryAttachToProcess(MyProcessId, out _myCommand);
if (!isSuccess)
{
Debug.Log($"Attach | Fail | {MyProcessId}");
return;
}
Debug.Log($"Attach | Success | {_myCommand.ProcessId}");
}
[MenuItem(Prefix + "/Get info", false, Priority + 40)]
// ReSharper disable once AsyncVoidMethod
private static async void GetInfo()
{
var result = await _myCommand.Task;
Debug.Log(result.ExitCode);
}
[MenuItem(Prefix + "/CtrlC", false, Priority + 50)]
private static void SoftStop()
{
_myCommand.TrySignalAsync(CommandSignal.ControlC);
}
[MenuItem(Prefix + "/Kill", false, Priority + 60)]
private static void Kill()
{
_myCommand.Kill();
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment