forked from redhat-developer/vscode-openshift-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserverlessFunctionLoader.ts
More file actions
244 lines (231 loc) · 9.64 KB
/
serverlessFunctionLoader.ts
File metadata and controls
244 lines (231 loc) · 9.64 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*-----------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*-----------------------------------------------------------------------------------------------*/
import * as cp from 'child_process';
import * as crypto from 'crypto';
import * as fs from 'fs/promises';
import * as JSYAML from 'js-yaml';
import * as path from 'path';
import * as vscode from 'vscode';
import { OdoImpl } from '../../odo';
import { ServerlessCommand, Utils } from '../../serverlessFunction/commands';
import { Functions } from '../../serverlessFunction/functions';
import { InvokeFunction } from '../../serverlessFunction/types';
import { CliExitData } from '../../util/childProcessUtil';
import { ExtensionID } from '../../util/constants';
import { Progress } from '../../util/progress';
import { selectWorkspaceFolder, selectWorkspaceFolders } from '../../util/workspace';
import { VsCommandError } from '../../vscommand';
import { loadWebviewHtml, validateName } from '../common-ext/utils';
export interface ServiceBindingFormResponse {
selectedService: string;
bindingName: string;
}
async function messageListener(panel: vscode.WebviewPanel, event: any): Promise<any> {
let response: CliExitData;
const eventName = event.action;
const functionName = event.name;
const functionPath: vscode.Uri = event.folderPath;
switch (eventName) {
case 'validateName':
const flag = validateName(functionName);
const defaultImages = !flag ? Functions.getInstance().getDefaultImages(functionName) : [];
panel?.webview.postMessage({
action: eventName,
error: !flag ? false : true,
helpText: !flag ? '' : flag,
name: functionName,
images: defaultImages
});
break;
case 'selectFile':
const options: vscode.OpenDialogOptions = {
canSelectMany: false,
openLabel: 'Select',
canSelectFiles: true,
canSelectFolders: false
};
const file = await vscode.window.showOpenDialog(options);
if (file && file[0]) {
panel?.webview.postMessage({
action: eventName,
filePath: file[0].fsPath
});
}
break;
case 'selectFolder':
const workspaceFolderItems = event.noWSFolder ? await selectWorkspaceFolder(true, 'Select Function Folder', functionName) : selectWorkspaceFolders();
panel?.webview.postMessage({
action: eventName,
wsFolderItems: event.noWSFolder ? [workspaceFolderItems] : workspaceFolderItems
});
break;
case 'createFunction':
const selctedFolder: vscode.Uri = vscode.Uri.file(path.join(functionPath.fsPath, functionName));
await Progress.execFunctionWithProgress(
`Creating function '${functionName}'`,
async () => {
response = await ServerlessFunctionViewLoader.createFunction(event.language, event.template, selctedFolder.fsPath, event.selectedImage);
});
if (response && response.error) {
void vscode.window.showErrorMessage(`Error while creating the function ${functionName}`);
} else {
const addedWSPath = vscode.Uri.from(selctedFolder);
const wsFolder = vscode.workspace.getWorkspaceFolder(addedWSPath);
if (!wsFolder) {
void vscode.window.showInformationMessage('The Created function was added into workspace');
vscode.workspace.updateWorkspaceFolders(vscode.workspace.workspaceFolders ? vscode.workspace.workspaceFolders.length : 0, null, { uri: addedWSPath });
}
panel.dispose();
await vscode.commands.executeCommand('openshift.Serverless.refresh');
}
break;
case 'invokeFunction':
const invokeFunData: InvokeFunction = {
instance: event.instance,
id: event.id,
path: event.path,
contentType: event.contentType,
format: event.format,
source: event.source,
type: event.type,
data: event.data,
file: event.file,
enableURL: event.enableURL,
invokeURL: event.invokeURL
}
await Functions.getInstance().invoke(functionName, invokeFunData);
panel.dispose();
break;
case 'getTemplates':
const templates = await Functions.getInstance().getTemplates();
panel?.webview.postMessage({
action: eventName,
basicTemplates: templates
})
default:
break;
}
}
export default class ServerlessFunctionViewLoader {
public static invokePanelMap: Map<string, vscode.WebviewPanel> = new Map<string, vscode.WebviewPanel>();
private static get extensionPath(): string {
return vscode.extensions.getExtension(ExtensionID).extensionPath;
}
/**
* Returns a webview panel with the "Add Service Binding" UI,
* or if there is an existing view for the given contextPath, focuses that view and returns null.
*
* @param contextPath the path to the component that's being binded to a service
* @param availableServices the list of all bindable services on the cluster
* @param listenerFactory the listener function to receive and process messages from the webview
* @return the webview as a promise
*/
static async loadView(
title: string,
invoke = false,
status?: string,
folderURI?: vscode.Uri,
url?: string
): Promise<vscode.WebviewPanel | null> {
if (ServerlessFunctionViewLoader.invokePanelMap.has(title)) {
const panel = ServerlessFunctionViewLoader.invokePanelMap.get(title);
panel.reveal(vscode.ViewColumn.One);
return null;
} else {
if (invoke) {
const panel = await this.createView(title);
const getEnvFuncId = crypto.randomUUID();
ServerlessFunctionViewLoader.invokePanelMap.set(title, panel);
void panel.webview.postMessage({
action: 'invoke',
instance: status,
name: title.substring(0, title.indexOf('-')).trim(),
id: getEnvFuncId,
uri: folderURI,
url: url
});
return panel;
} else if (!invoke) {
return await this.createView(title);
}
}
}
private static async createView(
title: string
): Promise<vscode.WebviewPanel> {
const localResourceRoot = vscode.Uri.file(
path.join(ServerlessFunctionViewLoader.extensionPath, 'out', 'serverlessFunctionViewer'),
);
let panel: vscode.WebviewPanel = vscode.window.createWebviewPanel(
'serverlessFunctionView',
title,
vscode.ViewColumn.One,
{
enableScripts: true,
localResourceRoots: [localResourceRoot],
retainContextWhenHidden: true,
},
);
panel.iconPath = vscode.Uri.file(
path.join(ServerlessFunctionViewLoader.extensionPath, 'images/context/cluster-node.png'),
);
panel.webview.html = await loadWebviewHtml(
'serverlessFunctionViewer',
panel,
);
panel.webview.onDidReceiveMessage((e) => messageListener(panel, e));
panel.onDidDispose(() => {
if (ServerlessFunctionViewLoader.invokePanelMap.has(title)) {
ServerlessFunctionViewLoader.invokePanelMap.delete(title);
}
panel = undefined;
});
return Promise.resolve(panel);
}
static async createFunction(
language: string,
template: string,
location: string,
image: string,
): Promise<CliExitData> {
let functionResponse: CliExitData;
try {
const response = await OdoImpl.Instance.execute(
ServerlessCommand.createFunction(language, template, location),
);
if (response && !response.error) {
const yamlContent = await Utils.getFuncYamlContent(location);
if (yamlContent) {
yamlContent.image = image;
await fs.rm(path.join(location, 'func.yaml'));
await fs.writeFile(
path.join(location, 'func.yaml'),
JSYAML.dump(yamlContent),
'utf-8',
);
functionResponse = {
error: undefined,
stderr: '',
stdout: 'Success',
};
}
} else {
await fs.rmdir(location);
functionResponse = response;
}
} catch (err) {
if (err instanceof VsCommandError) {
void vscode.window.showErrorMessage(err.message);
}
await fs.rmdir(location);
functionResponse = {
error: err as cp.ExecException,
stderr: '',
stdout: '',
};
}
return functionResponse;
}
}