diff options
author | Yoshiya Hinosawa <stibium121@gmail.com> | 2018-07-01 23:37:10 +0900 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2018-07-01 17:22:36 +0200 |
commit | ea35281d637cf326c4d808111a59d986c92e6a4b (patch) | |
tree | c02b9836c4d5736aec2a412c2f82c589ed4c1d33 /js/pbjs_hack.py | |
parent | 9528ee4a42eb0729a4467ddaaa3be573d9053fa2 (diff) |
chore: move //src/js to //js
refs: #285
Diffstat (limited to 'js/pbjs_hack.py')
-rwxr-xr-x | js/pbjs_hack.py | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/js/pbjs_hack.py b/js/pbjs_hack.py new file mode 100755 index 000000000..19b7c1691 --- /dev/null +++ b/js/pbjs_hack.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python +""" +gn can only run python scripts. + +Generates protobufjs code. +""" +import subprocess +import sys +import os +# TODO(ry) Ideally protobufjs output files should be written into +# target_gen_dir, but its difficult to get this working in a way that parcel can +# resolve their location. (Parcel does not support NODE_PATH.) Therefore this +# hack: write the generated msg.pb.js and msg.pb.d.ts outputs into the js/ +# folder, and we check them into the repo. Hopefully this hack can be removed at +# some point. If msg.proto is changed, commit changes to the generated JS +# files. + +js_path = os.path.dirname(os.path.realpath(__file__)) +pbjs_path = os.path.join(js_path, "node_modules", "protobufjs", "bin") +pbjs_bin = os.path.join(pbjs_path, "pbjs") +pbts_bin = os.path.join(pbjs_path, "pbts") +msg_pbjs_out = os.path.join(js_path, "msg.pb.js") +msg_pbts_out = os.path.join(js_path, "msg.pb.d.ts") +assert os.path.exists(pbjs_bin) +assert os.path.exists(pbts_bin) + +proto_in = sys.argv[1] +stamp_file = sys.argv[2] + + +def touch(fname): + if os.path.exists(fname): + os.utime(fname, None) + else: + open(fname, 'a').close() + + +subprocess.check_call([ + "node", pbjs_bin, "--target=static-module", "--wrapper=commonjs", + "--out=" + msg_pbjs_out, proto_in +]) +assert os.path.exists(msg_pbjs_out) + +subprocess.check_call( + ["node", pbts_bin, "--out=" + msg_pbts_out, msg_pbjs_out]) +assert os.path.exists(msg_pbts_out) + +touch(stamp_file) |