From 8eaa852edcd27fa953ff4dc4c950b08b74ab0fee Mon Sep 17 00:00:00 2001 From: David Feldhoff Date: Sat, 31 Oct 2020 12:12:38 +0100 Subject: [PATCH] Add feature to src folder of settings #61 --- .../src/App logic/Utils/config.ts | 5 +++- .../App logic/Utils/elementInsertionUtils.ts | 24 ++++++++++++++----- .../src/App logic/Utils/stringUtils.ts | 19 +++++++++++++++ .../src/App logic/Utils/testMethodUtils.ts | 6 ++--- .../src/App logic/Utils/workspaceUtils.ts | 20 ++++++++++++++++ 5 files changed, 63 insertions(+), 11 deletions(-) create mode 100644 vscode-extension/src/App logic/Utils/stringUtils.ts create mode 100644 vscode-extension/src/App logic/Utils/workspaceUtils.ts diff --git a/vscode-extension/src/App logic/Utils/config.ts b/vscode-extension/src/App logic/Utils/config.ts index 1103d0b..b9b4fd5 100644 --- a/vscode-extension/src/App logic/Utils/config.ts +++ b/vscode-extension/src/App logic/Utils/config.ts @@ -24,9 +24,12 @@ export class Config { static getPrefixThen(uri?: Uri): string { return this.getConfig(uri).get('prefixThen', ''); } - static getRemovalMode(uri?: Uri): string{ + static getRemovalMode(uri?: Uri): string { return this.getConfig(uri).get('removalMode', ''); } + static getTestSrcFolder(): string | undefined { + return this.getConfig().get('testDirectory'); + } private static getConfig(uri?: Uri): WorkspaceConfiguration { return workspace.getConfiguration(this.app, uri); } diff --git a/vscode-extension/src/App logic/Utils/elementInsertionUtils.ts b/vscode-extension/src/App logic/Utils/elementInsertionUtils.ts index 431da61..cb09cd2 100644 --- a/vscode-extension/src/App logic/Utils/elementInsertionUtils.ts +++ b/vscode-extension/src/App logic/Utils/elementInsertionUtils.ts @@ -1,30 +1,42 @@ import { writeFileSync } from "fs-extra"; -import { Position, Range, TextDocument, workspace, WorkspaceEdit } from "vscode"; +import { join } from "path"; +import { Position, Range, TextDocument, workspace, WorkspaceEdit, WorkspaceFolder } from "vscode"; import { MessageUpdate, TypeChanged } from "../../typings/types"; import { ALFullSyntaxTreeNodeExt } from "../AL Code Outline Ext/alFullSyntaxTreeNodeExt"; import { FullSyntaxTreeNodeKind } from "../AL Code Outline Ext/fullSyntaxTreeNodeKind"; import { TextRangeExt } from "../AL Code Outline Ext/textRangeExt"; import { ALFullSyntaxTreeNode } from "../AL Code Outline/alFullSyntaxTreeNode"; import { SyntaxTree } from "../AL Code Outline/syntaxTree"; +import { Config } from "./config"; import { ElementUtils } from "./elementUtils"; import { RangeUtils } from "./rangeUtils"; +import { StringUtils } from "./stringUtils"; import { TestCodeunitUtils } from "./testCodeunitUtils"; import { TestMethodUtils } from "./testMethodUtils"; +import { WorkspaceUtils } from "./workspaceUtils"; export class ElementInsertionUtils { public static async addSomethingNewToCode(msg: MessageUpdate): Promise { if (msg.Type == TypeChanged.Feature) { - writeFileSync(msg.FsPath, TestCodeunitUtils.getDefaultTestCodeunit(msg.NewValue), { encoding: 'utf8' }); - return true; + return ElementInsertionUtils.addNewFeatureToCode(msg); } else if (msg.Type == TypeChanged.ScenarioName) { - //TODO: Get the workspacefolder using the msg.Project, search for the feature and add the scenario there. - //msg.Id will contain the next ID of the Feature return await ElementInsertionUtils.addNewScenarioToCode(msg); } else { return await ElementInsertionUtils.addNewElementToCode(msg); } } + + private static addNewFeatureToCode(msg: MessageUpdate): boolean { + let srcFolder: string | undefined = Config.getTestSrcFolder(); + if (!srcFolder) + throw new Error('Please specify the source folder for your tests in the settings.'); + let fileName: string = new StringUtils(msg.NewValue).titleCase().removeSpecialChars().value() + '.al'; + let fsPath: string = WorkspaceUtils.getFullFsPathOfRelativePath(srcFolder, fileName); + writeFileSync(fsPath, TestCodeunitUtils.getDefaultTestCodeunit(msg.NewValue), { encoding: 'utf8' }); + return true; + } + static async addNewElementToCode(msg: MessageUpdate): Promise { if (msg.FsPath == '' && msg.Feature !== '' && msg.Project !== '') msg.FsPath = await ElementUtils.getFSPathOfFeature(msg.Project, msg.Feature); @@ -130,7 +142,7 @@ export class ElementInsertionUtils { } return { addEmptyLine: true, endPositionOfPreviousLine: scenarioRange.end }; } - + public static addElement(edit: WorkspaceEdit, document: TextDocument, positionToInsert: Position, type: TypeChanged, elementValue: string) { let textToAdd: string = '\r\n'; textToAdd += ' ' + ElementUtils.getElementComment(type, elementValue); diff --git a/vscode-extension/src/App logic/Utils/stringUtils.ts b/vscode-extension/src/App logic/Utils/stringUtils.ts new file mode 100644 index 0000000..9a98d46 --- /dev/null +++ b/vscode-extension/src/App logic/Utils/stringUtils.ts @@ -0,0 +1,19 @@ +export class StringUtils { + text: string; + constructor(text: string){ + this.text = text; + } + public titleCase(): StringUtils { + this.text = this.text.replace(/\w+/g, function (txt) { + return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); + }); + return this; + } + public removeSpecialChars(): StringUtils { + this.text = this.text.replace(/[^\w]/g, ''); + return this; + } + public value(){ + return this.text; + } +} \ No newline at end of file diff --git a/vscode-extension/src/App logic/Utils/testMethodUtils.ts b/vscode-extension/src/App logic/Utils/testMethodUtils.ts index 518aa59..cb0eb37 100644 --- a/vscode-extension/src/App logic/Utils/testMethodUtils.ts +++ b/vscode-extension/src/App logic/Utils/testMethodUtils.ts @@ -7,13 +7,11 @@ import { ALFullSyntaxTreeNode } from "../AL Code Outline/alFullSyntaxTreeNode"; import { SyntaxTree } from "../AL Code Outline/syntaxTree"; import { Config } from "./config"; import { RangeUtils } from "./rangeUtils"; +import { StringUtils } from "./stringUtils"; export class TestMethodUtils { public static getProcedureName(type: TypeChanged, name: string): string { - let nameTitleCase: string = name.replace(/\w+/g, function (txt) { - return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); - }); - nameTitleCase = nameTitleCase.replace(/[^\w]/g, ''); + let nameTitleCase: string = new StringUtils(name).titleCase().removeSpecialChars().value(); let prefix: string = ''; let uri: Uri | undefined = window.activeTextEditor?.document.uri; diff --git a/vscode-extension/src/App logic/Utils/workspaceUtils.ts b/vscode-extension/src/App logic/Utils/workspaceUtils.ts new file mode 100644 index 0000000..759af50 --- /dev/null +++ b/vscode-extension/src/App logic/Utils/workspaceUtils.ts @@ -0,0 +1,20 @@ +import { join } from "path"; +import { workspace, WorkspaceFolder } from "vscode"; + +export class WorkspaceUtils { + public static getFullFsPathOfRelativePath(relativeFolder: string, fileName: string) { + let relativeFsPath: string = join(relativeFolder, fileName); + let fullFsPath: string; + if (!workspace.workspaceFolders) + throw new Error('No workspacefolder opened.'); + if (workspace.workspaceFile) { + fullFsPath = join(workspace.workspaceFile.fsPath, relativeFsPath); + } else if (workspace.workspaceFolders.length == 1) { + let workspaceFolder: WorkspaceFolder = workspace.workspaceFolders[0]; + fullFsPath = join(workspaceFolder.uri.fsPath, relativeFsPath); + } else { + throw new Error('Expected to find the workspacefolder.') + } + return fullFsPath; + } +} \ No newline at end of file