예제 #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PromptNest" /> class.
        /// </summary>
        /// <param name="powerShellContext">
        /// The <see cref="PowerShellContext" /> to track prompt status for.
        /// </param>
        /// <param name="initialPowerShell">
        /// The <see cref="PowerShell" /> instance for the first frame.
        /// </param>
        /// <param name="consoleReader">
        /// The input handler.
        /// </param>
        /// <param name="versionSpecificOperations">
        /// The <see cref="IVersionSpecificOperations" /> for the calling
        /// <see cref="PowerShellContext" /> instance.
        /// </param>
        /// <remarks>
        /// This constructor should only be called when <see cref="PowerShellContext.CurrentRunspace" />
        /// is set to the initial runspace.
        /// </remarks>
        internal PromptNest(
            PowerShellContextService powerShellContext,
            PowerShell initialPowerShell,
            IHostInput consoleReader,
            IVersionSpecificOperations versionSpecificOperations)
        {
            _versionSpecificOperations = versionSpecificOperations;
            _consoleReader             = consoleReader;
            _powerShellContext         = powerShellContext;
            _frameStack = new ConcurrentStack <PromptNestFrame>();
            _frameStack.Push(
                new PromptNestFrame(
                    initialPowerShell,
                    NewHandleQueue()));

            var readLineShell = PowerShell.Create();

            readLineShell.Runspace = powerShellContext.CurrentRunspace.Runspace;
            _readLineFrame         = new PromptNestFrame(
                readLineShell,
                new AsyncQueue <RunspaceHandle>());

            ReleaseRunspaceHandleImpl(isReadLine: true);
        }
        private void Initialize(HostDetails hostDetails, Runspace initialRunspace)
        {
            Validate.IsNotNull("initialRunspace", initialRunspace);

            this.SessionState = PowerShellContextState.NotStarted;

            this.initialRunspace = initialRunspace;
            this.currentRunspace = initialRunspace;

            this.currentRunspace.Debugger.BreakpointUpdated += OnBreakpointUpdated;
            this.currentRunspace.Debugger.DebuggerStop      += OnDebuggerStop;

            this.powerShell          = PowerShell.Create();
            this.powerShell.Runspace = this.currentRunspace;

            // TODO: Should this be configurable?
            this.SetExecutionPolicy(ExecutionPolicy.RemoteSigned);

            // Get the PowerShell runtime version
            this.PowerShellVersion = GetPowerShellVersion();

            // Write out the PowerShell version for tracking purposes
            Logger.Write(
                LogLevel.Normal,
                string.Format(
                    "PowerShell runtime version: {0}",
                    this.PowerShellVersion));

            if (PowerShellVersion >= new Version(5, 0))
            {
                this.versionSpecificOperations = new PowerShell5Operations();
            }
            else if (PowerShellVersion.Major == 4)
            {
                this.versionSpecificOperations = new PowerShell4Operations();
            }
            else if (PowerShellVersion.Major == 3)
            {
                this.versionSpecificOperations = new PowerShell3Operations();
            }
            else
            {
                throw new NotSupportedException(
                          "This computer has an unsupported version of PowerShell installed: " +
                          PowerShellVersion.ToString());
            }

            // Configure the runspace's debugger
            this.versionSpecificOperations.ConfigureDebugger(
                this.currentRunspace);

            // Set the $profile variable in the runspace
            this.profilePaths =
                this.SetProfileVariableInCurrentRunspace(
                    hostDetails);

            // Now that initialization is complete we can watch for InvocationStateChanged
            this.powerShell.InvocationStateChanged += powerShell_InvocationStateChanged;

            this.SessionState = PowerShellContextState.Ready;

            // Now that the runspace is ready, enqueue it for first use
            RunspaceHandle runspaceHandle = new RunspaceHandle(this.currentRunspace, this);

            this.runspaceWaitQueue.EnqueueAsync(runspaceHandle).Wait();
        }