Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 29 additions & 14 deletions backend/.nycrc.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
{
"require": ["ts-node/register/transpile-only"],
"include": ["src/**/*.ts"],
"exclude": ["src/logger/*.ts", "src/panels/*.ts", "src/webSocketServer/*.ts", "src/output-channel-log.ts", "src/youi-adapter.ts"],
"reporter": ["lcov", "text"],
"extension": [".ts"],
"all": true,
"temp-dir": "./reports/.nyc_output",
"report-dir": "./reports/coverage",
"check-coverage": true,
"branches": 98,
"lines": 99,
"functions": 97,
"statements": 99
}
"require": [
"ts-node/register/transpile-only"
],
"include": [
"src/**/*.ts"
],
"exclude": [
"src/logger/*.ts",
"src/panels/*.ts",
"src/webSocketServer/*.ts",
"src/output-channel-log.ts",
"src/youi-adapter.ts"
],
"reporter": [
"lcov",
"text"
],
"extension": [
".ts"
],
"all": true,
"temp-dir": "./reports/.nyc_output",
"report-dir": "./reports/coverage",
"check-coverage": true,
"branches": 98,
"lines": 99,
"functions": 97,
"statements": 99
}
4 changes: 0 additions & 4 deletions backend/mocha.opts

This file was deleted.

