Example #1
0
        public ScriptDebugger(bool overrideExecutionPolicy, DTE2 dte2)
        {
            HostUi = new HostUi(this);

            InitialSessionState iss = InitialSessionState.CreateDefault();
            iss.ApartmentState = ApartmentState.STA;
            iss.ThreadOptions = PSThreadOptions.ReuseThread;


            _runspace = RunspaceFactory.CreateRunspace(this, iss);
            _runspace.Open();

            _runspaceRef = new RunspaceRef(_runspace);
            //TODO: I think this is a v4 thing. Probably need to look into it.
            //_runspaceRef.Runspace.Debugger.SetDebugMode(DebugModes.LocalScript | DebugModes.RemoteScript);
            
            //Provide access to the DTE via PowerShell. 
            //This also allows PoshTools to support StudioShell.
            _runspace.SessionStateProxy.PSVariable.Set("dte", dte2);
            ImportPoshToolsModule();
            LoadProfile();

            if (overrideExecutionPolicy)
            {
                SetupExecutionPolicy();
            }

            SetRunspace(Runspace);
        }
        public ScriptDebugger(bool overrideExecutionPolicy, DTE2 dte2)
        {
            HostUi = new HostUi(this);

            InitialSessionState iss = InitialSessionState.CreateDefault();

            iss.ApartmentState = ApartmentState.STA;
            iss.ThreadOptions  = PSThreadOptions.ReuseThread;


            _runspace = RunspaceFactory.CreateRunspace(this, iss);
            _runspace.Open();

            _runspaceRef = new RunspaceRef(_runspace);
            //TODO: I think this is a v4 thing. Probably need to look into it.
            //_runspaceRef.Runspace.Debugger.SetDebugMode(DebugModes.LocalScript | DebugModes.RemoteScript);

            //Provide access to the DTE via PowerShell.
            //This also allows PoshTools to support StudioShell.
            _runspace.SessionStateProxy.PSVariable.Set("dte", dte2);
            ImportPoshToolsModule();
            LoadProfile();

            if (overrideExecutionPolicy)
            {
                SetupExecutionPolicy();
            }

            SetRunspace(Runspace);
        }
Example #3
0
        /// <summary>
        /// Execute the current program node.
        /// </summary>
        /// <remarks>
        /// The node will either be a script file or script content; depending on the node
        /// passed to this function.
        /// </remarks>
        /// <param name="node"></param>
        public void Execute(ScriptProgramNode node)
        {
            CurrentExecutingNode = node;

            if (node.IsAttachedProgram)
            {
                string result = string.Empty;
                if (!node.IsRemoteProgram)
                {
                    result = DebuggingService.AttachToRunspace(node.Process.ProcessId);
                }
                else
                {
                    result = DebuggingService.AttachToRemoteRunspace(node.Process.ProcessId, node.Process.HostName);
                }

                if (!string.IsNullOrEmpty(result))
                {
                    // if either of the attaches returns an error, let the user know
                    MessageBox.Show(result, Resources.AttachErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);

                    // see what state we are in post error, if not in a local state, we need to try and get there
                    DebugScenario postCleanupScenario = DebuggingService.GetDebugScenario();

                    // try as hard as we can to detach/cleanup the mess for the length of CleanupRetryTimeout
                    TimeSpan  retryTimeSpan = TimeSpan.FromMilliseconds(DebugEngineConstants.CleanupRetryTimeout);
                    Stopwatch timeElapsed   = Stopwatch.StartNew();
                    while (timeElapsed.Elapsed < retryTimeSpan && postCleanupScenario != DebugScenario.Local)
                    {
                        postCleanupScenario = DebuggingService.CleanupAttach();
                    }

                    // if our efforts to cleanup the mess were unsuccessful, inform the user
                    if (postCleanupScenario != DebugScenario.Local)
                    {
                        MessageBox.Show(Resources.CleanupErrorMessage, Resources.DetachErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }

                    RefreshPrompt();
                    DebuggerFinished();
                }
            }
            else
            {
                string commandLine = node.FileName;

                if (node.IsFile)
                {
                    commandLine = String.Format(DebugEngineConstants.ExecutionCommandFormat, node.FileName, node.Arguments);
                    HostUi.VsOutputString(string.Format("{0}{1}{2}", GetPrompt(), node.FileName, Environment.NewLine));
                }
                Execute(commandLine);
            }
        }
Example #4
0
 /// <summary>
 /// PS execution terminating excpetion handler
 /// </summary>
 /// <param name="ex"></param>
 public void TerminateException(PowerShellRunTerminatingException ex)
 {
     if (TerminatingException != null)
     {
         // from editor debug run
         TerminatingException(this, new EventArgs <PowerShellRunTerminatingException>(ex));
     }
     else
     {
         // from REPL execution
         HostUi.VsOutputString(ex.Message);
     }
 }
Example #5
0
        internal ScriptDebugger(bool overrideExecutionPolicy, IPowerShellDebuggingService debuggingServiceTestHook)
        {
            OverrideExecutionPolicy = overrideExecutionPolicy;
            _debuggingServiceTest   = debuggingServiceTestHook;
            DebuggingService.SetRunspace(overrideExecutionPolicy);

            //TODO: remove once user prompt work is finished for debugging
            _runspace = RunspaceFactory.CreateRunspace();
            _runspace.Open();
            HostUi = new HostUi();

            BreakpointManager = new BreakpointManager();

            NativeMethods.SetForegroundWindow();
        }
Example #6
0
        /// <summary>
        /// Execute the specified command line.
        /// </summary>
        /// <param name="commandLine">Command line to execute.</param>
        public bool Execute(string commandLine)
        {
            Log.Info("Execute");

            try
            {
                return(ExecuteInternal(commandLine));
            }
            catch (Exception ex)
            {
                Log.Error("Failed to execute script", ex);
                HostUi.VsOutputString(ex.Message);
                return(false);
            }
            finally
            {
                DebuggerFinished();
            }
        }
Example #7
0
        internal void OpenFileInVS(string fullName)
        {
            var dte2 = (DTE80.DTE2)Package.GetGlobalService(typeof(DTE.DTE));

            if (dte2 != null)
            {
                try
                {
                    if (!dte2.ItemOperations.IsFileOpen(fullName))
                    {
                        dte2.ItemOperations.OpenFile(fullName);
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(DebugScenarioUtilities.ScenarioToFileOpenErrorMsg(DebuggingService.GetDebugScenario()), ex);
                    HostUi.VsOutputString(ex.Message);
                }
            }
        }