private void _DoRunspaceInitialization()

in DbgShell/ColorConsoleHost.cs [1265:1431]


        private void _DoRunspaceInitialization( bool skipProfiles,
                                                string dumpFile,
                                                string initialCommand,
                                                string file,
                                                Collection< CommandParameter > fileArgs,
                                                bool oneShot,
                                                PowerShell shell )
        {
            DbgEngDebugger.StartPreloadDbgEng();
            DbgProvider.HostSupportsColor = true;
            m_runspace.SessionStateProxy.PSVariable.Set( DbgProvider.GetHostSupportsColorPSVar() );

            // Run the built-in scripts
            RunspaceConfigurationEntryCollection< ScriptConfigurationEntry > scripts = null;
            if (m_runspaceConfig != null)
                scripts = m_runspaceConfig.InitializationScripts;

            if( (scripts == null) || (scripts.Count == 0) )
            {
                LogManager.Trace("There are no built-in scripts to run");
            }
            else
            {
                foreach( ScriptConfigurationEntry s in scripts )
                {
                    if( oneShot && (s.Name.StartsWith( c_SkipForOneShotPrefix, StringComparison.OrdinalIgnoreCase )) )
                    {
                        LogManager.Trace( "Skipping script because we're in \"one shot\" mode: {0}", s.Name );
                        continue;
                    }
                    LogManager.Trace( "Running script: '{0}'", s.Name );

                    // [danthom] TODO BUGBUG
                    // spec claims that Ctrl-C is not supposed to stop these.

                    // TODO: I think I shouldn't even need this... top-level handler can handle ctrl-C, not do anything
                    //using( new CtrlCInterceptor( ( x ) => { return true; } ) )
                    try
                    {
                        _ExecScriptNoExceptionHandling( shell,
                                                        s.Definition,
                                                        addToHistory: false );
                    }
                    catch( RuntimeException rte )
                    {
                        _ReportException( rte, shell );
                        // TODO BUGBUG: the real PowerShell will set the exit code and
                        // exit the input loop if init fails. (throwing from here)
                    }
                } // end foreach( ScriptConfigurationEntry )
            } // end( we have some init scripts )

            // If -iss has been specified, then there won't be a runspace
            // configuration to get the shell ID from, so we'll use the default...
            string shellId = null;
            if (m_runspaceConfig != null)
                shellId = m_runspaceConfig.ShellId;
            else
                shellId = "Microsoft.PowerShell"; // TODO: what will happen for custom shells built using Make-Shell.exe

            string allUsersProfile = HostUtilities.GetFullProfileFileName( null, false );
            string allUsersHostSpecificProfile = HostUtilities.GetFullProfileFileName( shellId, false );
            string currentUserProfile = HostUtilities.GetFullProfileFileName( null, true );
            string currentUserHostSpecificProfile = HostUtilities.GetFullProfileFileName( shellId, true );
            string dbgShellProfile = HostUtilities.GetFullDbgShellProfileFileName();

            // $PROFILE has to be set from the host
            // Should be "per-user,host-specific profile.ps1"
            // This should be set even if -noprofile is specified
            Runspace.SessionStateProxy.SetVariable( "PROFILE",
                                                    HostUtilities.GetDollarProfile(
                                                        allUsersProfile,
                                                        allUsersHostSpecificProfile,
                                                        currentUserProfile,
                                                        currentUserHostSpecificProfile,
                                                        dbgShellProfile) );
            if( !skipProfiles )
            {
                // Run the profiles.
                // Profiles are run in the following order:
                // 1. host independent profile meant for all users
                // 2. host specific profile meant for all users
                // 3. host independent profile of the current user
                // 4. host specific profile  of the current user

                // TODO: Shouldn't these be using 'shell' instead of creating their own?
                // So that $host.EnterNestedPrompt() will work?
                _RunProfileFile( allUsersProfile );
                _RunProfileFile( allUsersHostSpecificProfile );
                _RunProfileFile( currentUserProfile );
                _RunProfileFile( currentUserHostSpecificProfile );
                _RunProfileFile( dbgShellProfile );
            }
            else
            {
                LogManager.Trace( "-noprofile option specified: skipping profiles." );
            }

            // TODO: this will be nice to have for automated debug monitor-type thingies
            // If a file was specified as the argument to run, then run it...
            if( !String.IsNullOrEmpty( file ) )
            {
                LogManager.Trace( "Running file: {0}", file );
                shell.Commands.Clear();
                shell.Streams.ClearStreams();
                shell.AddCommand( file );
                if( fileArgs != null )
                {
                    foreach( CommandParameter p in fileArgs )
                    {
                        shell.AddParameter( p.Name, p.Value );
                    }
                }

                try
                {
                    _ExecScriptNoExceptionHandling( shell,
                                                    null,
                                                    addToHistory: false );
                }
                catch( RuntimeException rte )
                {
                    _ReportException( rte, shell );
                    // This might not get used if -NoExit was passed.
                    ExitCode = rte.HResult;
                }
            }
            else
            {
                if( !String.IsNullOrEmpty( dumpFile ) )
                {
                    LogManager.Trace( "Mounting dump file specified on command line: {0}", dumpFile );

                    try
                    {
                        _ExecScriptNoExceptionHandling( shell,
                                                        Util.Sprintf( "Mount-DbgDumpFile '{0}'", dumpFile ),
                                                        addToHistory: false );
                    }
                    catch( RuntimeException rte )
                    {
                        _ReportException( rte, shell );
                     // // This might not get used if -NoExit was passed.
                     // ExitCode = rte.HResult;
                    }
                }

                if( !String.IsNullOrEmpty( initialCommand ) )
                {
                    // Run the command passed on the command line
                    LogManager.Trace( "Running initial command: {0}", initialCommand );

                    try
                    {
                        _ExecScriptNoExceptionHandling( shell,
                                                        initialCommand,
                                                        addToHistory: false );
                    }
                    catch( RuntimeException rte )
                    {
                        _ReportException( rte, shell );
                        // This might not get used if -NoExit was passed.
                        ExitCode = rte.HResult;
                    }
                }
            }
        } // end _DoRunspaceInitialization()