bdledger-apis/grpc/tasks.py
2021-07-22 18:54:54 +08:00

59 lines
1.7 KiB
Python

import os
import platform
from pathlib import Path
from invoke import task
ROOT = Path(__file__).parent
PB_ROOT = ROOT / "pb"
# @task
# def clean(c, docs=False, bytecode=False, extra=""):
# patterns = ["build"]
# if docs:
# patterns.append("docs/_build")
# if bytecode:
# patterns.append("**/*.pyc")
# if extra:
# patterns.append(extra)
# for pattern in patterns:
# c.run("rm -rf {}".format(pattern))
@task(
help={
"target": "Target gRPC licent code to generate.",
}
)
def gen(c, target="types"):
"""
Generate gRPC client code.
"""
out_dir = "gen/" + target
if not os.path.exists(out_dir):
os.mkdir(out_dir)
npmbin_path = Path(c.run("npm bin").stdout.rstrip())
if target == "ts":
plugin_path = npmbin_path / ("protoc-gen-ts_proto" + (".cmd" if platform.system() == "Windows" else ""))
cmd = "protoc --plugin=protoc-gen-ts_proto={} " \
"--ts_proto_opt=outputServices=grpc-js " \
"--ts_proto_opt=esModuleInterop=true " \
"--ts_proto_opt=env=node " \
"--ts_proto_out={} -I pb {}".format(
plugin_path, out_dir, "./pb/bdware/bdledger/api/*.proto")
print(cmd)
c.run(cmd)
elif target == "types":
paths = Path(PB_ROOT).glob('**/*.proto')
for path in paths:
# because path is object not string
path_str = str(path)
print("Generating for", path_str)
cmd = "npx proto-loader-gen-types --includeComments --grpcLib @grpc/grpc-js -I pb -O {} {}".format(
out_dir, path_str)
print(cmd)
c.run(cmd)