forked from tokenio/sdk-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_proto.rb
executable file
·101 lines (86 loc) · 3.21 KB
/
build_proto.rb
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
#
# Fetches specified proto files from the artifact repository.
#
TOKEN_PROTOS_VER = "1.12.11"
RPC_PROTOS_VER = "1.0.3"
require 'open-uri'
require 'fileutils'
def fetch_protos()
def download(path, name, type, version)
file = "#{name}-#{type}-#{version}.jar"
puts("Downloading #{file} ...")
m2path = ENV["HOME"] + "/.m2/repository/#{path}/#{name}-#{type}/#{version}/#{file}"
if File.file?(m2path) then
FileUtils.cp(m2path, file)
else
url = "https://token.jfrog.io/token/libs-release/#{path}/#{name}-#{type}/#{version}/#{file}"
open(file, 'wb') do |file|
file << open(url).read
end
end
file
end
system("rm protos/common/**.proto")
system("rm -rf protos/external")
system("rm -rf protos/extensions")
system("mkdir -p protos/external")
file = download("io/token/proto", "tokenio-proto", "external", TOKEN_PROTOS_VER)
puts("unzipping #{file}")
system("unzip -d protos/external -o #{file} 'gateway/*.proto'")
system("rm -f #{file}");
system("mkdir -p protos/common")
file = download("io/token/proto", "tokenio-proto", "common", TOKEN_PROTOS_VER)
puts("unzipping #{file}")
system("unzip -d protos/common -o #{file} '**.proto'")
system("rm -f #{file}");
system("mkdir -p protos/extensions")
file = download("io/token/proto/extensions", "tokenio-proto", "extensions", RPC_PROTOS_VER)
system("unzip -d protos/ -o #{file} '*.proto'")
system("rm -f #{file}");
end
#
# Generates PHP code for the protos.
#
def generate_protos_cmd(src, path_to_protos, out_dir)
#Provide path to gRPC extension
protoc_dir = "./tools/" + ((RUBY_PLATFORM.include?"linux") ? "linux_x64" : "macosx_x64");
protoc = "#{protoc_dir}/protoc"
plugin = "#{protoc_dir}/grpc_php_plugin"
result = <<-CMD
mkdir -p #{out_dir}
#{protoc} \
--plugin=protoc-gen-grpc=#{plugin} \
--php_out=#{out_dir} \
--grpc_out=#{out_dir} \
-I #{src}/common \
-I #{src}/external \
-I #{src} \
-I . \
#{src}/#{path_to_protos}/*.proto
CMD
result
end
# Fetch the protos.
fetch_protos();
# Build the command that generates the protos.
dir = "./lib/Proto"
system("rm -rf #{dir}/Google");
system("rm -rf #{dir}/GPBMetadata");
system("rm -rf #{dir}/Io");
system("mkdir #{dir}");
Dir.chdir("./protos");
protoDirs = Dir["common/**/"]
protoDirs = protoDirs.select do |elem| !elem.start_with?("common/google") end # ignore generated files
Dir.chdir("..");
gencommand = generate_protos_cmd("./protos", "common/google/api", dir) +
generate_protos_cmd("./protos", "common/provider", dir) +
generate_protos_cmd("./protos", "common/tsp", dir) +
generate_protos_cmd("./protos", "common/grpcbridge/swagger", dir) +
generate_protos_cmd("./protos", "common/google/api", dir) +
generate_protos_cmd("./protos", "extensions", dir) +
generate_protos_cmd("./protos", "external/gateway", dir);
for protoDir in protoDirs
# remove trailing /
gencommand = gencommand + generate_protos_cmd("./protos", protoDir[0..protoDir.length-2], dir);
end
system(gencommand);