Skip to content

Commit

Permalink
Debug prelaunch task uses mvnw or mvn
Browse files Browse the repository at this point in the history
Signed-off-by: David Kwon <dakwon@redhat.com>
  • Loading branch information
dkwon17 authored and fbricon committed Oct 4, 2019
1 parent e9a65ab commit 04e7652
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 109 deletions.
203 changes: 119 additions & 84 deletions src/debugging/createDebugConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
this.createVSCodeDirIfMissing();
await this.addDebugTask();
await this.addDebugConfig();
}

await addDebugTask(directory);
await addDebugConfig();
}

async function addDebugTask(directory: Uri): Promise<void> {
await createTasksJsonIfMissing(directory);
const tasksJson = workspace.getConfiguration('tasks', directory);
const tasks: TaskDefinition[] = tasksJson.get<TaskDefinition[]>('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<DebugConfiguration[]>('configurations');
// const configToAdd: DebugConfiguration = getDebugResource<DebugConfiguration>('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<void> {
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<void> {
await this.createTasksJsonIfMissing();
const tasksJson = workspace.getConfiguration('tasks', this.workspaceDir);
const tasks: TaskDefinition[] = tasksJson.get<TaskDefinition[]>('tasks');
tasks.push(await this.getDebugTask());
await tasksJson.update('tasks', tasks, false);
}
}

async function createLaunchJsonIfMissing(directory: Uri): Promise<void> {
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<void> {
await this.createLaunchJsonIfMissing();
const launchJson = workspace.getConfiguration('launch', this.workspaceDir);
const configurations: DebugConfiguration[] = launchJson.get<DebugConfiguration[]>('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<void> {

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<void> {

async function getDebugTask(directory: Uri): Promise<TaskDefinition> {
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<TaskDefinition>('task.json');
taskToAdd.command = `${unixMvn} ${QUARKUS_DEV}`;
taskToAdd.windows.command = `${windowsMvn} ${QUARKUS_DEV}`;
private async getDebugTask(): Promise<TaskDefinition> {

return taskToAdd;
}
const QUARKUS_DEV: string = 'quarkus:dev';
let windowsMvn: string;
let unixMvn: string;

function getDebugResource<T>(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<TaskDefinition>('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<DebugConfiguration>('configuration.json');
}

private getDebugResource<T>(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;
});
}
}
5 changes: 3 additions & 2 deletions src/debugging/startDebugging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -38,7 +38,8 @@ async function startDebugging(): Promise<void> {
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);
}

Expand Down
3 changes: 2 additions & 1 deletion src/utils/mavenUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
8 changes: 8 additions & 0 deletions vscode-debug-files/configuration.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"preLaunchTask": "quarkus:dev",
"type": "java",
"request": "attach",
"hostName": "localhost",
"name": "Debug Quarkus application",
"port": 5005
}
16 changes: 0 additions & 16 deletions vscode-debug-files/launch.json

This file was deleted.

6 changes: 0 additions & 6 deletions vscode-debug-files/tasks.json

This file was deleted.

0 comments on commit 04e7652

Please sign in to comment.