chore(powershell-harness): refactor command code, add git provider to runspace, output command results to console
This commit is contained in:
parent
e6665a588b
commit
05d6eece99
1 changed files with 51 additions and 8 deletions
|
|
@ -2,6 +2,7 @@
|
||||||
using System.Management.Automation.Runspaces;
|
using System.Management.Automation.Runspaces;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using PowershellModule.Calendar;
|
using PowershellModule.Calendar;
|
||||||
|
using PowershellModule.Git;
|
||||||
|
|
||||||
namespace PowershellHarness;
|
namespace PowershellHarness;
|
||||||
|
|
||||||
|
|
@ -36,8 +37,26 @@ class Program
|
||||||
var host = new CustomHost(Console.WindowWidth);
|
var host = new CustomHost(Console.WindowWidth);
|
||||||
var runspace = InitialisePowershellHost(host);
|
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<DayOfWeek>())
|
foreach (var day in Enum.GetValues<DayOfWeek>())
|
||||||
{
|
{
|
||||||
InvokeCommand(runspace, GetCalendarCommand.FullName, [
|
InvokeCommand(runspace, GetCalendarCommand.FullName, [
|
||||||
|
|
@ -48,11 +67,15 @@ class Program
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// InvokeCommand(runspace, GetCalendarCommand.FullName, [
|
InvokeCommand(runspace, GetCalendarCommand.FullName, [
|
||||||
// new CommandParameter(nameof(GetCalendarCommand.MarkedDaySymbol), "🤫"),
|
new CommandParameter(nameof(GetCalendarCommand.MarkedDaySymbol), "🤫"),
|
||||||
// new CommandParameter(nameof(GetCalendarCommand.StartOfWeek), DayOfWeek.Saturday)
|
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.StartOfWeek), "Sunday")]);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void TestGitProvider(Runspace runspace)
|
||||||
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
private static CommandParameter CreateCommand(string name, string? argument = null)
|
private static CommandParameter CreateCommand(string name, string? argument = null)
|
||||||
|
|
@ -97,6 +120,10 @@ class Program
|
||||||
var getCalendarCommand = new SessionStateCmdletEntry(GetCalendarCommand.FullName, typeof(GetCalendarCommand), null);
|
var getCalendarCommand = new SessionStateCmdletEntry(GetCalendarCommand.FullName, typeof(GetCalendarCommand), null);
|
||||||
initialSessionState.Commands.Add(getCalendarCommand);
|
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
|
// Create a runspace from the state, open and return it
|
||||||
var runspace = RunspaceFactory.CreateRunspace(host, initialSessionState);
|
var runspace = RunspaceFactory.CreateRunspace(host, initialSessionState);
|
||||||
|
|
||||||
|
|
@ -115,15 +142,28 @@ class Program
|
||||||
using var pipeline = runspace.CreatePipeline();
|
using var pipeline = runspace.CreatePipeline();
|
||||||
// using var powershell = PowerShell.Create(runspace);
|
// 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);
|
var cmd = new Command(command);
|
||||||
if (parameters is not null)
|
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);
|
cmd.Parameters.Add(commandParameter);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sb.AppendLine();
|
||||||
|
|
||||||
pipeline.Commands.Add(cmd);
|
pipeline.Commands.Add(cmd);
|
||||||
// powershell.Commands.AddCommand(cmd);
|
// powershell.Commands.AddCommand(cmd);
|
||||||
|
|
||||||
|
|
@ -133,8 +173,11 @@ class Program
|
||||||
// var results = powershell.Invoke();
|
// var results = powershell.Invoke();
|
||||||
foreach (var result in results)
|
foreach (var result in results)
|
||||||
{
|
{
|
||||||
Console.Write(result);
|
sb.AppendLine(result.ToString());
|
||||||
|
// Console.WriteLine(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Console.WriteLine(sb);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue