From 3472c4b77add5c67fdcdd124cc8319dc05c5c1e7 Mon Sep 17 00:00:00 2001 From: walker Date: Tue, 30 May 2023 14:31:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0proto=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E7=BC=96=E8=AF=91=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 33 +++++++++++++---- package.json | 5 +-- scripts/proto.cjs | 91 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+), 8 deletions(-) create mode 100644 scripts/proto.cjs diff --git a/README.md b/README.md index e0bdb4d..5c02ef7 100644 --- a/README.md +++ b/README.md @@ -2,40 +2,61 @@ xian ncc 调度决策辅助客户端 -## Install the dependencies +## 项目 clone 说明 + +此项目包含子模块,拉取方式: + +- 1.在克隆主项目的时候带上参数 --recurse-submodules +- 2.或者 clone 完成后,在项目目录中执行:git submodule init && git submodule update + +## 安装依赖 + ```bash yarn # or npm install ``` -### Start the app in development mode (hot-code reloading, error reporting, etc.) +## 编译 proto 文件 + ```bash -quasar dev +yarn run proto +# or +npm run proto ``` +### Start the app in development mode (hot-code reloading, error reporting, etc.) + +```bash +quasar dev +# or +yarn run dev +# or +npm run dev +``` ### Lint the files + ```bash yarn lint # or npm run lint ``` - ### Format the files + ```bash yarn format # or npm run format ``` - - ### Build the app for production + ```bash quasar build ``` ### Customize the configuration + See [Configuring quasar.config.js](https://v2.quasar.dev/quasar-cli-vite/quasar-config-js). diff --git a/package.json b/package.json index 3ca2936..4378cfa 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,8 @@ "format": "prettier --write \"**/*.{js,ts,vue,scss,html,md,json}\" --ignore-path .gitignore", "test": "echo \"No test specified\" && exit 0", "dev": "quasar dev", - "build": "quasar build" + "build": "quasar build", + "protoc": "node scripts/proto.cjs" }, "dependencies": { "axios": "^1.2.1", @@ -37,4 +38,4 @@ "npm": ">= 6.13.4", "yarn": ">= 1.21.1" } -} \ No newline at end of file +} diff --git a/scripts/proto.cjs b/scripts/proto.cjs new file mode 100644 index 0000000..ea9735e --- /dev/null +++ b/scripts/proto.cjs @@ -0,0 +1,91 @@ +/** + * 将proto文件编译到 src/proto/ + */ +const { readdirSync } = require('fs'); +const { resolve } = require('path'); +const os = require('os'); + +const { exec } = require('child_process'); + +const messageDir = resolve(__dirname, '../xian-ncc-da-message'); +const protoDir = resolve(messageDir, 'protos'); +const destDir = resolve(__dirname, '../src/proto'); + +/** + * 递归处理所有proto文件生成 + * @param {*} file 文件 + * @param {*} path 目录 + */ +function recursiveGenerate(file, path = [], cmds = []) { + if (file.isFile()) { + // 文件,生成 + if (file.name.endsWith('.proto')) { + cmds.push(buildGenerateCmd(file.name, path)); + } else { + console.warn('不是proto文件', file.name); + } + } else if (file.isDirectory()) { + // 文件夹,递归 + readdirSync(resolve(protoDir, ...path, file.name), { + withFileTypes: true, + }).forEach((f) => { + const subPath = [...path, file.name]; + recursiveGenerate(f, subPath, cmds); + }); + } +} + +const isLinux = os.type().toLowerCase().includes('linux'); + +function buildGenerateCmd(name, path = []) { + const protoPath = resolve(protoDir, ...path); + const tsPath = resolve(destDir, ...path); + + let cmd = ['protoc', `-I=${protoPath}`, `--ts_out=${tsPath}`, `${name}`]; + let cmdStr = cmd.join(' '); + return cmdStr; +} + +function main() { + const protocDir = resolve(messageDir, 'protoc-22.2'); + const protocBin = resolve( + protocDir, + `bin/${isLinux ? 'linux-x86_64' : 'win64'}` + ); + const prepareCmds = []; + const setPathCmd = isLinux + ? ['export', `PATH=${protocBin}:${protocDir}:"$PATH"`].join(' ') + : ['set', `PATH=${protocBin};${protocDir};%PATH%`].join(' '); + const protocVersionCmd = ['protoc', '--version'].join(' '); + prepareCmds.push(setPathCmd); + prepareCmds.push(protocVersionCmd); + + readdirSync(protoDir, { + withFileTypes: true, + }).forEach((f) => { + recursiveGenerate(f, [], prepareCmds); + }); + + console.log(prepareCmds); + + exec( + prepareCmds.join(' && '), + { + maxBuffer: 1024 * 2000, + }, + (err, stdout, stderr) => { + if (err) { + console.error(err); + throw err; + } else if (stderr.length > 0) { + console.error(stderr.toString()); + throw new Error(stderr.toString()); + } else { + console.log(stdout); + } + } + ); +} + +// 执行 +main();