Skip to content

Commit 9f0529b

Browse files
authored
Merge pull request #11 from JuliaComputing/tan/gen
code generator improvements
2 parents f8f2f68 + 8d3cd75 commit 9f0529b

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/generate.jl

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
const package_regex = r"package\s(\S*)[\s]*;.*"
22
const service_regex = r"service\s(\S*)[\s]*{.*"
33

4-
function write_header(io, package, client_module_name)
4+
function write_header(io, generated_module, package, client_module_name)
55
print(io, """module $(client_module_name)
66
using gRPCClient
77
8-
include("$(package).jl")
8+
include("$(generated_module).jl")
99
using .$(package)
1010
1111
import Base: show
@@ -127,7 +127,7 @@ Generate a gRPC client from protobuf specification file.
127127
- `outdir`: Directory to write generated code into, created if not present
128128
already. Existing files if any will be overwtitten.
129129
"""
130-
function generate(proto::String; outdir::String=pwd())
130+
function generate(proto::String; outdir::String=pwd(), includes::Vector{String}=String[])
131131
if !isfile(proto)
132132
throw(ArgumentError("No such file - $proto"))
133133
end
@@ -138,14 +138,18 @@ function generate(proto::String; outdir::String=pwd())
138138
# determine the package name and service name
139139
package, services = detect_services(proto)
140140
protodir = dirname(proto)
141-
@info("Detected", package, services)
141+
includeflag = `-I=$protodir`
142+
for inc in includes
143+
includeflag = `$includeflag -I=$inc`
144+
end
145+
@info("Detected", package, services, includes)
142146

143147
# generate protobuf services
144148
mkpath(outdir)
145149
bindir = Sys.BINDIR
146150
pathenv = string(ENV["PATH"], Sys.iswindows() ? ";" : ":", bindir)
147151
withenv("PATH"=>pathenv) do
148-
ProtoBuf.protoc(`-I=$protodir --julia_out=$outdir $proto`)
152+
ProtoBuf.protoc(`$includeflag --julia_out=$outdir $proto`)
149153
end
150154

151155
# include the generated code and detect service method names
@@ -154,9 +158,9 @@ function generate(proto::String; outdir::String=pwd())
154158
Main.eval(:(include($generated_module_file)))
155159

156160
# generate the gRPC client code
157-
client_module_name = string(titlecase(package; strict=false), "Clients")
161+
client_module_name = string(titlecase(generated_module; strict=false), "Clients")
158162
open(joinpath(outdir, "$(client_module_name).jl"), "w") do grpcservice
159-
write_header(grpcservice, package, client_module_name)
163+
write_header(grpcservice, generated_module, package, client_module_name)
160164
for service in services
161165
methods = get_generated_method_table(string(package, "._", service, "_methods"))
162166
write_service(grpcservice, package, service, methods)

0 commit comments

Comments
 (0)