Skip to content

Commit

Permalink
[WIP] clean up the "create serverless function" view
Browse files Browse the repository at this point in the history
Signed-off-by: David Thompson <davthomp@redhat.com>
  • Loading branch information
datho7561 committed May 28, 2024
1 parent 102c808 commit 9d91c17
Showing 1 changed file with 29 additions and 72 deletions.
101 changes: 29 additions & 72 deletions src/webview/serverless-function/serverlessFunctionLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
* 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 { Odo } from '../../odo/odoWrapper';
import { ServerlessCommand, Utils } from '../../serverlessFunction/commands';

import sendTelemetry from '../../telemetry';
import { CliExitData } from '../../util/childProcessUtil';
import { Platform } from '../../util/platform';
Expand Down Expand Up @@ -43,50 +41,31 @@ async function getTemplates(): Promise<any[]> {
}

export default class ServerlessFunctionViewLoader {

public static invokePanelMap: Map<string, vscode.WebviewPanel> = new Map<string, vscode.WebviewPanel>();
private static panel: vscode.WebviewPanel = undefined;

private static templates: any[] = undefined;
private static currentlyInvoke: boolean = false;

private static status?: string = undefined;
private static folderURI?: vscode.Uri = undefined;
private static url?: string = undefined;

/**
* Returns the webview as a promise.
*
* @returns the webview as a promise
*/
static async loadView(
title: string,
invoke = false,
status?: string,
folderURI?: vscode.Uri,
url?: string
): Promise<vscode.WebviewPanel | null> {
ServerlessFunctionViewLoader.currentlyInvoke = invoke;
if (ServerlessFunctionViewLoader.invokePanelMap.has(title)) {
const panel = ServerlessFunctionViewLoader.invokePanelMap.get(title);
panel.reveal(vscode.ViewColumn.One);
return null;
static async loadView(title: string): Promise<vscode.WebviewPanel> {
if (this.panel) {
this.panel.reveal(vscode.ViewColumn.One);
return this.panel;
}
ServerlessFunctionViewLoader.status = status;
ServerlessFunctionViewLoader.folderURI = folderURI;
ServerlessFunctionViewLoader.url = url;
ServerlessFunctionViewLoader.templates = await getTemplates();
return await this.createView(title);
this.panel = await this.createView(title);
return this.panel;
}

private static async createView(
title: string
): Promise<vscode.WebviewPanel> {

private static async createView(title: string): Promise<vscode.WebviewPanel> {
const localResourceRoot = vscode.Uri.file(
path.join(getExtensionPath(), 'out', 'serverless-function', 'app'),
);

let panel: vscode.WebviewPanel = vscode.window.createWebviewPanel(
const panel: vscode.WebviewPanel = vscode.window.createWebviewPanel(
'serverlessFunctionView',
title,
vscode.ViewColumn.One,
Expand All @@ -100,17 +79,14 @@ export default class ServerlessFunctionViewLoader {
panel.iconPath = vscode.Uri.file(
path.join(getExtensionPath(), 'images/context/cluster-node.png'),
);
panel.webview.html = await loadWebviewHtml(
'serverless-function',
panel,
panel.webview.html = await loadWebviewHtml('serverless-function', panel);
const messageDisposable = panel.webview.onDidReceiveMessage((e) =>
ServerlessFunctionViewLoader.messageListener(panel, e),
);
panel.webview.onDidReceiveMessage((e) => ServerlessFunctionViewLoader.messageListener(panel, e));

panel.onDidDispose(() => {
if (ServerlessFunctionViewLoader.invokePanelMap.has(title)) {
ServerlessFunctionViewLoader.invokePanelMap.delete(title);
}
panel = undefined;
messageDisposable.dispose();
this.panel = undefined;
});

return Promise.resolve(panel);
Expand All @@ -122,41 +98,16 @@ export default class ServerlessFunctionViewLoader {
const functionName = event.name;
const functionPath: vscode.Uri = event.folderPath;
switch (eventName) {
case 'init' : {
if (ServerlessFunctionViewLoader.currentlyInvoke) {
const getEnvFuncId = crypto.randomUUID();
ServerlessFunctionViewLoader.invokePanelMap.set(panel.title, panel);
const yamlContent = await Utils.getFuncYamlContent(ServerlessFunctionViewLoader.folderURI.fsPath);
let template: string, runtime: string, basicTemplates: string[] = ['cloudevent', 'http'];
if (yamlContent) {
template = yamlContent.invoke;
runtime = yamlContent.runtime;
basicTemplates = template ? ServerlessFunctionViewLoader.templates[runtime] : basicTemplates;
}
void panel.webview.postMessage({
action: 'invoke',
instance: ServerlessFunctionViewLoader.status,
name: panel.title.substring(0, panel.title.indexOf('-')).trim(),
id: getEnvFuncId,
uri: ServerlessFunctionViewLoader.folderURI,
runtime,
template,
basicTemplates,
url: ServerlessFunctionViewLoader.url
});
} else {
void panel.webview.postMessage({
action: 'create',
basicTemplates: ServerlessFunctionViewLoader.templates
});
}
case 'init': {
void panel.webview.postMessage({
action: 'create',
basicTemplates: ServerlessFunctionViewLoader.templates,
});
break;
}
case 'validateName': {
const flag = validateName(functionName);
const defaultImages = !flag
? getDefaultImages(functionName)
: [];
const defaultImages = !flag ? getDefaultImages(functionName) : [];
void panel?.webview.postMessage({
action: eventName,
error: !flag ? false : true,
Expand Down Expand Up @@ -199,8 +150,14 @@ export default class ServerlessFunctionViewLoader {
await Progress.execFunctionWithProgress(
`Creating function '${functionName}'`,
async () => {
response = await ServerlessFunctionViewLoader.createFunction(event.language, event.template, selctedFolder.fsPath, event.selectedImage);
});
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}`,
Expand Down Expand Up @@ -232,7 +189,7 @@ export default class ServerlessFunctionViewLoader {
default:
break;
}
}
};

static async createFunction(
language: string,
Expand Down

0 comments on commit 9d91c17

Please sign in to comment.