Skip to content

Commit 6e33f11

Browse files
committed
feat: add genproto logic to rewrite ts imports
Signed-off-by: Christian Stewart <[email protected]>
1 parent e988a0e commit 6e33f11

File tree

1 file changed

+41
-11
lines changed

1 file changed

+41
-11
lines changed

Makefile

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ GOLANGCI_LINT=hack/bin/golangci-lint
1010
GO_MOD_OUTDATED=hack/bin/go-mod-outdated
1111
GOLIST=go list -f "{{ .Dir }}" -m
1212

13+
GORELEASER=hack/bin/goreleaser
14+
1315
export GO111MODULE=on
1416
undefine GOARCH
1517
undefine GOOS
@@ -70,17 +72,18 @@ genproto: vendor node_modules $(GOIMPORTS) $(PROTOWRAP) $(PROTOC_GEN_GO) $(PROTO
7072
set -eo pipefail; \
7173
export PROJECT=$$(go list -m); \
7274
export PATH=$$(pwd)/hack/bin:$${PATH}; \
73-
export OUT=$$(pwd)/vendor; \
75+
export OUT=./vendor; \
7476
mkdir -p $${OUT}/$$(dirname $${PROJECT}); \
75-
rm $$(pwd)/vendor/$${PROJECT} || true; \
76-
ln -s $$(pwd) $$(pwd)/vendor/$${PROJECT} ; \
77+
rm ./vendor/$${PROJECT} || true; \
78+
ln -s $$(pwd) ./vendor/$${PROJECT} ; \
7779
protogen() { \
80+
PROTO_FILES=$$(git ls-files "$$1"); \
7881
$(PROTOWRAP) \
7982
-I $${OUT} \
8083
--plugin=./node_modules/.bin/protoc-gen-es \
8184
--plugin=./node_modules/.bin/protoc-gen-es-starpc \
8285
--go-lite_out=$${OUT} \
83-
--go-lite_opt=features=marshal+unmarshal+size+equal+clone+text+json \
86+
--go-lite_opt=features=marshal+unmarshal+size+equal+json+clone+text \
8487
--go-starpc_out=$${OUT} \
8588
--es_out=$${OUT} \
8689
--es_opt target=ts \
@@ -91,16 +94,33 @@ genproto: vendor node_modules $(GOIMPORTS) $(PROTOWRAP) $(PROTOC_GEN_GO) $(PROTO
9194
--proto_path $${OUT} \
9295
--print_structure \
9396
--only_specified_files \
94-
$$(\
95-
git \
96-
ls-files "$$1" |\
97-
xargs printf -- \
98-
"$$(pwd)/vendor/$${PROJECT}/%s "); \
97+
$$(echo "$$PROTO_FILES" | xargs printf -- "./vendor/$${PROJECT}/%s "); \
98+
for proto_file in $${PROTO_FILES}; do \
99+
proto_dir=$$(dirname $$proto_file); \
100+
proto_name=$${proto_file%".proto"}; \
101+
TS_FILES=$$(git ls-files ":(glob)$${proto_dir}/${proto_name}*_pb.ts"); \
102+
if [ -z "$$TS_FILES" ]; then continue; fi; \
103+
for ts_file in $${TS_FILES}; do \
104+
ts_file_dir=$$(dirname $$ts_file); \
105+
relative_path=$${ts_file_dir#"./"}; \
106+
depth=$$(echo $$relative_path | awk -F/ '{print NF+1}'); \
107+
prefix=$$(printf '../%0.s' $$(seq 1 $$depth)); \
108+
istmts=$$(grep -oE "from\s+\"$$prefix[^\"]+\"" $$ts_file) || continue; \
109+
if [ -z "$$istmts" ]; then continue; fi; \
110+
ipaths=$$(echo "$$istmts" | awk -F'"' '{print $$2}'); \
111+
for import_path in $$ipaths; do \
112+
rel_import_path=$$(realpath -s --relative-to=./vendor \
113+
"./vendor/$${PROJECT}/$${ts_file_dir}/$${import_path}"); \
114+
go_import_path=$$(echo $$rel_import_path | sed -e "s|^|@go/|"); \
115+
sed -i -e "s|$$import_path|$$go_import_path|g" $$ts_file; \
116+
done; \
117+
done; \
118+
done; \
99119
}; \
100120
protogen "./*.proto"; \
101-
rm $$(pwd)/vendor/$${PROJECT} || true
121+
true || protogen "./signaling/rpc/signaling.proto"; \
122+
rm -f ./vendor/$${PROJECT}
102123
$(GOIMPORTS) -w ./
103-
npm run format:js
104124

105125
.PHONY: gen
106126
gen: genproto
@@ -129,3 +149,13 @@ test:
129149
format: $(GOFUMPT) $(GOIMPORTS)
130150
$(GOIMPORTS) -w ./
131151
$(GOFUMPT) -w ./
152+
153+
$(GORELEASER):
154+
cd ./hack; \
155+
go build -v \
156+
-o ./bin/goreleaser \
157+
github.com/goreleaser/goreleaser
158+
159+
.PHONY: release
160+
release: $(GORELEASER)
161+
$(GORELEASER) release --clean

0 commit comments

Comments
 (0)