Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions src/Microsoft.PowerShell.Linux.Host/PSL_profile.ps1
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
(get-psprovider 'FileSystem').Home = $env:HOME

function prompt
{
"PSL " + $(get-location) + "> "
function prompt {
"PSL " + $(get-location) + "> "
}

& {
for ($i = 0; $i -lt 26; $i++)
{
$funcname = ([System.Char]($i+65)) + ':'
$str = "function global:$funcname { set-location $funcname } "
invoke-expression $str
}
}
@"

Welcome to Project Magrathea!

This is the Core PowerShell On Linux /OS X console
==================================================
- Type 'get-help' for help
- Type 'exit' to exit

"@
48 changes: 24 additions & 24 deletions src/Microsoft.PowerShell.Linux.Host/host.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ namespace Microsoft.PowerShell.Linux.Host
using System.Management.Automation.Runspaces;

/// <summary>
/// This is a sample implementation of the PSHost abstract class for
/// console applications. Not all members are implemented. Those that
/// are not implemented throw a NotImplementedException exception or
/// This is a sample implementation of the PSHost abstract class for
/// console applications. Not all members are implemented. Those that
/// are not implemented throw a NotImplementedException exception or
/// return nothing.
/// </summary>
internal class MyHost : PSHost, IHostSupportsInteractiveSession
Expand Down Expand Up @@ -53,8 +53,8 @@ public MyHost(Listener Listener)


/// <summary>
/// Gets the culture information to use. This implementation
/// returns a snapshot of the culture information of the thread
/// Gets the culture information to use. This implementation
/// returns a snapshot of the culture information of the thread
/// that created this object.
/// </summary>
public override CultureInfo CurrentCulture
Expand All @@ -63,8 +63,8 @@ public override CultureInfo CurrentCulture
}

/// <summary>
/// Gets the UI culture information to use. This implementation
/// returns a snapshot of the UI culture information of the thread
/// Gets the UI culture information to use. This implementation
/// returns a snapshot of the UI culture information of the thread
/// that created this object.
/// </summary>
public override CultureInfo CurrentUICulture
Expand All @@ -73,7 +73,7 @@ public override CultureInfo CurrentUICulture
}

/// <summary>
/// Gets an identifier for this host. This implementation always
/// Gets an identifier for this host. This implementation always
/// returns the GUID allocated at instantiation time.
/// </summary>
public override Guid InstanceId
Expand All @@ -82,7 +82,7 @@ public override Guid InstanceId
}

/// <summary>
/// Gets a string that contains the name of this host implementation.
/// Gets a string that contains the name of this host implementation.
/// Keep in mind that this string may be used by script writers to
/// identify when your host is being used.
/// </summary>
Expand All @@ -102,7 +102,7 @@ public override PSHostUserInterface UI
}

/// <summary>
/// Gets the version object for this application. Typically this
/// Gets the version object for this application. Typically this
/// should match the version resource in the application.
/// </summary>
public override Version Version
Expand All @@ -113,7 +113,7 @@ public override Version Version
#region IHostSupportsInteractiveSession Properties

/// <summary>
/// Gets a value indicating whether a request
/// Gets a value indicating whether a request
/// to open a PSSession has been made.
/// </summary>
public bool IsRunspacePushed
Expand All @@ -132,9 +132,9 @@ public Runspace Runspace
#endregion IHostSupportsInteractiveSession Properties

/// <summary>
/// This API Instructs the host to interrupt the currently running
/// pipeline and start a new nested input loop. In this example this
/// functionality is not needed so the method throws a
/// This API Instructs the host to interrupt the currently running
/// pipeline and start a new nested input loop. In this example this
/// functionality is not needed so the method throws a
/// NotImplementedException exception.
/// </summary>
public override void EnterNestedPrompt()
Expand All @@ -144,8 +144,8 @@ public override void EnterNestedPrompt()
}

/// <summary>
/// This API instructs the host to exit the currently running input loop.
/// In this example this functionality is not needed so the method
/// This API instructs the host to exit the currently running input loop.
/// In this example this functionality is not needed so the method
/// throws a NotImplementedException exception.
/// </summary>
public override void ExitNestedPrompt()
Expand All @@ -155,34 +155,34 @@ public override void ExitNestedPrompt()
}

/// <summary>
/// This API is called before an external application process is
/// started. Typically it is used to save state so that the parent
/// can restore state that has been modified by a child process (after
/// the child exits). In this example this functionality is not
/// This API is called before an external application process is
/// started. Typically it is used to save state so that the parent
/// can restore state that has been modified by a child process (after
/// the child exits). In this example this functionality is not
/// needed so the method returns nothing.
/// </summary>
public override void NotifyBeginApplication()
{
return;
return;
}

/// <summary>
/// This API is called after an external application process finishes.
/// Typically it is used to restore state that a child process has
/// altered. In this example, this functionality is not needed so
/// altered. In this example, this functionality is not needed so
/// the method returns nothing.
/// </summary>
public override void NotifyEndApplication()
{
return;
return;
}

/// <summary>
/// Indicate to the host application that exit has
/// been requested. Pass the exit code that the host
/// application should use when exiting the process.
/// </summary>
/// <param name="exitCode">The exit code that the
/// <param name="exitCode">The exit code that the
/// host application should use.</param>
public override void SetShouldExit(int exitCode)
{
Expand Down
79 changes: 47 additions & 32 deletions src/Microsoft.PowerShell.Linux.Host/main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,6 @@ public static void Main(string[] args)
// only run if there was no script file passed in
if (initialScript == null)
{
// Display the welcome message.
Console.WriteLine();
Console.WriteLine("PowerShell for Linux interactive console");
Console.WriteLine("========================================");
Console.WriteLine();
Console.WriteLine("Current status:");
Console.WriteLine("- Type 'exit' to exit");
Console.WriteLine("- Utility and Management cmdlet modules are loadable");
Console.WriteLine();

listener.Run();
}
}
Expand Down Expand Up @@ -133,7 +123,7 @@ internal class Listener
private object instanceLock = new object();

