diff --git a/README.md b/README.md index 9b32f06..d3da2d3 100644 --- a/README.md +++ b/README.md @@ -1,34 +1,48 @@ -# BDChain gRPC API 指南 +## BDLedger gRPC API -## 编译 Go 版本 gRPC +### Generating gRPC code in languages -### 安装 +#### Installling tools -- [Protocol Buffers 3](https://github.com/protocolbuffers/protobuf/releases) - - 下载对应版本,并将可执行程序 `protoc` 的目录加入到环境变量 `PATH` 中 -- Go > 1.6 - ```bash - # 安装 Go 版本相关插件 - go get -u google.golang.org/grpc - go get -u github.com/golang/protobuf/protoc-gen-go - ``` -- Node.js - ```bash - npm install -g grpc-tools - ``` -- Web - ```bash - git clone https://github.com/grpc/grpc-web - cd grpc-web - sudo make install-plugin - ``` +Download the [Protocol Buffers compiler](https://github.com/protocolbuffers/protobuf/releases) (protoc-3.11.4-*) for your platform and add `protoc` to `PATH` environment variable -参考资料: +For generating Go code: +```bash +GIT_TAG="v1.4.0-rc.4" +go get -d -u github.com/golang/protobuf/protoc-gen-go +git -C "$(go env GOPATH)"/src/github.com/golang/protobuf checkout $GIT_TAG +go install github.com/golang/protobuf/protoc-gen-go +``` +(Update to google.golang.org/protobuf when https://github.com/grpc/grpc-go/pull/3435 resolves) +For generating Java code: From `bdledger-sdk-java` run `gradle generateProto` + +For generating Node.js code: +```bash +npm install -g grpc-tools +``` +For generating TypeScript definitions, also run: +```bash +npm install -g protoc-gen-ts +``` + +For frontend JavaScript: +```bash +git clone https://github.com/grpc/grpc-web +cd grpc-web +sudo make install-plugin +``` + +References: - [Go Quick Start](https://grpc.io/docs/quickstart/go.html#prerequisites) - [gRPC Go FAQ](https://github.com/grpc/grpc-go#faq) -### 编译 +**TODO: Look into [protobuf.js](https://github.com/protobufjs/protobuf.js) for Nodes.js and frontend** +**TODO: Look into [Kroto+](https://github.com/marcoferrer/kroto-plus) for Kotlin** -- 在本项目打开控制台,或者切换工作路径到本项目 -- 执行 `./scripts/gen-{{platform}}` +#### Generating code + +Run +```bash +./scripts/gen [go] [nodejs] [ts] +``` diff --git a/scripts/gen-go.bat b/scripts/gen-go.bat deleted file mode 100644 index 620d1bf..0000000 --- a/scripts/gen-go.bat +++ /dev/null @@ -1,20 +0,0 @@ -@echo off -cd /d "%~dp0.." -set dir=.\gen\go -set exe=protoc -set gen=protoc-gen-go - -if defined GOPATH set PATH=%PATH%;%GOPATH%\bin - -where /q %exe% || echo missing %exe% && exit /B -where /q %gen% || echo missing %gen% && exit /B - -if not exist %dir% mkdir %dir% -%exe% -I . --go_out=plugins=grpc:%dir% bdledger/api/common.proto -%exe% -I . --go_out=plugins=grpc:%dir% bdledger/api/error_details.proto -%exe% -I . --go_out=plugins=grpc:%dir% bdledger/api/node.proto -%exe% -I . --go_out=plugins=grpc:%dir% bdledger/api/ledger.proto -%exe% -I . --go_out=plugins=grpc:%dir% bdledger/api/query.proto -%exe% -I . --go_out=plugins=grpc:%dir% bdledger/api/contract.proto - -echo all done diff --git a/scripts/gen-nodejs.bat b/scripts/gen-nodejs.bat deleted file mode 100644 index c029ab1..0000000 --- a/scripts/gen-nodejs.bat +++ /dev/null @@ -1,20 +0,0 @@ -@echo off -cd /d "%~dp0.." -set dir=.\gen\nodejs -set exe=grpc_tools_node_protoc -set plugin=grpc_tools_node_protoc_plugin - -where /q %exe% || echo missing %exe% && exit /B -where /q %plugin% || echo missing %plugin% && exit /B - -for /f %%i in ('where %plugin%') do set pluginPath=%%i - -if not exist %dir% mkdir %dir% -call %exe% -I . --js_out=import_style=commonjs,binary:%dir% --grpc_out=%dir% --plugin=protoc-gen-grpc=%pluginPath% bdledger/api/common.proto -call %exe% -I . --js_out=import_style=commonjs,binary:%dir% --grpc_out=%dir% --plugin=protoc-gen-grpc=%pluginPath% bdledger/api/error_details.proto -call %exe% -I . --js_out=import_style=commonjs,binary:%dir% --grpc_out=%dir% --plugin=protoc-gen-grpc=%pluginPath% bdledger/api/node.proto -call %exe% -I . --js_out=import_style=commonjs,binary:%dir% --grpc_out=%dir% --plugin=protoc-gen-grpc=%pluginPath% bdledger/api/ledger.proto -call %exe% -I . --js_out=import_style=commonjs,binary:%dir% --grpc_out=%dir% --plugin=protoc-gen-grpc=%pluginPath% bdledger/api/query.proto -call %exe% -I . --js_out=import_style=commonjs,binary:%dir% --grpc_out=%dir% --plugin=protoc-gen-grpc=%pluginPath% bdledger/api/contract.proto - -echo all done diff --git a/scripts/gen.bat b/scripts/gen.bat new file mode 100644 index 0000000..46092bb --- /dev/null +++ b/scripts/gen.bat @@ -0,0 +1,37 @@ +@echo off +setlocal EnableDelayedExpansion + +cd /d "%~dp0.." +where /q protoc || echo Missing protoc && exit /b +set gen=.\gen +set pkg=bdware/bdledger/api +set protos=%pkg%/common.proto %pkg%/error_details.proto %pkg%/node.proto %pkg%/ledger.proto %pkg%/query.proto %pkg%/contract.proto + +for %%A in (%*) do ( + if "%%A"=="go" ( + set plugin=protoc-gen-go + where /q !plugin! || echo Missing !plugin! && exit /b + set out=%gen%\go + if not exist !out! mkdir !out! + echo Generating Go code + start /b protoc --go_out=plugins=grpc:!out! %protos% + ) + if "%%A"=="nodejs" ( + set plugin=grpc_tools_node_protoc_plugin + where /q !plugin! || echo missing !plugin! && exit /b + for /f %%i in ('where !plugin!') do set pluginPath=%%i + set out=%gen%\nodejs + if not exist !out! mkdir !out! + echo Generating Node.js code + start /b protoc --js_out=import_style=commonjs,binary:!nout! --grpc_out=!nout! --plugin=protoc-gen-grpc=!pluginPath! %protos% + ) + if "%%A"=="ts" ( + set plugin=protoc-gen-ts + where /q !plugin! || echo missing !plugin! && exit /B + for /f %%i in ('where !plugin!') do set pluginPath=%%i + set out=%gen%\nodejs + if not exist !out! mkdir !out! + echo Generating TypeScript definitions + start /b protoc --ts_out=!out! --plugin=protoc-gen-ts=!pluginPath! %protos% + ) +) diff --git a/scripts/gen.sh b/scripts/gen.sh new file mode 100644 index 0000000..69612a0 --- /dev/null +++ b/scripts/gen.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash + +cd "${0%/*}/.." + +# TODO