This repository has been archived by the owner on Jan 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
che-task-resolver.ts
68 lines (59 loc) · 2.62 KB
/
che-task-resolver.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*********************************************************************
* Copyright (c) 2018 Red Hat, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
**********************************************************************/
import { injectable, inject } from 'inversify';
import { TaskResolver } from '@theia/task/lib/browser';
import { VariableResolverService } from '@theia/variable-resolver/lib/browser';
import { TaskConfiguration } from '@theia/task/lib/common';
import { MachinePicker } from './machine-picker';
import { CheTaskConfiguration, Target, CHE_TASK_TYPE } from '../common/task-protocol';
import { CheWorkspaceClientService } from '../common/che-workspace-client-service';
/**
* Prepares a Che Task config for execution:
* - resolves the variables in a command line;
* - sets the current Che workspace's ID if it's not specified;
* - allows to choose a target machine if it's not specified.
*/
@injectable()
export class CheTaskResolver implements TaskResolver {
@inject(VariableResolverService)
protected readonly variableResolverService: VariableResolverService;
@inject(CheWorkspaceClientService)
protected readonly cheWorkspaceClient: CheWorkspaceClientService;
@inject(MachinePicker)
protected readonly machinePicker: MachinePicker;
async resolveTask(taskConfig: TaskConfiguration): Promise<TaskConfiguration> {
if (taskConfig.type !== CHE_TASK_TYPE) {
throw new Error(`Unsupported task configuration type: ${taskConfig.type}`);
}
const cheTaskConfig = taskConfig as CheTaskConfiguration;
const target = cheTaskConfig.target;
const resultTarget: Target = {};
if (target && target.workspaceId) {
resultTarget.workspaceId = target.workspaceId;
} else {
resultTarget.workspaceId = await this.cheWorkspaceClient.getWorkspaceId();
}
if (target && target.machineName) {
resultTarget.machineName = target.machineName;
} else {
resultTarget.machineName = await this.machinePicker.pick();
}
const resultTask: CheTaskConfiguration = {
type: cheTaskConfig.type,
label: cheTaskConfig.label,
command: await this.variableResolverService.resolve(cheTaskConfig.command),
target: resultTarget,
previewUrl: cheTaskConfig.previewUrl,
_source: 'che',
_scope: undefined
};
return resultTask;
}
}