From 025b69fae1bb7f01c465075632b298e0043a84b9 Mon Sep 17 00:00:00 2001 From: BoykoAlex Date: Wed, 20 Apr 2022 12:52:47 -0400 Subject: [PATCH] Polish auto connect to process for VSCode --- .../commons-vscode/src/launch-util.ts | 8 ++- .../lib/debug-config-provider.ts | 61 ++++++++++++------- .../vscode-spring-boot/package.json | 2 +- 3 files changed, 47 insertions(+), 24 deletions(-) diff --git a/vscode-extensions/commons-vscode/src/launch-util.ts b/vscode-extensions/commons-vscode/src/launch-util.ts index 05dfd0134..24524b68a 100644 --- a/vscode-extensions/commons-vscode/src/launch-util.ts +++ b/vscode-extensions/commons-vscode/src/launch-util.ts @@ -412,10 +412,10 @@ export interface ListenableSetting { export class ListenablePreferenceSetting implements ListenableSetting { private _onDidChangeValue = new EventEmitter(); + private _disposable: Disposable; constructor(private section: string) { - VSCode.workspace.onDidChangeConfiguration(e => { - console.log('Settings changed! value = ' + this.value); + this._disposable = VSCode.workspace.onDidChangeConfiguration(e => { if (e.affectsConfiguration(this.section)) { this._onDidChangeValue.fire(); } @@ -430,4 +430,8 @@ export class ListenablePreferenceSetting implements ListenableSetting { return this._onDidChangeValue.event; } + dispose(): any { + return this._disposable.dispose(); + } + } diff --git a/vscode-extensions/vscode-spring-boot/lib/debug-config-provider.ts b/vscode-extensions/vscode-spring-boot/lib/debug-config-provider.ts index 247a75484..805bb580f 100644 --- a/vscode-extensions/vscode-spring-boot/lib/debug-config-provider.ts +++ b/vscode-extensions/vscode-spring-boot/lib/debug-config-provider.ts @@ -3,6 +3,7 @@ import * as path from "path"; import * as VSCode from "vscode"; import { Disposable } from "vscode"; import psList from 'ps-list'; +import { ListenablePreferenceSetting } from "@pivotal-tools/commons-vscode/lib/launch-util"; const JMX_VM_ARG = '-Dspring.jmx.enabled=' const ADMIN_VM_ARG = '-Dspring.application.admin.enabled=' @@ -11,7 +12,7 @@ const BOOT_PROJECT_ARG = '-Dspring.boot.project.name='; class SpringBootDebugConfigProvider implements DebugConfigurationProvider { resolveDebugConfigurationWithSubstitutedVariables(folder: WorkspaceFolder | undefined, debugConfiguration: DebugConfiguration, token?: CancellationToken): ProviderResult { - if (isAutoConnect() && isActuatorOnClasspath(debugConfiguration)) { + if (isActuatorOnClasspath(debugConfiguration)) { if (debugConfiguration.vmArgs) { if (debugConfiguration.vmArgs.indexOf(JMX_VM_ARG) < 0) { debugConfiguration.vmArgs += ` ${JMX_VM_ARG}true`; @@ -37,21 +38,50 @@ interface ProcessEvent { shellProcessId: number } +function hookListenerToBooleanPreference(setting: string, listenerCreator: () => Disposable): Disposable { + const listenableSetting = new ListenablePreferenceSetting(setting); + let listener: Disposable | undefined = listenableSetting.value ? listenerCreator() : undefined; + listenableSetting.onDidChangeValue(() => { + if (listenableSetting.value) { + if (!listener) { + listener = listenerCreator(); + } + } else { + if (listener) { + listener.dispose(); + listener = undefined; + } + } + }); + + return { + dispose: () => { + if (listener) { + listener.dispose(); + } + listenableSetting.dispose(); + } + }; +} + export function startDebugSupport(): Disposable { - VSCode.debug.onDidReceiveDebugSessionCustomEvent(handleCustomDebugEvent); - return VSCode.debug.registerDebugConfigurationProvider('java', new SpringBootDebugConfigProvider(), VSCode.DebugConfigurationProviderTriggerKind.Initial); + return hookListenerToBooleanPreference( + 'boot-java.live-information.automatic-connection.on', + () => Disposable.from( + VSCode.debug.onDidReceiveDebugSessionCustomEvent(handleCustomDebugEvent), + VSCode.debug.registerDebugConfigurationProvider('java', new SpringBootDebugConfigProvider(), VSCode.DebugConfigurationProviderTriggerKind.Initial) + ) + ); } async function handleCustomDebugEvent(e: VSCode.DebugSessionCustomEvent): Promise { if (e.session?.type === 'java' && e?.body?.type === 'processid') { const debugConfiguration: DebugConfiguration = e.session.configuration; - if (isBootAppWithJmxSetup(debugConfiguration)) { - setTimeout(async () => { - const pid = await getAppPid(e.body as ProcessEvent); - const processKey = pid.toString(); - VSCode.commands.executeCommand('sts/livedata/connect', { processKey }); - }, 500); - } + setTimeout(async () => { + const pid = await getAppPid(e.body as ProcessEvent); + const processKey = pid.toString(); + VSCode.commands.executeCommand('sts/livedata/connect', { processKey }); + }, 500); } } @@ -84,14 +114,3 @@ function isActuatorJarFile(f: string): boolean { } return false; } - -function isBootAppWithJmxSetup(debugConfiguration: DebugConfiguration): boolean { - return debugConfiguration.vmArgs.indexOf(`${JMX_VM_ARG}true`) >= 0 - && debugConfiguration.vmArgs.indexOf(`${ADMIN_VM_ARG}true`) >= 0 - && debugConfiguration.vmArgs.indexOf(`${BOOT_PROJECT_ARG}${debugConfiguration.projectName}`) >= 0 - && isActuatorOnClasspath(debugConfiguration); -} - -function isAutoConnect(): boolean { - return VSCode.workspace.getConfiguration("boot-java.live-information.automatic-connection")?.get('on'); -} diff --git a/vscode-extensions/vscode-spring-boot/package.json b/vscode-extensions/vscode-spring-boot/package.json index a4f12974a..a1be670e7 100644 --- a/vscode-extensions/vscode-spring-boot/package.json +++ b/vscode-extensions/vscode-spring-boot/package.json @@ -78,7 +78,7 @@ "boot-java.live-information.automatic-connection.on": { "type": "boolean", "default": true, - "description": "Live Information - Automatic Process Connection Enabled" + "description": "Live Information - Automatic addition of JVM arguments enabling JMX and Process Connection via JMX Enabled" }, "boot-java.live-information.fetch-data.max-retries": { "type": "number",