Use different task type to prevent extension conflict

In addition, prevent multiple refresh processes and improve code readability
This commit is contained in:
Richard Willis 2019-11-13 13:52:35 +01:00
Родитель 5f025f2362
Коммит c79706d104
4 изменённых файлов: 52 добавлений и 32 удалений

Просмотреть файл

@ -60,7 +60,7 @@
"views": {
"explorer": [
{
"id": "gradle",
"id": "gradle-tree-view",
"name": "Gradle Tasks",
"when": "gradle:showTasksExplorer"
}
@ -140,7 +140,6 @@
"gradle.explorerNestedSubProjects": {
"type": "boolean",
"default": true,
"scope": "resource",
"description": "Show nested sub-projects in the explorer"
}
}
@ -153,9 +152,10 @@
],
"taskDefinitions": [
{
"type": "gradle",
"type": "richardwillis.gradle",
"required": [
"task"
"task",
"buildFile"
],
"properties": {
"task": {

Просмотреть файл

@ -79,13 +79,22 @@ function registerExplorer(
): GradleTasksTreeDataProvider | undefined {
if (workspace.workspaceFolders) {
const treeDataProvider = new GradleTasksTreeDataProvider(context);
treeDataProvider.refresh();
context.subscriptions.push(
window.createTreeView('gradle', {
window.createTreeView('gradle-tree-view', {
treeDataProvider: treeDataProvider,
showCollapseAll: true
})
);
return treeDataProvider;
}
return undefined;
}
function registerCommands(
context: ExtensionContext,
treeDataProvider: GradleTasksTreeDataProvider | undefined
) {
if (treeDataProvider) {
context.subscriptions.push(
commands.registerCommand(
'gradle.runTask',
@ -100,22 +109,28 @@ function registerExplorer(
treeDataProvider
)
);
return treeDataProvider;
}
return undefined;
}
interface ExtensionApi {}
export interface ExtensionApi {
outputChannel: OutputChannel;
}
export async function activate(
context: ExtensionContext
): Promise<ExtensionApi> {
const outputChannel = window.createOutputChannel('Gradle Tasks');
context.subscriptions.push(outputChannel);
registerTaskProvider(context, outputChannel);
treeDataProvider = registerExplorer(context);
if ((await hasGradleBuildFile()) && getIsTasksExplorerEnabled()) {
commands.executeCommand('setContext', 'gradle:showTasksExplorer', true);
if (await hasGradleBuildFile()) {
registerTaskProvider(context, outputChannel);
treeDataProvider = registerExplorer(context);
registerCommands(context, treeDataProvider);
if (treeDataProvider) {
treeDataProvider.refresh();
}
if (getIsTasksExplorerEnabled()) {
commands.executeCommand('setContext', 'gradle:showTasksExplorer', true);
}
}
return { outputChannel };
}

Просмотреть файл

@ -145,7 +145,7 @@ class NoTasksTreeItem extends TreeItem {
}
export class GradleTasksTreeDataProvider implements TreeDataProvider<TreeItem> {
private taskItemsPromise: Thenable<Task[]> | undefined = undefined;
private taskItemsPromise: Thenable<Task[]> = Promise.resolve([]);
private taskTree:
| WorkspaceTreeItem[]
| GradleBuildFileTreeItem[]
@ -163,14 +163,16 @@ export class GradleTasksTreeDataProvider implements TreeDataProvider<TreeItem> {
}
}
refresh() {
refresh(): Thenable<Task[]> {
invalidateTasksCache();
enableTaskDetection();
this.taskTree = null;
this.taskItemsPromise = tasks
.fetchTasks({ type: 'gradle' })
.then((tasks = []) => tasks.filter(task => task.source === 'gradle'));
this._onDidChangeTreeData.fire();
this.taskItemsPromise = tasks.fetchTasks().then((tasks = []) => {
this._onDidChangeTreeData.fire();
return tasks.filter(
task => task.definition.type === 'richardwillis.gradle'
);
});
return this.taskItemsPromise;
}

Просмотреть файл

@ -22,7 +22,7 @@ import {
} from './config';
let autoDetectOverride: boolean = false;
let cachedTasks: Task[] | undefined = undefined;
let cachedTasks: Promise<Task[]> | undefined = undefined;
export function enableTaskDetection() {
autoDetectOverride = true;
@ -42,7 +42,11 @@ export class GradleTaskProvider implements TaskProvider {
async provideTasks() {
try {
return await provideGradleTasks(this.statusBarItem, this.outputChannel);
const tasks = await provideGradleTasks(
this.statusBarItem,
this.outputChannel
);
return tasks;
} catch (e) {
this.outputChannel.append(`Error providing gradle tasks: ${e.message}\n`);
this.outputChannel.show();
@ -162,12 +166,12 @@ async function detectGradleTasks(
}
}
export async function provideGradleTasks(
export function provideGradleTasks(
statusBarItem: StatusBarItem,
outputChannel: OutputChannel
): Promise<Task[]> {
if (!cachedTasks) {
cachedTasks = await detectGradleTasks(statusBarItem, outputChannel);
cachedTasks = detectGradleTasks(statusBarItem, outputChannel);
}
return cachedTasks;
}
@ -216,18 +220,17 @@ export function getTaskName(task: string, relativePath: string | undefined) {
}
export async function createTask(
taskDefinition: GradleTaskDefinition | string,
taskDefinitionOrTaskName: GradleTaskDefinition | string,
taskName: string,
folder: WorkspaceFolder,
gradleBuildFileUri: Uri,
command: string,
subProjectBuildFileUri?: Uri
command: string
): Promise<Task> {
let definition: GradleTaskDefinition;
if (typeof taskDefinition === 'string') {
if (typeof taskDefinitionOrTaskName === 'string') {
let subProjectBuildFile: string | undefined = undefined;
if (taskDefinition.includes(':')) {
const [subProjectName] = taskDefinition.split(':');
if (taskDefinitionOrTaskName.includes(':')) {
const [subProjectName] = taskDefinitionOrTaskName.split(':');
const subProjectPath = path.join(folder.uri.fsPath, subProjectName);
const folderUri = Uri.file(subProjectPath);
const buildFile = (await getBuildFilePaths(folderUri))[0];
@ -235,13 +238,13 @@ export async function createTask(
}
definition = {
type: 'gradle',
task: taskDefinition,
type: 'richardwillis.gradle',
task: taskDefinitionOrTaskName,
buildFile: path.basename(gradleBuildFileUri.fsPath),
subProjectBuildFile
};
} else {
definition = taskDefinition;
definition = taskDefinitionOrTaskName;
}
function getCommandLine(task: string): string {