You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@daffodil.apache.org by sh...@apache.org on 2023/05/09 16:48:32 UTC

[daffodil-vscode] branch main updated: Keep terminal open after debug ends

This is an automated email from the ASF dual-hosted git repository.

shanedell pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/daffodil-vscode.git


The following commit(s) were added to refs/heads/main by this push:
     new 1df7e8a  Keep terminal open after debug ends
1df7e8a is described below

commit 1df7e8a3bea1c9b6ea3a0391467b1c4b5f8678b9
Author: Shane Dell <sh...@gmail.com>
AuthorDate: Fri May 5 12:19:36 2023 -0400

    Keep terminal open after debug ends
    
    - Grab active terminal if its a daffodil-debugger named terminal, otherwise create a new one.
    - Send command to run debugger to terminal.
    - This allows for the terminal to stay open after the debugger exists.
    - Allow for option to always create new terminal. This is mostly to support testing purposes.
    
    Closes #614
---
 src/daffodilDebuggerUtils.ts             | 17 +++++------
 src/tests/suite/daffodilDebugger.test.ts |  8 +++--
 src/utils.ts                             | 52 ++++++++++++++++++++++++++------
 3 files changed, 55 insertions(+), 22 deletions(-)

diff --git a/src/daffodilDebuggerUtils.ts b/src/daffodilDebuggerUtils.ts
index 9eafff3..9e7bf68 100644
--- a/src/daffodilDebuggerUtils.ts
+++ b/src/daffodilDebuggerUtils.ts
@@ -49,20 +49,17 @@ export async function buildDebugger(baseFolder: string, filePath: string) {
 export const stopDebugger = (id: number | undefined = undefined) =>
   child_process.exec(osCheck(`taskkill /F /PID ${id}`, `kill -9 ${id}`))
 
-export const shellPath = (scriptName: string) =>
-  osCheck(scriptName, '/bin/bash')
-
-export const shellArgs = (scriptName: string, port: number) =>
-  osCheck(
-    ['--listenPort', `${port}`],
-    ['--login', '-c', `${scriptName} --listenPort ${port}`]
-  )
+export const shellArgs = (scriptName: string, port: number) => [
+  '--listenPort',
+  `${port}`,
+]
 
 export async function runDebugger(
   rootPath: string,
   daffodilDebugClasspath: string,
   filePath: string,
-  serverPort: number
+  serverPort: number,
+  createTerminal: boolean = false
 ): Promise<vscode.Terminal> {
   const dfdlVersion = daffodilVersion(filePath)
   const artifact = daffodilArtifact(dfdlVersion)
@@ -80,7 +77,7 @@ export async function runDebugger(
   return await runScript(
     scriptPath,
     artifact.scriptName,
-    shellPath(artifact.scriptName),
+    createTerminal,
     shellArgs(artifact.scriptName, serverPort),
     {
       DAFFODIL_DEBUG_CLASSPATH: daffodilDebugClasspath,
diff --git a/src/tests/suite/daffodilDebugger.test.ts b/src/tests/suite/daffodilDebugger.test.ts
index f935759..b9cd8bf 100644
--- a/src/tests/suite/daffodilDebugger.test.ts
+++ b/src/tests/suite/daffodilDebugger.test.ts
@@ -50,8 +50,12 @@ suite('Daffodil Debugger', () => {
 
   before(async () => {
     await unzipFile(SCALA_PATH, PROJECT_ROOT)
-    debuggers.push(await runDebugger(PROJECT_ROOT, '', PACKAGE_PATH, 4711))
-    debuggers.push(await runDebugger(PROJECT_ROOT, '', PACKAGE_PATH, 4712))
+    debuggers.push(
+      await runDebugger(PROJECT_ROOT, '', PACKAGE_PATH, 4711, true)
+    )
+    debuggers.push(
+      await runDebugger(PROJECT_ROOT, '', PACKAGE_PATH, 4712, true)
+    )
   })
 
   after(async () => {
diff --git a/src/utils.ts b/src/utils.ts
index 50b9ee7..df9744b 100644
--- a/src/utils.ts
+++ b/src/utils.ts
@@ -26,6 +26,8 @@ import { TDMLConfig } from './adapter/activateDaffodilDebug'
 const defaultConf = vscode.workspace.getConfiguration()
 let currentConfig: vscode.DebugConfiguration
 
+const terminalName = 'daffodil-debugger'
+
 export const regexp = {
   comma: new RegExp(',', 'g'),
   slash: new RegExp('/', 'g'),
@@ -169,6 +171,7 @@ export async function unzipFile(zipFilePath: string, extractPath: string) {
 export async function displayTerminalExitStatus(terminal: vscode.Terminal) {
   vscode.window.onDidCloseTerminal((t) => {
     if (t.name === terminal.name && t.processId === terminal.processId) {
+      t.show()
       vscode.window.showInformationMessage(
         `Terminal exited with status code: ${t.exitStatus?.code}`
       )
@@ -196,10 +199,37 @@ export async function killProcess(id: number | undefined) {
 
 export const delay = (ms: number) => new Promise((res) => setTimeout(res, ms))
 
+// Grab active terminal if available and it can run a new command, else create new one
+export const getTerminal = (
+  hideTerminal: boolean,
+  env:
+    | {
+        [key: string]: string | null | undefined
+      }
+    | undefined,
+  createTerminal: boolean
+) => {
+  if (!createTerminal) {
+    if (vscode.window.activeTerminal) {
+      const activeTerminal = vscode.window.activeTerminal
+
+      // check allows for shell name or full path to shell in terminal name
+      if (activeTerminal.name.includes(terminalName)) return activeTerminal
+    }
+  }
+
+  // If no good active terminal available create new one
+  return vscode.window.createTerminal({
+    name: terminalName,
+    hideFromUser: hideTerminal,
+    env: env,
+  })
+}
+
 export async function runScript(
   scriptPath: string,
   scriptName: string,
-  shellPath: string | null = null,
+  createTerminal: boolean,
   shellArgs: string[] = [],
   env:
     | {
@@ -225,15 +255,17 @@ export async function runScript(
     }
   }
 
-  // Start server in terminal based on scriptName
-  const terminal = vscode.window.createTerminal({
-    name: scriptName,
-    cwd: path.join(scriptPath, 'bin'),
-    hideFromUser: hideTerminal,
-    shellPath: shellPath !== null ? shellPath : scriptName,
-    shellArgs: shellArgs,
-    env: env,
-  })
+  const terminal = getTerminal(hideTerminal, env, createTerminal)
+
+  // Create debugger run command
+  const fullPathToScript = path
+    .join(scriptPath, 'bin', scriptName)
+    // fix pathing as emtpy space needs a \ before it to not cause errors
+    .replace(' ', '\\ ')
+  const debuggerRunCommand = `${fullPathToScript} ${shellArgs.join(' ')}`
+
+  // Send debugger run command to terminal, when exists terminal will stay open
+  terminal.sendText(debuggerRunCommand)
 
   if (!hideTerminal) {
     terminal.show()