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