From 1871bc9151f15850350865ec86735664c68d3d2a Mon Sep 17 00:00:00 2001 From: zhaojunxia2020 Date: Fri, 28 Oct 2022 10:30:48 +0800 Subject: [PATCH] refactor service-gen main Signed-off-by: zhaojunxia2020 --- hdc/service-gen/src/gen/analyze.js | 41 ++---------------------- hdc/service-gen/src/gen/main.js | 48 ++++++++++++++++++++++++++++- hdc/service-gen/src/tools/FileRW.js | 3 ++ 3 files changed, 52 insertions(+), 40 deletions(-) diff --git a/hdc/service-gen/src/gen/analyze.js b/hdc/service-gen/src/gen/analyze.js index 2eccf5e6..4e45a457 100644 --- a/hdc/service-gen/src/gen/analyze.js +++ b/hdc/service-gen/src/gen/analyze.js @@ -15,9 +15,6 @@ const fs = require("fs"); const os = require("os"); const { NapiLog } = require("../tools/NapiLog"); -const { writeFile, createFolder } = require("../tools/FileRW"); -const re = require("../tools/re"); -const gen = require("./generate"); const path = require('path'); function parseFileAll(hFilePath) { @@ -120,13 +117,7 @@ function analyzeClasses(rootInfo, parseClasses) { } } -function wirte2Disk(fileInfo, destDir) { - let filePath = re.pathJoin(destDir, fileInfo.name); - writeFile(filePath, fileInfo.content); -} - function doAnalyze(hFilePath, cmdParam) { - let destDir = cmdParam.out; let parseResult = parseFileAll(hFilePath); let rootInfo = { "serviceName": "", @@ -135,39 +126,11 @@ function doAnalyze(hFilePath, cmdParam) { "includes": [], "serviceId": cmdParam.serviceId == null ? "9002" : cmdParam.serviceId } - // 1. h文件解析保存为结构体 + analyzeNameSpace(rootInfo, parseResult); analyzeClasses(rootInfo, parseResult.classes); rootInfo.includes = parseResult.includes; - - // 2. 根据结构体生成代码 - let fileContent = gen.doGenerate(rootInfo); - - // 3. 创建service工程目录 - let serviceFolder = rootInfo.serviceName.toLowerCase() + "service"; - let outputPath = destDir + "/" + serviceFolder; - createFolder(re.pathJoin(destDir, serviceFolder)); - createFolder(re.pathJoin(outputPath, "include")); - createFolder(re.pathJoin(outputPath, "interface")); - createFolder(re.pathJoin(outputPath, "sa_profile")); - createFolder(re.pathJoin(outputPath, "etc")); - createFolder(re.pathJoin(outputPath, "src")); - - // 4. 生成代码保存为文件 - wirte2Disk(fileContent.iServiceHFile, outputPath + "/interface"); - wirte2Disk(fileContent.proxyHFile, outputPath + "/include"); - wirte2Disk(fileContent.stubHFile, outputPath + "/include"); - wirte2Disk(fileContent.serviceHFile, outputPath + "/include"); - wirte2Disk(fileContent.proxyCppFile, outputPath + "/src"); - wirte2Disk(fileContent.stubCppFile, outputPath + "/src"); - wirte2Disk(fileContent.serviceCppFile, outputPath + "/src"); - wirte2Disk(fileContent.clientCppFile, outputPath + "/src"); - wirte2Disk(fileContent.buildGnFile, outputPath); - wirte2Disk(fileContent.bundleJsonFile, outputPath); - wirte2Disk(fileContent.profileGnFile, outputPath + "/sa_profile"); - wirte2Disk(fileContent.profileXmlFile, outputPath + "/sa_profile"); - wirte2Disk(fileContent.serviceCfgFile, outputPath + "/etc"); - wirte2Disk(fileContent.serviceCfgGnFile, outputPath + "/etc"); + return rootInfo; } module.exports = { diff --git a/hdc/service-gen/src/gen/main.js b/hdc/service-gen/src/gen/main.js index 1286825c..f3c5db22 100644 --- a/hdc/service-gen/src/gen/main.js +++ b/hdc/service-gen/src/gen/main.js @@ -15,10 +15,13 @@ const path = require("path"); const stdio = require("stdio"); const fs = require('fs'); +const re = require("../tools/re"); const { NapiLog } = require("../tools/NapiLog"); const { print } = require("../tools/tool"); const analyze = require("./analyze"); +const gen = require("./generate"); +const { writeFile, createFolder } = require("../tools/FileRW"); let ops = stdio.getopt({ 'filename': { key: 'f', args: 1, description: ".d.ts file", default: "" }, @@ -80,12 +83,55 @@ function readDirFiles() { }); } +function wirte2Disk(fileInfo, destDir) { + let filePath = re.pathJoin(destDir, fileInfo.name); + writeFile(filePath, fileInfo.content); +} + +function genServiceFile(fileName) { + // 1. h文件解析保存为结构体 + let rootInfo = analyze.doAnalyze(fileName, ops); + + // 2. 根据结构体生成代码 + let fileContent = gen.doGenerate(rootInfo); + + // 3. 创建service工程目录 + let servicePath = re.pathJoin(ops.out, rootInfo.serviceName.toLowerCase() + "service"); + let etcPath = re.pathJoin(servicePath, "etc"); + let includePath = re.pathJoin(servicePath, "include"); + let interfacePath = re.pathJoin(servicePath, "interface"); + let profilePath = re.pathJoin(servicePath, "sa_profile"); + let srcPath = re.pathJoin(servicePath, "src"); + createFolder(servicePath); + createFolder(etcPath); + createFolder(includePath); + createFolder(interfacePath); + createFolder(profilePath); + createFolder(srcPath); + + // 4. 生成代码保存为文件 + wirte2Disk(fileContent.serviceCfgFile, etcPath); + wirte2Disk(fileContent.serviceCfgGnFile, etcPath); + wirte2Disk(fileContent.proxyHFile, includePath); + wirte2Disk(fileContent.stubHFile, includePath); + wirte2Disk(fileContent.serviceHFile, includePath); + wirte2Disk(fileContent.iServiceHFile, interfacePath); + wirte2Disk(fileContent.profileGnFile, profilePath); + wirte2Disk(fileContent.profileXmlFile, profilePath); + wirte2Disk(fileContent.proxyCppFile, srcPath); + wirte2Disk(fileContent.stubCppFile, srcPath); + wirte2Disk(fileContent.serviceCppFile, srcPath); + wirte2Disk(fileContent.clientCppFile, srcPath); + wirte2Disk(fileContent.buildGnFile, servicePath); + wirte2Disk(fileContent.bundleJsonFile, servicePath); +} + function checkGenerate(fileName) { NapiLog.logInfo("check file []".format(fileName)); let suffix = fileName.split('.').pop().toLowerCase(); if (suffix === 'h') { NapiLog.logInfo("Generating service code from file " + fileName); - analyze.doAnalyze(fileName, ops); + genServiceFile(fileName); } else { NapiLog.logError('Only .h file is supported.'); } diff --git a/hdc/service-gen/src/tools/FileRW.js b/hdc/service-gen/src/tools/FileRW.js index a0dded80..97dcdf1c 100644 --- a/hdc/service-gen/src/tools/FileRW.js +++ b/hdc/service-gen/src/tools/FileRW.js @@ -135,6 +135,9 @@ function writeFile(fn, str) { } function createFolder(path) { + if (fs.existsSync(path)) { + return; + } fs.mkdirSync(path) } -- Gitee