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 { Logger } from '../common/log'; 23import { 24 EVENT_ERROR, 25 EVENT_INFORMATION, 26 EVENT_PROGRESS, 27 EVENT_WARNING 28} from '../common/eventtype'; 29 30import { genHdfFile } from '../gen/genhdf'; 31 32export class H2hdfMod extends IModel { 33 name: string; 34 versionTag: string = '4.1'; 35 serviceId: string = '19000'; 36 private static instance: H2hdfMod; 37 38 constructor() { 39 super(); 40 this.name = 'h2dtsmod'; 41 } 42 43 static getInstance(): IModel { 44 if (!H2hdfMod.instance) { 45 H2hdfMod.instance = new H2hdfMod(); 46 } 47 return H2hdfMod.instance; 48 } 49 50 setVersionTag(version: string) { 51 this.versionTag = version; 52 } 53 54 setServiceId(id: string) { 55 this.serviceId = id; 56 } 57 58 init(uri: vscode.Uri): void { 59 this.uri = uri; 60 } 61 62 async generateHdf(hdfInputPath: string, versionTag: string) { 63 // analyze 64 let funDescList = await parseHeaderFile(hdfInputPath); 65 Logger.getInstance().debug('parse header file res: ' + funDescList); 66 Logger.getInstance().debug('parse header file jsonstr: ' + JSON.stringify(funDescList)); 67 this.emmitEventForKey(EVENT_PROGRESS, 50, PARSE_COMPLETE); 68 // generator 69 let out = path.dirname(hdfInputPath); 70 let driverName = path.basename(hdfInputPath, '.h').toLocaleLowerCase(); 71 let rootInfo = { 72 driverName: driverName, 73 funcs: funDescList.funcs, 74 versionTag: versionTag 75 }; 76 genHdfFile(rootInfo, out); 77 // progress.report({ increment: 100, message: GEN_COMPLETE + out}); 78 this.emmitEventForKey(EVENT_PROGRESS, 100, GEN_COMPLETE + out); 79 80 // toast the message of output dir 81 const choice = await vscode.window.showInformationMessage('outPath:', path.dirname(hdfInputPath), OPEN_IN_EXPLORER); 82 if (choice === OPEN_IN_EXPLORER) { 83 // open the output in folder 84 vscode.commands.executeCommand('revealFileInOS', vscode.Uri.file(hdfInputPath)); 85 } 86 } 87 88 async doStart(): Promise<void> { 89 try { 90 if (this.uri) { 91 this.generateHdf(this.uri.fsPath, this.versionTag); 92 } else { 93 let errmsg = 'parse header file error with undefine uri.'; 94 Logger.getInstance().error(errmsg); 95 this.emmitEventForKey(EVENT_ERROR, -1, errmsg); 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}