diff --git a/src/CfgEditor/CfgData.ts b/src/CfgEditor/CfgData.ts index 244d86cb..06e2bf1f 100644 --- a/src/CfgEditor/CfgData.ts +++ b/src/CfgEditor/CfgData.ts @@ -15,7 +15,7 @@ */ import * as ini from "ini"; -import { ICfgData } from "./ICfgData"; +import { CfgInfo, ICfgData } from "./ICfgData"; import { Sections } from "./Sections"; // NOTE: Why is not function overloadding used? Its maintain costs expensive. @@ -33,7 +33,7 @@ import { Sections } from "./Sections"; // export class CfgData extends ICfgData { constructor(cfg = undefined) { - super(cfg, Sections.onecc); + super(cfg, Sections.onecc, "one.editor.cfg", ".cfg"); } setWithConfig(cfg: any): void { @@ -86,4 +86,17 @@ export class CfgData extends ICfgData { config[section] = ini.parse(value); this.resolveDeprecated(); } + + generateCfgInfo(modelName: string, extName: string): CfgInfo { + return { + title: `Create ONE configuration of '${modelName}.${extName}' :`, + viewType: this.viewType, + extType: this.extType, + content: `[onecc] +one-import-${extName}=True +[one-import-${extName}] +input_path=${modelName}.${extName} +`, + }; + } } diff --git a/src/CfgEditor/EdgeTPUCfgData.ts b/src/CfgEditor/EdgeTPUCfgData.ts index 9f1bb5bd..b829ec54 100644 --- a/src/CfgEditor/EdgeTPUCfgData.ts +++ b/src/CfgEditor/EdgeTPUCfgData.ts @@ -15,12 +15,12 @@ */ import * as ini from "ini"; -import { ICfgData } from "./ICfgData"; +import { CfgInfo, ICfgData } from "./ICfgData"; import { Sections } from "./Sections"; export class EdgeTPUCfgData extends ICfgData { constructor(cfg = undefined) { - super(cfg, Sections.edgetpu); + super(cfg, Sections.edgetpu, "one.editor.edgetpucfg", ".edgetpucfg"); } setWithConfig(cfg: any): void { @@ -48,4 +48,20 @@ export class EdgeTPUCfgData extends ICfgData { const config = this.getAsConfig(); config[section] = ini.parse(value); } + + generateCfgInfo(modelName: string, extName: string): CfgInfo { + return { + title: `Create EdgeTPU configuration of '${modelName}.${extName}' :`, + viewType: this.viewType, + extType: this.extType, + content: `[edgetpu-compiler] +edgetpu-compile=True +edgetpu-profile=False + +[edgetpu-compile] +input_path=${modelName}.${extName} +output_path=${modelName}_edgetpu.${extName} +`, + }; + } } diff --git a/src/CfgEditor/ICfgData.ts b/src/CfgEditor/ICfgData.ts index b4575118..5b32590b 100644 --- a/src/CfgEditor/ICfgData.ts +++ b/src/CfgEditor/ICfgData.ts @@ -15,15 +15,29 @@ */ import * as ini from "ini"; +export type CfgInfo = { + title: string; + viewType: string; + extType: string; + content: string; +}; + export abstract class ICfgData { - //config can be undefined private _config: any; - //section must be existed private _section!: string[]; + private _viewType!: string; + private _extType!: string; - constructor(_config: any, _section: string[]) { + constructor( + _config: any, + _section: string[], + _viewType: string, + _extType: string + ) { this._config = _config; this._section = _section; + this._viewType = _viewType; + this._extType = _extType; } // sets data with object decoded or parsed @@ -36,6 +50,17 @@ export abstract class ICfgData { value: string ): void; abstract updateSectionWithValue(section: string, value: string): void; + //Return information about each cfgType + abstract generateCfgInfo(modelName: string, extName: string): CfgInfo; + + //getter + get viewType(): string { + return this._viewType; + } + + get extType(): string { + return this._extType; + } // set cfgData's config // only child class can use this method diff --git a/src/OneExplorer/OneExplorer.ts b/src/OneExplorer/OneExplorer.ts index 1fe0bd30..082ee109 100644 --- a/src/OneExplorer/OneExplorer.ts +++ b/src/OneExplorer/OneExplorer.ts @@ -19,13 +19,15 @@ import * as fs from "fs"; import * as path from "path"; import * as vscode from "vscode"; -import { CfgEditorPanel } from "../CfgEditor/CfgEditorPanel"; import { obtainWorkspaceRoots } from "../Utils/Helpers"; import { Logger } from "../Utils/Logger"; import { ConfigObj } from "./ConfigObject"; import { ArtifactAttr } from "./ArtifactLocator"; import { OneStorage } from "./OneStorage"; +import { CfgInfo, ICfgData } from "../CfgEditor/ICfgData"; +import { EdgeTPUCfgData } from "../CfgEditor/EdgeTPUCfgData"; +import { CfgData } from "../CfgEditor/CfgData"; // Exported for unit testing only export { @@ -1137,8 +1139,61 @@ export class OneTreeDataProvider implements vscode.TreeDataProvider { } /** - * Create ONE configuration file for a base model - * Input box is prefilled as .cfg + * Select the option for the configuration you want to create + * Return information about the selected option + * + * @param modelName A base model's name + * @param extName A base model's extension name + * + */ + private async generateCfgInfo( + modelName: string, + extName: string + ): Promise { + //Options must be added according to extension + const options: vscode.QuickPickItem[] = [ + { label: ".cfg", description: "configuration file of onecc compiler" }, + ]; + + if (extName === "tflite") { + options.push({ + label: ".edgetpucfg", + description: "configuration file of edge tpu compiler", + }); + } + + const placeHolder = options.map((option) => option.label).join(" / "); + + let selectedLabel: string | undefined = ".cfg"; + let cfgData: ICfgData | undefined = undefined; + + //If options array only has the `.cfg` option, skip selecting it. + if (options.length !== 1) { + const selectedOption = await vscode.window.showQuickPick(options, { + title: "Pick a configuration to create", + placeHolder: placeHolder, + }); + selectedLabel = selectedOption?.label; + } + + switch (selectedLabel) { + case ".cfg": + cfgData = new CfgData(); + break; + case ".edgetpucfg": + cfgData = new EdgeTPUCfgData(); + break; + default: + cfgData = undefined; + break; + } + + return cfgData?.generateCfgInfo(modelName, extName); + } + + /** + * A configuration file is created for the selected option. + * Input box is prefilled as . * The operation will be cancelled if the file already exists. * * @command one.explorer.createCfg @@ -1149,18 +1204,19 @@ export class OneTreeDataProvider implements vscode.TreeDataProvider { const modelName = path.parse(node.path).name; const extName = path.parse(node.path).ext.slice(1); - // TODO(dayo) Auto-configure more fields - const content = `[onecc] -one-import-${extName}=True -[one-import-${extName}] -input_path=${modelName}.${extName} -`; + const cfgInfo = await this.generateCfgInfo(modelName, extName); + + //When the user presses the ESC button, it is cancelled + if (cfgInfo === undefined) { + return; + } + // TODO(dayo) Auto-configure more fields const validateInputPath = (cfgName: string): string | undefined => { const cfgPath: string = path.join(dirPath, cfgName); - if (!cfgName.endsWith(".cfg")) { - return `A file extension must be .cfg`; + if (!cfgName.endsWith(cfgInfo.extType)) { + return `A file extension must be ${cfgInfo.extType}`; } if (fs.existsSync(cfgPath)) { @@ -1170,10 +1226,13 @@ input_path=${modelName}.${extName} vscode.window .showInputBox({ - title: `Create ONE configuration of '${modelName}.${extName}' :`, + title: cfgInfo.title, placeHolder: `Enter a file name`, - value: `${modelName}.cfg`, - valueSelection: [0, `${modelName}.cfg`.length - `.cfg`.length], + value: `${modelName}${cfgInfo.extType}`, + valueSelection: [ + 0, + `${modelName}${cfgInfo.extType}`.length - `${cfgInfo.extType}`.length, + ], validateInput: validateInputPath, }) .then((value) => { @@ -1187,7 +1246,7 @@ input_path=${modelName}.${extName} const edit = new vscode.WorkspaceEdit(); edit.createFile(uri); - edit.insert(uri, new vscode.Position(0, 0), content); + edit.insert(uri, new vscode.Position(0, 0), cfgInfo.content); vscode.workspace.applyEdit(edit).then((isSuccess) => { if (isSuccess) { @@ -1196,7 +1255,7 @@ input_path=${modelName}.${extName} vscode.commands.executeCommand( "vscode.openWith", uri, - CfgEditorPanel.viewType + cfgInfo.viewType ); }); } else {