From 04e7652182876bc6ef2da49d68d7328e4e9a77d8 Mon Sep 17 00:00:00 2001 From: David Kwon Date: Fri, 27 Sep 2019 12:13:58 -0400 Subject: [PATCH] Debug prelaunch task uses mvnw or mvn Signed-off-by: David Kwon --- src/debugging/createDebugConfig.ts | 203 +++++++++++++++----------- src/debugging/startDebugging.ts | 5 +- src/utils/mavenUtils.ts | 3 +- vscode-debug-files/configuration.json | 8 + vscode-debug-files/launch.json | 16 -- vscode-debug-files/tasks.json | 6 - 6 files changed, 132 insertions(+), 109 deletions(-) create mode 100644 vscode-debug-files/configuration.json delete mode 100644 vscode-debug-files/launch.json delete mode 100644 vscode-debug-files/tasks.json diff --git a/src/debugging/createDebugConfig.ts b/src/debugging/createDebugConfig.ts index 88e30199..9ac06e85 100644 --- a/src/debugging/createDebugConfig.ts +++ b/src/debugging/createDebugConfig.ts @@ -20,108 +20,143 @@ import { DebugConfiguration, TaskDefinition, Uri, workspace, TaskPanelKind } fro import { getDefaultMavenExecutable, getUnixMavenWrapperExecuteable, getWindowsMavenWrapperExecutable, mavenWrapperExists } from '../utils/mavenUtils'; import { parse } from 'comment-json'; -/** - * Creates a .vscode/ directory in `directory` (if it does not exist) - * and adds a new task and debug configuration which starts the quarkus:dev command - * @param directory workspace directory to generate debug config in - */ -export async function createDebugConfig(directory: Uri) { +export class DebugConfigCreator { + + private workspaceDir: Uri; + private dotVSCodeDir: string; + private tasksJsonDir: string; + private launchJsonDir: string; + + constructor(directory: Uri) { + this.workspaceDir = directory; + this.dotVSCodeDir = directory.fsPath + '/.vscode'; + this.tasksJsonDir = this.dotVSCodeDir + '/tasks.json'; + this.launchJsonDir = this.dotVSCodeDir + '/launch.json'; + } - const vscodeDir: string = directory.fsPath + '/.vscode'; - if (!fs.existsSync(vscodeDir)) { - fs.mkdirSync(vscodeDir); + public async createFiles(): Promise { + this.createVSCodeDirIfMissing(); + await this.addDebugTask(); + await this.addDebugConfig(); } - await addDebugTask(directory); - await addDebugConfig(); -} - -async function addDebugTask(directory: Uri): Promise { - await createTasksJsonIfMissing(directory); - const tasksJson = workspace.getConfiguration('tasks', directory); - const tasks: TaskDefinition[] = tasksJson.get('tasks'); - tasks.push(await getDebugTask(directory)); - await tasksJson.update('tasks', tasks, false); - -} - -async function addDebugConfig() { - await createLaunchJsonIfMissing(directory); - - // if (fs.existsSync(vscodeDir + '/launch.json')) { - // const launchConfig = workspace.getConfiguration('launch', directory); - // const configurations: DebugConfiguration[] = launchConfig.get('configurations'); - // const configToAdd: DebugConfiguration = getDebugResource('launch.json').configurations[0]; - // configurations.push(configToAdd); - // await launchConfig.update('configurations', configurations, false); - // } else { - // let launchContent: string = getDebugResourceAsString('launch.json'); - // launchContent = replaceWithSpacesIfNeeded(launchContent); - // fs.writeFileSync(vscodeDir + '/launch.json', launchContent); - // } -} + private createVSCodeDirIfMissing(): void { + if (!fs.existsSync(this.dotVSCodeDir)) { + fs.mkdirSync(this.dotVSCodeDir); + } + } -/** - * Creates a tasks.json file (with no tasks) in .vscode/ directory, if - * one does not exist already - * @param dotVSCodeDir absolute path to the .vscode/ directory - */ -async function createTasksJsonIfMissing(directory: Uri): Promise { - const vscodeDir: string = directory.fsPath + '/.vscode'; - if (!fs.existsSync(vscodeDir + '/tasks.json')) { - await workspace.getConfiguration('tasks', directory).update('version', "2.0.0"); - await workspace.getConfiguration('tasks', directory).update('tasks', []); + private async addDebugTask(): Promise { + await this.createTasksJsonIfMissing(); + const tasksJson = workspace.getConfiguration('tasks', this.workspaceDir); + const tasks: TaskDefinition[] = tasksJson.get('tasks'); + tasks.push(await this.getDebugTask()); + await tasksJson.update('tasks', tasks, false); } -} -async function createLaunchJsonIfMissing(directory: Uri): Promise { - const vscodeDir: string = directory.fsPath + '/.vscode'; - if (!fs.existsSync(vscodeDir + '/launch.json')) { - await workspace.getConfiguration('launch', directory).update('version', "0.2.0"); - await workspace.getConfiguration('launch', directory).update('configurations', []); + private async addDebugConfig(): Promise { + await this.createLaunchJsonIfMissing(); + const launchJson = workspace.getConfiguration('launch', this.workspaceDir); + const configurations: DebugConfiguration[] = launchJson.get('configurations'); + configurations.push(this.getDebugConfig()); + await launchJson.update('configurations', configurations, false); + } + + /** + * Creates a tasks.json file (with no tasks) in .vscode/ directory, if + * one does not exist already + * @param dotVSCodeDir absolute path to the .vscode/ directory + */ + private async createTasksJsonIfMissing(): Promise { + + if (fs.existsSync(this.tasksJsonDir)) { + return; + } + + // create tasks.json file in .vscode/ + await workspace.getConfiguration('tasks', this.workspaceDir).update('version', "2.0.0"); + await workspace.getConfiguration('tasks', this.workspaceDir).update('tasks', []); + + if (fs.existsSync(this.tasksJsonDir)) { + this.prependTasksJsonComment(); + } } -} + private async createLaunchJsonIfMissing(): Promise { -async function getDebugTask(directory: Uri): Promise { + if (fs.existsSync(this.launchJsonDir)) { + return; + } - const QUARKUS_DEV: string = 'quarkus:dev'; - let windowsMvn: string; - let unixMvn: string; + await workspace.getConfiguration('launch', this.workspaceDir).update('version', "0.2.0"); + await workspace.getConfiguration('launch', this.workspaceDir).update('configurations', []); - if (mavenWrapperExists(workspace.getWorkspaceFolder(directory))) { - windowsMvn = getWindowsMavenWrapperExecutable(); - unixMvn = getUnixMavenWrapperExecuteable(); - } else { - windowsMvn = await getDefaultMavenExecutable(); - unixMvn = await getDefaultMavenExecutable(); + if (fs.existsSync(this.launchJsonDir)) { + this.prependLaunchJsonComment(); + } } - const taskToAdd: TaskDefinition = getDebugResource('task.json'); - taskToAdd.command = `${unixMvn} ${QUARKUS_DEV}`; - taskToAdd.windows.command = `${windowsMvn} ${QUARKUS_DEV}`; + private async getDebugTask(): Promise { - return taskToAdd; -} + const QUARKUS_DEV: string = 'quarkus:dev'; + let windowsMvn: string; + let unixMvn: string; -function getDebugResource(filename: string): T { - return parse(getDebugResourceAsString(filename)); -} + if (await mavenWrapperExists(workspace.getWorkspaceFolder(this.workspaceDir))) { + windowsMvn = getWindowsMavenWrapperExecutable(); + unixMvn = getUnixMavenWrapperExecuteable(); + } else { + windowsMvn = await getDefaultMavenExecutable(); + unixMvn = await getDefaultMavenExecutable(); + } -function getDebugResourceAsString(filename: string): string { - const pathToFile = path.resolve(__dirname, '../vscode-debug-files/' + filename); - return fs.readFileSync(pathToFile).toString(); -} + const taskToAdd: TaskDefinition = this.getDebugResource('task.json'); + taskToAdd.command = `${unixMvn} ${QUARKUS_DEV}`; + taskToAdd.windows.command = `${windowsMvn} ${QUARKUS_DEV}`; -function replaceWithSpacesIfNeeded(str: string): string { - if (workspace.getConfiguration('editor').get('insertSpaces')) { - const numSpaces: number = workspace.getConfiguration('editor').get('tabSize'); - return replaceTabsWithSpaces(str, numSpaces); + return taskToAdd; } - return str; -} + private getDebugConfig(): DebugConfiguration { + return this.getDebugResource('configuration.json'); + } + + private getDebugResource(filename: string): T { + return parse(this.getDebugResourceAsString(filename)); + } + + private getDebugResourceAsString(filename: string): string { + const pathToFile = path.resolve(__dirname, '../vscode-debug-files/' + filename); + return fs.readFileSync(pathToFile).toString(); + } -function replaceTabsWithSpaces(str: string, numSpaces: number): string { - return str.replace(/\t/g, ' '.repeat(numSpaces)); + private prependTasksJsonComment() { + let comment: string = `// See https://go.microsoft.com/fwlink/?LinkId=733558\n`; + comment += `// for the documentation about the tasks.json format\n`; + this.prependToFile(this.tasksJsonDir, comment); + } + + private prependLaunchJsonComment() { + let comment: string = `// A launch configuration that compiles the extension and then opens it inside a new window\n`; + comment += `// Use IntelliSense to learn about possible attributes.\n`; + comment += `// Hover to view descriptions of existing attributes.\n`; + comment += `// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387\n`; + this.prependToFile(this.launchJsonDir, comment); + } + + /** + * Referenced from https://stackoverflow.com/a/49889780 + * @param fileDir + * @param str + */ + private prependToFile(fileDir: string, str: string) { + const data = fs.readFileSync(fileDir); + const fd = fs.openSync(fileDir, 'w+'); + const insert = new Buffer(str); + fs.writeSync(fd, insert, 0, insert.length, 0); + fs.writeSync(fd, data, 0, data.length, insert.length); + fs.close(fd, (err) => { + if (err) throw err; + }); + } } \ No newline at end of file diff --git a/src/debugging/startDebugging.ts b/src/debugging/startDebugging.ts index 5e97d207..5d317c74 100644 --- a/src/debugging/startDebugging.ts +++ b/src/debugging/startDebugging.ts @@ -16,7 +16,7 @@ import { WorkspaceFolder, debug, window, workspace, DebugConfiguration } from 'vscode'; import { containsMavenQuarkusProject } from '../utils/workspaceUtils'; -import { createDebugConfig } from '../debugging/createDebugConfig'; +import { DebugConfigCreator } from '../debugging/createDebugConfig'; import { getQuarkusDevDebugConfig } from '../utils/launchConfigUtils'; export async function tryStartDebugging() { @@ -38,7 +38,8 @@ async function startDebugging(): Promise { let debugConfig: DebugConfiguration|undefined = await getQuarkusDevDebugConfig(workspaceFolder); if (!debugConfig) { - await createDebugConfig(workspaceFolder.uri); + const debugConfigCreator: DebugConfigCreator = new DebugConfigCreator(workspaceFolder.uri); + await debugConfigCreator.createFiles(); debugConfig = await getQuarkusDevDebugConfig(workspaceFolder); } diff --git a/src/utils/mavenUtils.ts b/src/utils/mavenUtils.ts index 0ba72282..ab3d5e74 100644 --- a/src/utils/mavenUtils.ts +++ b/src/utils/mavenUtils.ts @@ -48,7 +48,8 @@ export function getMavenWrapperFilename(): string { * @param workspaceFolder */ export async function mavenWrapperExists(workspaceFolder: WorkspaceFolder) { - return getMavenWrapperPathFromPom(workspaceFolder.uri, workspaceFolder) !== undefined; + const b = await getMavenWrapperPathFromPom(workspaceFolder.uri, workspaceFolder) !== undefined; + return b; } /** diff --git a/vscode-debug-files/configuration.json b/vscode-debug-files/configuration.json new file mode 100644 index 00000000..b2318fa7 --- /dev/null +++ b/vscode-debug-files/configuration.json @@ -0,0 +1,8 @@ +{ + "preLaunchTask": "quarkus:dev", + "type": "java", + "request": "attach", + "hostName": "localhost", + "name": "Debug Quarkus application", + "port": 5005 +} \ No newline at end of file diff --git a/vscode-debug-files/launch.json b/vscode-debug-files/launch.json deleted file mode 100644 index 2435d0a6..00000000 --- a/vscode-debug-files/launch.json +++ /dev/null @@ -1,16 +0,0 @@ -// Use IntelliSense to learn about possible attributes. -// Hover to view descriptions of existing attributes. -// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 -{ - "version": "0.2.0", - "configurations": [ - { - "preLaunchTask": "quarkus:dev", - "type": "java", - "request": "attach", - "hostName": "localhost", - "name": "Debug Quarkus application", - "port": 5005 - } - ] -} \ No newline at end of file diff --git a/vscode-debug-files/tasks.json b/vscode-debug-files/tasks.json deleted file mode 100644 index 6b8e62c7..00000000 --- a/vscode-debug-files/tasks.json +++ /dev/null @@ -1,6 +0,0 @@ -// See https://go.microsoft.com/fwlink/?LinkId=733558 -// for the documentation about the tasks.json format -{ - "version": "2.0.0", - "tasks": [] -} \ No newline at end of file