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 { DtscppRootInfo, GenInfo } from '../gen/datatype'; 20import { genDtscppFromH } from '../gen/gendtscpp'; 21import { GEN_COMPLETE, OPEN_IN_EXPLORER, PARSE_COMPLETE } from '../common/constants'; 22import { Logger } from '../common/log'; 23import { 24 EVENT_ERROR, 25 EVENT_INFORMATION, 26 EVENT_PROGRESS, 27 EVENT_WARNING 28} from '../common/eventtype'; 29import { genDtsCppFile } from '../gen/gendtscpp'; 30 31export class H2dtscppMod extends IModel { 32 name: string; 33 private static instance: H2dtscppMod; 34 constructor() { 35 super(); 36 this.name = 'h2dtscppmod'; 37 } 38 39 static getInstance(): IModel { 40 if (!H2dtscppMod.instance) { 41 H2dtscppMod.instance = new H2dtscppMod(); 42 } 43 return H2dtscppMod.instance; 44 } 45 46 init(uri: vscode.Uri): void { 47 this.uri = uri; 48 } 49 50 async doStart(): Promise<void> { 51 try { 52 if (this.uri) { 53 // analyze 54 let parseRes = await parseHeaderFile(this.uri.fsPath); 55 let fileName = path.basename(this.uri.fsPath, '.h'); 56 Logger.getInstance().debug('parse header file res: ' + parseRes); 57 Logger.getInstance().debug('parse header file jsonstr: ' + JSON.stringify(parseRes)); 58 59 // progress.report({ increment: 50, message: PARSE_COMPLETE }); 60 this.emmitEventForKey(EVENT_PROGRESS, 50, PARSE_COMPLETE); 61 62 // let rootInfo: DtscppRootInfo = { 63 // funcs: funDescList.funcs, 64 // rawFilePath: this.uri.fsPath, 65 // fileName: fileName // xxx 66 // }; 67 68 let rootInfo: GenInfo = { 69 parseObj: parseRes, 70 rawFilePath: this.uri.fsPath, // e://xxx.h 71 fileName: path.basename(this.uri.fsPath, '.h') // xxx 72 }; 73 74 // generator 75 let out = path.dirname(this.uri.fsPath); 76 // genDtsCppFile(rootInfo, out); 77 genDtscppFromH(rootInfo); 78 // progress.report({ increment: 100, message: GEN_COMPLETE + out }); 79 this.emmitEventForKey(EVENT_PROGRESS, 100, PARSE_COMPLETE + out); 80 81 // show genarate path 82 const choice = await vscode.window.showInformationMessage( 83 'outPath:', path.dirname(this.uri.fsPath), OPEN_IN_EXPLORER); 84 if (choice === OPEN_IN_EXPLORER) { 85 // open the folder 86 vscode.commands.executeCommand( 87 'revealFileInOS', vscode.Uri.file(this.uri.fsPath)); 88 } 89 } else { 90 let errmsg = 'parse header file error with undefine uri'; 91 Logger.getInstance().error(errmsg); 92 this.emmitEventForKey(EVENT_ERROR, -1, errmsg); 93 } 94 } catch (e) { 95 let errmsg = 'parse header file error: ' + JSON.stringify(e); 96 Logger.getInstance().error(errmsg); 97 this.emmitEventForKey(EVENT_ERROR, -1, errmsg); 98 } 99 } 100 101 async doStop(): Promise<void> { 102 throw new Error("Method not implemented."); 103 } 104 105 async doPause(): Promise<void> { 106 throw new Error("Method not implemented."); 107 } 108 109 async doResume(): Promise<void> { 110 throw new Error("Method not implemented."); 111 } 112}