diff --git a/extension/src/tasks/GradleTaskProvider.ts b/extension/src/tasks/GradleTaskProvider.ts index be3c0211f..97c158583 100644 --- a/extension/src/tasks/GradleTaskProvider.ts +++ b/extension/src/tasks/GradleTaskProvider.ts @@ -1,12 +1,13 @@ import * as vscode from 'vscode'; import { GradleTaskDefinition } from '.'; import { logger } from '../logger'; -import { createTaskFromDefinition, loadTasksForProjectRoots } from './taskUtil'; +import { + loadTasksForProjectRoots, + resolveTaskFromDefinition, +} from './taskUtil'; import { TaskId } from '../stores/types'; import { RootProjectsStore } from '../stores'; -import { RootProject } from '../rootProject/RootProject'; import { GradleClient } from '../client'; -import { getConfigJavaDebug } from '../util/config'; import { EventWaiter } from '../util/EventWaiter'; export class GradleTaskProvider @@ -47,9 +48,7 @@ export class GradleTaskProvider ): Promise { const { definition } = _task; const gradleTaskDefinition = definition as GradleTaskDefinition; - const workspaceFolder = vscode.workspace.getWorkspaceFolder( - vscode.Uri.file(gradleTaskDefinition.workspaceFolder) - ); + const workspaceFolder = _task.scope as vscode.WorkspaceFolder; if (!workspaceFolder) { logger.error( 'Unable to provide Gradle task. Invalid workspace folder: ', @@ -57,15 +56,9 @@ export class GradleTaskProvider ); return undefined; } - const javaDebug = getConfigJavaDebug(workspaceFolder); - const rootProject = new RootProject( - workspaceFolder, - vscode.Uri.file(gradleTaskDefinition.projectFolder), - javaDebug - ); - return createTaskFromDefinition( + return resolveTaskFromDefinition( gradleTaskDefinition, - rootProject, + workspaceFolder, this.client ); } diff --git a/extension/src/tasks/taskUtil.ts b/extension/src/tasks/taskUtil.ts index 9342636d4..e1e8a7aff 100644 --- a/extension/src/tasks/taskUtil.ts +++ b/extension/src/tasks/taskUtil.ts @@ -23,6 +23,7 @@ import { getGradleConfig, getConfigIsAutoDetectionEnabled, getConfigReuseTerminals, + getConfigJavaDebug, } from '../util/config'; const cancellingTasks: Map = new Map(); @@ -205,6 +206,72 @@ export function createTaskFromDefinition( return task; } +export function resolveTaskFromDefinition( + definition: Required, + workspaceFolder: vscode.WorkspaceFolder, + client: GradleClient +): vscode.Task | undefined { + const taskName = buildTaskName(definition); + const args = [definition.script] + .concat(parseArgsStringToArgv(definition.args.trim())) + .filter(Boolean); + const task = new vscode.Task( + definition, + workspaceFolder, + taskName, + 'gradle', + new vscode.CustomExecution( + async ( + resolvedDefinition: vscode.TaskDefinition + ): Promise => { + const resolvedTaskDefinition = + resolvedDefinition as GradleTaskDefinition; + const resolvedWorkspaceFolder = + vscode.workspace.getWorkspaceFolder( + vscode.Uri.file(resolvedTaskDefinition.workspaceFolder) + ) || workspaceFolder; + const javaDebug = getConfigJavaDebug(resolvedWorkspaceFolder); + const rootProject = new RootProject( + resolvedWorkspaceFolder, + vscode.Uri.file(resolvedTaskDefinition.projectFolder), + javaDebug + ); + const cancellationKey = getRunTaskCommandCancellationKey( + rootProject.getProjectUri().fsPath, + definition.script + ); + + const executeTerminal = new GradleRunnerTerminal( + rootProject, + args, + cancellationKey, + client + ); + executeTerminal.setTask(task); + return executeTerminal; + } + ), + ['$gradle'] + ); + + const reuseTerminals = getConfigReuseTerminals(); + let panelKind = vscode.TaskPanelKind.Dedicated; + if (reuseTerminals === 'off') { + panelKind = vscode.TaskPanelKind.New; + } else if (reuseTerminals === 'all') { + panelKind = vscode.TaskPanelKind.Shared; + } + task.presentationOptions = { + showReuseMessage: false, + clear: true, + echo: true, + focus: true, + panel: panelKind, + reveal: vscode.TaskRevealKind.Always, + }; + return task; +} + function createVSCodeTaskFromGradleTask( gradleTask: GradleTask, rootProject: RootProject,