From 95f5767805e2135618117d72be92f04e3e87a223 Mon Sep 17 00:00:00 2001 From: Lars Hvam Date: Thu, 26 Oct 2023 08:50:49 +0200 Subject: [PATCH] refactor --- src/web/abapgit.ts | 1 - src/web/artifacts.ts | 108 +++++++++++++++++++++++++++++++------------ 2 files changed, 78 insertions(+), 31 deletions(-) diff --git a/src/web/abapgit.ts b/src/web/abapgit.ts index f1ea947..a868222 100644 --- a/src/web/abapgit.ts +++ b/src/web/abapgit.ts @@ -15,7 +15,6 @@ export class abapGit { } const xml = new TextDecoder().decode(result); - console.dir(xml); const match = xml.match(/(.*)<\/STARTING_FOLDER>/); if (match) { const found = match[1]; diff --git a/src/web/artifacts.ts b/src/web/artifacts.ts index 7e21bd3..7677456 100644 --- a/src/web/artifacts.ts +++ b/src/web/artifacts.ts @@ -12,6 +12,78 @@ export interface AnyArtifact { ///////////////////////// +function findTopFolder(filenames: vscode.Uri[]): string | undefined { + const folders = new Set(); + for (const filename of filenames) { + folders.add(Utils.dirname(filename).toString()); + } + const list = Array.from(folders).sort(); + return list[0]; +} + +function findSubFolders(path: string, filenames: vscode.Uri[]): string[] { + const folders = new Set(); + for (const filename of filenames) { + folders.add(Utils.dirname(filename).toString()); + } + const list = Array.from(folders).filter(f => f.replace(path, "").split("/").length === 2); + return list.sort(); +} + +function buildFolder(path: string, filenames: vscode.Uri[]): AnyArtifact { + const sub: AnyArtifact[] = []; + + const subFolders = findSubFolders(path, filenames); + for (const subFolder of subFolders) { + sub.push(buildFolder(subFolder, filenames)); + } + + for (const filename of filenames) { + const dirname = Utils.dirname(filename).toString(); + if (dirname !== path) { + continue; + } + + const basename = Utils.basename(filename); + if (basename === "package.devc.xml") { + continue; + } + + const split = basename.split("."); + if (split.length < 3) { + // then its not a abapGit or AFF file + continue; + } + + const type = split[1].toUpperCase(); + const name = split[0].toUpperCase(); + const found = sub.find((r) => r.name === name && r.description === type); + if (found) { + found.sub.push({ + name: Utils.basename(filename), + description: "", + sub: [], + file: filename, + }); + } else { + sub.push({ + name: name, + description: type, + file: filename, + sub: [], + }); + } + } + + const parsed = vscode.Uri.parse(path); + return { + name: Utils.basename(parsed), + description: "", + file: parsed, + sub: sub, + }; +} + export async function findArtifacts(): Promise { const ret: AnyArtifact[] = []; @@ -20,38 +92,14 @@ export async function findArtifacts(): Promise { const startingPattern = await abapGit.findStartingFolderPattern(folder.uri); const pattern = new vscode.RelativePattern(folder, startingPattern); + const filenames = await vscode.workspace.findFiles(pattern); - for (const filename of filenames) { - const basename = Utils.basename(filename); - if (basename === "package.devc.xml") { - continue; - } - - const split = basename.split("."); - if (split.length < 3) { - // then its not a abapGit or AFF file - continue; - } - - const type = split[1].toUpperCase(); - const name = split[0].toUpperCase(); - const found = ret.find((r) => r.name === name && r.description === type); - if (found) { - found.sub.push({ - name: Utils.basename(filename), - description: "", - sub: [], - file: filename, - }); - } else { - ret.push({ - name: name, - description: type, - file: filename, - sub: [], - }); - } + + const top = findTopFolder(filenames); + if (top === undefined) { + return []; } + ret.push(buildFolder(top, filenames)); } return ret;