From 1aa1b25b1adb6700299903f593b8f089eff7df91 Mon Sep 17 00:00:00 2001 From: Ryo Narita Date: Wed, 2 Dec 2020 15:42:40 +0900 Subject: [PATCH 1/2] Use nodejs scripts instead of sed to strip some imports --- pkg/app/web/BUILD.bazel | 46 ++++++++++++++---------------------- pkg/app/web/strip-imports.js | 18 ++++++++++++++ 2 files changed, 36 insertions(+), 28 deletions(-) create mode 100644 pkg/app/web/strip-imports.js diff --git a/pkg/app/web/BUILD.bazel b/pkg/app/web/BUILD.bazel index 080696ebc7..35010f83a0 100644 --- a/pkg/app/web/BUILD.bazel +++ b/pkg/app/web/BUILD.bazel @@ -1,6 +1,7 @@ load("@npm//webpack-cli:index.bzl", webpack = "webpack_cli") load("@npm//typescript:index.bzl", "tsc") load("@npm_bazel_labs//:index.bzl", "ts_proto_library") +load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary") load(":jest.bzl", "jest_test") ts_proto_library( @@ -13,42 +14,31 @@ ts_proto_library( proto = "//pkg/model:model_proto", ) +nodejs_binary( + name = "strip_imports", + entry_point = ":strip-imports.js", +) + genrule( name = "build_api", - srcs = [":api_proto"], + srcs = [ + ":api_proto", + ], outs = ["api_client"], - cmd = """ - mkdir $(OUTS) - for f in $(SRCS) - do - sed -ie "s|.*validate_pb.*||g" $$f - sed -ie "s|'.*pkg|'pipe/pkg|g" $$f - sed -ie "s|.*github_com_gogo_protobuf_gogoproto_gogo_pb.*||g" $$f - sed -ie "s|pipe/pkg/model|pipe/pkg/app/web/model|g" $$f - cp $$f $(OUTS)/ - done - """, - output_to_bindir = 1, - visibility = ["//visibility:public"], + cmd = "$(locations strip_imports) $(OUTS) $(SRCS)", + output_to_bindir = True, + tools = [":strip_imports"], ) genrule( name = "build_model", - srcs = [":model_proto_ts"], + srcs = [ + ":model_proto_ts", + ], outs = ["model"], - cmd = """ - mkdir $(OUTS) - for f in $(SRCS) - do - sed -ie "s|.*validate_pb.*||g" $$f - sed -ie "s|'.*pkg|'pipe/pkg|g" $$f - sed -ie "s|.*github_com_gogo_protobuf_gogoproto_gogo_pb.*||g" $$f - sed -ie "s|pipe/pkg/model|pipe/pkg/app/web/model|g" $$f - cp $$f $(OUTS)/ - done - """, - output_to_bindir = 1, - visibility = ["//visibility:public"], + cmd = "$(locations strip_imports) $(OUTS) $(SRCS)", + output_to_bindir = True, + tools = [":strip_imports"], ) tsc( diff --git a/pkg/app/web/strip-imports.js b/pkg/app/web/strip-imports.js new file mode 100644 index 0000000000..f1738c3089 --- /dev/null +++ b/pkg/app/web/strip-imports.js @@ -0,0 +1,18 @@ +/* eslint-disable @typescript-eslint/no-var-requires */ +const fs = require("fs"); +const path = require("path"); +const OUTPUT_DIR = process.argv[2]; +const files = process.argv.slice(3); + +fs.mkdirSync(OUTPUT_DIR); +files.forEach((file) => { + const basename = path.basename(file); + const f = fs.readFileSync(file, { + encoding: "utf-8", + }); + + fs.writeFileSync( + `${OUTPUT_DIR}/${basename}`, + f.replace(/.*validate_pb.*/g, "").replace(/'.*pkg/g, "'pipe/pkg/app/web") + ); +}); From c4b11f0aca0c11d9dbfb2a07a5c4a20ea054d272 Mon Sep 17 00:00:00 2001 From: Ryo Narita Date: Wed, 2 Dec 2020 19:02:45 +0900 Subject: [PATCH 2/2] Fix nits --- pkg/app/web/BUILD.bazel | 8 ++------ pkg/app/web/strip-imports.js | 1 - 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/pkg/app/web/BUILD.bazel b/pkg/app/web/BUILD.bazel index 35010f83a0..b7c91f516b 100644 --- a/pkg/app/web/BUILD.bazel +++ b/pkg/app/web/BUILD.bazel @@ -21,9 +21,7 @@ nodejs_binary( genrule( name = "build_api", - srcs = [ - ":api_proto", - ], + srcs = [":api_proto"], outs = ["api_client"], cmd = "$(locations strip_imports) $(OUTS) $(SRCS)", output_to_bindir = True, @@ -32,9 +30,7 @@ genrule( genrule( name = "build_model", - srcs = [ - ":model_proto_ts", - ], + srcs = [":model_proto_ts"], outs = ["model"], cmd = "$(locations strip_imports) $(OUTS) $(SRCS)", output_to_bindir = True, diff --git a/pkg/app/web/strip-imports.js b/pkg/app/web/strip-imports.js index f1738c3089..a9d210cea5 100644 --- a/pkg/app/web/strip-imports.js +++ b/pkg/app/web/strip-imports.js @@ -1,4 +1,3 @@ -/* eslint-disable @typescript-eslint/no-var-requires */ const fs = require("fs"); const path = require("path"); const OUTPUT_DIR = process.argv[2];