Task query and execution polish

This commit is contained in:
Dirk Baeumer
2018-03-26 17:04:31 +02:00
parent e45a276a07
commit 6577bde536
8 changed files with 121 additions and 36 deletions

View File

@@ -517,18 +517,21 @@ export function createApiFactory(
registerTaskProvider: (type: string, provider: vscode.TaskProvider) => {
return extHostTask.registerTaskProvider(extension, provider);
},
fetchTasks: proposedApiFunction(extension, (): Thenable<vscode.Task[]> => {
// fetchTasks: proposedApiFunction(extension, (): Thenable<vscode.Task[]> => {
// return extHostTask.executeTaskProvider();
// }),
// executeTask: proposedApiFunction(extension, (task: vscode.Task): Thenable<vscode.TaskExecution> => {
// return extHostTask.executeTask(extension, task);
// }),
fetchTasks: (): Thenable<vscode.Task[]> => {
return extHostTask.executeTaskProvider();
}),
executeTask: proposedApiFunction(extension, (task: vscode.Task): Thenable<vscode.TaskExecution> => {
},
executeTask: (task: vscode.Task): Thenable<vscode.TaskExecution> => {
return extHostTask.executeTask(extension, task);
}),
},
onDidStartTask: (listeners, thisArgs?, disposables?) => {
return extHostTask.onDidStartTask(listeners, thisArgs, disposables);
},
terminateTask: proposedApiFunction(extension, (task: vscode.TaskExecution): void => {
extHostTask.terminateTask(task);
}),
onDidEndTask: (listeners, thisArgs?, disposables?) => {
return extHostTask.onDidEndTask(listeners, thisArgs, disposables);
},

View File

@@ -391,7 +391,7 @@ export interface MainThreadTaskShape extends IDisposable {
$registerTaskProvider(handle: number): TPromise<void>;
$executeTaskProvider(): TPromise<TaskDTO[]>;
$executeTask(task: TaskHandleDTO | TaskDTO): TPromise<TaskExecutionDTO>;
$terminateTask(task: TaskExecutionDTO): TPromise<void>;
$terminateTask(id: string): TPromise<void>;
$unregisterTaskProvider(handle: number): TPromise<void>;
}

View File

@@ -613,6 +613,7 @@ namespace TaskDTO {
if (!execution || !definition || !scope) {
return undefined;
}
let group = (value.group as types.TaskGroup) ? (value.group as types.TaskGroup).id : undefined;
let result: TaskDTO = {
_id: (value as types.Task)._id,
definition,
@@ -624,7 +625,7 @@ namespace TaskDTO {
},
execution,
isBackground: value.isBackground,
group: (value.group as types.TaskGroup).id,
group: group,
presentationOptions: TaskPresentationOptionsDTO.from(value.presentationOptions),
problemMatchers: value.problemMatchers,
hasDefinedMatchers: (value as types.Task).hasDefinedMatchers
@@ -675,17 +676,26 @@ namespace TaskDTO {
}
class TaskExecutionImpl implements vscode.TaskExecution {
constructor(readonly _id: string) {
constructor(readonly _id: string, private readonly _task: vscode.Task, private readonly _tasks: ExtHostTask) {
}
get task(): vscode.Task {
return this._task;
}
public terminate(): void {
this._tasks.terminateTask(this);
}
}
namespace TaskExecutionDTO {
export function to(value: TaskExecutionDTO): vscode.TaskExecution {
return new TaskExecutionImpl(value.id);
export function to(value: TaskExecutionDTO, tasks: ExtHostTask): vscode.TaskExecution {
return new TaskExecutionImpl(value.id, TaskDTO.to(value.task, tasks.extHostWorkspace), tasks);
}
export function from(value: vscode.TaskExecution): TaskExecutionDTO {
return {
id: (value as TaskExecutionImpl)._id
id: (value as TaskExecutionImpl)._id,
task: undefined
};
}
}
@@ -712,6 +722,10 @@ export class ExtHostTask implements ExtHostTaskShape {
this._handlers = new Map<number, HandlerData>();
}
public get extHostWorkspace(): ExtHostWorkspace {
return this._extHostWorkspace;
}
public registerTaskProvider(extension: IExtensionDescription, provider: vscode.TaskProvider): vscode.Disposable {
if (!provider) {
return new types.Disposable(() => { });
@@ -742,15 +756,19 @@ export class ExtHostTask implements ExtHostTaskShape {
let tTask = (task as types.Task);
// We have a preserved ID. So the task didn't change.
if (tTask._id !== void 0) {
return this._proxy.$executeTask(TaskHandleDTO.from(tTask)).then(value => TaskExecutionDTO.to(value));
return this._proxy.$executeTask(TaskHandleDTO.from(tTask)).then(value => new TaskExecutionImpl(value.id, task, this));
} else {
return this._proxy.$executeTask(TaskDTO.from(task, extension)).then(value => TaskExecutionDTO.to(value));
let dto = TaskDTO.from(task, extension);
if (dto === void 0) {
return Promise.reject(new Error('Task is not valid'));
}
return this._proxy.$executeTask(dto).then(value => new TaskExecutionImpl(value.id, task, this));
}
}
public $taskStarted(execution: TaskExecutionDTO): void {
this._onDidExecuteTask.fire({
execution: TaskExecutionDTO.to(execution)
execution: TaskExecutionDTO.to(execution, this)
});
}
@@ -762,12 +780,12 @@ export class ExtHostTask implements ExtHostTaskShape {
if (!(execution instanceof TaskExecutionImpl)) {
throw new Error('No valid task execution provided');
}
return this._proxy.$terminateTask(TaskExecutionDTO.from(execution));
return this._proxy.$terminateTask((execution as TaskExecutionImpl)._id);
}
public $taskEnded(execution: TaskExecutionDTO): void {
this._onDidTerminateTask.fire({
execution: TaskExecutionDTO.to(execution)
execution: TaskExecutionDTO.to(execution, this)
});
}

View File

@@ -1343,6 +1343,20 @@ export class ProcessExecution implements vscode.ProcessExecution {
set options(value: vscode.ProcessExecutionOptions) {
this._options = value;
}
public computeId(): string {
const hash = crypto.createHash('md5');
hash.update('process');
if (this._process !== void 0) {
hash.update(this._process);
}
if (this._args && this._args.length > 0) {
for (let arg of this._args) {
hash.update(arg);
}
}
return hash.digest('hex');
}
}
export class ShellExecution implements vscode.ShellExecution {
@@ -1411,6 +1425,23 @@ export class ShellExecution implements vscode.ShellExecution {
set options(value: vscode.ShellExecutionOptions) {
this._options = value;
}
public computeId(): string {
const hash = crypto.createHash('md5');
hash.update('shell');
if (this._commandLine !== void 0) {
hash.update(this._commandLine);
}
if (this._command !== void 0) {
hash.update(typeof this._command === 'string' ? this._command : this._command.value);
}
if (this._args && this._args.length > 0) {
for (let arg of this._args) {
hash.update(typeof arg === 'string' ? arg : arg.value);
}
}
return hash.digest('hex');
}
}
export enum ShellQuoting {
@@ -1485,7 +1516,24 @@ export class Task implements vscode.Task {
}
private clear(): void {
if (this.__id === void 0) {
return;
}
this.__id = undefined;
this._scope = undefined;
this._definitionKey = undefined;
this._definition = undefined;
if (this._execution instanceof ProcessExecution) {
this._definition = {
type: 'process',
id: this._execution.computeId()
};
} else if (this._execution instanceof ShellExecution) {
this._definition = {
type: 'shell',
id: this._execution.computeId()
};
}
}
get definition(): vscode.TaskDefinition {