添加proto消息编译脚本

This commit is contained in:
walker 2023-05-30 14:31:27 +08:00
parent 911aafe8a2
commit 3472c4b77a
3 changed files with 121 additions and 8 deletions

View File

@ -2,40 +2,61 @@
xian ncc 调度决策辅助客户端 xian ncc 调度决策辅助客户端
## Install the dependencies ## 项目 clone 说明
此项目包含子模块,拉取方式:
- 1.在克隆主项目的时候带上参数 --recurse-submodules
- 2.或者 clone 完成后在项目目录中执行git submodule init && git submodule update
## 安装依赖
```bash ```bash
yarn yarn
# or # or
npm install npm install
``` ```
### Start the app in development mode (hot-code reloading, error reporting, etc.) ## 编译 proto 文件
```bash ```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 ### Lint the files
```bash ```bash
yarn lint yarn lint
# or # or
npm run lint npm run lint
``` ```
### Format the files ### Format the files
```bash ```bash
yarn format yarn format
# or # or
npm run format npm run format
``` ```
### Build the app for production ### Build the app for production
```bash ```bash
quasar build quasar build
``` ```
### Customize the configuration ### Customize the configuration
See [Configuring quasar.config.js](https://v2.quasar.dev/quasar-cli-vite/quasar-config-js). See [Configuring quasar.config.js](https://v2.quasar.dev/quasar-cli-vite/quasar-config-js).

View File

@ -10,7 +10,8 @@
"format": "prettier --write \"**/*.{js,ts,vue,scss,html,md,json}\" --ignore-path .gitignore", "format": "prettier --write \"**/*.{js,ts,vue,scss,html,md,json}\" --ignore-path .gitignore",
"test": "echo \"No test specified\" && exit 0", "test": "echo \"No test specified\" && exit 0",
"dev": "quasar dev", "dev": "quasar dev",
"build": "quasar build" "build": "quasar build",
"protoc": "node scripts/proto.cjs"
}, },
"dependencies": { "dependencies": {
"axios": "^1.2.1", "axios": "^1.2.1",
@ -37,4 +38,4 @@
"npm": ">= 6.13.4", "npm": ">= 6.13.4",
"yarn": ">= 1.21.1" "yarn": ">= 1.21.1"
} }
} }

91
scripts/proto.cjs Normal file
View File

@ -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();