36 changes: 18 additions & 18 deletions backend/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "code-snippet",
"version": "0.0.22",
"displayName": "Code Snippet",
"version": "0.0.23",
"displayName": "Code Snippet",
"publisher": "SAPOSS",
"author": {
"name": "SAP SE"
Expand Down Expand Up @@ -110,29 +110,29 @@
"compile": "tsc -p ./",
"watch": "tsc -watch -p ./",
"package": "npm run webpack && vsce package .",
"test": "nyc mocha -p tsconfig.json --opts ./mocha.opts",
"test": "nyc mocha -p tsconfig.json --recursive tests/**/*.spec.ts",
"ws:run": "node ./out/src/webSocketServer/index.js",
"lint": "eslint . --ext .ts,.tsx --cache",
"lint:fix": "eslint . --ext .ts,.tsx --cache --fix"
},
"dependencies": {
"@sap-devx/webview-rpc": "^0.2.2",
"@vscode-logging/logger": "1.1.0",
"datauri": "^2.0.0",
"fs-extra": "^8.1.0",
"lodash": "^4.17.15",
"strip-ansi": "^6.0.0",
"ws": "^7.2.5",
"@sap-devx/webview-rpc": "0.2.3",
"@vscode-logging/logger": "1.2.1",
"datauri": "3.0.0",
"fs-extra": "9.0.1",
"lodash": "4.17.20",
"strip-ansi": "6.0.0",
"ws": "7.4.0",
"yeoman-environment": "2.10.3"
},
"devDependencies": {
"@types/chai": "^4.2.9",
"@types/fs-extra": "^8.1.0",
"@types/chai": "^4.2.14",
"@types/fs-extra": "^9.0.4",
"@types/inquirer": "^6.5.0",
"@types/lodash": "^4.14.150",
"@types/mocha": "^5.2.7",
"@types/lodash": "^4.14.165",
"@types/mocha": "^7.0.0",
"@types/node": "^10.17.21",
"@types/sinon": "^7.5.0",
"@types/sinon": "^9.0.9",
"@types/ws": "^6.0.3",
"@types/yeoman-environment": "2.10.2",
"@typescript-eslint/parser": "^2.30.0",
Expand All @@ -143,9 +143,9 @@
"typescript": "^3.9.7",
"chai": "^4.2.0",
"copy-webpack-plugin": "^5.0.5",
"mocha": "^6.2.2",
"nyc": "^14.1.1",
"sinon": "^7.5.0",
"mocha": "^7.0.0",
"nyc": "^15.1.0",
"sinon": "^9.2.1",
"ts-loader": "^6.2.1",
"ts-node": "^8.9.1",
"string-replace-loader": "^2.1.1",
Expand Down
16 changes: 12 additions & 4 deletions backend/src/code-snippet.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as _ from "lodash";
import * as Environment from "yeoman-environment";
import * as inquirer from "inquirer";
import { AppLog } from "./app-log";
import { AppEvents } from "./app-events";
Expand All @@ -8,6 +7,7 @@ import Generator = require("yeoman-generator");
import { IChildLogger } from "@vscode-logging/logger";
import TerminalAdapter = require("yeoman-environment/lib/adapter");


export class CodeSnippet {

private static funcReplacer(key: any, value: any) {
Expand All @@ -24,7 +24,6 @@ export class CodeSnippet {
private currentQuestions: TerminalAdapter.Questions<any>;
private snippetName: string;
private readonly customQuestionEventHandlers: Map<string, Map<string, Function>>;
private errorThrown = false;

constructor(rpc: IRpc, appEvents: AppEvents, outputChannel: AppLog, logger: IChildLogger, uiOptions: any) {
this.rpc = rpc;
Expand All @@ -50,7 +49,17 @@ export class CodeSnippet {
}

private async getState() {
return this.uiOptions;
let state = _.omit(this.uiOptions, ["snippet"]);
try {
// valiation for rpc
JSON.stringify(state);
} catch (error) {
// save stateError and remove contributorInfo.context
state = _.omit(state, ["contributorInfo.context"]);
_.set(state, "stateError", true);
}

return state;
}

public registerCustomQuestionEventHandler(questionType: string, methodName: string, handler: Function): void {
Expand Down Expand Up @@ -165,7 +174,6 @@ export class CodeSnippet {
}

private async onFailure(snippetrName: string, error: any) {
this.errorThrown = true;
const messagePrefix = `${snippetrName} snippet failed.`;
const errorMessage: string = await this.logError(error, messagePrefix);
this.appEvents.doSnippeDone(false, errorMessage);
Expand Down
34 changes: 22 additions & 12 deletions backend/src/contributors.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,51 @@
import * as vscode from 'vscode';
import * as _ from 'lodash';
import { IChildLogger } from '@vscode-logging/logger';
import { getClassLogger } from './logger/logger-wrapper';


export class Contributors {
public static async getSnippet(contributerInfo: any) {
const contributorId = _.get(contributerInfo, "contributorId");
const extension = Contributors.getContributorExtension(contributorId);
private readonly logger: IChildLogger;

constructor() {
this.logger = getClassLogger("Contributors");
}

public async getSnippet(contributorInfo: any) {
const contributorId = _.get(contributorInfo, "contributorId");
const extension = this.getContributorExtension(contributorId);
if (extension) {
try {
const api = await this.getApiPromise(extension as vscode.Extension<any>);
const snippetContext = _.get(contributerInfo, "context");
const snippetContext = _.get(contributorInfo, "context");
const snippets = api.getCodeSnippets(snippetContext);
const snippetName = _.get(contributerInfo, "snippetName");
const snippetName = _.get(contributorInfo, "snippetName");
return snippets.get(snippetName);
} catch (error) {
const errorMessage = _.get(error, "stack", _.get(error, "message", error));
console.error(errorMessage);
// TODO: Add Logger.error
this.logger.error(`Could not get '${contributorId}' snippet`, errorMessage);
}
}
}

private static getApiPromise(extension: vscode.Extension<any>) {
return (extension.isActive ? extension.exports : extension.activate());
private getApiPromise(extension: vscode.Extension<any>): Thenable<any> {
return (extension.isActive ? Promise.resolve(extension.exports) : extension.activate());
}

private static getContributorExtension(contributorId: string) {
private getContributorExtension(contributorId: string) {
return _.find(vscode.extensions.all, (extension: vscode.Extension<any>) => {
const extensionDependencies: string[] = _.get(extension, "packageJSON.extensionDependencies");
if (_.includes(extensionDependencies, "saposs.code-snippet")) {
if (contributorId === Contributors.getExtensionId(extension)) {
if (contributorId === this.getExtensionId(extension)) {
return extension;
}
}

this.logger.warn(`Extension '${contributorId}' could not be found.`);
});
}

private static getExtensionId(extension: vscode.Extension<any>) {
private getExtensionId(extension: vscode.Extension<any>) {
const extensionName: string = _.get(extension, "packageJSON.name");
const extensionPublisher: string = _.get(extension, "packageJSON.publisher");
return `${extensionPublisher}.${extensionName}`;
Expand Down
4 changes: 2 additions & 2 deletions backend/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ function registerAndSubscribeCommand(cId: string, cAction: any) {

function registerWebviewPanelSerializer(abstractPanel: AbstractWebviewPanel) {
vscode.window.registerWebviewPanelSerializer(abstractPanel.viewType, {
async deserializeWebviewPanel(webviewPanel: vscode.WebviewPanel, state?: any) {
abstractPanel.setWebviewPanel(webviewPanel, state);
async deserializeWebviewPanel(webViewPanel: vscode.WebviewPanel, state?: any) {
abstractPanel.setWebviewPanel(webViewPanel, state);
}
});
}
Expand Down
4 changes: 2 additions & 2 deletions backend/src/panels/AbstractWebviewPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as path from 'path';
import * as _ from 'lodash';
import * as fsextra from 'fs-extra';
import { IChildLogger } from '@vscode-logging/logger';
import { getLogger } from '../logger/logger-wrapper';
import { getClassLogger } from '../logger/logger-wrapper';


export abstract class AbstractWebviewPanel {
Expand All @@ -24,7 +24,7 @@ export abstract class AbstractWebviewPanel {
this.extensionPath = context.extensionPath;
this.mediaPath = path.join(context.extensionPath, "dist", "media");
this.htmlFileName = "index.html";
this.logger = getLogger();
this.logger = getClassLogger("AbstractWebviewPanel");
this.disposables = [];
}

Expand Down
22 changes: 15 additions & 7 deletions backend/src/panels/CodeSnippetPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,29 @@ import { Contributors } from "../contributors";

export class CodeSnippetPanel extends AbstractWebviewPanel {
public static CODE_SNIPPET = "Code Snippet";

private static channel: vscode.OutputChannel;

public toggleOutput() {
this.outputChannel.showOutput();
}

public setWebviewPanel(webViewPanel: vscode.WebviewPanel, uiOptions?: any) {
super.setWebviewPanel(webViewPanel, uiOptions);
const contributorInfo = _.get(uiOptions, "contributorInfo", uiOptions);

if (_.get(uiOptions, "stateError")) {
this.logger.error("test");
this.logger.error(`'${contributorInfo.contributorId}' snippet state could not be saved. JSON.stringify issue.`);
return webViewPanel.dispose();
}

const contributerInfo = _.get(uiOptions, "contributerInfo", uiOptions);
Contributors.getSnippet(contributerInfo).then(snippet => {
this.contributors.getSnippet(contributorInfo).then(snippet => {
if (_.isNil(snippet)) {
this.webViewPanel.dispose();
return vscode.window.showErrorMessage("Can not find snippet.");
this.logger.error(`'${contributorInfo.contributorId}' snippet could not be found.`);
return this.webViewPanel.dispose();
}

super.setWebviewPanel(webViewPanel, uiOptions);

this.messages = _.assign({}, backendMessages, snippet.getMessages());
const rpc = new RpcExtension(this.webViewPanel.webview);
this.outputChannel = new OutputChannelLog(this.messages.channelName);
Expand All @@ -40,7 +46,7 @@ export class CodeSnippetPanel extends AbstractWebviewPanel {
vscodeEvents,
this.outputChannel,
this.logger,
{ messages: this.messages, snippet, contributerInfo});
{ messages: this.messages, snippet, contributorInfo});
this.codeSnippet.registerCustomQuestionEventHandler("file-browser", "getFilePath", this.showOpenFileDialog.bind(this));
this.codeSnippet.registerCustomQuestionEventHandler("folder-browser", "getPath", this.showOpenFolderDialog.bind(this));

Expand All @@ -59,12 +65,14 @@ export class CodeSnippetPanel extends AbstractWebviewPanel {
private codeSnippet: CodeSnippet;
private messages: any;
private outputChannel: AppLog;
private readonly contributors: Contributors;

public constructor(context: vscode.ExtensionContext) {
super(context);
this.viewType = "codeSnippet";
this.viewTitle = CodeSnippetPanel.CODE_SNIPPET;
this.focusedKey = "codeSnippet.Focused";
this.contributors = new Contributors();
}

private async showOpenFileDialog(currentPath: string): Promise<string> {
Expand Down
Loading