|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Released under the MIT License. |
| 4 | +# Copyright, 2025-2026, by Samuel Williams. |
| 5 | + |
| 6 | +# Generate Ruby protobuf classes from Envoy .proto files |
| 7 | +# @parameter proto_dir [String] Directory containing .proto files (default: "proto") |
| 8 | +# @parameter output_dir [String] Output directory for generated Ruby files (default: "lib") |
| 9 | +def generate_protos(proto_dir: "proto", output_dir: "lib") |
| 10 | + require "fileutils" |
| 11 | + |
| 12 | + proto_dir = File.expand_path(proto_dir) |
| 13 | + output_dir = File.expand_path(output_dir) |
| 14 | + |
| 15 | + # Core discovery service files (most important) |
| 16 | + discovery_files = [ |
| 17 | + "envoy/service/discovery/v3/discovery.proto", |
| 18 | + "envoy/service/discovery/v3/ads.proto" |
| 19 | + ] |
| 20 | + |
| 21 | + # Core config files needed for discovery |
| 22 | + config_files = [ |
| 23 | + "envoy/config/core/v3/base.proto", |
| 24 | + "envoy/config/core/v3/address.proto", |
| 25 | + "envoy/config/core/v3/config_source.proto", |
| 26 | + "envoy/config/cluster/v3/cluster.proto", |
| 27 | + "envoy/config/endpoint/v3/endpoint.proto" |
| 28 | + ] |
| 29 | + |
| 30 | + # Google protobuf well-known types |
| 31 | + google_files = [ |
| 32 | + "google/protobuf/any.proto", |
| 33 | + "google/protobuf/duration.proto", |
| 34 | + "google/protobuf/timestamp.proto", |
| 35 | + "google/protobuf/struct.proto", |
| 36 | + "google/protobuf/empty.proto", |
| 37 | + "google/protobuf/wrappers.proto", |
| 38 | + "google/rpc/status.proto" |
| 39 | + ] |
| 40 | + |
| 41 | + all_files = discovery_files + config_files + google_files |
| 42 | + |
| 43 | + # Create output directories |
| 44 | + FileUtils.mkdir_p(output_dir) |
| 45 | + |
| 46 | + # Generate Ruby code |
| 47 | + all_files.each do |proto_file| |
| 48 | + full_path = File.join(proto_dir, proto_file) |
| 49 | + next unless File.exist?(full_path) |
| 50 | + |
| 51 | + Console.info{"Generating #{proto_file}..."} |
| 52 | + |
| 53 | + system( |
| 54 | + "protoc", |
| 55 | + "--ruby_out=#{output_dir}", |
| 56 | + "--proto_path=#{proto_dir}", |
| 57 | + "--proto_path=#{File.join(proto_dir, 'google')}", |
| 58 | + full_path, |
| 59 | + out: File::NULL, |
| 60 | + err: File::NULL |
| 61 | + ) or begin |
| 62 | + Console.warn{"Failed to generate #{proto_file} (may have missing dependencies)"} |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + # Count generated files |
| 67 | + generated = Dir.glob(File.join(output_dir, "**/*_pb.rb")).count |
| 68 | + |
| 69 | + Console.info{"Generated #{generated} protobuf Ruby files in #{output_dir}"} |
| 70 | +end |
| 71 | + |
| 72 | +# Generate all protobuf files (including optional dependencies) |
| 73 | +# This will attempt to generate all .proto files, even if some fail |
| 74 | +# @parameter proto_dir [String] Directory containing .proto files (default: "proto") |
| 75 | +# @parameter output_dir [String] Output directory for generated Ruby files (default: "lib") |
| 76 | +def generate_all_protos(proto_dir: "proto", output_dir: "lib") |
| 77 | + require "fileutils" |
| 78 | + |
| 79 | + proto_dir = File.expand_path(proto_dir) |
| 80 | + output_dir = File.expand_path(output_dir) |
| 81 | + |
| 82 | + # Find all .proto files |
| 83 | + proto_files = Dir.glob(File.join(proto_dir, "**/*.proto")) |
| 84 | + |
| 85 | + Console.info{"Found #{proto_files.count} .proto files"} |
| 86 | + |
| 87 | + # Generate each file |
| 88 | + success_count = 0 |
| 89 | + fail_count = 0 |
| 90 | + |
| 91 | + proto_files.each do |proto_file| |
| 92 | + relative_path = proto_file.sub(/^#{proto_dir}\//, "") |
| 93 | + |
| 94 | + Console.debug{"Generating #{relative_path}..."} |
| 95 | + |
| 96 | + if system( |
| 97 | + "protoc", |
| 98 | + "--ruby_out=#{output_dir}", |
| 99 | + "--proto_path=#{proto_dir}", |
| 100 | + "--proto_path=#{File.join(proto_dir, 'google')}", |
| 101 | + proto_file, |
| 102 | + out: File::NULL, |
| 103 | + err: File::NULL |
| 104 | + ) |
| 105 | + success_count += 1 |
| 106 | + else |
| 107 | + fail_count += 1 |
| 108 | + Console.debug{"Failed: #{relative_path}"} |
| 109 | + end |
| 110 | + end |
| 111 | + |
| 112 | + # Count generated files |
| 113 | + generated = Dir.glob(File.join(output_dir, "**/*_pb.rb")).count |
| 114 | + |
| 115 | + Console.info{"Generated #{generated} protobuf Ruby files (#{success_count} succeeded, #{fail_count} failed)"} |
| 116 | +end |
0 commit comments