Skip to content

Commit

Permalink
Merge pull request #53 from SAP/gen_filter
Browse files Browse the repository at this point in the history
added generator filter logic
  • Loading branch information
rimasirich authored Dec 17, 2019
2 parents c44f3db + d6bc5d2 commit 460fe3e
Show file tree
Hide file tree
Showing 12 changed files with 455 additions and 109 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
{
"type": "node",
"request": "launch",
"name": "Mocha Tests",
"name": "backend unit tests",
"program": "${workspaceFolder}/backend/node_modules/mocha/bin/mocha",
"args": [
"-r",
Expand Down Expand Up @@ -74,7 +74,7 @@
]
},
{
"name": "Test frontend",
"name": "frontend unit tests",
"type": "node",
"request": "launch",
"runtimeArgs": [
Expand Down
9 changes: 4 additions & 5 deletions backend/.nycrc.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
{
"require": ["ts-node/register/transpile-only"],
"include": ["src/**/*.ts"],
"exclude": ["src/run-configuration*", "src/cfView.ts", "src/cfViewCommands.ts", "src/sql-tools.ts"],
"reporter": ["lcov", "text"],
"extension": [".ts"],
"all": true,
"temp-dir": "./reports/.nyc_output",
"report-dir": "./reports/coverage",
"check-coverage": true,
"branches": 0,
"lines": 24,
"functions": 17,
"statements": 25
"branches": 22,
"lines": 41,
"functions": 30,
"statements": 41
}
16 changes: 9 additions & 7 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@
],
"activationEvents": [
"*",
"onCommand:sap.loadYeomanUI"
"onCommand:loadYeomanUI_projects",
"onCommand:loadYeomanUI"
],
"main": "./dist/extension",
"contributes": {
"commands": [
{
"command": "sap.loadYeomanUI",
"title": "Yeoman User Interface"
}
]
"commands": [{
"command": "loadYeomanUI_projects",
"title": "Yeoman UI Project Generators"
}, {
"command": "loadYeomanUI",
"title": "Yeoman UI Generators"
}]
},
"scripts": {
"frontend": "npm run frontend:install && npm run frontend:build && npm run frontend:copy",
Expand Down
1 change: 0 additions & 1 deletion backend/src/datauri.ts

This file was deleted.

41 changes: 27 additions & 14 deletions backend/src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import * as fs from 'fs';
import * as fsextra from 'fs-extra';
import * as _ from 'lodash';
import * as path from 'path';
import * as vscode from 'vscode';
import { YeomanUI } from "./yeomanui";
import {RpcExtension} from '@sap-devx/webview-rpc/out.ext/rpc-extension';
import { YouiLog } from "./youi-log";
import { OutputChannelLog } from './output-channel-log';
import { GeneratorFilter } from './filter';


export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(
vscode.commands.registerCommand('sap.loadYeomanUI', () => {
YeomanUIPanel.createOrShow(context.extensionPath);
})
);
vscode.commands.registerCommand('loadYeomanUI', (filterObj?: any) => {
YeomanUIPanel.createOrShow(context.extensionPath, GeneratorFilter.create(filterObj));
}));

context.subscriptions.push(
vscode.commands.registerCommand('loadYeomanUI_projects', () => {
vscode.commands.executeCommand("loadYeomanUI", {"type": "project"});
}));

if (vscode.window.registerWebviewPanelSerializer) {
// Make sure we register a serializer in activation event
Expand All @@ -33,14 +40,16 @@ export class YeomanUIPanel {
*/
public static readonly viewType = 'yeomanui';
public static currentPanel: YeomanUIPanel | undefined;
public static genFilter: GeneratorFilter;

public static createOrShow(extensionPath: string) {
const column = vscode.window.activeTextEditor
? vscode.window.activeTextEditor.viewColumn
: undefined;
public static createOrShow(extensionPath: string, genFilter?: GeneratorFilter) {
YeomanUIPanel.genFilter = genFilter;

const column = _.get(vscode.window, "activeTextEditor.viewColumn");

// If we already have a panel, show it.
if (YeomanUIPanel.currentPanel) {
YeomanUIPanel.currentPanel.yeomanui.setGenFilter(YeomanUIPanel.genFilter);
YeomanUIPanel.currentPanel.panel.reveal(column);
return;
}
Expand All @@ -55,7 +64,7 @@ export class YeomanUIPanel {
enableScripts: true,

// And restrict the webview to only loading content from our extension's `media` directory.
localResourceRoots: [vscode.Uri.file(path.join(extensionPath, 'dist', 'media'))]
localResourceRoots: [vscode.Uri.file(YeomanUIPanel.getMediaPath(extensionPath))]
}
);

Expand All @@ -79,7 +88,9 @@ export class YeomanUIPanel {
this.extensionPath = extensionPath;
this.rpc = new RpcExtension(this.panel.webview);
const logger: YouiLog = new OutputChannelLog();

this.yeomanui = new YeomanUI(this.rpc, logger);
this.yeomanui.setGenFilter(YeomanUIPanel.genFilter);

// Set the webview's initial html content
this._update();
Expand Down Expand Up @@ -131,14 +142,16 @@ export class YeomanUIPanel {
}
}

private static getMediaPath(extensionPath: string): string {
return path.join(extensionPath, 'dist', 'media');
}

private _update() {
// TODO: don't use sync
let indexHtml: string = fs.readFileSync(path.join(this.extensionPath, 'dist', 'media', 'index.html'), "utf8");
let indexHtml: string = fsextra.readFileSync(path.join(YeomanUIPanel.getMediaPath(this.extensionPath), 'index.html'), "utf8");
if (indexHtml) {
// Local path to main script run in the webview
const scriptPathOnDisk = vscode.Uri.file(
path.join(this.extensionPath, 'dist', 'media', path.sep)
);
const scriptPathOnDisk = vscode.Uri.file(path.join(YeomanUIPanel.getMediaPath(this.extensionPath), path.sep));
const scriptUri = this.panel.webview.asWebviewUri(scriptPathOnDisk);

// TODO: very fragile: assuming double quotes and src is first attribute
Expand Down
44 changes: 41 additions & 3 deletions backend/src/filter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,42 @@
export enum Type {
import * as _ from "lodash";

export enum GeneratorType {
project = "project",
module = "module"
}
module = "module",
all = "all"
}

function getCategories(filterObject?: any): string[] {
const categories: string[] = _.get(filterObject, 'categories', []);
if (_.isArray(categories)) {
const strValues = _.filter(categories, category => {
return _.isString(category);
});
if (_.isEmpty(_.difference(categories, strValues))) {
return categories;
}
}

return [];
}

function getType(filterObject?: any): GeneratorType {
const genType: GeneratorType = _.get(filterObject, "type", GeneratorType.all);
if (_.includes(_.values(GeneratorType), genType)) {
return genType;
}

return GeneratorType.all;
}

export class GeneratorFilter {
constructor(public readonly type: GeneratorType,
public readonly categories: string[]) {}

public static create(filterObject?: any) {
const categories: string[] = getCategories(filterObject);
const type: GeneratorType = getType(filterObject);

return new GeneratorFilter(type, categories);
}
}
Loading

0 comments on commit 460fe3e

Please sign in to comment.