/// <summary>
/// Gets or sets a value indicating whether the host application
/// Gets or sets a value indicating whether the host application
/// should exit.
/// </summary>
public bool ShouldExit
Expand All @@ -143,7 +133,7 @@ public bool ShouldExit
}

/// <summary>
/// Gets or sets a value indicating whether the host application
/// Gets or sets a value indicating whether the host application
/// should exit.
/// </summary>
public int ExitCode
Expand All @@ -154,8 +144,8 @@ public int ExitCode

public Listener(string initialScript)
{
// Create the host and runspace instances for this interpreter.
// Note that this application does not support console files so
// Create the host and runspace instances for this interpreter.
// Note that this application does not support console files so
// only the default snap-ins will be available.
this.myHost = new MyHost(this);
InitialSessionState iss = InitialSessionState.CreateDefault2();
Expand All @@ -176,8 +166,7 @@ public Listener(string initialScript)
PSCommand[] profileCommands = HostUtilities.GetProfileCommands("PSL");
foreach (PSCommand command in profileCommands)
{
this.currentPowerShell.Commands = command;
this.currentPowerShell.Invoke();
RunCommand(command);
}
}
finally
Expand Down Expand Up @@ -226,13 +215,39 @@ public string Prompt(Runspace rs)
}

/// <summary>
/// A helper class that builds and executes a pipeline that writes
/// to the default output path. Any exceptions that are thrown are
/// just passed to the caller. Since all output goes to the default
/// Runs individual commands
/// </summary>
/// <param name="command">command to run</param>
internal void RunCommand(PSCommand command)
{
if (command == null)
{
return;
}

command.AddCommand("out-default");
command.Commands[0].MergeMyResults(PipelineResultTypes.Error, PipelineResultTypes.Output);

this.currentPowerShell.Commands = command;

try
{
this.currentPowerShell.Invoke();
}
catch (RuntimeException e)
{
this.ReportException(e);
}
}

/// <summary>
/// A helper class that builds and executes a pipeline that writes
/// to the default output path. Any exceptions that are thrown are
/// just passed to the caller. Since all output goes to the default
/// outter, this method does not return anything.
/// </summary>
/// <param name="cmd">The script to run.</param>
/// <param name="input">Any input arguments to pass to the script.
/// <param name="input">Any input arguments to pass to the script.
/// If null then nothing is passed in.</param>
private void executeHelper(string cmd, object input)
{
Expand All @@ -250,17 +265,17 @@ private void executeHelper(string cmd, object input)
this.currentPowerShell = PowerShell.Create();
}

// Add a script and command to the pipeline and then run the pipeline. Place
// the results in the currentPowerShell variable so that the pipeline can be
// Add a script and command to the pipeline and then run the pipeline. Place
// the results in the currentPowerShell variable so that the pipeline can be
// stopped.
try
{
this.currentPowerShell.Runspace = this.myRunSpace;

this.currentPowerShell.AddScript(cmd);

// Add the default outputter to the end of the pipe and then call the
// MergeMyResults method to merge the output and error streams from the
// Add the default outputter to the end of the pipe and then call the
// MergeMyResults method to merge the output and error streams from the
// pipeline. This will result in the output being written using the PSHost
// and PSHostUserInterface classes instead of returning objects to the host
// application.
Expand All @@ -282,8 +297,8 @@ private void executeHelper(string cmd, object input)
}
finally
{
// Dispose the PowerShell object and set currentPowerShell to null.
// It is locked because currentPowerShell may be accessed by the
// Dispose the PowerShell object and set currentPowerShell to null.
// It is locked because currentPowerShell may be accessed by the
// ctrl-C handler.
lock (this.instanceLock)
{
Expand All @@ -294,11 +309,11 @@ private void executeHelper(string cmd, object input)
}

/// <summary>
/// To display an exception using the display formatter,
/// To display an exception using the display formatter,
/// run a second pipeline passing in the error record.
/// The runtime will bind this to the $input variable,
/// which is why $input is being piped to the Out-String
/// cmdlet. The WriteErrorLine method is called to make sure
/// cmdlet. The WriteErrorLine method is called to make sure
/// the error gets displayed in the correct error color.
/// </summary>
/// <param name="e">The exception to display.</param>
Expand Down Expand Up @@ -347,7 +362,7 @@ private void ReportException(Exception e)
}
finally
{
// Dispose of the pipeline and set it to null, locking it because
// Dispose of the pipeline and set it to null, locking it because
// currentPowerShell may be accessed by the ctrl-C handler.
lock (this.instanceLock)
{
Expand All @@ -360,7 +375,7 @@ private void ReportException(Exception e)

/// <summary>
/// Basic script execution routine. Any runtime exceptions are
/// caught and passed back to the Windows PowerShell engine to
/// caught and passed back to the Windows PowerShell engine to
/// display.
/// </summary>
/// <param name="cmd">Script to run.</param>
Expand All @@ -382,9 +397,9 @@ private void Execute(string cmd)
/// pipeline Stop() method to stop execution. If any exceptions occur
/// they are printed to the console but otherwise ignored.
/// </summary>
/// <param name="sender">See sender property documentation of
/// <param name="sender">See sender property documentation of
/// ConsoleCancelEventHandler.</param>
/// <param name="e">See e property documentation of
/// <param name="e">See e property documentation of
/// ConsoleCancelEventHandler.</param>
private void HandleControlC(object sender, ConsoleCancelEventArgs e)
{
Expand Down
Loading