diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b4b112d19..82aad9297 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -275,7 +275,7 @@ jobs: - name: Generate run: scripts/check_generate_code.py - name: Generate gRPC - run: bash scripts/check-grpc-generated-code.sh + run: scripts/check-grpc-generated-code.py build-benchmarks: name: Build Benchmarks (on Linux) diff --git a/grpc/examples/generate.sh b/grpc/examples/generate.sh deleted file mode 100755 index 0f051da8e..000000000 --- a/grpc/examples/generate.sh +++ /dev/null @@ -1,71 +0,0 @@ -#!/bin/bash -# -# Copyright 2021 Google Inc. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -set -e - -current_dir=`pwd` - -cd ../.. - -main_dir=`pwd` - -cd ${current_dir} - -# Looks for flatc within the root dir & debug -if [ -e ${main_dir}/flatc ]; then - alias fbc='${main_dir}/flatc' -elif [ -e ${main_dir}/Debug/flatc ]; then - alias fbc='${main_dir}/Debug/flatc' -else - echo 'flatc' could not be found. Make sure to build FlatBuffers from the \ - $rootdir directory. - exit 1 -fi - -generator="--grpc $current_dir/greeter.fbs" - -# Regenerate Go lang code -cd go - -cd greeter -fbc --bfbs-filenames ../.. --go ${generator} - -cd ${current_dir} - -# Regenerate Python code -cd python - -cd greeter - -fbc --bfbs-filenames ../.. --python ${generator} - -cd ${current_dir} - -# Regenerate Swift code -cd swift - -cd Greeter/Sources/Model -fbc --bfbs-filenames ../../../.. --swift --gen-json-emit ${generator} - -cd ${current_dir} - -# Regenerate Typescript code -cd ts - -cd greeter/src -fbc --bfbs-filenames ../../.. --ts ${generator} - -cd ${current_dir} diff --git a/grpc/examples/ts/greeter/src/greeter_generated.ts b/grpc/examples/ts/greeter/src/greeter_generated.ts new file mode 100644 index 000000000..ddda80de8 --- /dev/null +++ b/grpc/examples/ts/greeter/src/greeter_generated.ts @@ -0,0 +1,4 @@ +// automatically generated by the FlatBuffers compiler, do not modify + +export { HelloReply } from './models/hello-reply'; +export { HelloRequest } from './models/hello-request'; diff --git a/scripts/check-grpc-generated-code.py b/scripts/check-grpc-generated-code.py new file mode 100755 index 000000000..c9a43837a --- /dev/null +++ b/scripts/check-grpc-generated-code.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python3 +# +# Copyright 2022 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import subprocess +import sys +import generate_grpc_examples +from pathlib import Path + +# Get the path where this script is located so we can invoke the script from +# any directory and have the paths work correctly. +script_path = Path(__file__).parent.resolve() + +# Get the root path as an absolute path, so all derived paths are absolute. +root_path = script_path.parent.absolute() + +print("Generating GRPC code...") +generate_grpc_examples.GenerateGRPCExamples() + +result = subprocess.run(["git", "diff", "--quiet"], cwd=root_path) + +if result.returncode != 0: + print( + "\n" + "ERROR: ********************************************************\n" + "ERROR: * The following differences were found after running *\n" + "ERROR: * the script/generate_grpc_examples.py script. Maybe *\n" + "ERROR: * you forgot to run it after making changes in a *\n" + "ERROR: * generator or schema? *\n" + "ERROR: ********************************************************\n" + ) + subprocess.run(["git", "diff", "--binary", "--exit-code"], cwd=root_path) + sys.exit(result.returncode) diff --git a/scripts/check-grpc-generated-code.sh b/scripts/check-grpc-generated-code.sh deleted file mode 100755 index 4541be16e..000000000 --- a/scripts/check-grpc-generated-code.sh +++ /dev/null @@ -1,32 +0,0 @@ -#!/bin/bash -# -# Copyright 2021 Google Inc. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -set -e - -echo "Checks generated grpc code" -cd grpc/examples -sh generate.sh -cd .. - -if ! git diff --quiet; then - echo >&2 - echo "ERROR: ********************************************************" >&2 - echo "ERROR: The following differences were found after running the" >&2 - echo "ERROR: grpc/example/generate.sh script. Maybe you forgot to run" >&2 - echo "ERROR: it after making changes in a generator or schema?" >&2 - echo "ERROR: ********************************************************" >&2 - echo >&2 - git diff --binary --exit-code -fi diff --git a/scripts/generate_code.py b/scripts/generate_code.py index 4ddb3daff..93f4c5905 100755 --- a/scripts/generate_code.py +++ b/scripts/generate_code.py @@ -20,6 +20,7 @@ import glob import platform import shutil import subprocess +import generate_grpc_examples from pathlib import Path parser = argparse.ArgumentParser() @@ -514,3 +515,6 @@ def flatc_annotate(schema, include=None, data=None, cwd=tests_path): flatc_annotate( schema="monster_test.fbs", include="include_test", data="monsterdata_test.mon" ) + +# Run the generate_grpc_examples script +generate_grpc_examples.GenerateGRPCExamples() diff --git a/scripts/generate_grpc_examples.py b/scripts/generate_grpc_examples.py new file mode 100755 index 000000000..c5dbba51e --- /dev/null +++ b/scripts/generate_grpc_examples.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python3 +# +# Copyright 2022 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from util import flatc, root_path +from pathlib import Path + +grpc_examples_path = Path(root_path, "grpc/examples") + +greeter_schema = Path(grpc_examples_path, "greeter.fbs") + +COMMON_ARGS = [ + "--grpc", + "--bfbs-filenames", + str(grpc_examples_path), +] + +def GenerateGRPCExamples(): + + flatc( + COMMON_ARGS + + [ + "--go", + ], + schema=greeter_schema, + cwd=Path(grpc_examples_path, "go/greeter"), + ) + + flatc( + COMMON_ARGS + + [ + "--python", + ], + schema=greeter_schema, + cwd=Path(grpc_examples_path, "python/greeter"), + ) + + flatc( + COMMON_ARGS + + [ + "--swift", + "--gen-json-emit", + ], + schema=greeter_schema, + cwd=Path(grpc_examples_path, "swift/Greeter/Sources/Model"), + ) + + flatc( + COMMON_ARGS + + [ + "--ts", + ], + schema=greeter_schema, + cwd=Path(grpc_examples_path, "ts/greeter/src"), + ) + +if __name__ == "__main__": + GenerateGRPCExamples() diff --git a/scripts/util.py b/scripts/util.py new file mode 100644 index 000000000..365ba2de9 --- /dev/null +++ b/scripts/util.py @@ -0,0 +1,51 @@ +# Copyright 2022 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import platform +import subprocess +from pathlib import Path + +# Get the path where this script is located so we can invoke the script from +# any directory and have the paths work correctly. +script_path = Path(__file__).parent.resolve() + +# Get the root path as an absolute path, so all derived paths are absolute. +root_path = script_path.parent.absolute() + +# Get the location of the flatc executable, reading from the first command line +# argument or defaulting to default names. +flatc_exe = Path("flatc" if not platform.system() == "Windows" else "flatc.exe") + +# Find and assert flatc compiler is present. +if root_path in flatc_exe.parents: + flatc_exe = flatc_exe.relative_to(root_path) +flatc_path = Path(root_path, flatc_exe) +assert flatc_path.exists(), "Cannot find the flatc compiler " + str(flatc_path) + +# Execute the flatc compiler with the specified parameters +def flatc(options, schema, prefix=None, include=None, data=None, cwd=root_path): + cmd = [str(flatc_path)] + options + if prefix: + cmd += ["-o"] + [prefix] + if include: + cmd += ["-I"] + [include] + if isinstance(schema, Path): + cmd += [str(schema)] + elif isinstance(schema, str): + cmd += [schema] + else: + cmd += schema + if data: + cmd += [data] if isinstance(data, str) else data + return subprocess.check_call(cmd, cwd=str(cwd))