diff --git a/README.md b/README.md index 44c67d4f..b47b8587 100644 --- a/README.md +++ b/README.md @@ -221,7 +221,7 @@ tableextension 50100 "Just Some Table Extension" extends Customer //18 * `DisableWaldoSnippets`: Disables the CRS snippets that come with this extension. When you change the setting, you need to restart VSCode twice. Once for disabling the snippets on activation (at that time, the snippets are still loaded). And the second time to actually not load the snippets anymore. * `SkipWarningMessageOnRenameAll`: Skips the Warning when renaming all files which can disturb custom VS tasks. * `RenameWithGit`: Use 'git mv' to rename a file. This keeps history of the file, but stages the rename, which you should commit separately. **The feature is still in preview-mode, therefore the default value is 'false'** - +* `ReorganizeByNamespace`: This is a feature that allows for the automatic reorganization of files by creating folder structures based on the namespaces defined within the files. **The feature is still in preview-mode, therefore the default value is 'false'** ## Skip String manipulation You can skip string manipulation by adding comments to your code: diff --git a/package.json b/package.json index 97516cef..e7b7e408 100644 --- a/package.json +++ b/package.json @@ -267,6 +267,12 @@ "description": "Use 'git mv' to rename a file. This keeps history of the file, but stages the rename, which you should commit separately. The feature is still in preview-mode, therefore the default value is 'false'", "scope": "resource" }, + "CRS.ReorganizeByNamespace": { + "type": "boolean", + "default": false, + "description": "This is a feature that allows for the automatic reorganization of files by creating folder structures based on the namespaces defined within the files. The feature is still in preview-mode, therefore the default value is 'false'", + "scope": "resource" + }, "CRS.SearchObjectNamesRegexPattern": { "type": "string", "default": "^\\w+ (\\d* )?\"*", diff --git a/src/NAVObject.ts b/src/NAVObject.ts index ffbc26f2..775e674b 100644 --- a/src/NAVObject.ts +++ b/src/NAVObject.ts @@ -8,6 +8,7 @@ export class NAVObject { public objectType: string; public objectId: string; public objectName: string; + public objectNamespace: string; public objectActions: NAVObjectAction[] = new Array(); public tableFields: NAVTableField[] = new Array(); public pageFields: NAVPageField[] = new Array(); @@ -155,6 +156,7 @@ export class NAVObject { this.objectType = ''; this.objectId = ''; this.objectName = ''; + this.objectNamespace = ''; this.extendedObjectName = ''; this.extendedObjectId = ''; var ObjectNamePattern = '"[^"]*"' // All characters except " @@ -273,7 +275,11 @@ export class NAVObject { return null } } - + var patternObject = new RegExp('namespace\\s+([A-Za-z0-9_.]+?)\\s*;', "i"); + var match = this.NAVObjectText.match(patternObject); + if (match && match[1]) { + this.objectNamespace = match[1]; + } this.objectType = this.objectType.trim().toString(); this.objectId = this.objectId.trim().toString(); this.objectName = this.objectName.trim().toString().replace(/["]/g, ''); diff --git a/src/Settings.ts b/src/Settings.ts index 656ac9d8..64bdf4b9 100644 --- a/src/Settings.ts +++ b/src/Settings.ts @@ -34,6 +34,7 @@ export class Settings { static readonly DisableDefaultAlSnippets = 'DisableDefaultAlSnippets'; static readonly DisableCRSSnippets = 'DisableCRSSnippets'; static readonly RenameWithGit = 'RenameWithGit'; + static readonly ReorganizeByNamespace = 'ReorganizeByNamespace'; static readonly Browser = 'browser'; static readonly Incognito = 'incognito'; static readonly packageCachePath = 'packageCachePath'; @@ -100,6 +101,7 @@ export class Settings { this.SettingCollection[this.DisableCRSSnippets] = this.getSetting(this.DisableCRSSnippets); this.SettingCollection[this.PublicWebBaseUrl] = this.getSetting(this.PublicWebBaseUrl); this.SettingCollection[this.RenameWithGit] = this.getSetting(this.RenameWithGit); + this.SettingCollection[this.ReorganizeByNamespace] = this.getSetting(this.ReorganizeByNamespace); this.SettingCollection[this.SearchObjectNamesRegexPattern] = this.getSetting(this.SearchObjectNamesRegexPattern); this.SettingCollection[this.DependencyGraphIncludeTestApps] = this.getSetting(this.DependencyGraphIncludeTestApps); this.SettingCollection[this.DependencyGraphExcludeAppNames] = this.getSetting(this.DependencyGraphExcludeAppNames); diff --git a/src/WorkspaceFiles.ts b/src/WorkspaceFiles.ts index c710227b..40087869 100644 --- a/src/WorkspaceFiles.ts +++ b/src/WorkspaceFiles.ts @@ -88,7 +88,7 @@ export class WorkspaceFiles { if (navObject.objectFileName && navObject.objectFileName != '' && fixedname && fixedname != '') { let objectFolder = path.join(vscode.workspace.getWorkspaceFolder(fileName).uri.fsPath, this.getDestinationFolder(navObject, settings)); - let objectTypeFolder = path.join(objectFolder, this.getObjectTypeFolder(navObject)); + let objectTypeFolder = path.join(objectFolder, this.getObjectTypeFolder(navObject, settings));//Boe let objectSubFolder = path.join(objectTypeFolder, this.getObjectSubFolder(navObject)); let destinationFileName = path.join(objectSubFolder, fixedname); @@ -97,9 +97,10 @@ export class WorkspaceFiles { return fileName.fsPath; } else { - (!fs.existsSync(objectFolder)) ? fs.mkdirSync(objectFolder) : ''; - (!fs.existsSync(objectTypeFolder)) ? fs.mkdirSync(objectTypeFolder) : ''; - (!fs.existsSync(objectSubFolder)) ? fs.mkdirSync(objectSubFolder) : ''; + //(!fs.existsSync(objectFolder)) ? fs.mkdirSync(objectFolder) : ''; + //(!fs.existsSync(objectTypeFolder)) ? fs.mkdirSync(objectTypeFolder) : ''; + //(!fs.existsSync(objectSubFolder)) ? fs.mkdirSync(objectSubFolder) : ''; + this.createDirectoryIfNotExists(objectSubFolder); withGit = withGit ? withGit : (git.isGitRepositorySync() && settings[Settings.RenameWithGit]) this.DoRenameFile(fileName, destinationFileName, withGit) @@ -308,13 +309,19 @@ export class WorkspaceFiles { return mySettings[Settings.AlSubFolderName] } - static getObjectTypeFolder(navObject: NAVObject): string { + static getObjectTypeFolder(navObject: NAVObject, mySettings: any): string { if (navObject.objectCodeunitSubType) { if (navObject.objectCodeunitSubType.toLowerCase() == 'test') { return '' } } + if (mySettings[Settings.ReorganizeByNamespace]) + { + let directoryPath = path.join(...navObject.objectNamespace.split(".")) + return directoryPath + } + return navObject.objectType } @@ -326,6 +333,20 @@ export class WorkspaceFiles { return ""; } + static createDirectoryIfNotExists(dir) { + const segments = dir.split(path.sep); + let currentPath = segments[0]; + + for (let i = 1; i < segments.length; i++) { + if (segments[i]) { + currentPath = path.join(currentPath, segments[i]); + if (!fs.existsSync(currentPath)) { + fs.mkdirSync(currentPath); + } + } + } + } + static async CreateGraphVizDependencyGraph() { crsOutput.showOutput(`Creating dependency graph (GraphViz) from apps in this workspace`, true); diff --git a/src/test/suite/NAVTestObjectLibrary.ts b/src/test/suite/NAVTestObjectLibrary.ts index e0ca45bb..323a50ed 100644 --- a/src/test/suite/NAVTestObjectLibrary.ts +++ b/src/test/suite/NAVTestObjectLibrary.ts @@ -113,6 +113,27 @@ export function getNormalCodeunitWithLongName(): NAVTestObject { return object; } +export function getNormalCodeunitWithNamespace(): NAVTestObject { + let object = new NAVTestObject; + + object.ObjectFileName = 'Cod50100.justAName.al' + object.ObjectText = ` + namespace spycoclown.test; + using Microsoft.Inventory.Item; + codeunit 50100 "Test Overload" + { + [EventSubscriber(ObjectType::Codeunit, Codeunit::LogInManagement, 'OnAfterLogInStart', '', false, false)] + local procedure TestOverLoad() + var + Item: Record Item; + begin + item.CalculateClassification(true, 'namespace'); + end; + } + ` + return object; +} + export function getTestCodeunit(): NAVTestObject { let object = new NAVTestObject; diff --git a/src/test/suite/WorkspaceFiles.test.ts b/src/test/suite/WorkspaceFiles.test.ts index d256b8c8..6df50603 100644 --- a/src/test/suite/WorkspaceFiles.test.ts +++ b/src/test/suite/WorkspaceFiles.test.ts @@ -34,7 +34,7 @@ suite("WorkspaceFiles Tests", () => { let navTestObject = NAVTestObjectLibrary.getTestCodeunit(); let navObject = new NAVObject(navTestObject.ObjectText, testSettings, navTestObject.ObjectFileName); - let foldersuggestion = WorkspaceFiles.getObjectTypeFolder(navObject); + let foldersuggestion = WorkspaceFiles.getObjectTypeFolder(navObject,testSettings); assert.strictEqual(foldersuggestion.toLowerCase(), ''); }) @@ -44,9 +44,20 @@ suite("WorkspaceFiles Tests", () => { let navTestObject = NAVTestObjectLibrary.getNormalCodeunitWithLongName(); let navObject = new NAVObject(navTestObject.ObjectText, testSettings, navTestObject.ObjectFileName); - let foldersuggestion = WorkspaceFiles.getObjectTypeFolder(navObject); + let foldersuggestion = WorkspaceFiles.getObjectTypeFolder(navObject,testSettings); assert.strictEqual(foldersuggestion.toLowerCase().toString(), navObject.objectType.toString()); assert.notStrictEqual(foldersuggestion.toLowerCase().toString(), ''); }) + test("getObjectTypeFolder - return namespace", () => { + let testSettings = Settings.GetConfigSettings(null); + testSettings[Settings.ReorganizeByNamespace] = true; + let navTestObject = NAVTestObjectLibrary.getNormalCodeunitWithNamespace(); + let navObject = new NAVObject(navTestObject.ObjectText, testSettings, navTestObject.ObjectFileName); + + let foldersuggestion = WorkspaceFiles.getObjectTypeFolder(navObject,testSettings); + + assert.notStrictEqual(foldersuggestion.toLowerCase().toString(), navObject.objectNamespace.toLowerCase().toString()); + assert.strictEqual(foldersuggestion.toLowerCase().toString(), 'spycoclown\\test'); + }) }) \ No newline at end of file