From aa915270fdf106485b727363902e587a8256893e Mon Sep 17 00:00:00 2001 From: wangshi Date: Mon, 10 Feb 2025 16:16:46 +0800 Subject: [PATCH 1/4] refact gen Signed-off-by: wangshi --- src/vscode_plugin/src/gen/genCommonFile.ts | 39 ------- src/vscode_plugin/src/gen/genCommonFunc.ts | 104 ------------------ src/vscode_plugin/src/gen/genclientcppfile.ts | 46 -------- src/vscode_plugin/src/gen/genhdf.ts | 8 +- src/vscode_plugin/src/gen/genidlfile.ts | 93 ---------------- src/vscode_plugin/src/gen/geniservicehfile.ts | 41 ------- src/vscode_plugin/src/gen/genproxycppfile.ts | 72 ------------ src/vscode_plugin/src/gen/genproxyhfile.ts | 30 ----- src/vscode_plugin/src/gen/gensa.ts | 18 +-- src/vscode_plugin/src/gen/gensacppfile.ts | 64 ----------- src/vscode_plugin/src/gen/gensahfile.ts | 30 ----- .../src/gen/genservicecppfile.ts | 38 ------- src/vscode_plugin/src/gen/genservicehfile.ts | 62 ----------- src/vscode_plugin/src/gen/genstubcppfile.ts | 85 -------------- src/vscode_plugin/src/gen/genstubhfile.ts | 36 ------ 15 files changed, 13 insertions(+), 753 deletions(-) delete mode 100644 src/vscode_plugin/src/gen/genCommonFile.ts delete mode 100644 src/vscode_plugin/src/gen/genCommonFunc.ts delete mode 100644 src/vscode_plugin/src/gen/genclientcppfile.ts delete mode 100644 src/vscode_plugin/src/gen/genidlfile.ts delete mode 100644 src/vscode_plugin/src/gen/geniservicehfile.ts delete mode 100644 src/vscode_plugin/src/gen/genproxycppfile.ts delete mode 100644 src/vscode_plugin/src/gen/genproxyhfile.ts delete mode 100644 src/vscode_plugin/src/gen/gensacppfile.ts delete mode 100644 src/vscode_plugin/src/gen/gensahfile.ts delete mode 100644 src/vscode_plugin/src/gen/genservicecppfile.ts delete mode 100644 src/vscode_plugin/src/gen/genservicehfile.ts delete mode 100644 src/vscode_plugin/src/gen/genstubcppfile.ts delete mode 100644 src/vscode_plugin/src/gen/genstubhfile.ts diff --git a/src/vscode_plugin/src/gen/genCommonFile.ts b/src/vscode_plugin/src/gen/genCommonFile.ts deleted file mode 100644 index 3bc1b805..00000000 --- a/src/vscode_plugin/src/gen/genCommonFile.ts +++ /dev/null @@ -1,39 +0,0 @@ -/* -* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -import * as fs from 'fs'; -import { HdfRootInfo, ServiceRootInfo } from "./datatype"; -import { replaceAll } from '../common/tool'; - -// 生成sa非特殊处理的文件, 如xxx.cfg -export function genSaCommonFile(rootInfo: ServiceRootInfo, filePath: string, fileContent: string) { - fileContent = replaceAll(fileContent, '[serviceName]', rootInfo.serviceName); - fileContent = replaceAll(fileContent, '[marcoName]', rootInfo.serviceName.toUpperCase()); - fileContent = replaceAll(fileContent, '[lowServiceName]', rootInfo.serviceName.toLowerCase()); - fileContent = replaceAll(fileContent, '[serviceId]', rootInfo.serviceId); - fs.writeFileSync(filePath, fileContent); -} - -// 生成hdf非特殊处理的文件, 如xxx.hcs -export function genHdfCommonFile(rootInfo: HdfRootInfo, filePath: string, fileContent: string) { - let upperName = rootInfo.driverName.substring(0, 1).toLocaleUpperCase(); - let marcoName = upperName + rootInfo.driverName.substring(1, rootInfo.driverName.length); - let upperDriverName = rootInfo.driverName.toLocaleUpperCase(); - fileContent = replaceAll(fileContent, '[driverName]', rootInfo.driverName); - fileContent = replaceAll(fileContent, '[marcoName]', marcoName); - fileContent = replaceAll(fileContent, '[driverUpperName]', upperDriverName); - fs.writeFileSync(filePath, fileContent); -} - diff --git a/src/vscode_plugin/src/gen/genCommonFunc.ts b/src/vscode_plugin/src/gen/genCommonFunc.ts deleted file mode 100644 index 98c9eb46..00000000 --- a/src/vscode_plugin/src/gen/genCommonFunc.ts +++ /dev/null @@ -1,104 +0,0 @@ -/* -* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -import { getTab } from '../common/tool'; -import { getReg, match } from '../common/re'; -import { format } from 'util' -import { FuncObj, ParamObj } from './datatype'; -import { transferMap } from '../template/functypemap_template' - -export function getFuncParamStr(params: ParamObj[]) { - let paramStr = ''; - for (let i = 0; i < params.length; ++i) { - paramStr += (i === 0) ? '' : ', '; - paramStr += params[i].type + ' ' + params[i].name; - } - return paramStr; -} - -// 生成头文件中的方法声明内容 -export function genDeclareContent(funcList: FuncObj[]) { - let funcTab = getTab(1); - let saFuncHContent = ''; - for (let i = 0; i < funcList.length; ++i) { - let paramStr = getFuncParamStr(funcList[i].parameters); - // proxy.h中的方法定义 - saFuncHContent += (i === 0) ? '' : '\n' + funcTab; - saFuncHContent += format('%s %s(%s) override;', funcList[i].returns, funcList[i].name, paramStr); - } - return saFuncHContent; -} - -// 常用类型转换表, 将C语言常见类型(key)转换为remote data读写函数使用的类型(value) -// 例如 ErrCode 类型在框架中的系统原型为int类型,这里映射成int32_t, -// 因为int32_t类型在 DATA_W_MAP/DATA_R_MAP 表中有对应的读写数据方法(WriteInt32/ReadInt32) -const TYPE_DEF_MAP = new Map( - [['ErrCode', 'int32_t'], ['char', 'int8_t'], ['short', 'int16_t'], ['int', 'int32_t'], ['long', 'int64_t'], - ['unsigned char', 'uint8_t'], ['unsigned short', 'uint16_t'], ['unsigned int', 'uint32_t'], - ['unsigned long', 'uint64_t'], ['double_t', 'double'], ['float_t', 'float'], ['size_t', 'double'], - ['long long', 'double'], ['long double', 'double'], ['std::string', 'string'] - ]); - -function getParcelType(srcType: string) { - let parcelType = TYPE_DEF_MAP.get(srcType); - return parcelType === undefined ? srcType : parcelType; -} - -function getTransferContent(parcelVecType: string, isWrite: number) { - let rwFunc = ''; - for (let index = 0; index < transferMap.length; index++) { - if (parcelVecType === transferMap[index].fromType) { - rwFunc = transferMap[index].tranferContent[isWrite]; - } - } - return rwFunc; -} - -export function genWrite(srcName: string, parcelName: string, vType: string) { - let matchs = match('(std::)?vector<([\x21-\x7e]+)[ ]?>', vType); - if (matchs) { - // vector类型变量包装成parcel data - let rawType = getReg(vType, matchs.regs[2]); - let parcelVecType = 'vector<' + getParcelType(rawType) + '>'; - let wVecFunc = getTransferContent(parcelVecType, 0); - if (wVecFunc === '') { - return ''; - } - return format('%s.%s(%s);', parcelName, wVecFunc, srcName); - } - - let parcelType = getParcelType(vType); - let wFunc = getTransferContent(parcelType, 0); - - return format('%s.%s(%s);', parcelName, wFunc, srcName); -} - -export function genRead(parcelName: string, destObj: ParamObj) { - let matchs = match('(std::)?vector<([\x21-\x7e]+)[ ]?>', destObj.type); - if (matchs) { - // 从parcel data中读取vector类型变量 - let rawType = getReg(destObj.type, matchs.regs[2]); - let parcelVecType = getParcelType(rawType); - let rVecFunc = 'vector<' + getTransferContent(parcelVecType, 1) + '>'; - if (rVecFunc === '') { - return ''; - } - return format('%s.%s(&(%s));', parcelName, rVecFunc, parcelName); - } - - let parcelType = getParcelType(destObj.type); - let rFunc = getTransferContent(parcelType, 1); - return format('%s = %s.%s();', destObj.name, parcelName, rFunc); -} \ No newline at end of file diff --git a/src/vscode_plugin/src/gen/genclientcppfile.ts b/src/vscode_plugin/src/gen/genclientcppfile.ts deleted file mode 100644 index 3d6f92f9..00000000 --- a/src/vscode_plugin/src/gen/genclientcppfile.ts +++ /dev/null @@ -1,46 +0,0 @@ - -/* -* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -import { replaceAll, getTab } from '../common/tool'; -import * as fs from 'fs'; -import { format } from 'util' -import { FuncObj, ParamObj, ServiceRootInfo } from './datatype'; - -function getClientFuncParamStr(params: ParamObj[]) { - let paramStr = ''; - for (let i = 0; i < params.length; ++i) { - paramStr += (i === 0) ? '' : ', '; - paramStr += params[i].name; - } - return paramStr; -} - -// 生成xxx_client.cpp -export function genClientCppFile(rootInfo: ServiceRootInfo, filePath: string, fileContent: string) { - let clientFuncCpp = ''; - let funcList: FuncObj[] = rootInfo.funcs; - let funcTab = getTab(1); - for (let i = 0; i < funcList.length; ++i) { - let clientParamStr = getClientFuncParamStr(funcList[i].parameters); - clientFuncCpp += (i === 0) ? '' : '\n' + funcTab; - clientFuncCpp += format('// proxy->%s(%s);', funcList[i].name, clientParamStr); - } - fileContent = replaceAll(fileContent, '[serviceName]', rootInfo.serviceName); - fileContent = replaceAll(fileContent, '[marcoName]', rootInfo.serviceName.toUpperCase()); - fileContent = replaceAll(fileContent, '[lowServiceName]', rootInfo.serviceName.toLowerCase()); - fileContent = replaceAll(fileContent, '[clientFuncInvoke]', clientFuncCpp); - fs.writeFileSync(filePath, fileContent); -} \ No newline at end of file diff --git a/src/vscode_plugin/src/gen/genhdf.ts b/src/vscode_plugin/src/gen/genhdf.ts index a9823930..84f69bf5 100644 --- a/src/vscode_plugin/src/gen/genhdf.ts +++ b/src/vscode_plugin/src/gen/genhdf.ts @@ -17,10 +17,10 @@ import * as path from 'path'; import * as fs from 'fs'; import { DirTemp, HdfRootInfo } from "./datatype"; import { hdf_version_map } from '../template/hdf/hdfdir'; -import { genIdlFile } from './genidlfile'; -import { genServiceHFile } from './genservicehfile'; -import { genServiceCppFile } from './genservicecppfile'; -import { genHdfCommonFile } from './genCommonFile'; +import { genIdlFile } from './tools/genidlfile'; +import { genServiceHFile } from './tools/genservicehfile'; +import { genServiceCppFile } from './tools/genservicecppfile'; +import { genHdfCommonFile } from './tools/gencommonfile'; const fileHandlers: { [key: string]: Function } = { 'I[marcoName]Interface.idl': genIdlFile, diff --git a/src/vscode_plugin/src/gen/genidlfile.ts b/src/vscode_plugin/src/gen/genidlfile.ts deleted file mode 100644 index ed573f51..00000000 --- a/src/vscode_plugin/src/gen/genidlfile.ts +++ /dev/null @@ -1,93 +0,0 @@ -/* -* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -import * as fs from 'fs'; -import { FuncObj, HdfRootInfo } from "./datatype"; -import { idlTransferType } from '../template/functypemap_template'; -import { format } from 'util'; -import { getTab, replaceAll } from '../common/tool'; - -// 常用类型转换表, 将C语言常见类型(key)转换为remote data读写函数使用的类型(value) -// 例如 ErrCode 类型在框架中的系统原型为int类型,这里映射成int32_t, -// 因为int32_t类型在 DATA_W_MAP/DATA_R_MAP 表中有对应的读写数据方法(WriteInt32/ReadInt32) -const TYPE_DEF_MAP = new Map( - [['std::string', 'string'], ['char *', 'string'] -]); - -function getParcelType(srcType: string) { - let parcelType = TYPE_DEF_MAP.get(srcType); - return parcelType === undefined ? srcType : parcelType; -} - -// idlTransferType -function getIdlType(cType: string) { - let rawType = getParcelType(cType); - for (let index = 0; index < idlTransferType.length; index++) { - if (rawType === idlTransferType[index].fromType) { - return idlTransferType[index].tranferContent; - } - } - return cType; -} - -function isReturn(type: string) { - if (type) { - if (type.indexOf('&')>0 || type.indexOf('**')>0) { - return true; - } - } - return false; -} - -function getIdlFuncParamStr(funcObj: FuncObj) { - let idlParams = ''; - for (let i = 0; i < funcObj.parameters.length; ++i) { - let type = getIdlType(funcObj.parameters[i].type); - // idlParams += (i === 0) && (funcObj.returns !== 'void') ? '' : ', '; - if (isReturn(funcObj.parameters[i].type)) { - idlParams += '[out] ' + type + ' ' + funcObj.parameters[i].name + ', '; - } else { - idlParams += '[in] ' + type + ' ' + funcObj.parameters[i].name + ', '; - } - } - if (funcObj.returns !== 'void') { - let retType = getIdlType(funcObj.returns); - let outName = funcObj.name + 'Out' - idlParams += '[out] ' + retType + ' ' + outName; - } else { - // 如果返回值是void, 去掉参数列表结尾逗号 - idlParams = idlParams.substring(0, idlParams.length - 2); - } - return idlParams; -} - -// 每个方法一个文件 -export function genIdlFile(rootInfo: HdfRootInfo, filePath: string, fileContent: string) { - let funcTab = getTab(1); - let idlFuncDefine = ''; - let funcList: FuncObj[] = rootInfo.funcs; - for (let i = 0; i < funcList.length; ++i) { - // 生成idl接口定义 - let paramIdlStr = getIdlFuncParamStr(funcList[i]); - idlFuncDefine += (i === 0) ? '' : '\n' + funcTab; - idlFuncDefine += format('%s(%s);', funcList[i].name, paramIdlStr); - } - let upperName = rootInfo.driverName.substring(0, 1).toLocaleUpperCase(); - let marcoName = upperName + rootInfo.driverName.substring(1, rootInfo.driverName.length); - fileContent = replaceAll(fileContent, '[driverName]', rootInfo.driverName); - fileContent = replaceAll(fileContent, '[marcoName]', marcoName); - fileContent = replaceAll(fileContent, '[idlFunDeclare]', idlFuncDefine); - fs.writeFileSync(filePath, fileContent); -} \ No newline at end of file diff --git a/src/vscode_plugin/src/gen/geniservicehfile.ts b/src/vscode_plugin/src/gen/geniservicehfile.ts deleted file mode 100644 index b498ca35..00000000 --- a/src/vscode_plugin/src/gen/geniservicehfile.ts +++ /dev/null @@ -1,41 +0,0 @@ -/* -* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -import { replaceAll, getTab } from '../common/tool'; -import * as fs from 'fs'; -import { format } from 'util' -import { FuncObj, ServiceRootInfo } from './datatype'; -import { getFuncParamStr } from './genCommonFunc'; - -export function genIServiceHFile(rootInfo: ServiceRootInfo, filePath: string, fileContent: string) { - let iServiceFuncH = ''; - let funcEnumStr = ''; - let funcList: FuncObj[] = rootInfo.funcs; - let funcTab = getTab(1); - let enumTab = getTab(2); - for (let i = 0; i < funcList.length; ++i) { - let funcEnum = funcList[i].name.toUpperCase(); - let paramStr = getFuncParamStr(funcList[i].parameters); - iServiceFuncH += (i === 0) ? '' : '\n' + funcTab; - iServiceFuncH += format('virtual %s %s(%s) = 0;', funcList[i].returns, funcList[i].name, paramStr); - funcEnumStr += (i === 0) ? '' : ',\n' + enumTab; - funcEnumStr += funcEnum; - } - fileContent = replaceAll(fileContent, '[serviceName]', rootInfo.serviceName); - fileContent = replaceAll(fileContent, '[marcoName]', rootInfo.serviceName.toUpperCase()); - fileContent = replaceAll(fileContent, '[funcEnum]', funcEnumStr); - fileContent = replaceAll(fileContent, '[iServiceHFunctions]', iServiceFuncH); - fs.writeFileSync(filePath, fileContent); -} \ No newline at end of file diff --git a/src/vscode_plugin/src/gen/genproxycppfile.ts b/src/vscode_plugin/src/gen/genproxycppfile.ts deleted file mode 100644 index 9df53062..00000000 --- a/src/vscode_plugin/src/gen/genproxycppfile.ts +++ /dev/null @@ -1,72 +0,0 @@ -/* -* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -import { getTab, replaceAll } from '../common/tool'; -import * as fs from 'fs'; -import { format } from 'util' -import { FuncObj, ParamObj, ServiceRootInfo } from './datatype'; -import { genRead, genWrite, getFuncParamStr } from './genCommonFunc'; -import { proxyFuncTemplate } from '../template/func_template'; - -function genProxyFunc(funcInfo: FuncObj, className: string, paramStr: string, funcEnum: string) { - let proxyFunc = replaceAll(proxyFuncTemplate, '[serviceName]', className); - proxyFunc = replaceAll(proxyFunc, '[funcName]', funcInfo.name); - proxyFunc = replaceAll(proxyFunc, '[params]', paramStr); - proxyFunc = replaceAll(proxyFunc, '[retType]', funcInfo.returns); - proxyFunc = replaceAll(proxyFunc, '[funcEnum]', funcEnum); - - // 入参处理 - let writeDataStr = ''; - let tab = getTab(1); - for (let i = 0; i < funcInfo.parameters.length; ++i) { - let param = funcInfo.parameters[i]; - writeDataStr += (i === 0) ? '' : '\n' + tab; - writeDataStr += genWrite(param.name, 'data', param.type) - } - proxyFunc = replaceAll(proxyFunc, '[writeData]', writeDataStr); - - // 返回值处理 - let readReplyStr = ''; - if (funcInfo.returns !== 'void') { - readReplyStr = format('%s result;', funcInfo.returns); - let destObj: ParamObj = { - 'name': 'result', - 'type': funcInfo.returns, - 'arraySize': -1, - }; - readReplyStr += '\n' + tab + genRead('reply', destObj); - readReplyStr += '\n' + tab + 'return result;'; - } - proxyFunc = replaceAll(proxyFunc, '[readReply]', readReplyStr); - - return proxyFunc; -} - -// 生成 xxx_service_proxy.cpp -export function genProxyCppFile(rootInfo: ServiceRootInfo, filePath: string, fileContent: string) { - let funcList: FuncObj[] = rootInfo.funcs; - let proxyFuncCpp = ''; - for (let i = 0; i < funcList.length; ++i) { - let funcEnum = funcList[i].name.toUpperCase(); - let paramStr = getFuncParamStr(funcList[i].parameters); - // proxy.cpp中的方法实现 - proxyFuncCpp += genProxyFunc(funcList[i], rootInfo.serviceName, paramStr, funcEnum); - } - fileContent = replaceAll(fileContent, '[serviceName]', rootInfo.serviceName); - fileContent = replaceAll(fileContent, '[marcoName]', rootInfo.serviceName.toUpperCase()); - fileContent = replaceAll(fileContent, '[lowServiceName]', rootInfo.serviceName.toLowerCase()); - fileContent = replaceAll(fileContent, '[remoteFuncImpl]', proxyFuncCpp); - fs.writeFileSync(filePath, fileContent); -} \ No newline at end of file diff --git a/src/vscode_plugin/src/gen/genproxyhfile.ts b/src/vscode_plugin/src/gen/genproxyhfile.ts deleted file mode 100644 index 0df4d188..00000000 --- a/src/vscode_plugin/src/gen/genproxyhfile.ts +++ /dev/null @@ -1,30 +0,0 @@ -/* -* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -import { replaceAll } from '../common/tool'; -import * as fs from 'fs'; -import { FuncObj, ServiceRootInfo } from './datatype'; -import { genDeclareContent } from './genCommonFunc'; - -// 生成 xxx_service_proxy.h -export function genProxyHFile(rootInfo: ServiceRootInfo, filePath: string, fileContent: string) { - let funcList: FuncObj[] = rootInfo.funcs; - let proxyFuncHContent = genDeclareContent(funcList); - fileContent = replaceAll(fileContent, '[serviceName]', rootInfo.serviceName); - fileContent = replaceAll(fileContent, '[marcoName]', rootInfo.serviceName.toUpperCase()); - fileContent = replaceAll(fileContent, '[lowServiceName]', rootInfo.serviceName.toLowerCase()); - fileContent = replaceAll(fileContent, '[proxyHFunctions]', proxyFuncHContent); - fs.writeFileSync(filePath, fileContent); -} \ No newline at end of file diff --git a/src/vscode_plugin/src/gen/gensa.ts b/src/vscode_plugin/src/gen/gensa.ts index 7cdd108f..ba2a5e09 100644 --- a/src/vscode_plugin/src/gen/gensa.ts +++ b/src/vscode_plugin/src/gen/gensa.ts @@ -18,15 +18,15 @@ import { sa_version_map } from '../template/sa/sadir'; import * as path from 'path'; import * as fs from 'fs'; import { DirTemp, ServiceRootInfo } from './datatype'; -import { genProxyHFile } from './genproxyhfile'; -import { genProxyCppFile } from './genproxycppfile'; -import { genSaHFile } from './gensahfile'; -import { genSaCppFile } from './gensacppfile'; -import { genIServiceHFile } from './geniservicehfile'; -import { genStubHFile } from './genstubhfile'; -import { genStubCppFile } from './genstubcppfile'; -import { genClientCppFile } from './genclientcppfile'; -import { genSaCommonFile } from './genCommonFile'; +import { genProxyHFile } from './tools/genproxyhfile'; +import { genProxyCppFile } from './tools/genproxycppfile'; +import { genSaHFile } from './tools/gensahfile'; +import { genSaCppFile } from './tools/gensacppfile'; +import { genIServiceHFile } from './tools/geniservicehfile'; +import { genStubHFile } from './tools/genstubhfile'; +import { genStubCppFile } from './tools/genstubcppfile'; +import { genClientCppFile } from './tools/genclientcppfile'; +import { genSaCommonFile } from './tools/gencommonfile'; const fileHandlers: { [key: string]: Function } = { '[serviceName]_service_proxy.h': genProxyHFile, diff --git a/src/vscode_plugin/src/gen/gensacppfile.ts b/src/vscode_plugin/src/gen/gensacppfile.ts deleted file mode 100644 index 3a893d76..00000000 --- a/src/vscode_plugin/src/gen/gensacppfile.ts +++ /dev/null @@ -1,64 +0,0 @@ -/* -* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -import { replaceAll } from '../common/tool'; -const numericTypes = ['short', 'int', 'long', 'long long', 'float', 'double']; -const boolType = ['bool']; -const charType = ['char', 'string']; -import { serviceFuncImplTemplate } from '../template/func_template'; -import * as fs from 'fs'; -import { FuncObj, ServiceRootInfo } from './datatype'; -import { getFuncParamStr } from './genCommonFunc'; - -function genServiceFunc(funcInfo: FuncObj, className: string, paramStr: string) { - let serviceFunc = replaceAll(serviceFuncImplTemplate, '[retType]', funcInfo.returns); - // 根据类型初始化返回值 - let initRetvalue; - // let paramsName = ''; - if (numericTypes.includes(funcInfo.returns)) { - // 数值类型初始化为0 - initRetvalue = '0'; - } else if (boolType.includes(funcInfo.returns)) { - // 布尔类型初始化为true - initRetvalue = 'true'; - } else if (charType.includes(funcInfo.returns)) { - // 字符类型初始化为空字符'' - initRetvalue = ''; - } else { - // 对于其他类型,这里可以根据需要进行处理 - // 假设是指针类型或其他复杂类型 - initRetvalue = 'nullptr'; - } - serviceFunc = replaceAll(serviceFunc, '[initRetvalue]', initRetvalue); - serviceFunc = replaceAll(serviceFunc, '[serviceName]', className); - serviceFunc = replaceAll(serviceFunc, '[funcName]', funcInfo.name); - serviceFunc = replaceAll(serviceFunc, '[params]', paramStr); - return serviceFunc; -} - -// 生成 xxx_service.cpp -export function genSaCppFile(rootInfo: ServiceRootInfo, filePath: string, fileContent: string) { - let funcList: FuncObj[] = rootInfo.funcs; - let serviceFuncCpp = ''; - for (let i = 0; i < funcList.length; ++i) { - let paramStr = getFuncParamStr(funcList[i].parameters); - serviceFuncCpp += genServiceFunc(funcList[i], rootInfo.serviceName, paramStr); - } - fileContent = replaceAll(fileContent, '[serviceName]', rootInfo.serviceName); - fileContent = replaceAll(fileContent, '[marcoName]', rootInfo.serviceName.toUpperCase()); - fileContent = replaceAll(fileContent, '[lowServiceName]', rootInfo.serviceName.toLowerCase()); - fileContent = replaceAll(fileContent, '[serviceFuncImpl]', serviceFuncCpp); - fs.writeFileSync(filePath, fileContent); -} \ No newline at end of file diff --git a/src/vscode_plugin/src/gen/gensahfile.ts b/src/vscode_plugin/src/gen/gensahfile.ts deleted file mode 100644 index b5a8f9f8..00000000 --- a/src/vscode_plugin/src/gen/gensahfile.ts +++ /dev/null @@ -1,30 +0,0 @@ -/* -* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -import { replaceAll } from '../common/tool'; -import * as fs from 'fs'; -import { FuncObj, ServiceRootInfo } from './datatype'; -import { genDeclareContent } from './genCommonFunc'; - -// 生成 xxx_service.h -export function genSaHFile(rootInfo: ServiceRootInfo, filePath: string, fileContent: string) { - let funcList: FuncObj[] = rootInfo.funcs; - let saFuncHContent = genDeclareContent(funcList); - fileContent = replaceAll(fileContent, '[serviceName]', rootInfo.serviceName); - fileContent = replaceAll(fileContent, '[marcoName]', rootInfo.serviceName.toUpperCase()); - fileContent = replaceAll(fileContent, '[lowServiceName]', rootInfo.serviceName.toLowerCase()); - fileContent = replaceAll(fileContent, '[serviceHFunctions]', saFuncHContent); - fs.writeFileSync(filePath, fileContent); -} \ No newline at end of file diff --git a/src/vscode_plugin/src/gen/genservicecppfile.ts b/src/vscode_plugin/src/gen/genservicecppfile.ts deleted file mode 100644 index f5b36673..00000000 --- a/src/vscode_plugin/src/gen/genservicecppfile.ts +++ /dev/null @@ -1,38 +0,0 @@ -/* -* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -import * as fs from 'fs'; -import { FuncObj, HdfRootInfo } from "./datatype"; -import { replaceAll } from '../common/tool'; -import { getServiceFuncParamStr } from './genservicehfile'; -import { hdiServiceFuncTemplate } from '../template/func_template'; - -export function genServiceCppFile(rootInfo: HdfRootInfo, filePath: string, fileContent: string) { - let hdiServiceFuncCpp = ''; - let funcList: FuncObj[] = rootInfo.funcs; - let upperName = rootInfo.driverName.substring(0, 1).toLocaleUpperCase(); - let marcoName = upperName + rootInfo.driverName.substring(1, rootInfo.driverName.length); - for (let i = 0; i < funcList.length; ++i) { - // xxx_interface_service.cpp的实现 - let paramStr = getServiceFuncParamStr(funcList[i]); - let serviceCppContent = replaceAll(hdiServiceFuncTemplate, '[functionName]', funcList[i].name); - serviceCppContent = replaceAll(serviceCppContent, '[marcoName]', marcoName); - hdiServiceFuncCpp = replaceAll(serviceCppContent, '[params]', paramStr); - } - fileContent = replaceAll(fileContent, '[driverName]', rootInfo.driverName); - fileContent = replaceAll(fileContent, '[marcoName]', marcoName); - fileContent = replaceAll(fileContent, '[serviceFuncListImpl]', hdiServiceFuncCpp); - fs.writeFileSync(filePath, fileContent); -} \ No newline at end of file diff --git a/src/vscode_plugin/src/gen/genservicehfile.ts b/src/vscode_plugin/src/gen/genservicehfile.ts deleted file mode 100644 index 1b9a8311..00000000 --- a/src/vscode_plugin/src/gen/genservicehfile.ts +++ /dev/null @@ -1,62 +0,0 @@ -/* -* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -import * as fs from 'fs'; -import { FuncObj, HdfRootInfo } from "./datatype"; -import { format } from 'util'; -import { getTab, replaceAll } from '../common/tool'; - -// 干脆这个用来表示hdf的service.h算了, 暂时先把hdf和sa的分开 - -// 生成方法实现 -export function getServiceFuncParamStr(funcObj: FuncObj) { - let paramStr = ''; - for (let i = 0; i < funcObj.parameters.length; ++i) { - // paramStr += (i === 0) ? '' : ', '; - // paramStr += funcObj.parameters[i].type + ' ' + funcObj.parameters[i].name + ', '; - paramStr += format('const %s& %s, ', funcObj.parameters[i].type, funcObj.parameters[i].name); - } - if (funcObj.returns !== 'void') { - let outName = funcObj.name + 'Out' - if (funcObj.returns === 'string') { - funcObj.returns = 'std::string'; - } - paramStr += funcObj.returns + '& ' + outName; - } else { - // 如果返回值是void, 去掉参数列表结尾逗号 - paramStr = paramStr.substring(0, paramStr.length - 2); - } - return paramStr; -} - -export function genServiceHFile(rootInfo: HdfRootInfo, filePath: string, fileContent: string) { - let hdiServiceFuncH = ''; - let funcTab = getTab(1); - let funcList: FuncObj[] = rootInfo.funcs; - for (let i = 0; i < funcList.length; ++i) { - // xxx_interface_service.h方法定义 - let paramStr = getServiceFuncParamStr(funcList[i]); - hdiServiceFuncH += (i === 0) ? '' : '\n' + funcTab; - hdiServiceFuncH += format('int32_t %s(%s) override;', funcList[i].name, paramStr) - } - let upperName = rootInfo.driverName.substring(0, 1).toLocaleUpperCase(); - let marcoName = upperName + rootInfo.driverName.substring(1, rootInfo.driverName.length); - let upperDriverName = rootInfo.driverName.toLocaleUpperCase(); - fileContent = replaceAll(fileContent, '[driverName]', rootInfo.driverName); - fileContent = replaceAll(fileContent, '[marcoName]', marcoName); - fileContent = replaceAll(fileContent, '[driverUpperName]', upperDriverName); - fileContent = replaceAll(fileContent, '[serviceFuncDeclare]', hdiServiceFuncH); - fs.writeFileSync(filePath, fileContent); -} \ No newline at end of file diff --git a/src/vscode_plugin/src/gen/genstubcppfile.ts b/src/vscode_plugin/src/gen/genstubcppfile.ts deleted file mode 100644 index b171181f..00000000 --- a/src/vscode_plugin/src/gen/genstubcppfile.ts +++ /dev/null @@ -1,85 +0,0 @@ -/* -* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -import { replaceAll, getTab } from '../common/tool'; -import { stubInnerFuncTemplate } from '../template/func_template'; -import * as fs from 'fs'; -import { format } from 'util' -import { FuncObj, ServiceRootInfo } from './datatype'; -import { genRead, genWrite } from './genCommonFunc'; - -function genStubInnerFunc(funcInfo: FuncObj, className: string) { - let innerFunc = replaceAll(stubInnerFuncTemplate, '[serviceName]', className); - innerFunc = replaceAll(innerFunc, '[funcName]', funcInfo.name); - - // 入参处理 - // 生成服务端读取客户端传参的代码段 - let readDataStr = ''; - let tab = getTab(1); - // 调用业务方法时传入的入参列表 - let innerParamStr = ''; - for (let i = 0; i < funcInfo.parameters.length; ++i) { - let param = funcInfo.parameters[i]; - let innerParamName = param.name + 'Val'; - if (i > 0) { - readDataStr += '\n' + tab; - innerParamStr += ' ,'; - } - - //将remote请求中的参数值读取到内部参数变量中 - // 定义内部参数变量 - readDataStr += format('%s %s;', param.type, innerParamName); - let destObj = { - 'name': param.name + 'Val', - 'type': param.type, - 'arraySize': -1 - }; - readDataStr += '\n' + tab + genRead('data', destObj); - innerParamStr += innerParamName; - } - innerFunc = replaceAll(innerFunc, '[readData]', readDataStr); - - // 调用service的实际业务逻辑实现方法 - // 生成调用服务端实现并返回结果的代码段 - let writeReplyStr = ''; - if (funcInfo.returns === 'void') { - writeReplyStr += format('%s(%s); // call business implementation', funcInfo.name, innerParamStr); - writeReplyStr += '\n' + tab + 'reply.WriteInt32(retCode);'; - } else { - writeReplyStr += format('%s retVal = %s(%s); // call business implementation', - funcInfo.returns, funcInfo.name, innerParamStr); - writeReplyStr += '\n' + tab + 'reply.WriteInt32(retCode);'; - writeReplyStr += '\n' + tab + genWrite('retVal', 'reply', funcInfo.returns); - } - innerFunc = replaceAll(innerFunc, '[writeReply]', writeReplyStr); - return innerFunc; -} - -// 生成 xxx_service_stub.cpp -export function genStubCppFile(rootInfo: ServiceRootInfo, filePath: string, fileContent: string) { - let stubInnerFuncMap = ''; - let stubInnerFuncCpp = ''; - let funcList: FuncObj[] = rootInfo.funcs; - for (let i = 0; i < funcList.length; ++i) { - let funcEnum = funcList[i].name.toUpperCase(); - stubInnerFuncMap += format('innerFuncs_[%s] = &%sStub::%sInner;', funcEnum, rootInfo.serviceName, funcList[i].name); - stubInnerFuncCpp += genStubInnerFunc(funcList[i], rootInfo.serviceName); - } - fileContent = replaceAll(fileContent, '[serviceName]', rootInfo.serviceName); - fileContent = replaceAll(fileContent, '[lowServiceName]', rootInfo.serviceName.toLowerCase()); - fileContent = replaceAll(fileContent, '[innerFuncMap]', stubInnerFuncMap); - fileContent = replaceAll(fileContent, '[innerFuncImpl]', stubInnerFuncCpp); - fs.writeFileSync(filePath, fileContent); -} \ No newline at end of file diff --git a/src/vscode_plugin/src/gen/genstubhfile.ts b/src/vscode_plugin/src/gen/genstubhfile.ts deleted file mode 100644 index 4f59ac0c..00000000 --- a/src/vscode_plugin/src/gen/genstubhfile.ts +++ /dev/null @@ -1,36 +0,0 @@ -/* -* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -import { replaceAll, getTab } from '../common/tool'; -import * as fs from 'fs'; -import { format } from 'util' -import { FuncObj, ServiceRootInfo } from './datatype'; - -// 生成 xxx_service_stub.h -export function genStubHFile(rootInfo: ServiceRootInfo, filePath: string, fileContent: string) { - let stubInnerFuncH = ''; - let funcList: FuncObj[] = rootInfo.funcs; - let funcTab = getTab(1); - for (let i = 0; i < funcList.length; ++i) { - stubInnerFuncH += (i === 0) ? '' : '\n' + funcTab; - stubInnerFuncH += - format('ErrCode %sInner(MessageParcel &data, MessageParcel &reply);', funcList[i].name); - } - fileContent = replaceAll(fileContent, '[serviceName]', rootInfo.serviceName); - fileContent = replaceAll(fileContent, '[marcoName]', rootInfo.serviceName.toUpperCase()); - fileContent = replaceAll(fileContent, '[lowServiceName]', rootInfo.serviceName.toLowerCase()); - fileContent = replaceAll(fileContent, '[innerFuncDef]', stubInnerFuncH); - fs.writeFileSync(filePath, fileContent); -} \ No newline at end of file From cabcb3894195f343e36678154cf03f862d62c458 Mon Sep 17 00:00:00 2001 From: wangshi Date: Mon, 10 Feb 2025 16:18:09 +0800 Subject: [PATCH 2/4] update export function Signed-off-by: wangshi --- src/vscode_plugin/src/common/tool.ts | 2 +- .../src/gen/tools/genclientcppfile.ts | 51 +++++++++ .../src/gen/tools/gencommonfile.ts | 39 +++++++ .../src/gen/tools/gencommonfunc.ts | 104 ++++++++++++++++++ src/vscode_plugin/src/gen/tools/genidlfile.ts | 99 +++++++++++++++++ .../src/gen/tools/geniservicehfile.ts | 46 ++++++++ .../src/gen/tools/genproxycppfile.ts | 77 +++++++++++++ .../src/gen/tools/genproxyhfile.ts | 35 ++++++ .../src/gen/tools/gensacppfile.ts | 69 ++++++++++++ src/vscode_plugin/src/gen/tools/gensahfile.ts | 35 ++++++ .../src/gen/tools/genservicecppfile.ts | 43 ++++++++ .../src/gen/tools/genservicehfile.ts | 67 +++++++++++ .../src/gen/tools/genstubcppfile.ts | 90 +++++++++++++++ .../src/gen/tools/genstubhfile.ts | 40 +++++++ src/vscode_plugin/src/parse/parsec.ts | 18 +-- src/vscode_plugin/src/parse/parsets.ts | 4 +- src/vscode_plugin/src/test/readme.md | 62 +++++++++++ .../tool.test.ts} | 3 +- .../src/test/suite/gen/genidlfile.test.ts | 65 +++++++++++ .../src/test/suite/parse/parsec.test.ts | 63 +++++++++++ 20 files changed, 999 insertions(+), 13 deletions(-) create mode 100644 src/vscode_plugin/src/gen/tools/genclientcppfile.ts create mode 100644 src/vscode_plugin/src/gen/tools/gencommonfile.ts create mode 100644 src/vscode_plugin/src/gen/tools/gencommonfunc.ts create mode 100644 src/vscode_plugin/src/gen/tools/genidlfile.ts create mode 100644 src/vscode_plugin/src/gen/tools/geniservicehfile.ts create mode 100644 src/vscode_plugin/src/gen/tools/genproxycppfile.ts create mode 100644 src/vscode_plugin/src/gen/tools/genproxyhfile.ts create mode 100644 src/vscode_plugin/src/gen/tools/gensacppfile.ts create mode 100644 src/vscode_plugin/src/gen/tools/gensahfile.ts create mode 100644 src/vscode_plugin/src/gen/tools/genservicecppfile.ts create mode 100644 src/vscode_plugin/src/gen/tools/genservicehfile.ts create mode 100644 src/vscode_plugin/src/gen/tools/genstubcppfile.ts create mode 100644 src/vscode_plugin/src/gen/tools/genstubhfile.ts create mode 100644 src/vscode_plugin/src/test/readme.md rename src/vscode_plugin/src/test/suite/{common_tool.test.ts => common/tool.test.ts} (98%) create mode 100644 src/vscode_plugin/src/test/suite/gen/genidlfile.test.ts create mode 100644 src/vscode_plugin/src/test/suite/parse/parsec.test.ts diff --git a/src/vscode_plugin/src/common/tool.ts b/src/vscode_plugin/src/common/tool.ts index 070945cd..9cba22a4 100644 --- a/src/vscode_plugin/src/common/tool.ts +++ b/src/vscode_plugin/src/common/tool.ts @@ -14,7 +14,7 @@ */ export function replaceAll(s: string, sfrom: string, sto: any) { - console.log('[replaceall] s:'+s+' sfrom:'+sfrom+' sto:'+sto); + // console.log('[replaceall] s:'+s+' sfrom:'+sfrom+' sto:'+sto); if (s && sfrom && sto) { while (s.indexOf(sfrom) >= 0) { s = s.replace(sfrom, sto); diff --git a/src/vscode_plugin/src/gen/tools/genclientcppfile.ts b/src/vscode_plugin/src/gen/tools/genclientcppfile.ts new file mode 100644 index 00000000..779963d7 --- /dev/null +++ b/src/vscode_plugin/src/gen/tools/genclientcppfile.ts @@ -0,0 +1,51 @@ + +/* +* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import { replaceAll, getTab } from '../../common/tool'; +import * as fs from 'fs'; +import { format } from 'util' +import { FuncObj, ParamObj, ServiceRootInfo } from '../datatype'; + +function getClientFuncParamStr(params: ParamObj[]) { + let paramStr = ''; + for (let i = 0; i < params.length; ++i) { + paramStr += (i === 0) ? '' : ', '; + paramStr += params[i].name; + } + return paramStr; +} + +export function doGenClientCppFile(rootInfo: ServiceRootInfo, fileContent: string): string { + let clientFuncCpp = ''; + let funcList: FuncObj[] = rootInfo.funcs; + let funcTab = getTab(1); + for (let i = 0; i < funcList.length; ++i) { + let clientParamStr = getClientFuncParamStr(funcList[i].parameters); + clientFuncCpp += (i === 0) ? '' : '\n' + funcTab; + clientFuncCpp += format('// proxy->%s(%s);', funcList[i].name, clientParamStr); + } + fileContent = replaceAll(fileContent, '[serviceName]', rootInfo.serviceName); + fileContent = replaceAll(fileContent, '[marcoName]', rootInfo.serviceName.toUpperCase()); + fileContent = replaceAll(fileContent, '[lowServiceName]', rootInfo.serviceName.toLowerCase()); + fileContent = replaceAll(fileContent, '[clientFuncInvoke]', clientFuncCpp); + return fileContent; +} + +// 生成xxx_client.cpp +export function genClientCppFile(rootInfo: ServiceRootInfo, filePath: string, fileContent: string) { + + fs.writeFileSync(filePath, fileContent); +} \ No newline at end of file diff --git a/src/vscode_plugin/src/gen/tools/gencommonfile.ts b/src/vscode_plugin/src/gen/tools/gencommonfile.ts new file mode 100644 index 00000000..219c0f6b --- /dev/null +++ b/src/vscode_plugin/src/gen/tools/gencommonfile.ts @@ -0,0 +1,39 @@ +/* +* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import * as fs from 'fs'; +import { HdfRootInfo, ServiceRootInfo } from "../datatype"; +import { replaceAll } from '../../common/tool'; + +// 生成sa非特殊处理的文件, 如xxx.cfg +export function genSaCommonFile(rootInfo: ServiceRootInfo, filePath: string, fileContent: string) { + fileContent = replaceAll(fileContent, '[serviceName]', rootInfo.serviceName); + fileContent = replaceAll(fileContent, '[marcoName]', rootInfo.serviceName.toUpperCase()); + fileContent = replaceAll(fileContent, '[lowServiceName]', rootInfo.serviceName.toLowerCase()); + fileContent = replaceAll(fileContent, '[serviceId]', rootInfo.serviceId); + fs.writeFileSync(filePath, fileContent); +} + +// 生成hdf非特殊处理的文件, 如xxx.hcs +export function genHdfCommonFile(rootInfo: HdfRootInfo, filePath: string, fileContent: string) { + let upperName = rootInfo.driverName.substring(0, 1).toLocaleUpperCase(); + let marcoName = upperName + rootInfo.driverName.substring(1, rootInfo.driverName.length); + let upperDriverName = rootInfo.driverName.toLocaleUpperCase(); + fileContent = replaceAll(fileContent, '[driverName]', rootInfo.driverName); + fileContent = replaceAll(fileContent, '[marcoName]', marcoName); + fileContent = replaceAll(fileContent, '[driverUpperName]', upperDriverName); + fs.writeFileSync(filePath, fileContent); +} + diff --git a/src/vscode_plugin/src/gen/tools/gencommonfunc.ts b/src/vscode_plugin/src/gen/tools/gencommonfunc.ts new file mode 100644 index 00000000..f2cd0f11 --- /dev/null +++ b/src/vscode_plugin/src/gen/tools/gencommonfunc.ts @@ -0,0 +1,104 @@ +/* +* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import { getTab } from '../../common/tool'; +import { getReg, match } from '../../common/re'; +import { format } from 'util' +import { FuncObj, ParamObj } from '../datatype'; +import { transferMap } from '../../template/functypemap_template' + +export function getFuncParamStr(params: ParamObj[]) { + let paramStr = ''; + for (let i = 0; i < params.length; ++i) { + paramStr += (i === 0) ? '' : ', '; + paramStr += params[i].type + ' ' + params[i].name; + } + return paramStr; +} + +// 生成头文件中的方法声明内容 +export function genDeclareContent(funcList: FuncObj[]) { + let funcTab = getTab(1); + let saFuncHContent = ''; + for (let i = 0; i < funcList.length; ++i) { + let paramStr = getFuncParamStr(funcList[i].parameters); + // proxy.h中的方法定义 + saFuncHContent += (i === 0) ? '' : '\n' + funcTab; + saFuncHContent += format('%s %s(%s) override;', funcList[i].returns, funcList[i].name, paramStr); + } + return saFuncHContent; +} + +// 常用类型转换表, 将C语言常见类型(key)转换为remote data读写函数使用的类型(value) +// 例如 ErrCode 类型在框架中的系统原型为int类型,这里映射成int32_t, +// 因为int32_t类型在 DATA_W_MAP/DATA_R_MAP 表中有对应的读写数据方法(WriteInt32/ReadInt32) +const TYPE_DEF_MAP = new Map( + [['ErrCode', 'int32_t'], ['char', 'int8_t'], ['short', 'int16_t'], ['int', 'int32_t'], ['long', 'int64_t'], + ['unsigned char', 'uint8_t'], ['unsigned short', 'uint16_t'], ['unsigned int', 'uint32_t'], + ['unsigned long', 'uint64_t'], ['double_t', 'double'], ['float_t', 'float'], ['size_t', 'double'], + ['long long', 'double'], ['long double', 'double'], ['std::string', 'string'] + ]); + +export function getParcelType(srcType: string) { + let parcelType = TYPE_DEF_MAP.get(srcType); + return parcelType === undefined ? srcType : parcelType; +} + +export function getTransferContent(parcelVecType: string, isWrite: number) { + let rwFunc = ''; + for (let index = 0; index < transferMap.length; index++) { + if (parcelVecType === transferMap[index].fromType) { + rwFunc = transferMap[index].tranferContent[isWrite]; + } + } + return rwFunc; +} + +export function genWrite(srcName: string, parcelName: string, vType: string) { + let matchs = match('(std::)?vector<([\x21-\x7e]+)[ ]?>', vType); + if (matchs) { + // vector类型变量包装成parcel data + let rawType = getReg(vType, matchs.regs[2]); + let parcelVecType = 'vector<' + getParcelType(rawType) + '>'; + let wVecFunc = getTransferContent(parcelVecType, 0); + if (wVecFunc === '') { + return ''; + } + return format('%s.%s(%s);', parcelName, wVecFunc, srcName); + } + + let parcelType = getParcelType(vType); + let wFunc = getTransferContent(parcelType, 0); + + return format('%s.%s(%s);', parcelName, wFunc, srcName); +} + +export function genRead(parcelName: string, destObj: ParamObj) { + let matchs = match('(std::)?vector<([\x21-\x7e]+)[ ]?>', destObj.type); + if (matchs) { + // 从parcel data中读取vector类型变量 + let rawType = getReg(destObj.type, matchs.regs[2]); + let parcelVecType = getParcelType(rawType); + let rVecFunc = 'vector<' + getTransferContent(parcelVecType, 1) + '>'; + if (rVecFunc === '') { + return ''; + } + return format('%s.%s(&(%s));', parcelName, rVecFunc, parcelName); + } + + let parcelType = getParcelType(destObj.type); + let rFunc = getTransferContent(parcelType, 1); + return format('%s = %s.%s();', destObj.name, parcelName, rFunc); +} \ No newline at end of file diff --git a/src/vscode_plugin/src/gen/tools/genidlfile.ts b/src/vscode_plugin/src/gen/tools/genidlfile.ts new file mode 100644 index 00000000..e592243d --- /dev/null +++ b/src/vscode_plugin/src/gen/tools/genidlfile.ts @@ -0,0 +1,99 @@ +/* +* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import * as fs from 'fs'; +import { FuncObj, HdfRootInfo } from "../datatype"; +import { idlTransferType } from '../../template/functypemap_template'; +import { format } from 'util'; +import { getTab, replaceAll } from '../../common/tool'; + +// 常用类型转换表, 将C语言常见类型(key)转换为remote data读写函数使用的类型(value) +// 例如 ErrCode 类型在框架中的系统原型为int类型,这里映射成int32_t, +// 因为int32_t类型在 DATA_W_MAP/DATA_R_MAP 表中有对应的读写数据方法(WriteInt32/ReadInt32) +const TYPE_DEF_MAP = new Map([ + ['std::string', 'string'], + ['char *', 'string'] +]); + +export function getParcelType(srcType: string) { + let parcelType = TYPE_DEF_MAP.get(srcType); + return parcelType === undefined ? srcType : parcelType; +} + +// idlTransferType +export function getIdlType(cType: string) { + let rawType = getParcelType(cType); + for (let index = 0; index < idlTransferType.length; index++) { + if (rawType === idlTransferType[index].fromType) { + return idlTransferType[index].tranferContent; + } + } + return cType; +} + +export function isReturn(type: string) { + if (type) { + if (type.indexOf('&')>0 || type.indexOf('**')>0) { + return true; + } + } + return false; +} + +export function getIdlFuncParamStr(funcObj: FuncObj) { + let idlParams = ''; + for (let i = 0; i < funcObj.parameters.length; ++i) { + let type = getIdlType(funcObj.parameters[i].type); + // idlParams += (i === 0) && (funcObj.returns !== 'void') ? '' : ', '; + if (isReturn(funcObj.parameters[i].type)) { + idlParams += '[out] ' + type + ' ' + funcObj.parameters[i].name + ', '; + } else { + idlParams += '[in] ' + type + ' ' + funcObj.parameters[i].name + ', '; + } + } + if (funcObj.returns !== 'void') { + let retType = getIdlType(funcObj.returns); + let outName = funcObj.name + 'Out' + idlParams += '[out] ' + retType + ' ' + outName; + } else { + // 如果返回值是void, 去掉参数列表结尾逗号 + idlParams = idlParams.substring(0, idlParams.length - 2); + } + return idlParams; +} + +export function doGenIdlFile(rootInfo: HdfRootInfo, fileContent: string): string { + let funcTab = getTab(1); + let idlFuncDefine = ''; + let funcList: FuncObj[] = rootInfo.funcs; + for (let i = 0; i < funcList.length; ++i) { + // 生成idl接口定义 + let paramIdlStr = getIdlFuncParamStr(funcList[i]); + idlFuncDefine += (i === 0) ? '' : '\n' + funcTab; + idlFuncDefine += format('%s(%s);', funcList[i].name, paramIdlStr); + } + let upperName = rootInfo.driverName.substring(0, 1).toLocaleUpperCase(); + let marcoName = upperName + rootInfo.driverName.substring(1, rootInfo.driverName.length); + fileContent = replaceAll(fileContent, '[driverName]', rootInfo.driverName); + fileContent = replaceAll(fileContent, '[marcoName]', marcoName); + fileContent = replaceAll(fileContent, '[idlFunDeclare]', idlFuncDefine); + return fileContent; +} + +// 每个方法一个文件 +export function genIdlFile(rootInfo: HdfRootInfo, filePath: string, fileContent: string) { + fileContent = doGenIdlFile(rootInfo, fileContent); + fs.writeFileSync(filePath, fileContent); +} \ No newline at end of file diff --git a/src/vscode_plugin/src/gen/tools/geniservicehfile.ts b/src/vscode_plugin/src/gen/tools/geniservicehfile.ts new file mode 100644 index 00000000..557b1e0e --- /dev/null +++ b/src/vscode_plugin/src/gen/tools/geniservicehfile.ts @@ -0,0 +1,46 @@ +/* +* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import { replaceAll, getTab } from '../../common/tool'; +import * as fs from 'fs'; +import { format } from 'util' +import { FuncObj, ServiceRootInfo } from '../datatype'; +import { getFuncParamStr } from '../tools/gencommonfunc'; + +export function doGenIServiceHFile(rootInfo: ServiceRootInfo, fileContent: string): string { + let iServiceFuncH = ''; + let funcEnumStr = ''; + let funcList: FuncObj[] = rootInfo.funcs; + let funcTab = getTab(1); + let enumTab = getTab(2); + for (let i = 0; i < funcList.length; ++i) { + let funcEnum = funcList[i].name.toUpperCase(); + let paramStr = getFuncParamStr(funcList[i].parameters); + iServiceFuncH += (i === 0) ? '' : '\n' + funcTab; + iServiceFuncH += format('virtual %s %s(%s) = 0;', funcList[i].returns, funcList[i].name, paramStr); + funcEnumStr += (i === 0) ? '' : ',\n' + enumTab; + funcEnumStr += funcEnum; + } + fileContent = replaceAll(fileContent, '[serviceName]', rootInfo.serviceName); + fileContent = replaceAll(fileContent, '[marcoName]', rootInfo.serviceName.toUpperCase()); + fileContent = replaceAll(fileContent, '[funcEnum]', funcEnumStr); + fileContent = replaceAll(fileContent, '[iServiceHFunctions]', iServiceFuncH); + return fileContent; +} + +export function genIServiceHFile(rootInfo: ServiceRootInfo, filePath: string, fileContent: string) { + fileContent = doGenIServiceHFile(rootInfo, fileContent); + fs.writeFileSync(filePath, fileContent); +} \ No newline at end of file diff --git a/src/vscode_plugin/src/gen/tools/genproxycppfile.ts b/src/vscode_plugin/src/gen/tools/genproxycppfile.ts new file mode 100644 index 00000000..377acb93 --- /dev/null +++ b/src/vscode_plugin/src/gen/tools/genproxycppfile.ts @@ -0,0 +1,77 @@ +/* +* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import { getTab, replaceAll } from '../../common/tool'; +import * as fs from 'fs'; +import { format } from 'util' +import { FuncObj, ParamObj, ServiceRootInfo } from '../datatype'; +import { genRead, genWrite, getFuncParamStr } from '../tools/gencommonfunc'; +import { proxyFuncTemplate } from '../../template/func_template'; + +export function genProxyFunc(funcInfo: FuncObj, className: string, paramStr: string, funcEnum: string) { + let proxyFunc = replaceAll(proxyFuncTemplate, '[serviceName]', className); + proxyFunc = replaceAll(proxyFunc, '[funcName]', funcInfo.name); + proxyFunc = replaceAll(proxyFunc, '[params]', paramStr); + proxyFunc = replaceAll(proxyFunc, '[retType]', funcInfo.returns); + proxyFunc = replaceAll(proxyFunc, '[funcEnum]', funcEnum); + + // 入参处理 + let writeDataStr = ''; + let tab = getTab(1); + for (let i = 0; i < funcInfo.parameters.length; ++i) { + let param = funcInfo.parameters[i]; + writeDataStr += (i === 0) ? '' : '\n' + tab; + writeDataStr += genWrite(param.name, 'data', param.type) + } + proxyFunc = replaceAll(proxyFunc, '[writeData]', writeDataStr); + + // 返回值处理 + let readReplyStr = ''; + if (funcInfo.returns !== 'void') { + readReplyStr = format('%s result;', funcInfo.returns); + let destObj: ParamObj = { + 'name': 'result', + 'type': funcInfo.returns, + 'arraySize': -1, + }; + readReplyStr += '\n' + tab + genRead('reply', destObj); + readReplyStr += '\n' + tab + 'return result;'; + } + proxyFunc = replaceAll(proxyFunc, '[readReply]', readReplyStr); + + return proxyFunc; +} + +export function doGenProxyCppFile(rootInfo: ServiceRootInfo, fileContent: string) { + let funcList: FuncObj[] = rootInfo.funcs; + let proxyFuncCpp = ''; + for (let i = 0; i < funcList.length; ++i) { + let funcEnum = funcList[i].name.toUpperCase(); + let paramStr = getFuncParamStr(funcList[i].parameters); + // proxy.cpp中的方法实现 + proxyFuncCpp += genProxyFunc(funcList[i], rootInfo.serviceName, paramStr, funcEnum); + } + fileContent = replaceAll(fileContent, '[serviceName]', rootInfo.serviceName); + fileContent = replaceAll(fileContent, '[marcoName]', rootInfo.serviceName.toUpperCase()); + fileContent = replaceAll(fileContent, '[lowServiceName]', rootInfo.serviceName.toLowerCase()); + fileContent = replaceAll(fileContent, '[remoteFuncImpl]', proxyFuncCpp); + return fileContent; +} + +// 生成 xxx_service_proxy.cpp +export function genProxyCppFile(rootInfo: ServiceRootInfo, filePath: string, fileContent: string) { + fileContent = doGenProxyCppFile(rootInfo, fileContent); + fs.writeFileSync(filePath, fileContent); +} \ No newline at end of file diff --git a/src/vscode_plugin/src/gen/tools/genproxyhfile.ts b/src/vscode_plugin/src/gen/tools/genproxyhfile.ts new file mode 100644 index 00000000..a7c19e9d --- /dev/null +++ b/src/vscode_plugin/src/gen/tools/genproxyhfile.ts @@ -0,0 +1,35 @@ +/* +* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import { replaceAll } from '../../common/tool'; +import * as fs from 'fs'; +import { FuncObj, ServiceRootInfo } from '../datatype'; +import { genDeclareContent } from './gencommonfunc'; + +export function doGenProxyHFile(rootInfo: ServiceRootInfo, fileContent: string): string { + let funcList: FuncObj[] = rootInfo.funcs; + let proxyFuncHContent = genDeclareContent(funcList); + fileContent = replaceAll(fileContent, '[serviceName]', rootInfo.serviceName); + fileContent = replaceAll(fileContent, '[marcoName]', rootInfo.serviceName.toUpperCase()); + fileContent = replaceAll(fileContent, '[lowServiceName]', rootInfo.serviceName.toLowerCase()); + fileContent = replaceAll(fileContent, '[proxyHFunctions]', proxyFuncHContent); + return fileContent; +} + +// 生成 xxx_service_proxy.h +export function genProxyHFile(rootInfo: ServiceRootInfo, filePath: string, fileContent: string) { + fileContent = doGenProxyHFile(rootInfo, fileContent); + fs.writeFileSync(filePath, fileContent); +} \ No newline at end of file diff --git a/src/vscode_plugin/src/gen/tools/gensacppfile.ts b/src/vscode_plugin/src/gen/tools/gensacppfile.ts new file mode 100644 index 00000000..137bfbff --- /dev/null +++ b/src/vscode_plugin/src/gen/tools/gensacppfile.ts @@ -0,0 +1,69 @@ +/* +* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import { replaceAll } from '../../common/tool'; +const numericTypes = ['short', 'int', 'long', 'long long', 'float', 'double']; +const boolType = ['bool']; +const charType = ['char', 'string']; +import { serviceFuncImplTemplate } from '../../template/func_template'; +import * as fs from 'fs'; +import { FuncObj, ServiceRootInfo } from '../datatype'; +import { getFuncParamStr } from './gencommonfunc'; + +export function genServiceFunc(funcInfo: FuncObj, className: string, paramStr: string) { + let serviceFunc = replaceAll(serviceFuncImplTemplate, '[retType]', funcInfo.returns); + // 根据类型初始化返回值 + let initRetvalue; + // let paramsName = ''; + if (numericTypes.includes(funcInfo.returns)) { + // 数值类型初始化为0 + initRetvalue = '0'; + } else if (boolType.includes(funcInfo.returns)) { + // 布尔类型初始化为true + initRetvalue = 'true'; + } else if (charType.includes(funcInfo.returns)) { + // 字符类型初始化为空字符'' + initRetvalue = ''; + } else { + // 对于其他类型,这里可以根据需要进行处理 + // 假设是指针类型或其他复杂类型 + initRetvalue = 'nullptr'; + } + serviceFunc = replaceAll(serviceFunc, '[initRetvalue]', initRetvalue); + serviceFunc = replaceAll(serviceFunc, '[serviceName]', className); + serviceFunc = replaceAll(serviceFunc, '[funcName]', funcInfo.name); + serviceFunc = replaceAll(serviceFunc, '[params]', paramStr); + return serviceFunc; +} + +export function doGenSaCppFile(rootInfo: ServiceRootInfo, fileContent: string): string { + let funcList: FuncObj[] = rootInfo.funcs; + let serviceFuncCpp = ''; + for (let i = 0; i < funcList.length; ++i) { + let paramStr = getFuncParamStr(funcList[i].parameters); + serviceFuncCpp += genServiceFunc(funcList[i], rootInfo.serviceName, paramStr); + } + fileContent = replaceAll(fileContent, '[serviceName]', rootInfo.serviceName); + fileContent = replaceAll(fileContent, '[marcoName]', rootInfo.serviceName.toUpperCase()); + fileContent = replaceAll(fileContent, '[lowServiceName]', rootInfo.serviceName.toLowerCase()); + fileContent = replaceAll(fileContent, '[serviceFuncImpl]', serviceFuncCpp); + return fileContent; +} + +// 生成 xxx_service.cpp +export function genSaCppFile(rootInfo: ServiceRootInfo, filePath: string, fileContent: string) { + fileContent = doGenSaCppFile(rootInfo, fileContent); + fs.writeFileSync(filePath, fileContent); +} \ No newline at end of file diff --git a/src/vscode_plugin/src/gen/tools/gensahfile.ts b/src/vscode_plugin/src/gen/tools/gensahfile.ts new file mode 100644 index 00000000..2e6364b7 --- /dev/null +++ b/src/vscode_plugin/src/gen/tools/gensahfile.ts @@ -0,0 +1,35 @@ +/* +* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import { replaceAll } from '../../common/tool'; +import * as fs from 'fs'; +import { FuncObj, ServiceRootInfo } from '../datatype'; +import { genDeclareContent } from './gencommonfunc'; + +export function doGenSaHFile(rootInfo: ServiceRootInfo, fileContent: string): string { + let funcList: FuncObj[] = rootInfo.funcs; + let saFuncHContent = genDeclareContent(funcList); + fileContent = replaceAll(fileContent, '[serviceName]', rootInfo.serviceName); + fileContent = replaceAll(fileContent, '[marcoName]', rootInfo.serviceName.toUpperCase()); + fileContent = replaceAll(fileContent, '[lowServiceName]', rootInfo.serviceName.toLowerCase()); + fileContent = replaceAll(fileContent, '[serviceHFunctions]', saFuncHContent); + return fileContent; +} + +// 生成 xxx_service.h +export function genSaHFile(rootInfo: ServiceRootInfo, filePath: string, fileContent: string) { + fileContent = doGenSaHFile(rootInfo, fileContent); + fs.writeFileSync(filePath, fileContent); +} \ No newline at end of file diff --git a/src/vscode_plugin/src/gen/tools/genservicecppfile.ts b/src/vscode_plugin/src/gen/tools/genservicecppfile.ts new file mode 100644 index 00000000..9fbabc08 --- /dev/null +++ b/src/vscode_plugin/src/gen/tools/genservicecppfile.ts @@ -0,0 +1,43 @@ +/* +* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import * as fs from 'fs'; +import { FuncObj, HdfRootInfo } from "../datatype"; +import { replaceAll } from '../../common/tool'; +import { getServiceFuncParamStr } from './genservicehfile'; +import { hdiServiceFuncTemplate } from '../../template/func_template'; + +export function doGenServiceCppFile(rootInfo: HdfRootInfo, fileContent: string): string { + let hdiServiceFuncCpp = ''; + let funcList: FuncObj[] = rootInfo.funcs; + let upperName = rootInfo.driverName.substring(0, 1).toLocaleUpperCase(); + let marcoName = upperName + rootInfo.driverName.substring(1, rootInfo.driverName.length); + for (let i = 0; i < funcList.length; ++i) { + // xxx_interface_service.cpp的实现 + let paramStr = getServiceFuncParamStr(funcList[i]); + let serviceCppContent = replaceAll(hdiServiceFuncTemplate, '[functionName]', funcList[i].name); + serviceCppContent = replaceAll(serviceCppContent, '[marcoName]', marcoName); + hdiServiceFuncCpp = replaceAll(serviceCppContent, '[params]', paramStr); + } + fileContent = replaceAll(fileContent, '[driverName]', rootInfo.driverName); + fileContent = replaceAll(fileContent, '[marcoName]', marcoName); + fileContent = replaceAll(fileContent, '[serviceFuncListImpl]', hdiServiceFuncCpp); + return fileContent; +} + +export function genServiceCppFile(rootInfo: HdfRootInfo, filePath: string, fileContent: string) { + fileContent = doGenServiceCppFile(rootInfo, fileContent); + fs.writeFileSync(filePath, fileContent); +} \ No newline at end of file diff --git a/src/vscode_plugin/src/gen/tools/genservicehfile.ts b/src/vscode_plugin/src/gen/tools/genservicehfile.ts new file mode 100644 index 00000000..a3e0c9fc --- /dev/null +++ b/src/vscode_plugin/src/gen/tools/genservicehfile.ts @@ -0,0 +1,67 @@ +/* +* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import * as fs from 'fs'; +import { FuncObj, HdfRootInfo } from "../datatype"; +import { format } from 'util'; +import { getTab, replaceAll } from '../../common/tool'; + +// 干脆这个用来表示hdf的service.h算了, 暂时先把hdf和sa的分开 + +// 生成方法实现 +export function getServiceFuncParamStr(funcObj: FuncObj) { + let paramStr = ''; + for (let i = 0; i < funcObj.parameters.length; ++i) { + // paramStr += (i === 0) ? '' : ', '; + // paramStr += funcObj.parameters[i].type + ' ' + funcObj.parameters[i].name + ', '; + paramStr += format('const %s& %s, ', funcObj.parameters[i].type, funcObj.parameters[i].name); + } + if (funcObj.returns !== 'void') { + let outName = funcObj.name + 'Out' + if (funcObj.returns === 'string') { + funcObj.returns = 'std::string'; + } + paramStr += funcObj.returns + '& ' + outName; + } else { + // 如果返回值是void, 去掉参数列表结尾逗号 + paramStr = paramStr.substring(0, paramStr.length - 2); + } + return paramStr; +} + +export function doGenServiceHFile(rootInfo: HdfRootInfo, fileContent: string): string { + let hdiServiceFuncH = ''; + let funcTab = getTab(1); + let funcList: FuncObj[] = rootInfo.funcs; + for (let i = 0; i < funcList.length; ++i) { + // xxx_interface_service.h方法定义 + let paramStr = getServiceFuncParamStr(funcList[i]); + hdiServiceFuncH += (i === 0) ? '' : '\n' + funcTab; + hdiServiceFuncH += format('int32_t %s(%s) override;', funcList[i].name, paramStr) + } + let upperName = rootInfo.driverName.substring(0, 1).toLocaleUpperCase(); + let marcoName = upperName + rootInfo.driverName.substring(1, rootInfo.driverName.length); + let upperDriverName = rootInfo.driverName.toLocaleUpperCase(); + fileContent = replaceAll(fileContent, '[driverName]', rootInfo.driverName); + fileContent = replaceAll(fileContent, '[marcoName]', marcoName); + fileContent = replaceAll(fileContent, '[driverUpperName]', upperDriverName); + fileContent = replaceAll(fileContent, '[serviceFuncDeclare]', hdiServiceFuncH); + return fileContent; +} + +export function genServiceHFile(rootInfo: HdfRootInfo, filePath: string, fileContent: string) { + fileContent = doGenServiceHFile(rootInfo, fileContent); + fs.writeFileSync(filePath, fileContent); +} \ No newline at end of file diff --git a/src/vscode_plugin/src/gen/tools/genstubcppfile.ts b/src/vscode_plugin/src/gen/tools/genstubcppfile.ts new file mode 100644 index 00000000..b44f7623 --- /dev/null +++ b/src/vscode_plugin/src/gen/tools/genstubcppfile.ts @@ -0,0 +1,90 @@ +/* +* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import { replaceAll, getTab } from '../../common/tool'; +import { stubInnerFuncTemplate } from '../../template/func_template'; +import * as fs from 'fs'; +import { format } from 'util' +import { FuncObj, ServiceRootInfo } from '../datatype'; +import { genRead, genWrite } from './gencommonfunc'; + +function genStubInnerFunc(funcInfo: FuncObj, className: string) { + let innerFunc = replaceAll(stubInnerFuncTemplate, '[serviceName]', className); + innerFunc = replaceAll(innerFunc, '[funcName]', funcInfo.name); + + // 入参处理 + // 生成服务端读取客户端传参的代码段 + let readDataStr = ''; + let tab = getTab(1); + // 调用业务方法时传入的入参列表 + let innerParamStr = ''; + for (let i = 0; i < funcInfo.parameters.length; ++i) { + let param = funcInfo.parameters[i]; + let innerParamName = param.name + 'Val'; + if (i > 0) { + readDataStr += '\n' + tab; + innerParamStr += ' ,'; + } + + //将remote请求中的参数值读取到内部参数变量中 + // 定义内部参数变量 + readDataStr += format('%s %s;', param.type, innerParamName); + let destObj = { + 'name': param.name + 'Val', + 'type': param.type, + 'arraySize': -1 + }; + readDataStr += '\n' + tab + genRead('data', destObj); + innerParamStr += innerParamName; + } + innerFunc = replaceAll(innerFunc, '[readData]', readDataStr); + + // 调用service的实际业务逻辑实现方法 + // 生成调用服务端实现并返回结果的代码段 + let writeReplyStr = ''; + if (funcInfo.returns === 'void') { + writeReplyStr += format('%s(%s); // call business implementation', funcInfo.name, innerParamStr); + writeReplyStr += '\n' + tab + 'reply.WriteInt32(retCode);'; + } else { + writeReplyStr += format('%s retVal = %s(%s); // call business implementation', + funcInfo.returns, funcInfo.name, innerParamStr); + writeReplyStr += '\n' + tab + 'reply.WriteInt32(retCode);'; + writeReplyStr += '\n' + tab + genWrite('retVal', 'reply', funcInfo.returns); + } + innerFunc = replaceAll(innerFunc, '[writeReply]', writeReplyStr); + return innerFunc; +} + +export function doGenStubCppFile(rootInfo: ServiceRootInfo, fileContent: string): string { + let stubInnerFuncMap = ''; + let stubInnerFuncCpp = ''; + let funcList: FuncObj[] = rootInfo.funcs; + for (let i = 0; i < funcList.length; ++i) { + let funcEnum = funcList[i].name.toUpperCase(); + stubInnerFuncMap += format('innerFuncs_[%s] = &%sStub::%sInner;', funcEnum, rootInfo.serviceName, funcList[i].name); + stubInnerFuncCpp += genStubInnerFunc(funcList[i], rootInfo.serviceName); + } + fileContent = replaceAll(fileContent, '[serviceName]', rootInfo.serviceName); + fileContent = replaceAll(fileContent, '[lowServiceName]', rootInfo.serviceName.toLowerCase()); + fileContent = replaceAll(fileContent, '[innerFuncMap]', stubInnerFuncMap); + fileContent = replaceAll(fileContent, '[innerFuncImpl]', stubInnerFuncCpp); + return fileContent; +} + +// 生成 xxx_service_stub.cpp +export function genStubCppFile(rootInfo: ServiceRootInfo, filePath: string, fileContent: string) { + fileContent = doGenStubCppFile(rootInfo, fileContent); + fs.writeFileSync(filePath, fileContent); +} \ No newline at end of file diff --git a/src/vscode_plugin/src/gen/tools/genstubhfile.ts b/src/vscode_plugin/src/gen/tools/genstubhfile.ts new file mode 100644 index 00000000..caf7a1a5 --- /dev/null +++ b/src/vscode_plugin/src/gen/tools/genstubhfile.ts @@ -0,0 +1,40 @@ +/* +* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import { replaceAll, getTab } from '../../common/tool'; +import * as fs from 'fs'; +import { format } from 'util' +import { FuncObj, ServiceRootInfo } from '../datatype'; + +export function doGenStubHFile(rootInfo: ServiceRootInfo, fileContent: string): string { + let stubInnerFuncH = ''; + let funcList: FuncObj[] = rootInfo.funcs; + let funcTab = getTab(1); + for (let i = 0; i < funcList.length; ++i) { + stubInnerFuncH += (i === 0) ? '' : '\n' + funcTab; + stubInnerFuncH += + format('ErrCode %sInner(MessageParcel &data, MessageParcel &reply);', funcList[i].name); + } + fileContent = replaceAll(fileContent, '[serviceName]', rootInfo.serviceName); + fileContent = replaceAll(fileContent, '[marcoName]', rootInfo.serviceName.toUpperCase()); + fileContent = replaceAll(fileContent, '[lowServiceName]', rootInfo.serviceName.toLowerCase()); + fileContent = replaceAll(fileContent, '[innerFuncDef]', stubInnerFuncH); + return fileContent; +} +// 生成 xxx_service_stub.h +export function genStubHFile(rootInfo: ServiceRootInfo, filePath: string, fileContent: string) { + fileContent = doGenStubHFile(rootInfo, fileContent); + fs.writeFileSync(filePath, fileContent); +} \ No newline at end of file diff --git a/src/vscode_plugin/src/parse/parsec.ts b/src/vscode_plugin/src/parse/parsec.ts index 014bc1b4..733894a7 100644 --- a/src/vscode_plugin/src/parse/parsec.ts +++ b/src/vscode_plugin/src/parse/parsec.ts @@ -20,7 +20,7 @@ import { ParamObj, FuncObj, StructObj, ClassObj, EnumObj, UnionObj, ParseObj } f import fs = require('fs'); -function parseEnum(data: string) { +export function parseEnum(data: string) { // 使用正则表达式提取枚举定义 const enumRegex = /typedef\s+enum\s+(\w*)\s*{([^}]*)}\s*(\w+);|enum\s+(\w+)\s*{([^}]*)}\s*;/g; const enums: EnumObj[] = []; @@ -43,7 +43,7 @@ function parseEnum(data: string) { return enums; } -function parseUnion(data: string) { +export function parseUnion(data: string) { // 使用正则表达式提取联合体定义 const unionRegex = /typedef\s+union\s*(\w*)\s*{([^}]*)}\s*(\w+)\s*;|union\s+(\w+)\s*{([^}]*)}\s*;/g; const unions: UnionObj[] = []; @@ -91,7 +91,7 @@ function parseUnion(data: string) { return unions; } -function parseStruct(data: string) { +export function parseStruct(data: string) { // 使用正则表达式提取结构体定义 // const structRegex = /typedef\s+struct\s+(\w+)\s*{([^}]*)}\s*(\w+);/g; // const structRegex = /(\btypedef\b\s+)?struct\s+\w*\s*{([^}]*)}\s*(\w+);/g; @@ -138,7 +138,7 @@ function parseStruct(data: string) { return structs; } // /^(const\s+)?([\w\s*]+)\s+(\w+)(?:\[(\d+)\])?$/ -function parseParameters(members: string[]): ParamObj[] { +export function parseParameters(members: string[]): ParamObj[] { // const memberRegex = /^(const\s+)?([\w\s*]+)\s+(\w+)(?:\[(\d+)\])?$/; const memberRegex = /^(const\s+)?([\w\s*]+)\s+(\w+)(?:\[(\d*)\])?$/; // console.info(` parseParameters members: ${JSON.stringify(members)}`); @@ -157,7 +157,7 @@ function parseParameters(members: string[]): ParamObj[] { }).filter((m): m is ParamObj => m !== null); } -function parseMembers(members: string[]): ParamObj[] { +export function parseMembers(members: string[]): ParamObj[] { const memberRegex = /(?:public:|private:)?\s*(\w+(?:\s+\w+)?)\s+(\w+)(?:\[(\d+)\])?/; // console.info(` parseMembers members: ${JSON.stringify(members)}`); return members.map(member => { @@ -174,7 +174,7 @@ function parseMembers(members: string[]): ParamObj[] { }).filter((m): m is ParamObj => m !== null); } -function parseMethods(functions: string[]): FuncObj[] { +export function parseMethods(functions: string[]): FuncObj[] { const functionRegex = /^(\w[\w\s]*\*?)\s+(\w+)\((.*?)\)$/; // 正则表达式匹配返回值、函数名和参数 // const functionRegex = /(\w+)\s+(\w+)\(([^)]*)\)/; @@ -196,7 +196,7 @@ function parseMethods(functions: string[]): FuncObj[] { }).filter((f): f is FuncObj => f !== null); } -function parseClass(data: string) { +export function parseClass(data: string) { // 使用正则表达式提取类定义 const classRegex = /class\s+(\w+)\s*{([^}]*)}/g; const classes: ClassObj[] = [] @@ -241,7 +241,7 @@ function parseClass(data: string) { return classes; } -function parseFunctionOld(data: string) { +export function parseFunctionOld(data: string) { // 使用正则表达式提取函数定义 const functionRegex1 = /([a-zA-Z_]\w*\s+)+([*a-zA-Z_]\w+)\s*\(([^)]*)\)\s*(?={|;)/g; const functionRegex2 = /(\w+\s*\(.*?\)\s+)(\w+)\s*\((.*?)\);\s*/g; @@ -326,7 +326,7 @@ function parseFunctionOld(data: string) { // } } -function parseFunction(data: string): FuncObj[] { +export function parseFunction(data: string): FuncObj[] { // const funcRegex = /^(static\s+)?(const\s+)?([\w\s\[\]*]+)\s+(\w+)\s*\(([^)]*)\);/gm; const funcRegex = /(?:typedef\s+([\w\s\[\]*]+)\s+\(\*\s*(\w+)\)\s*\(([^)]*)\);|^(static\s+)?(const\s+)?([\w\s\[\]*]+)\s+(\w+)\s*\(([^)]*)\);)/gm const functions: FuncObj[] = [] diff --git a/src/vscode_plugin/src/parse/parsets.ts b/src/vscode_plugin/src/parse/parsets.ts index a01a3636..3cc24d83 100644 --- a/src/vscode_plugin/src/parse/parsets.ts +++ b/src/vscode_plugin/src/parse/parsets.ts @@ -52,7 +52,7 @@ const OBJECT_TYPE = 180; let gchecker: ts.TypeChecker; -function getTypeAliasSubtypes(typeAlias: ts.TypeAliasDeclaration, list: ParamObj[]) { +export function getTypeAliasSubtypes(typeAlias: ts.TypeAliasDeclaration, list: ParamObj[]) { // 检查类型是否为类型节点 const typeNode = typeAlias.type; // console.log('getTypeAliasSubtypes'); @@ -90,7 +90,7 @@ function getTypeAliasSubtypes(typeAlias: ts.TypeAliasDeclaration, list: ParamObj return []; } -function getParamType(paramType: any) { +export function getParamType(paramType: any) { if (paramType === undefined) { return 'void'; } diff --git a/src/vscode_plugin/src/test/readme.md b/src/vscode_plugin/src/test/readme.md new file mode 100644 index 00000000..f4db2c6b --- /dev/null +++ b/src/vscode_plugin/src/test/readme.md @@ -0,0 +1,62 @@ +### Run test + +1. Modify launch.json + + ```json + //add test config + { + "name": "Run Extension Tests", + "type": "extensionHost", + "request": "launch", + "runtimeExecutable": "${execPath}", + "args": [ + "--extensionDevelopmentPath=${workspaceFolder}", + "--extensionTestsPath=${workspaceFolder}/out/test/suite/index" + ], + "outFiles": ["${workspaceFolder}/out/test/**/*.js"], + "preLaunchTask": "npm: watch" + } + + ``` + + + +2. start the test by using “F5” + + + +3. get result in output of "debug console(调试控制台)" + + + +4. add testcase: following test-files in "test\suit" + + | test-dir | test-target | testcase | result | tester | + | --------- | ----------------- | -------- | ------ | ------ | + | common | re | | | zmh | + | common | tool | | | hrt | + | gen/tools | genclientcppfile | | | | + | | gencommonfile | | | | + | | gencommonfunc | | | | + | | genidlfile | | | | + | | geniservicehfile | | | | + | | genproxycppfile | | | | + | | genproxyhfile | | | | + | | gensacppfile | | | | + | | gensahfile | | | | + | | genservicecppfile | | | | + | | genservicehfile | | | | + | | genstubcppfile | | | | + | | genstubhfile | | | | + | gen | gencpp | | | | + | | gendts | | | | + | | gendtscpp | | | | + | | genhdf | | | | + | | gensa | | | | + | | gentest | | | | + | parse | parsec | | | | + | | parsets | | | | + + + +5. test report diff --git a/src/vscode_plugin/src/test/suite/common_tool.test.ts b/src/vscode_plugin/src/test/suite/common/tool.test.ts similarity index 98% rename from src/vscode_plugin/src/test/suite/common_tool.test.ts rename to src/vscode_plugin/src/test/suite/common/tool.test.ts index ab7b1d81..b44b43b4 100644 --- a/src/vscode_plugin/src/test/suite/common_tool.test.ts +++ b/src/vscode_plugin/src/test/suite/common/tool.test.ts @@ -18,7 +18,7 @@ import * as assert from 'assert'; // You can import and use all API from the 'vscode' module // as well as import your extension to test it import * as vscode from 'vscode'; -import * as tools from '../../common/tool' +import * as tools from '../../../common/tool' // import * as myExtension from '../../extension'; suite('Common_Tool_Test_Suite', () => { @@ -28,6 +28,7 @@ suite('Common_Tool_Test_Suite', () => { test('replaceall_test_1', () => { let resultStr = tools.replaceAll('hello_world', 'or', 'er'); assert.strictEqual(resultStr, 'hello_werld'); + resultStr = tools.replaceAll('hello_world', 'l', 'r'); assert.strictEqual(resultStr, 'herro_worrd'); }); diff --git a/src/vscode_plugin/src/test/suite/gen/genidlfile.test.ts b/src/vscode_plugin/src/test/suite/gen/genidlfile.test.ts new file mode 100644 index 00000000..6a095254 --- /dev/null +++ b/src/vscode_plugin/src/test/suite/gen/genidlfile.test.ts @@ -0,0 +1,65 @@ +/* +* Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import * as assert from 'assert'; + +// You can import and use all API from the 'vscode' module +// as well as import your extension to test it +import * as vscode from 'vscode'; +import * as genIdlFile from '../../../gen/tools/genidlfile' +// import * as myExtension from '../../extension'; + +suite('GenIdl_file_Suite', () => { + vscode.window.showInformationMessage('Start all tests.'); + + //1, 测试 parseEnum 一般情况 + test('getParcelType_test_1', () => { + let resStr = genIdlFile.getParcelType(''); + assert.strictEqual(resStr, resStr); + + resStr = genIdlFile.getParcelType('std::string'); + assert.strictEqual(resStr, 'string'); + }); + + //2, 测试边界情况 + test('getParcelType_test_2', () => { + let resStr = genIdlFile.getParcelType('std::string'); + assert.strictEqual(resStr, 'string'); + + resStr = genIdlFile.getParcelType('char *'); + assert.strictEqual(resStr, 'string'); + + resStr = genIdlFile.getParcelType('char *'); + assert.strictEqual(resStr, resStr); + }); + + //3, 测试异常情况 + test('getParcelType_test_3', () => { + let resStr = genIdlFile.getParcelType(''); + assert.strictEqual(resStr, ''); + + resStr = genIdlFile.getParcelType('string'); + assert.strictEqual(resStr, 'string'); + + resStr = genIdlFile.getParcelType('char*'); + assert.strictEqual(resStr, 'char*'); + }); + + //4, 测试错误情况 + test('getParcelType_test_4', () => { + let resStr = genIdlFile.getParcelType(''); + assert.strictEqual(resStr, ''); + }); +}); diff --git a/src/vscode_plugin/src/test/suite/parse/parsec.test.ts b/src/vscode_plugin/src/test/suite/parse/parsec.test.ts new file mode 100644 index 00000000..45a2ec55 --- /dev/null +++ b/src/vscode_plugin/src/test/suite/parse/parsec.test.ts @@ -0,0 +1,63 @@ +/* +* Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import * as assert from 'assert'; + +// You can import and use all API from the 'vscode' module +// as well as import your extension to test it +import * as vscode from 'vscode'; +import * as parsec from '../../../parse/parsec' +// import * as myExtension from '../../extension'; + +suite('Parse_C_Suite', () => { + vscode.window.showInformationMessage('Start all tests.'); + + //1, 测试 parseEnum 一般情况 + test('parseEnum_test_1', () => { + let enumObjList = parsec.parseEnum('hello_world'); + assert.strictEqual(enumObjList.length, 0); + }); + + //2, 测试边界情况 + test('parseEnum_test_2', () => { + let enumObjList = parsec.parseEnum('enum { ENUM_1 }'); + assert.strictEqual(enumObjList.length, 0); + + enumObjList = parsec.parseEnum('enum { ENUM_1, ENUM_2 }'); + assert.strictEqual(enumObjList.length, 0); + + enumObjList = parsec.parseEnum('enum { ENUM_1, ENUM_2 }; enum { ENUM_10, ENUM_20 };'); + assert.strictEqual(enumObjList.length, 0); + }); + + //3, 测试异常情况 + test('parseEnum_test_3', () => { + let teststr: string = ''; + let enumObjList = parsec.parseEnum(teststr); + assert.strictEqual(enumObjList.length, 0); + + enumObjList = parsec.parseEnum('enum'); + assert.strictEqual(enumObjList.length, 0); + + enumObjList = parsec.parseEnum('enum { ENUM_1, ENUM_1 }'); + assert.strictEqual(enumObjList.length, 0); + }); + + //4, 测试错误情况 + test('replaceall_test_4', () => { + let enumObjList = parsec.parseEnum(''); + assert.strictEqual(enumObjList.length, 0); + }); +}); From 2adaac4334020f0040190552679b8de4a07f362a Mon Sep 17 00:00:00 2001 From: wangshi Date: Mon, 10 Feb 2025 16:18:41 +0800 Subject: [PATCH 3/4] update package json Signed-off-by: wangshi --- src/vscode_plugin/package.json | 214 ++++++++++++++++----------------- 1 file changed, 107 insertions(+), 107 deletions(-) diff --git a/src/vscode_plugin/package.json b/src/vscode_plugin/package.json index 2f469853..031d0135 100644 --- a/src/vscode_plugin/package.json +++ b/src/vscode_plugin/package.json @@ -22,7 +22,7 @@ "onCommand:extension.h2hdf" ], "main": "./out/extension.js", - "l10n":"./l10n", + "l10n": "./l10n", "contributes": { "commands": [ { @@ -38,28 +38,27 @@ "title": "%extension.dts2cpp.title%" }, { - "command": "extension.h2sa", - "title": "%extension.h2sa.title%" + "command": "extension.h2sa", + "title": "%extension.h2sa.title%" }, { - "command": "extension.h2hdf", - "title": "%extension.h2hdf.title%" - + "command": "extension.h2hdf", + "title": "%extension.h2hdf.title%" }, - { + { "command": "extension.ohcrosscompile", "title": "%extension.ohcrosscompile.title%" }, - { - "command": "extension.ohGenerator", - "title": "%extension.ohGenerator.title%" + { + "command": "extension.ohGenerator", + "title": "%extension.ohGenerator.title%" } ], "viewsWelcome": [ - { - "view": "workbench.explorer.emptyView", - "contents": "%Generate NAPI definition code and framework code for OpenHarmony. \n[OHGenerator](command:extension.ohGenerator)%" - } + { + "view": "workbench.explorer.emptyView", + "contents": "%Generate NAPI definition code and framework code for OpenHarmony. \n[OHGenerator](command:extension.ohGenerator)%" + } ], "submenus": [ { @@ -70,14 +69,14 @@ "menus": { "gen-menulist": [ { - "command": "extension.h2sa", - "when": "resourceExtname == .h", - "group": "2_workspace" + "command": "extension.h2sa", + "when": "resourceExtname == .h", + "group": "2_workspace" }, { - "command": "extension.h2hdf", - "when": "resourceExtname == .h", - "group": "2_workspace" + "command": "extension.h2hdf", + "when": "resourceExtname == .h", + "group": "2_workspace" }, { "command": "extension.h2dts", @@ -109,78 +108,78 @@ ] }, "snippets": [ - { - "language": "cpp", - "path": "./snippets/napi_class_snippets.json" - }, - { - "language": "cpp", - "path": "./snippets/napi_thread_snippets.json" - }, - { - "language": "cpp", - "path": "./snippets/napi_asyncwork_snippets.json" - }, - { - "language": "cpp", - "path": "./snippets/napi_promise_snippets.json" - }, - { - "language": "cpp", - "path": "./snippets/napi_callback_snippets.json" - }, - { - "language": "cpp", - "path": "./snippets/napi_enum_snippets.json" - }, - { - "language": "cpp", - "path": "./snippets/napi_struct_snippets.json" - }, - { - "language": "cpp", - "path": "./snippets/napi_variable_snippets.json" - }, - { - "language": "cpp", - "path": "./snippets/napi_external_snippets.json" - }, - { - "language": "cpp", - "path": "./snippets/napi_error_snippets.json" - }, - { - "language": "cpp", - "path": "./snippets/aki_function_snippets.json" - }, - { - "language": "cpp", - "path": "./snippets/aki_class_snippets.json" - }, - { - "language": "cpp", - "path": "./snippets/aki_struct_snippets.json" - }, - { - "language": "cpp", - "path": "./snippets/aki_enum_snippets.json" - }, - { - "language": "cpp", - "path": "./snippets/aki_callback_snippets.json" - }, - { - "language": "cpp", - "path": "./snippets/aki_promise_snippets.json" - }, - { - "language": "cpp", - "path": "./snippets/aki_thread_snippets.json" - }, - { - "language": "cpp", - "path": "./snippets/aki_value_snippets.json" - } + { + "language": "cpp", + "path": "./snippets/napi_class_snippets.json" + }, + { + "language": "cpp", + "path": "./snippets/napi_thread_snippets.json" + }, + { + "language": "cpp", + "path": "./snippets/napi_asyncwork_snippets.json" + }, + { + "language": "cpp", + "path": "./snippets/napi_promise_snippets.json" + }, + { + "language": "cpp", + "path": "./snippets/napi_callback_snippets.json" + }, + { + "language": "cpp", + "path": "./snippets/napi_enum_snippets.json" + }, + { + "language": "cpp", + "path": "./snippets/napi_struct_snippets.json" + }, + { + "language": "cpp", + "path": "./snippets/napi_variable_snippets.json" + }, + { + "language": "cpp", + "path": "./snippets/napi_external_snippets.json" + }, + { + "language": "cpp", + "path": "./snippets/napi_error_snippets.json" + }, + { + "language": "cpp", + "path": "./snippets/aki_function_snippets.json" + }, + { + "language": "cpp", + "path": "./snippets/aki_class_snippets.json" + }, + { + "language": "cpp", + "path": "./snippets/aki_struct_snippets.json" + }, + { + "language": "cpp", + "path": "./snippets/aki_enum_snippets.json" + }, + { + "language": "cpp", + "path": "./snippets/aki_callback_snippets.json" + }, + { + "language": "cpp", + "path": "./snippets/aki_promise_snippets.json" + }, + { + "language": "cpp", + "path": "./snippets/aki_thread_snippets.json" + }, + { + "language": "cpp", + "path": "./snippets/aki_value_snippets.json" + } ] }, "scripts": { @@ -192,23 +191,24 @@ "test": "node ./out/test/runTest.js" }, "devDependencies": { - "@types/glob": "^7.1.1", - "@types/mocha": "^5.2.6", - "@types/node": "^16.17.0", - "@types/tar": "^6.1.13", - "@types/vscode": "^1.73.0", - "@typescript-eslint/eslint-plugin": "^5.30.0", - "@typescript-eslint/parser": "^5.30.0", - "@vscode/test-electron": "^1.6.1", - "glob": "^7.1.4", - "mocha": "^6.1.4", - "source-map-support": "^0.5.12", - "eslint": "^8.13.0" + "@types/glob": "^7.1.1", + "@types/mocha": "^5.2.6", + "@types/node": "^16.17.0", + "@types/tar": "^6.1.13", + "@types/vscode": "^1.73.0", + "@typescript-eslint/eslint-plugin": "^5.30.0", + "@typescript-eslint/parser": "^5.30.0", + "@vscode/test-electron": "^1.6.1", + "eslint": "^8.13.0", + "glob": "^7.1.4", + "mocha": "^6.1.4", + "mocha-junit-reporter": "^2.2.1", + "source-map-support": "^0.5.12" }, "dependencies": { - "vsce": "^2.15.0", + "@vscode/l10n": "^0.0.10", + "tar": "^7.4.3", "typescript": "^4.7.2", - "@vscode/l10n": "^0.0.10", - "tar": "^7.4.3" + "vsce": "^2.15.0" } } From c7162ed43df1cd6823de16689f5ab3324f20199e Mon Sep 17 00:00:00 2001 From: wangshi Date: Mon, 10 Feb 2025 17:59:04 +0800 Subject: [PATCH 4/4] add test report Signed-off-by: wangshi --- src/vscode_plugin/package.json | 16 +++++++++++++ src/vscode_plugin/src/common/conf.ts | 28 +++++++++++++++++++++++ src/vscode_plugin/src/common/file.ts | 22 ++++++++++++++++++ src/vscode_plugin/src/test/suite/index.ts | 8 +++++++ 4 files changed, 74 insertions(+) create mode 100644 src/vscode_plugin/src/common/conf.ts create mode 100644 src/vscode_plugin/src/common/file.ts diff --git a/src/vscode_plugin/package.json b/src/vscode_plugin/package.json index 031d0135..8f760379 100644 --- a/src/vscode_plugin/package.json +++ b/src/vscode_plugin/package.json @@ -24,6 +24,22 @@ "main": "./out/extension.js", "l10n": "./l10n", "contributes": { + "configuration": { + "type": "object", + "title": "YourExtensionConfiguration", + "properties": { + "napiExtension.outSetting": { + "type": "string", + "default": "./", + "description": "description of outpath" + }, + "testReport.canOutput": { + "type": "boolean", + "default": false, + "description": "description of output flag" + } + } + }, "commands": [ { "command": "extension.h2dts", diff --git a/src/vscode_plugin/src/common/conf.ts b/src/vscode_plugin/src/common/conf.ts new file mode 100644 index 00000000..cd111224 --- /dev/null +++ b/src/vscode_plugin/src/common/conf.ts @@ -0,0 +1,28 @@ +/* +* Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +const vscode = require("vscode"); + +export function getOutputPath(): string { + const config = vscode.workspace.getConfiguration('napiExtension'); + const outSetting = config.get("outSetting"); + return outSetting; +} + +export function getReportConf(): string { + const config = vscode.workspace.getConfiguration('testReport'); + const outSetting = config.get("canOutput"); + return outSetting; + } diff --git a/src/vscode_plugin/src/common/file.ts b/src/vscode_plugin/src/common/file.ts new file mode 100644 index 00000000..01d6ff78 --- /dev/null +++ b/src/vscode_plugin/src/common/file.ts @@ -0,0 +1,22 @@ +/* +* Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +const vscode = require("vscode"); + +export function getOutputPath(): string { + const config = vscode.workspace.getConfiguration('napiExtension'); + const outSetting = config.get("outSetting"); + return outSetting; +} diff --git a/src/vscode_plugin/src/test/suite/index.ts b/src/vscode_plugin/src/test/suite/index.ts index b2f6c650..383e3d97 100644 --- a/src/vscode_plugin/src/test/suite/index.ts +++ b/src/vscode_plugin/src/test/suite/index.ts @@ -16,6 +16,7 @@ import * as path from 'path'; import * as Mocha from 'mocha'; import * as glob from 'glob'; +import { getOutputPath, getReportConf } from '../../common/conf'; export function run(): Promise { // Create the mocha test @@ -23,6 +24,13 @@ export function run(): Promise { ui: 'tdd' }); mocha.useColors(true); + + let reportTestResult = getReportConf(); + if (reportTestResult) { + let outpath = getOutputPath(); + outpath = outpath + 'testres.xunit.xml'; + mocha.reporter('xunit', { output: outpath }); + } const testsRoot = path.resolve(__dirname, '..');