1/* 2* Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. 3* Licensed under the Apache License, Version 2.0 (the "License"); 4* you may not use this file except in compliance with the License. 5* You may obtain a copy of the License at 6* 7* http://www.apache.org/licenses/LICENSE-2.0 8* 9* Unless required by applicable law or agreed to in writing, software 10* distributed under the License is distributed on an "AS IS" BASIS, 11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12* See the License for the specific language governing permissions and 13* limitations under the License. 14*/ 15import * as vscode from 'vscode'; 16import * as path from 'path'; 17import { IModel } from "./imodel"; 18import { parseHeaderFile } from '../parse/parsec'; 19import { GenInfo } from '../gen/datatype'; 20import { genDtsFile } from '../gen/gendts'; 21import { GEN_COMPLETE, OPEN_IN_EXPLORER, PARSE_COMPLETE } from '../common/constants'; 22 23import { 24 EVENT_ERROR, 25 EVENT_INFORMATION, 26 EVENT_PROGRESS, 27 EVENT_WARNING 28} from '../common/eventtype'; 29import { parseTsFile } from '../parse/parsets'; 30import { genCppFile, genCppFromDts } from '../gen/gendtscpp'; 31import { Logger } from '../common/log'; 32export class Dts2cppMod extends IModel { 33 name: string; 34 private static instance: Dts2cppMod; 35 constructor() { 36 super(); 37 this.name = 'dts2cppmod'; 38 } 39 40 static getInstance(): IModel { 41 if (!Dts2cppMod.instance) { 42 Dts2cppMod.instance = new Dts2cppMod(); 43 } 44 return Dts2cppMod.instance; 45 } 46 47 init(uri: vscode.Uri): void { 48 this.uri = uri; 49 } 50 51 async doStart(): Promise<void> { 52 try { 53 if (this.uri) { 54 const filename = path.basename(this.uri.fsPath); 55 Logger.getInstance().debug('get filename ' ); 56 if (filename.endsWith('.d.ts')) { 57 // Display a message box to the user 58 // analyze 59 let res = parseTsFile(this.uri.fsPath); 60 Logger.getInstance().info('res: ' + JSON.stringify(res)); 61 // progress.report({ increment: 50, message: PARSE_COMPLETE }); 62 this.emmitEventForKey(EVENT_PROGRESS, 50, PARSE_COMPLETE); 63 // generator 64 let out = path.dirname(this.uri.fsPath); 65 // genCppFile(res, this.uri.fsPath, out); 66 let rootInfo: GenInfo = { 67 parseObj: res, 68 rawFilePath: this.uri.fsPath, // e://xxx.d.ts 69 fileName: path.basename(this.uri.fsPath, '.d.ts') // xxx 70 }; 71 genCppFromDts(rootInfo); 72 // progress.report({ increment: 100, message: GEN_COMPLETE + out }); 73 this.emmitEventForKey(EVENT_PROGRESS, 100, PARSE_COMPLETE + out); 74 75 // show genarate path 76 const choice = await vscode.window.showInformationMessage( 77 'outPath:', path.dirname(this.uri.fsPath), OPEN_IN_EXPLORER); 78 if (choice === OPEN_IN_EXPLORER) { 79 // open the folder 80 vscode.commands.executeCommand( 81 'revealFileInOS', vscode.Uri.file(this.uri.fsPath)); 82 } 83 } else { 84 let errmsg = 'not dts uri is : ' + this.uri.fsPath; 85 Logger.getInstance().error(errmsg); 86 // Display a message box to the user 87 //vscode.window.showInformationMessage(`${this.uri.fsPath} is not a .d.ts file!`); 88 this.emmitEventForKey(EVENT_ERROR, -1, errmsg); 89 } 90 } else { 91 let errmsg = 'parse header file error with undefine uri'; 92 Logger.getInstance().error(errmsg); 93 this.emmitEventForKey(EVENT_ERROR, -1, errmsg); 94 } 95 } catch (e) { 96 let errmsg = 'parse header file error: ' + JSON.stringify(e); 97 Logger.getInstance().error(errmsg); 98 this.emmitEventForKey(EVENT_ERROR, -1, errmsg); 99 } 100 } 101 102 async doStop(): Promise<void> { 103 throw new Error("Method not implemented."); 104 } 105 106 async doPause(): Promise<void> { 107 throw new Error("Method not implemented."); 108 } 109 110 async doResume(): Promise<void> { 111 throw new Error("Method not implemented."); 112 } 113}