forked from npv2k1/learn-grpc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodegen.js
More file actions
executable file
·297 lines (246 loc) · 12.1 KB
/
codegen.js
File metadata and controls
executable file
·297 lines (246 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/usr/bin/env node
const { Command } = require('commander');
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const program = new Command();
// Configuration
const PROTO_DIR = path.join(__dirname, 'proto');
const GENERATED_DIR = path.join(__dirname, 'generated');
const PROTOC_PATH = path.join(__dirname, 'protoc', 'bin', 'protoc');
// Ensure protoc exists
if (!fs.existsSync(PROTOC_PATH)) {
console.log(chalk.yellow('Protoc not found. Downloading and installing...'));
try {
const os = process.platform;
const arch = process.arch;
let protocUrl;
let protocFile;
if (os === 'linux' && arch === 'x64') {
protocFile = 'protoc-24.4-linux-x86_64.zip';
} else if (os === 'darwin' && arch === 'x64') {
protocFile = 'protoc-24.4-osx-x86_64.zip';
} else if (os === 'darwin' && arch === 'arm64') {
protocFile = 'protoc-24.4-osx-aarch_64.zip';
} else if (os === 'win32') {
protocFile = 'protoc-24.4-win64.zip';
} else {
console.error(chalk.red(`Error: Unsupported platform ${os}-${arch}. Please install protoc manually.`));
process.exit(1);
}
protocUrl = `https://github.com/protocolbuffers/protobuf/releases/download/v24.4/${protocFile}`;
console.log(chalk.blue(`Downloading ${protocFile}...`));
execSync(`curl -LO ${protocUrl}`, { stdio: 'inherit' });
execSync(`unzip -q ${protocFile} -d protoc && chmod +x protoc/bin/protoc`, { stdio: 'inherit' });
execSync(`rm -f ${protocFile}`, { stdio: 'inherit' });
console.log(chalk.green('Protoc installed successfully!'));
} catch (error) {
console.error(chalk.red('Failed to install protoc automatically. Please install it manually.'));
console.error(chalk.red('Visit: https://github.com/protocolbuffers/protobuf/releases'));
process.exit(1);
}
}
// Language-specific generators
const generators = {
go: {
name: 'Go',
outputDir: path.join(GENERATED_DIR, 'go'),
generate: (protoFiles) => {
console.log(chalk.blue(`Generating Go code...`));
// Install Go plugins if not present
try {
execSync('go install google.golang.org/protobuf/cmd/protoc-gen-go@latest', { stdio: 'inherit' });
execSync('go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest', { stdio: 'inherit' });
} catch (error) {
console.log(chalk.yellow('Note: Go plugins installation may have failed. Make sure Go is installed and $GOPATH/bin is in PATH.'));
}
// Add GOPATH/bin to PATH
const goPath = execSync('go env GOPATH', { encoding: 'utf-8' }).trim();
const newPath = `${goPath}/bin:${process.env.PATH}`;
for (const protoFile of protoFiles) {
const cmd = `${PROTOC_PATH} --go_out=${generators.go.outputDir} --go_opt=paths=source_relative --go-grpc_out=${generators.go.outputDir} --go-grpc_opt=paths=source_relative --proto_path=${PROTO_DIR} ${protoFile}`;
try {
execSync(cmd, { stdio: 'inherit', env: { ...process.env, PATH: newPath } });
console.log(chalk.green(`✓ Generated Go code for ${protoFile}`));
} catch (error) {
console.error(chalk.red(`✗ Failed to generate Go code for ${protoFile}: ${error.message}`));
}
}
}
},
python: {
name: 'Python',
outputDir: path.join(GENERATED_DIR, 'python'),
generate: (protoFiles) => {
console.log(chalk.blue(`Generating Python code...`));
// Install grpcio-tools if not present
try {
execSync('pip3 install grpcio-tools', { stdio: 'inherit' });
} catch (error) {
console.log(chalk.yellow('Note: grpcio-tools installation may have failed. Make sure Python and pip are installed.'));
}
for (const protoFile of protoFiles) {
const cmd = `python3 -m grpc_tools.protoc --proto_path=${PROTO_DIR} --python_out=${generators.python.outputDir} --pyi_out=${generators.python.outputDir} --grpc_python_out=${generators.python.outputDir} ${protoFile}`;
try {
execSync(cmd, { stdio: 'inherit' });
console.log(chalk.green(`✓ Generated Python code for ${protoFile}`));
} catch (error) {
console.error(chalk.red(`✗ Failed to generate Python code for ${protoFile}: ${error.message}`));
}
}
}
},
typescript: {
name: 'TypeScript (NestJS)',
outputDir: path.join(GENERATED_DIR, 'typescript'),
generate: (protoFiles) => {
console.log(chalk.blue(`Generating TypeScript code for NestJS...`));
for (const protoFile of protoFiles) {
// Generate for general TypeScript
const cmd1 = `${PROTOC_PATH} --plugin=protoc-gen-ts_proto=${path.join(__dirname, 'node_modules', '.bin', 'protoc-gen-ts_proto')} --ts_proto_out=${generators.typescript.outputDir} --proto_path=${PROTO_DIR} ${protoFile} --ts_proto_opt=outputEncodeMethods=false,outputJsonMethods=false,outputClientImpl=false`;
// Generate for NestJS
const cmd2 = `${PROTOC_PATH} --plugin=protoc-gen-ts_proto=${path.join(__dirname, 'node_modules', '.bin', 'protoc-gen-ts_proto')} --ts_proto_out=${path.join(generators.typescript.outputDir, 'nestjs')} --proto_path=${PROTO_DIR} ${protoFile} --ts_proto_opt=nestJs=true,addGrpcMetadata=true,addNestjsRestParameter=true`;
try {
// Ensure nestjs subdirectory exists
fs.mkdirSync(path.join(generators.typescript.outputDir, 'nestjs'), { recursive: true });
execSync(cmd1, { stdio: 'inherit' });
execSync(cmd2, { stdio: 'inherit' });
console.log(chalk.green(`✓ Generated TypeScript code for ${protoFile}`));
} catch (error) {
console.error(chalk.red(`✗ Failed to generate TypeScript code for ${protoFile}: ${error.message}`));
}
}
}
},
rust: {
name: 'Rust',
outputDir: path.join(GENERATED_DIR, 'rust'),
generate: (protoFiles) => {
console.log(chalk.blue(`Generating Rust code...`));
// Create a basic Cargo.toml for the generated code
const cargoToml = `[package]
name = "grpc-generated"
version = "0.1.0"
edition = "2021"
[dependencies]
tonic = "0.10"
prost = "0.12"
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
[build-dependencies]
tonic-build = "0.10"
`;
const buildRs = `fn main() {
let proto_files = vec![
${protoFiles.map(f => ` "${f}",`).join('\n')}
];
tonic_build::configure()
.build_server(true)
.out_dir("./src")
.compile(&proto_files, &["${PROTO_DIR}"])
.unwrap_or_else(|e| panic!("protobuf compile error: {}", e));
for proto_file in &proto_files {
println!("cargo:rerun-if-changed=${PROTO_DIR}/{}", proto_file);
}
}`;
try {
fs.writeFileSync(path.join(generators.rust.outputDir, 'Cargo.toml'), cargoToml);
fs.writeFileSync(path.join(generators.rust.outputDir, 'build.rs'), buildRs);
fs.mkdirSync(path.join(generators.rust.outputDir, 'src'), { recursive: true });
// Create a basic lib.rs that will be populated after build
let libRs = '// Generated gRPC code will be included here\n';
fs.writeFileSync(path.join(generators.rust.outputDir, 'src', 'lib.rs'), libRs);
// Run the build process which will generate the actual proto files
console.log(chalk.blue('Running tonic-build to generate proto files...'));
execSync('cargo build', { cwd: generators.rust.outputDir, stdio: 'inherit' });
// Now find the generated files and update lib.rs
const buildOutDir = path.join(generators.rust.outputDir, 'target', 'debug', 'build');
const packageDirs = fs.readdirSync(buildOutDir).filter(d => d.startsWith('grpc-generated-'));
if (packageDirs.length > 0) {
const outDir = path.join(buildOutDir, packageDirs[0], 'out');
if (fs.existsSync(outDir)) {
const generatedFiles = fs.readdirSync(outDir).filter(f => f.endsWith('.rs'));
const modules = generatedFiles.map(f => {
const moduleName = f.replace('.rs', '').replace('.', '_');
return `pub mod ${moduleName} {\n include!(concat!(env!("OUT_DIR"), "/${f}"));\n}`;
});
libRs = '// Generated gRPC code\n' + modules.join('\n\n');
fs.writeFileSync(path.join(generators.rust.outputDir, 'src', 'lib.rs'), libRs);
}
}
console.log(chalk.green(`✓ Generated Rust code for all proto files`));
} catch (error) {
console.error(chalk.red(`✗ Failed to generate Rust code: ${error.message}`));
}
}
}
};
function getProtoFiles() {
if (!fs.existsSync(PROTO_DIR)) {
console.error(chalk.red(`Error: Proto directory ${PROTO_DIR} not found.`));
process.exit(1);
}
const files = fs.readdirSync(PROTO_DIR)
.filter(file => file.endsWith('.proto'));
if (files.length === 0) {
console.error(chalk.red('Error: No .proto files found in proto directory.'));
process.exit(1);
}
return files;
}
function ensureOutputDirectory(outputDir) {
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
}
function generateForLanguage(language) {
const generator = generators[language];
if (!generator) {
console.error(chalk.red(`Error: Unsupported language '${language}'. Supported languages: ${Object.keys(generators).join(', ')}`));
process.exit(1);
}
const protoFiles = getProtoFiles();
ensureOutputDirectory(generator.outputDir);
console.log(chalk.cyan(`Generating ${generator.name} code from ${protoFiles.length} proto file(s)...`));
console.log(chalk.gray(`Proto files: ${protoFiles.join(', ')}`));
console.log(chalk.gray(`Output directory: ${generator.outputDir}`));
generator.generate(protoFiles);
}
function generateAll() {
console.log(chalk.cyan('Generating code for all supported languages...'));
for (const language of Object.keys(generators)) {
console.log(chalk.cyan(`\n--- ${generators[language].name} ---`));
try {
generateForLanguage(language);
} catch (error) {
console.error(chalk.red(`Failed to generate ${generators[language].name} code: ${error.message}`));
}
}
console.log(chalk.green('\n🎉 Code generation completed!'));
}
// CLI setup
program
.name('grpc-codegen')
.description('Multi-language gRPC code generator')
.version('1.0.0');
program
.option('-l, --language <language>', 'generate code for specific language (go, python, typescript, rust)')
.option('--list', 'list supported languages')
.action((options) => {
if (options.list) {
console.log('Supported languages:');
for (const [key, generator] of Object.entries(generators)) {
console.log(` ${chalk.blue(key)}: ${generator.name}`);
}
return;
}
if (options.language) {
generateForLanguage(options.language);
} else {
generateAll();
}
});
// Handle direct execution
if (require.main === module) {
program.parse();
}