Skip to content

Commit e0c1bc7

Browse files
0divsitole
andauthored
Make codegen robust and deterministic for js and python SDKs (#664)
# Description This PR allows to codegen without worrying about your environment and dependencies for consistent outcomes in what code is generated from openapi and envd protobuf spec. Will generate files for `js-sdk` and `python-sdk` - [x] create Docker image with all the pinned deps needed to codegen - [x] create codegen script using this container with mapped volumes - [x] adapt Makefiles to this new approach - [x] adapt where we install the connect-python protobuf binary for this to work # Test ```sh make codegen # this command is similar to `make generate` but w/o the hassle Generating SDK code from openapi and envd spec cd packages/js-sdk && pnpm generate > [email protected] generate /workspace/packages/js-sdk > python ./../../spec/remove_extra_tags.py sandboxes templates && openapi-typescript ../../spec/openapi_generated.yml -x api_key --arr ay-length --alphabetize --output src/api/schema.gen.ts ✨ openapi-typescript 7.6.1 🚀 ../../spec/openapi_generated.yml → src/api/schema.gen.ts [44.2ms] cd packages/js-sdk && pnpm generate-envd-api > [email protected] generate-envd-api /workspace/packages/js-sdk > openapi-typescript ../../spec/envd/envd.yaml -x api_key --array-length --alphabetize --output src/envd/schema.gen.ts ✨ openapi-typescript 7.6.1 🚀 ../../spec/envd/envd.yaml → src/envd/schema.gen.ts [22.7ms] [...] cd packages/python-sdk && make generate-api All done! ✨ 🍰 ✨ ``` --------- Co-authored-by: Jiri Sveceny <[email protected]>
1 parent e9f39b0 commit e0c1bc7

File tree

64 files changed

+1055
-451
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+1055
-451
lines changed

Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ update-api-spec:
33
@./scripts/update-api-spec.sh
44
@echo "Done"
55

6+
.PHONY: codegen
7+
codegen:
8+
@echo "Generating SDK code from openapi and envd spec"
9+
@./scripts/codegen.sh
610

711
generate: generate-js generate-python
812

@@ -12,7 +16,9 @@ generate-js:
1216
cd spec/envd && buf generate --template buf-js.gen.yaml
1317

1418
generate-python:
15-
$(MAKE) -C packages/connect-python build
19+
if [ ! -f "/go/bin/protoc-gen-connect-python" ]; then \
20+
$(MAKE) -C packages/connect-python build; \
21+
fi
1622
cd packages/python-sdk && make generate-api
1723
cd spec/envd && buf generate --template buf-python.gen.yaml
1824
cd packages/python-sdk && ./scripts/fix-python-pb.sh && black .

codegen.Dockerfile

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
FROM golang:1.23
2+
3+
# Install Golang deps
4+
RUN go install github.com/bufbuild/buf/cmd/[email protected] && \
5+
go install google.golang.org/protobuf/cmd/[email protected] && \
6+
go install connectrpc.com/connect/cmd/[email protected]
7+
8+
# Install our custom protoc plugin, connect-python
9+
COPY ./packages/connect-python /packages/connect-python
10+
RUN cd /packages/connect-python && make bin/protoc-gen-connect-python
11+
12+
13+
FROM python:3.9
14+
15+
# Set working directory
16+
WORKDIR /workspace
17+
18+
ENV PROTOC_ZIP=protoc-29.3-linux-aarch_64.zip
19+
20+
RUN curl -OL https://github.com/protocolbuffers/protobuf/releases/download/v29.3/$PROTOC_ZIP
21+
RUN unzip -o $PROTOC_ZIP -d /usr/local bin/protoc
22+
23+
# Copy installed Go deps from previous build step
24+
COPY --from=0 /go /go
25+
26+
# Add Go binary to PATH
27+
ENV PATH="/go/bin:${PATH}"
28+
29+
# Install Python deps
30+
RUN pip install black==23.7.0 pyyaml==6.0.2 openapi-python-client==0.24.3
31+
32+
# Install Node.js and npm
33+
RUN apt-get update && \
34+
apt-get install -y curl && \
35+
curl -fsSL https://deb.nodesource.com/setup_18.x | bash - && \
36+
apt-get install -y nodejs && \
37+
apt-get clean && \
38+
rm -rf /var/lib/apt/lists/*
39+
40+
# Install Node.js deps
41+
RUN npm install -g pnpm @connectrpc/[email protected] @bufbuild/[email protected]
42+
43+
# Generate when container starts
44+
CMD ["make", "generate"]

packages/connect-python/Makefile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ upload: clean
2121

2222

2323
bin/$(plugin): $(wildcard cmd/$(plugin)/*.go) pyproject.toml Makefile
24-
go build -o $@ \
25-
-ldflags "-w -s" \
26-
./cmd/$(plugin)
24+
go install -ldflags "-w -s" ./cmd/$(plugin)
2725

2826
.PHONY: dev fmt lint upload clean build
2927

0 commit comments

Comments
 (0)