From c184c667fd8fa03fbde0764675b51212ae709909 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20B=C3=B6ckelmann?= Date: Thu, 21 Mar 2024 08:43:28 +0100 Subject: [PATCH] Delete Unreferenced Gamesystems --- app/main.ts | 4 ++ app/storage/FileUtils.js | 10 ++++ app/storage/FileUtils.ts | 11 +++++ app/storage/storing/GamesystemStorage.js | 47 +++++++++++------- app/storage/storing/GamesystemStorage.ts | 49 +++++++++++++------ src/app/app.component.ts | 2 +- .../serializer/GamesystemSerializer.ts | 1 - 7 files changed, 89 insertions(+), 35 deletions(-) diff --git a/app/main.ts b/app/main.ts index 1264ef5..f651a10 100644 --- a/app/main.ts +++ b/app/main.ts @@ -5,6 +5,7 @@ import {GameModelLoader} from "./storage/loader/GameModelLoader"; import {StoredGameModel} from "./storage/StoredGameModel"; import {ScriptAccountStorage} from "./storage/storing/ScriptAccountStoring"; import {ModelComponentFileDirectory} from "./storage/ModelComponentFileDirectory"; +import {GamesystemStorage} from "./storage/storing/GamesystemStorage"; let win: BrowserWindow | null = null; const args = process.argv.slice(1), @@ -215,6 +216,9 @@ function saveProject() { function recieveGameModelToStore(gameModel: StoredGameModel) { const scriptAccountStorage = new ScriptAccountStorage(path.join(projectDirectory, ModelComponentFileDirectory.SCRIPTACCOUNT_DIR_NAME)) scriptAccountStorage.storeScriptAccounts(gameModel.storedScriptAccounts) + + const gamesystemStorage = new GamesystemStorage(path.join(projectDirectory, ModelComponentFileDirectory.GAMESYSTEM_DIR_NAME)) + gamesystemStorage.storeGamesystems(gameModel.storedGamesystems) } /*function deleteComponent(component: DeleteModel) { diff --git a/app/storage/FileUtils.js b/app/storage/FileUtils.js index a13984e..55d504f 100644 --- a/app/storage/FileUtils.js +++ b/app/storage/FileUtils.js @@ -19,6 +19,16 @@ class FileUtils { (0, fs_1.mkdirSync)(parentDirectory, { recursive: true }); } } + static removeFiles(files) { + files.forEach(file => { + if (fs.lstatSync(file).isDirectory()) { + fs.rmdirSync(file); + } + else { + fs.unlinkSync(file); + } + }); + } } exports.FileUtils = FileUtils; //# sourceMappingURL=FileUtils.js.map \ No newline at end of file diff --git a/app/storage/FileUtils.ts b/app/storage/FileUtils.ts index decb4fc..cfab96a 100644 --- a/app/storage/FileUtils.ts +++ b/app/storage/FileUtils.ts @@ -1,6 +1,7 @@ import * as fs from "fs"; import * as path from "node:path"; import {mkdirSync} from "fs"; +import {lstatSync} from "node:fs"; export class FileUtils { @@ -19,4 +20,14 @@ export class FileUtils { mkdirSync(parentDirectory, {recursive: true}) } } + + public static removeFiles(files: string[]) { + files.forEach(file => { + if(fs.lstatSync(file).isDirectory()) { + fs.rmdirSync(file) + } else { + fs.unlinkSync(file); + } + }) + } } diff --git a/app/storage/storing/GamesystemStorage.js b/app/storage/storing/GamesystemStorage.js index a75c04c..fb9ca66 100644 --- a/app/storage/storing/GamesystemStorage.js +++ b/app/storage/storing/GamesystemStorage.js @@ -9,33 +9,46 @@ class GamesystemStorage { this.gamesystemRootDir = gamesystemRootDir; } storeGamesystems(gamesystems) { - this.detectUnusedGamesystemFiles(this.gamesystemRootDir, gamesystems); + const unreferencedFiles = this.detectUnusedGamesystemFiles(gamesystems); + FileUtils_1.FileUtils.removeFiles(unreferencedFiles); gamesystems.forEach(gamesystem => this.storeGamesystem(gamesystem)); } - detectUnusedGamesystemFiles(gamesystemDir, gamesystems) { - const gamesystemFiles = FileUtils_1.FileUtils.listFilesInDirectory(gamesystemDir); - gamesystemFiles.forEach(gamesystemFile => { - if (fs.lstatSync(gamesystemFile).isDirectory()) { - this.detectUnusedGamesystemFiles(gamesystemFile, gamesystems); + detectUnusedGamesystemFiles(gamesystems) { + const unreferencedFiles = []; + const gamesystemFiles = FileUtils_1.FileUtils.listFilesInDirectory(this.gamesystemRootDir); + while (gamesystemFiles.length > 0) { + const currentGamesystemFile = gamesystemFiles.shift(); + const referencedGamesystemName = path.parse(path.basename(currentGamesystemFile)).name; + const referencedGamesystem = this.findReferencedGamesystem(referencedGamesystemName, gamesystems); + if (referencedGamesystem == undefined) { + unreferencedFiles.push(currentGamesystemFile); } else { - const currentFileName = path.parse(path.basename(gamesystemFile)).name; - const searchedGamesystem = gamesystems.find(gamesystem => path.basename(gamesystem.fileName) === currentFileName); - if (searchedGamesystem != undefined) { - if (path.dirname(searchedGamesystem.fileName) === currentFileName) { - //Aus Simple wurde Product => Delete .json - fs.unlinkSync(gamesystemFile); + const decodedJSONData = JSON.parse(referencedGamesystem.jsonString); + if (decodedJSONData.childsystems != undefined) { + //Check if current file is a directory. When it is a directory, everything is fine + if (fs.lstatSync(currentGamesystemFile).isDirectory()) { + } + else { + const parentDirName = path.basename(path.dirname(currentGamesystemFile)); + if (parentDirName !== referencedGamesystemName) { + unreferencedFiles.push(currentGamesystemFile); + } } } - else { - fs.unlinkSync(gamesystemFile); - } } - }); + if (fs.lstatSync(currentGamesystemFile).isDirectory()) { + gamesystemFiles.push(...FileUtils_1.FileUtils.listFilesInDirectory(currentGamesystemFile)); + } + } + return unreferencedFiles; + } + findReferencedGamesystem(referencedName, gamesystems) { + const searchedGamesystem = gamesystems.find(gamesystem => path.basename(gamesystem.fileName) === referencedName); + return searchedGamesystem; } storeGamesystem(gamesystem) { const gamesystemFile = path.join(...gamesystem.fileName.split("/")); - console.log(gamesystem.jsonString); const completeGamesystemFile = path.join(this.gamesystemRootDir, gamesystemFile); FileUtils_1.FileUtils.prepareFileForWriting(completeGamesystemFile); fs.writeFileSync(completeGamesystemFile + ".json", gamesystem.jsonString, 'utf-8'); diff --git a/app/storage/storing/GamesystemStorage.ts b/app/storage/storing/GamesystemStorage.ts index a25848d..c414f65 100644 --- a/app/storage/storing/GamesystemStorage.ts +++ b/app/storage/storing/GamesystemStorage.ts @@ -13,33 +13,50 @@ export class GamesystemStorage { } public storeGamesystems(gamesystems: StoreComponent[]) { - this.detectUnusedGamesystemFiles(this.gamesystemRootDir, gamesystems) + const unreferencedFiles = this.detectUnusedGamesystemFiles(gamesystems) + FileUtils.removeFiles(unreferencedFiles) gamesystems.forEach(gamesystem => this.storeGamesystem(gamesystem)) } - private detectUnusedGamesystemFiles(gamesystemDir: string, gamesystems: StoreComponent[]) { - const gamesystemFiles = FileUtils.listFilesInDirectory(gamesystemDir); - gamesystemFiles.forEach(gamesystemFile => { - if(fs.lstatSync(gamesystemFile).isDirectory()) { - this.detectUnusedGamesystemFiles(gamesystemFile, gamesystems) + private detectUnusedGamesystemFiles(gamesystems: StoreComponent[]) { + const unreferencedFiles: string[] = [] + const gamesystemFiles = FileUtils.listFilesInDirectory(this.gamesystemRootDir); + while(gamesystemFiles.length > 0) { + const currentGamesystemFile = gamesystemFiles.shift() + const referencedGamesystemName = path.parse(path.basename(currentGamesystemFile!)).name + const referencedGamesystem = this.findReferencedGamesystem(referencedGamesystemName, gamesystems) + if(referencedGamesystem == undefined) { + unreferencedFiles.push(currentGamesystemFile!) } else { - const currentFileName = path.parse(path.basename(gamesystemFile)).name - const searchedGamesystem = gamesystems.find(gamesystem => path.basename(gamesystem.fileName) === currentFileName); - if(searchedGamesystem != undefined) { - if(path.dirname(searchedGamesystem.fileName) === currentFileName) { - //Aus Simple wurde Product => Delete .json - fs.unlinkSync(gamesystemFile) + const decodedJSONData = JSON.parse(referencedGamesystem!.jsonString) + if(decodedJSONData.childsystems != undefined) { + //Check if current file is a directory. When it is a directory, everything is fine + if(fs.lstatSync(currentGamesystemFile!).isDirectory()) { + + } else { + const parentDirName = path.basename(path.dirname(currentGamesystemFile!)) + if(parentDirName !== referencedGamesystemName) { + unreferencedFiles.push(currentGamesystemFile!) + } } - } else { - fs.unlinkSync(gamesystemFile) } } - }) + + if(fs.lstatSync(currentGamesystemFile!).isDirectory()) { + gamesystemFiles.push(... FileUtils.listFilesInDirectory(currentGamesystemFile!)) + } + } + + return unreferencedFiles; + } + + private findReferencedGamesystem(referencedName: string, gamesystems: StoreComponent[]): StoreComponent | undefined { + return gamesystems.find(gamesystem => + path.basename(gamesystem.fileName) === referencedName) } private storeGamesystem(gamesystem: StoreComponent) { const gamesystemFile = path.join(... gamesystem.fileName.split("/")) - console.log(gamesystem.jsonString) const completeGamesystemFile = path.join(this.gamesystemRootDir, gamesystemFile) FileUtils.prepareFileForWriting(completeGamesystemFile) fs.writeFileSync(completeGamesystemFile + ".json", gamesystem.jsonString, 'utf-8') diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 36d1fd0..c1f5ec7 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -106,7 +106,7 @@ export class AppComponent implements OnInit{ this.gameModel!.removeScriptAccount(affectedModelComponent); //this.electronService.ipcRenderer.send('delete-component', new DeleteModel(affectedModelComponent.componentName, ModelComponentType.SCRIPTACCOUNT)) } else if(affectedModelComponent instanceof Gamesystem) { - //this.gameModel!.removeGamesystem(affectedModelComponent); + this.gameModel!.removeGamesystem(affectedModelComponent); //this.electronService.ipcRenderer.send('delete-component', new DeleteModel(affectedModelComponent.componentName, ModelComponentType.GAMESYTEM)) this.gamesystemOverview!.refresh() } diff --git a/src/app/project/serializer/GamesystemSerializer.ts b/src/app/project/serializer/GamesystemSerializer.ts index 3088fe6..eadb92e 100644 --- a/src/app/project/serializer/GamesystemSerializer.ts +++ b/src/app/project/serializer/GamesystemSerializer.ts @@ -39,7 +39,6 @@ export class GamesystemSerializer { } }, SerializeConstants.JSON_INDENT) - console.log(jsonString) return new StoreComponent(jsonString, fileName, ModelComponentType.GAMESYTEM) }