Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
larshp committed Oct 26, 2023
1 parent 5f5898f commit 95f5767
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 31 deletions.
1 change: 0 additions & 1 deletion src/web/abapgit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export class abapGit {
}

const xml = new TextDecoder().decode(result);
console.dir(xml);
const match = xml.match(/<STARTING_FOLDER>(.*)<\/STARTING_FOLDER>/);
if (match) {
const found = match[1];
Expand Down
108 changes: 78 additions & 30 deletions src/web/artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,78 @@ export interface AnyArtifact {

/////////////////////////

function findTopFolder(filenames: vscode.Uri[]): string | undefined {
const folders = new Set<string>();
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<string>();
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<AnyArtifact[]> {
const ret: AnyArtifact[] = [];

Expand All @@ -20,38 +92,14 @@ export async function findArtifacts(): Promise<AnyArtifact[]> {

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;
Expand Down

0 comments on commit 95f5767

Please sign in to comment.