Indent and whitespace cleanup powershell-run.cs

This commit is contained in:
Andrew Schwartzmeyer 2015-06-17 11:36:19 -07:00
parent 9351c05c82
commit 8f96a9e786

View file

@ -13,7 +13,7 @@ namespace ps_hello_world
{ {
// this is all from https://msdn.microsoft.com/en-us/library/ee706570%28v=vs.85%29.aspx // this is all from https://msdn.microsoft.com/en-us/library/ee706570%28v=vs.85%29.aspx
internal class MyRawUserInterface : PSHostRawUserInterface internal class MyRawUserInterface : PSHostRawUserInterface
{ {
/// <summary> /// <summary>
/// Gets or sets the background color of the displayed text. /// Gets or sets the background color of the displayed text.
@ -24,9 +24,9 @@ namespace ps_hello_world
get { return Console.BackgroundColor; } get { return Console.BackgroundColor; }
set { Console.BackgroundColor = value; } set { Console.BackgroundColor = value; }
} }
/// <summary> /// <summary>
/// Gets or sets the size of the host buffer. In this example the /// Gets or sets the size of the host buffer. In this example the
/// buffer size is adapted from the Console buffer size members. /// buffer size is adapted from the Console buffer size members.
/// </summary> /// </summary>
public override Size BufferSize public override Size BufferSize
@ -36,23 +36,23 @@ namespace ps_hello_world
//get { return new Size(Console.BufferWidth, Console.BufferHeight); } //get { return new Size(Console.BufferWidth, Console.BufferHeight); }
//set { Console.SetBufferSize(value.Width, value.Height); } //set { Console.SetBufferSize(value.Width, value.Height); }
} }
/// <summary> /// <summary>
/// Gets or sets the cursor position. In this example this /// Gets or sets the cursor position. In this example this
/// functionality is not needed so the property throws a /// functionality is not needed so the property throws a
/// NotImplementException exception. /// NotImplementException exception.
/// </summary> /// </summary>
public override Coordinates CursorPosition public override Coordinates CursorPosition
{ {
get { throw new NotImplementedException( get { throw new NotImplementedException(
"The method or operation is not implemented."); } "The method or operation is not implemented."); }
set { throw new NotImplementedException( set { throw new NotImplementedException(
"The method or operation is not implemented."); } "The method or operation is not implemented."); }
} }
/// <summary> /// <summary>
/// Gets or sets the size of the displayed cursor. In this example /// Gets or sets the size of the displayed cursor. In this example
/// the cursor size is taken directly from the Console.CursorSize /// the cursor size is taken directly from the Console.CursorSize
/// property. /// property.
/// </summary> /// </summary>
public override int CursorSize public override int CursorSize
@ -62,7 +62,7 @@ namespace ps_hello_world
//get { return Console.CursorSize; } //get { return Console.CursorSize; }
//set { Console.CursorSize = value; } //set { Console.CursorSize = value; }
} }
/// <summary> /// <summary>
/// Gets or sets the foreground color of the displayed text. /// Gets or sets the foreground color of the displayed text.
/// This maps to the corresponding Console.ForgroundColor property. /// This maps to the corresponding Console.ForgroundColor property.
@ -72,91 +72,91 @@ namespace ps_hello_world
get { return Console.ForegroundColor; } get { return Console.ForegroundColor; }
set { Console.ForegroundColor = value; } set { Console.ForegroundColor = value; }
} }
/// <summary> /// <summary>
/// Gets a value indicating whether the user has pressed a key. This maps /// Gets a value indicating whether the user has pressed a key. This maps
/// to the corresponding Console.KeyAvailable property. /// to the corresponding Console.KeyAvailable property.
/// </summary> /// </summary>
public override bool KeyAvailable public override bool KeyAvailable
{ {
get { return false; } get { return false; }
// get { return Console.KeyAvailable; } // get { return Console.KeyAvailable; }
} }
/// <summary> /// <summary>
/// Gets the dimensions of the largest window that could be /// Gets the dimensions of the largest window that could be
/// rendered in the current display, if the buffer was at the least /// rendered in the current display, if the buffer was at the least
/// that large. This example uses the Console.LargestWindowWidth and /// that large. This example uses the Console.LargestWindowWidth and
/// Console.LargestWindowHeight properties to determine the returned /// Console.LargestWindowHeight properties to determine the returned
/// value of this property. /// value of this property.
/// </summary> /// </summary>
public override Size MaxPhysicalWindowSize public override Size MaxPhysicalWindowSize
{ {
// get { return new Size(Console.LargestWindowWidth, Console.LargestWindowHeight); } // get { return new Size(Console.LargestWindowWidth, Console.LargestWindowHeight); }
get { return new Size(1024,768); } get { return new Size(1024,768); }
} }
/// <summary> /// <summary>
/// Gets the dimentions of the largest window size that can be /// Gets the dimentions of the largest window size that can be
/// displayed. This example uses the Console.LargestWindowWidth and /// displayed. This example uses the Console.LargestWindowWidth and
/// console.LargestWindowHeight properties to determine the returned /// console.LargestWindowHeight properties to determine the returned
/// value of this property. /// value of this property.
/// </summary> /// </summary>
public override Size MaxWindowSize public override Size MaxWindowSize
{ {
// get { return new Size(Console.LargestWindowWidth, Console.LargestWindowHeight); } // get { return new Size(Console.LargestWindowWidth, Console.LargestWindowHeight); }
get { return new Size(1024,768); } get { return new Size(1024,768); }
} }
/// <summary> /// <summary>
/// Gets or sets the position of the displayed window. This example /// Gets or sets the position of the displayed window. This example
/// uses the Console window position APIs to determine the returned /// uses the Console window position APIs to determine the returned
/// value of this property. /// value of this property.
/// </summary> /// </summary>
public override Coordinates WindowPosition public override Coordinates WindowPosition
{ {
// get { return new Coordinates(Console.WindowLeft, Console.WindowTop); } // get { return new Coordinates(Console.WindowLeft, Console.WindowTop); }
// set { Console.SetWindowPosition(value.X, value.Y); } // set { Console.SetWindowPosition(value.X, value.Y); }
get { return new Coordinates(0,0); } get { return new Coordinates(0,0); }
set { } set { }
} }
/// <summary> /// <summary>
/// Gets or sets the size of the displayed window. This example /// Gets or sets the size of the displayed window. This example
/// uses the corresponding Console window size APIs to determine the /// uses the corresponding Console window size APIs to determine the
/// returned value of this property. /// returned value of this property.
/// </summary> /// </summary>
public override Size WindowSize public override Size WindowSize
{ {
// get { return new Size(Console.WindowWidth, Console.WindowHeight); } // get { return new Size(Console.WindowWidth, Console.WindowHeight); }
// set { Console.SetWindowSize(value.Width, value.Height); } // set { Console.SetWindowSize(value.Width, value.Height); }
get { return new Size(1024,768); } get { return new Size(1024,768); }
set { } set { }
} }
/// <summary> /// <summary>
/// Gets or sets the title of the displayed window. The example /// Gets or sets the title of the displayed window. The example
/// maps the Console.Title property to the value of this property. /// maps the Console.Title property to the value of this property.
/// </summary> /// </summary>
public override string WindowTitle public override string WindowTitle
{ {
// get { return Console.Title; } // get { return Console.Title; }
// set { Console.Title = value; } // set { Console.Title = value; }
get { return "window title"; } get { return "window title"; }
set { } set { }
} }
/// <summary> /// <summary>
/// This API resets the input buffer. In this example this /// This API resets the input buffer. In this example this
/// functionality is not needed so the method returns nothing. /// functionality is not needed so the method returns nothing.
/// </summary> /// </summary>
public override void FlushInputBuffer() public override void FlushInputBuffer()
{ {
} }
/// <summary> /// <summary>
/// This API returns a rectangular region of the screen buffer. In /// This API returns a rectangular region of the screen buffer. In
/// this example this functionality is not needed so the method throws /// this example this functionality is not needed so the method throws
/// a NotImplementException exception. /// a NotImplementException exception.
/// </summary> /// </summary>
/// <param name="rectangle">Defines the size of the rectangle.</param> /// <param name="rectangle">Defines the size of the rectangle.</param>
@ -164,58 +164,58 @@ namespace ps_hello_world
public override BufferCell[,] GetBufferContents(Rectangle rectangle) public override BufferCell[,] GetBufferContents(Rectangle rectangle)
{ {
throw new NotImplementedException( throw new NotImplementedException(
"The method or operation is not implemented."); "The method or operation is not implemented.");
} }
/// <summary> /// <summary>
/// This API reads a pressed, released, or pressed and released keystroke /// This API reads a pressed, released, or pressed and released keystroke
/// from the keyboard device, blocking processing until a keystroke is /// from the keyboard device, blocking processing until a keystroke is
/// typed that matches the specified keystroke options. In this example /// typed that matches the specified keystroke options. In this example
/// this functionality is not needed so the method throws a /// this functionality is not needed so the method throws a
/// NotImplementException exception. /// NotImplementException exception.
/// </summary> /// </summary>
/// <param name="options">Options, such as IncludeKeyDown, used when /// <param name="options">Options, such as IncludeKeyDown, used when
/// reading the keyboard.</param> /// reading the keyboard.</param>
/// <returns>Throws a NotImplementedException exception.</returns> /// <returns>Throws a NotImplementedException exception.</returns>
public override KeyInfo ReadKey(ReadKeyOptions options) public override KeyInfo ReadKey(ReadKeyOptions options)
{ {
throw new NotImplementedException( throw new NotImplementedException(
"The method or operation is not implemented."); "The method or operation is not implemented.");
} }
/// <summary> /// <summary>
/// This API crops a region of the screen buffer. In this example /// This API crops a region of the screen buffer. In this example
/// this functionality is not needed so the method throws a /// this functionality is not needed so the method throws a
/// NotImplementException exception. /// NotImplementException exception.
/// </summary> /// </summary>
/// <param name="source">The region of the screen to be scrolled.</param> /// <param name="source">The region of the screen to be scrolled.</param>
/// <param name="destination">The region of the screen to receive the /// <param name="destination">The region of the screen to receive the
/// source region contents.</param> /// source region contents.</param>
/// <param name="clip">The region of the screen to include in the operation.</param> /// <param name="clip">The region of the screen to include in the operation.</param>
/// <param name="fill">The character and attributes to be used to fill all cell.</param> /// <param name="fill">The character and attributes to be used to fill all cell.</param>
public override void ScrollBufferContents(Rectangle source, Coordinates destination, Rectangle clip, BufferCell fill) public override void ScrollBufferContents(Rectangle source, Coordinates destination, Rectangle clip, BufferCell fill)
{ {
throw new NotImplementedException( throw new NotImplementedException(
"The method or operation is not implemented."); "The method or operation is not implemented.");
} }
/// <summary> /// <summary>
/// This method copies an array of buffer cells into the screen buffer /// This method copies an array of buffer cells into the screen buffer
/// at a specified location. In this example this functionality is /// at a specified location. In this example this functionality is
/// not needed so the method throws a NotImplementedException exception. /// not needed so the method throws a NotImplementedException exception.
/// </summary> /// </summary>
/// <param name="origin">The parameter is not used.</param> /// <param name="origin">The parameter is not used.</param>
/// <param name="contents">The parameter is not used.</param> /// <param name="contents">The parameter is not used.</param>
public override void SetBufferContents(Coordinates origin, public override void SetBufferContents(Coordinates origin,
BufferCell[,] contents) BufferCell[,] contents)
{ {
throw new NotImplementedException( throw new NotImplementedException(
"The method or operation is not implemented."); "The method or operation is not implemented.");
} }
/// <summary> /// <summary>
/// This method copies a given character, foreground color, and background /// This method copies a given character, foreground color, and background
/// color to a region of the screen buffer. In this example this /// color to a region of the screen buffer. In this example this
/// functionality is not needed so the method throws a /// functionality is not needed so the method throws a
/// NotImplementException exception./// </summary> /// NotImplementException exception./// </summary>
/// <param name="rectangle">Defines the area to be filled. </param> /// <param name="rectangle">Defines the area to be filled. </param>
@ -223,213 +223,213 @@ namespace ps_hello_world
public override void SetBufferContents(Rectangle rectangle, BufferCell fill) public override void SetBufferContents(Rectangle rectangle, BufferCell fill)
{ {
throw new NotImplementedException( throw new NotImplementedException(
"The method or operation is not implemented."); "The method or operation is not implemented.");
} }
} }
internal class MyHostUI : PSHostUserInterface internal class MyHostUI : PSHostUserInterface
{ {
private MyRawUserInterface myRawUi = new MyRawUserInterface(); private MyRawUserInterface myRawUi = new MyRawUserInterface();
public override PSHostRawUserInterface RawUI public override PSHostRawUserInterface RawUI
{ {
get { return this.myRawUi; } get { return this.myRawUi; }
} }
public override Dictionary<string, PSObject> Prompt( public override Dictionary<string, PSObject> Prompt(
string caption, string caption,
string message, string message,
System.Collections.ObjectModel.Collection<FieldDescription> descriptions) System.Collections.ObjectModel.Collection<FieldDescription> descriptions)
{ {
throw new NotImplementedException( throw new NotImplementedException(
"The method or operation is not implemented."); "The method or operation is not implemented.");
} }
public override int PromptForChoice(string caption, string message, System.Collections.ObjectModel.Collection<ChoiceDescription> choices, int defaultChoice) public override int PromptForChoice(string caption, string message, System.Collections.ObjectModel.Collection<ChoiceDescription> choices, int defaultChoice)
{
throw new NotImplementedException("The method or operation is not implemented.");
}
public override PSCredential PromptForCredential(
string caption,
string message,
string userName,
string targetName)
{ {
throw new NotImplementedException("The method or operation is not implemented."); throw new NotImplementedException("The method or operation is not implemented.");
} }
public override PSCredential PromptForCredential( public override PSCredential PromptForCredential(
string caption, string caption,
string message, string message,
string userName, string userName,
string targetName, string targetName)
PSCredentialTypes allowedCredentialTypes,
PSCredentialUIOptions options)
{ {
throw new NotImplementedException("The method or operation is not implemented."); throw new NotImplementedException("The method or operation is not implemented.");
} }
public override PSCredential PromptForCredential(
string caption,
string message,
string userName,
string targetName,
PSCredentialTypes allowedCredentialTypes,
PSCredentialUIOptions options)
{
throw new NotImplementedException("The method or operation is not implemented.");
}
public override string ReadLine() public override string ReadLine()
{ {
return Console.ReadLine(); return Console.ReadLine();
} }
public override System.Security.SecureString ReadLineAsSecureString() public override System.Security.SecureString ReadLineAsSecureString()
{ {
throw new NotImplementedException("The method or operation is not implemented."); throw new NotImplementedException("The method or operation is not implemented.");
} }
public override void Write(string value) public override void Write(string value)
{ {
Console.BackgroundColor = ConsoleColor.Green; Console.BackgroundColor = ConsoleColor.Green;
Console.Write(value); Console.Write(value);
Console.ResetColor(); Console.ResetColor();
} }
public override void Write( public override void Write(
ConsoleColor foregroundColor, ConsoleColor foregroundColor,
ConsoleColor backgroundColor, ConsoleColor backgroundColor,
string value) string value)
{ {
// Colors are ignored. // Colors are ignored.
Console.ForegroundColor = foregroundColor; Console.ForegroundColor = foregroundColor;
Console.BackgroundColor = backgroundColor; Console.BackgroundColor = backgroundColor;
Console.Write(value); Console.Write(value);
Console.ResetColor(); Console.ResetColor();
} }
public override void WriteDebugLine(string message) public override void WriteDebugLine(string message)
{ {
Console.BackgroundColor = ConsoleColor.Gray; Console.BackgroundColor = ConsoleColor.Gray;
Console.Write(String.Format( Console.Write(String.Format(
CultureInfo.CurrentCulture, CultureInfo.CurrentCulture,
"DEBUG: {0}", "DEBUG: {0}",
message)); message));
Console.ResetColor(); Console.ResetColor();
Console.WriteLine(); Console.WriteLine();
} }
public override void WriteErrorLine(string value) public override void WriteErrorLine(string value)
{ {
Console.BackgroundColor = ConsoleColor.Red; Console.BackgroundColor = ConsoleColor.Red;
Console.Write(String.Format( Console.Write(String.Format(
CultureInfo.CurrentCulture, CultureInfo.CurrentCulture,
"ERROR: {0}", "ERROR: {0}",
value)); value));
Console.ResetColor(); Console.ResetColor();
Console.WriteLine(); Console.WriteLine();
} }
public override void WriteLine() public override void WriteLine()
{ {
System.Console.WriteLine(); System.Console.WriteLine();
} }
public override void WriteLine(string value) public override void WriteLine(string value)
{ {
Write(value); Write(value);
Console.WriteLine(); Console.WriteLine();
} }
public override void WriteLine(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value) public override void WriteLine(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value)
{ {
Write(foregroundColor,backgroundColor,value); Write(foregroundColor,backgroundColor,value);
Console.WriteLine(); Console.WriteLine();
} }
public override void WriteProgress(long sourceId, ProgressRecord record) public override void WriteProgress(long sourceId, ProgressRecord record)
{ {
} }
public override void WriteVerboseLine(string message) public override void WriteVerboseLine(string message)
{ {
Console.BackgroundColor = ConsoleColor.DarkGray; Console.BackgroundColor = ConsoleColor.DarkGray;
Console.WriteLine(String.Format(CultureInfo.CurrentCulture, "VERBOSE: {0}", message)); Console.WriteLine(String.Format(CultureInfo.CurrentCulture, "VERBOSE: {0}", message));
Console.ResetColor(); Console.ResetColor();
} }
public override void WriteWarningLine(string message) public override void WriteWarningLine(string message)
{ {
Console.BackgroundColor = ConsoleColor.Yellow; Console.BackgroundColor = ConsoleColor.Yellow;
Console.WriteLine(String.Format(CultureInfo.CurrentCulture, "WARNING: {0}", message)); Console.WriteLine(String.Format(CultureInfo.CurrentCulture, "WARNING: {0}", message));
Console.ResetColor(); Console.ResetColor();
} }
} }
internal class MyHost : PSHost internal class MyHost : PSHost
{ {
private Program program; private Program program;
private CultureInfo originalCultureInfo = new CultureInfo("en-US"); private CultureInfo originalCultureInfo = new CultureInfo("en-US");
private CultureInfo originalUICultureInfo = new CultureInfo("en-US"); private CultureInfo originalUICultureInfo = new CultureInfo("en-US");
private Guid myId = Guid.NewGuid(); private Guid myId = Guid.NewGuid();
public MyHost(Program program)
{
this.program = program;
}
private MyHostUI myHostUI = new MyHostUI(); public MyHost(Program program)
{
this.program = program;
}
public override System.Globalization.CultureInfo CurrentCulture private MyHostUI myHostUI = new MyHostUI();
{
get { return this.originalCultureInfo; }
}
public override System.Globalization.CultureInfo CurrentUICulture public override System.Globalization.CultureInfo CurrentCulture
{ {
get { return this.originalUICultureInfo; } get { return this.originalCultureInfo; }
} }
public override Guid InstanceId public override System.Globalization.CultureInfo CurrentUICulture
{ {
get { return myId; } get { return this.originalUICultureInfo; }
} }
public override string Name public override Guid InstanceId
{ {
get { return "MyHost"; } get { return myId; }
} }
public override PSHostUserInterface UI public override string Name
{ {
get { return myHostUI; } get { return "MyHost"; }
} }
public override Version Version public override PSHostUserInterface UI
{ {
get { return new Version(0,0,0,0); } get { return myHostUI; }
} }
public override void EnterNestedPrompt() public override Version Version
{ {
throw new NotImplementedException("EnterNestedPrompt not implemented"); get { return new Version(0,0,0,0); }
} }
public override void ExitNestedPrompt() public override void EnterNestedPrompt()
{ {
throw new NotImplementedException("ExitNestedPrompt not implemented"); throw new NotImplementedException("EnterNestedPrompt not implemented");
} }
public override void NotifyBeginApplication()
{
Console.WriteLine("MyHost: NotifyBeginApplication");
return;
}
public override void NotifyEndApplication() public override void ExitNestedPrompt()
{ {
return; throw new NotImplementedException("ExitNestedPrompt not implemented");
} }
public override void SetShouldExit(int exitCode) public override void NotifyBeginApplication()
{ {
Console.WriteLine("SetShouldExit: " + exitCode); Console.WriteLine("MyHost: NotifyBeginApplication");
} return;
} }
public override void NotifyEndApplication()
{
return;
}
public override void SetShouldExit(int exitCode)
{
Console.WriteLine("SetShouldExit: " + exitCode);
}
}
class Program class Program
{ {
@ -441,16 +441,16 @@ namespace ps_hello_world
// PH: this debugging requires a change in the core PS stuff that was stashed away // PH: this debugging requires a change in the core PS stuff that was stashed away
// during source cleanup // during source cleanup
/* /*
PowerShellAssemblyLoadContext asmLoadContext = PowerShellAssemblyLoadContextInitializer.AsmLoadContext; PowerShellAssemblyLoadContext asmLoadContext = PowerShellAssemblyLoadContextInitializer.AsmLoadContext;
IEnumerable<Assembly> assemblies = asmLoadContext.GetAssemblies("System.Management.Automation"); IEnumerable<Assembly> assemblies = asmLoadContext.GetAssemblies("System.Management.Automation");
foreach (Assembly a in assemblies) foreach (Assembly a in assemblies)
{ {
Console.WriteLine("a: " + a); Console.WriteLine("a: " + a);
}*/ }*/
} }
static void test1(string[] args) static void test1(string[] args)
{ {
InitialSessionState iss = InitialSessionState.CreateDefault2(); InitialSessionState iss = InitialSessionState.CreateDefault2();
Runspace rs = RunspaceFactory.CreateRunspace(iss); Runspace rs = RunspaceFactory.CreateRunspace(iss);
rs.Open(); rs.Open();
@ -464,40 +464,39 @@ namespace ps_hello_world
{ {
Console.Write(str); Console.Write(str);
} }
} }
static void test2(string[] args) static void test2(string[] args)
{ {
MyHost myHost = new MyHost(new Program()); MyHost myHost = new MyHost(new Program());
InitialSessionState iss = InitialSessionState.CreateDefault2(); InitialSessionState iss = InitialSessionState.CreateDefault2();
using (Runspace rs = RunspaceFactory.CreateRunspace(myHost,iss)) using (Runspace rs = RunspaceFactory.CreateRunspace(myHost,iss))
{ {
rs.Open(); rs.Open();
using (PowerShell ps = PowerShell.Create()) using (PowerShell ps = PowerShell.Create())
{ {
ps.Runspace = rs; ps.Runspace = rs;
foreach (var arg in args)
{
Console.WriteLine("script: " + arg);
ps.AddScript(arg);
}
ps.AddCommand("out-default");
ps.Commands.Commands[0].MergeMyResults(PipelineResultTypes.Error,PipelineResultTypes.Output);
ps.Invoke();
}
}
}
foreach (var arg in args)
{
Console.WriteLine("script: " + arg);
ps.AddScript(arg);
}
ps.AddCommand("out-default");
ps.Commands.Commands[0].MergeMyResults(PipelineResultTypes.Error,PipelineResultTypes.Output);
ps.Invoke();
}
}
}
static void Main(string[] args) static void Main(string[] args)
{ {
init(); init();
//test1(args); //test1(args);
test2(args); test2(args);
} }
} }
} }