From 05d6eece99c180c163e588c0c7f7568a3f3069d1 Mon Sep 17 00:00:00 2001 From: Scott Date: Thu, 6 Aug 2026 14:46:06 +1000 Subject: [PATCH] chore(powershell-harness): refactor command code, add git provider to runspace, output command results to console --- src/PowershellHarness/Program.cs | 59 +++++++++++++++++++++++++++----- 1 file changed, 51 insertions(+), 8 deletions(-) diff --git a/src/PowershellHarness/Program.cs b/src/PowershellHarness/Program.cs index 077a9b7..4bbd0be 100644 --- a/src/PowershellHarness/Program.cs +++ b/src/PowershellHarness/Program.cs @@ -2,6 +2,7 @@ using System.Management.Automation.Runspaces; using System.Text; using PowershellModule.Calendar; +using PowershellModule.Git; namespace PowershellHarness; @@ -36,8 +37,26 @@ class Program var host = new CustomHost(Console.WindowWidth); var runspace = InitialisePowershellHost(host); - // InvokeCommand(runspace, GetCalendarCommand.FullName); + host.UI.SetNextPromptChoice(3); + InvokeCommand(runspace, "New-PSDrive", [ + CreateCommand("name", "git-test"), + CreateCommand("PSProvider", "GitRepo"), + CreateCommand("Root", "\\"), + ]); + + InvokeCommand(runspace, "Set-Location", + [ + // Technically this can just be the command but this is a bit easier + CreateCommand("path", "git-test:/") + ]); + + InvokeCommand(runspace, "Get-Location"); + } + + private static void CalendarTestCommands(Runspace runspace) + { + InvokeCommand(runspace, GetCalendarCommand.FullName); foreach (var day in Enum.GetValues()) { InvokeCommand(runspace, GetCalendarCommand.FullName, [ @@ -48,11 +67,15 @@ class Program ]); } - // InvokeCommand(runspace, GetCalendarCommand.FullName, [ - // new CommandParameter(nameof(GetCalendarCommand.MarkedDaySymbol), "🤫"), - // new CommandParameter(nameof(GetCalendarCommand.StartOfWeek), DayOfWeek.Saturday) - // ]); - // InvokeCommand(runspace, GetCalendarCommand.FullName, [new CommandParameter(nameof(GetCalendarCommand.StartOfWeek), "Sunday")]); + InvokeCommand(runspace, GetCalendarCommand.FullName, [ + new CommandParameter(nameof(GetCalendarCommand.MarkedDaySymbol), "🤫"), + new CommandParameter(nameof(GetCalendarCommand.StartOfWeek), DayOfWeek.Saturday) + ]); + InvokeCommand(runspace, GetCalendarCommand.FullName, [new CommandParameter(nameof(GetCalendarCommand.StartOfWeek), "Sunday")]); + } + + private static void TestGitProvider(Runspace runspace) + { } private static CommandParameter CreateCommand(string name, string? argument = null) @@ -97,6 +120,10 @@ class Program var getCalendarCommand = new SessionStateCmdletEntry(GetCalendarCommand.FullName, typeof(GetCalendarCommand), null); initialSessionState.Commands.Add(getCalendarCommand); + + var gitProvider = new SessionStateProviderEntry(GitProvider.Name, typeof(GitProvider), null); + initialSessionState.Providers.Add(gitProvider); + // Create a runspace from the state, open and return it var runspace = RunspaceFactory.CreateRunspace(host, initialSessionState); @@ -114,16 +141,29 @@ class Program // and this is just a debug harness so it doesn't really matter for now using var pipeline = runspace.CreatePipeline(); // using var powershell = PowerShell.Create(runspace); + + // StringBuilder to store the output of this command including any output results (but not errors yet) + // this is just a rudimentary test and the pwsh debug profile should be used instead as it loads the module + // in a full powershell window with debugger attached. Just no automatic command running sadly. + var sb = new StringBuilder(); + + sb.Append(command); var cmd = new Command(command); if (parameters is not null) { - foreach (var commandParameter in parameters) + var param = parameters.ToList(); + sb.Append(' ') + .AppendJoin(' ', param.Select(x => $"-{x.Name} {x.Value}")); + + foreach (var commandParameter in param) { cmd.Parameters.Add(commandParameter); } } + sb.AppendLine(); + pipeline.Commands.Add(cmd); // powershell.Commands.AddCommand(cmd); @@ -133,8 +173,11 @@ class Program // var results = powershell.Invoke(); foreach (var result in results) { - Console.Write(result); + sb.AppendLine(result.ToString()); + // Console.WriteLine(result); } + + Console.WriteLine(sb); } catch (Exception ex) {