diff --git a/src/MPQEditor/MPQEditor.ts b/src/MPQEditor/MPQEditor.ts index 17533c79..a27d9198 100644 --- a/src/MPQEditor/MPQEditor.ts +++ b/src/MPQEditor/MPQEditor.ts @@ -51,6 +51,38 @@ export class MPQEditorProvider implements vscode.CustomTextEditorProvider { ); } + /** + * @brief create file with default mpq configuration + * @returns valid uri of file on success or undefined on failure + */ + public static async createDefaultMPQ( + mpqName: string, + dirPath: string, + circleName: string + ): Promise { + const content = `{"default_quantization_dtype": "uint8", + "default_granularity": "channel", + "layers": [], + "model_path": "${circleName}"}`; + + // 'uri' path is not occupied, assured by validateInputPath + const uri = vscode.Uri.file(`${dirPath}/${mpqName}`); + + const edit = new vscode.WorkspaceEdit(); + edit.createFile(uri); + edit.insert(uri, new vscode.Position(0, 0), content); + + try { + await vscode.workspace.applyEdit(edit); + let document = await vscode.workspace.openTextDocument(uri); + document.save(); + } catch (error) { + return undefined; + } + + return uri; + } + /** * @brief A helper function to validate mpqName * @note It checks whether diff --git a/src/Tests/MPQEditor/MPQEditor.test.ts b/src/Tests/MPQEditor/MPQEditor.test.ts index 51b1ae6c..71ddef61 100644 --- a/src/Tests/MPQEditor/MPQEditor.test.ts +++ b/src/Tests/MPQEditor/MPQEditor.test.ts @@ -14,6 +14,9 @@ * limitations under the License. */ +import * as fs from "fs"; +import * as path from "path"; + import { assert } from "chai"; import { MPQEditorProvider } from "../../MPQEditor/MPQEditor"; import { TestBuilder } from "../TestBuilder"; @@ -92,5 +95,41 @@ suite("MPQEditor", function () { assert.throws(() => MPQEditorProvider.findMPQName("", dirPath)); }); }); + + suite("#createDefaultMPQ", function () { + test("test createDefaultMPQ", function () { + // create dummy mpq.json file + const dirPath: string = testBuilder.dirInTemp; + const mpqName: string = "model-test-createMPQ.mpq.json"; + const circleName: string = "model-test-createMPQ.circle"; + MPQEditorProvider.createDefaultMPQ(mpqName, dirPath, circleName).then( + (uri) => { + assert.isTrue(uri !== undefined); + const mpqPath: string = path.join(dirPath, mpqName); + assert.isTrue(fs.existsSync(mpqPath)); + const contents: string = fs.readFileSync(mpqPath, "utf-8"); + const cont: any = JSON.parse(contents); + assert.strictEqual(cont["default_quantization_dtype"], "uint8"); + assert.strictEqual(cont["default_granularity"], "channel"); + assert.strictEqual(cont["model_path"], circleName); + } + ); + }); + test("NEG: test createDefaultMPQ on exsisting file", function () { + // create dummy mpq.json file + const dirPath: string = testBuilder.dirInTemp; + const mpqName: string = "model-test-createMPQ_NEG.mpq.json"; + const circleName: string = "model-test-createMPQ_NEG.circle"; + + const content = `empty content`; + testBuilder.writeFileSync(mpqName, content); + + MPQEditorProvider.createDefaultMPQ(mpqName, dirPath, circleName).then( + (uri) => { + assert.isTrue(uri === undefined); + } + ); + }); + }); }); });