Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Resolve custom task #1035

Merged
merged 1 commit into from
Nov 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 7 additions & 14 deletions extension/src/tasks/GradleTaskProvider.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -47,25 +48,17 @@ export class GradleTaskProvider
): Promise<vscode.Task | undefined> {
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: ',
gradleTaskDefinition.workspaceFolder
);
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
);
}
Expand Down
67 changes: 67 additions & 0 deletions extension/src/tasks/taskUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
getGradleConfig,
getConfigIsAutoDetectionEnabled,
getConfigReuseTerminals,
getConfigJavaDebug,
} from '../util/config';

const cancellingTasks: Map<string, vscode.Task> = new Map();
Expand Down Expand Up @@ -205,6 +206,72 @@ export function createTaskFromDefinition(
return task;
}

export function resolveTaskFromDefinition(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here just copy the main logics from createTaskFromDefinition and use CustomExecution to resolve the VSCode variables

definition: Required<GradleTaskDefinition>,
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<vscode.Pseudoterminal> => {
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,
Expand Down