Skip to content
This repository was archived by the owner on Mar 31, 2025. It is now read-only.

Commit 7444115

Browse files
authored
Feat: codegen service (#2)
2 parents b9a8927 + e2b4287 commit 7444115

File tree

406 files changed

+488
-820
lines changed

Some content is hidden

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

406 files changed

+488
-820
lines changed

.devcontainer/Dockerfile

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ ENV PYTHONDONTWRITEBYTECODE=1 \
55
USER=compiler
66

77
RUN useradd --create-home --shell /bin/bash $USER && \
8+
mkdir /toggl_py && \
9+
mkdir -p /home/$USER/.local/bin && \
10+
chown -R $USER /toggl_py && \
811
chown -R $USER /home/$USER
912

1013
RUN apt update && \
@@ -14,13 +17,18 @@ RUN apt update && \
1417
apt install -y python3.11 python3-pip && \
1518
ln -s /usr/bin/python3.11 /usr/bin/python
1619

17-
RUN wget https://repo1.maven.org/maven2/io/swagger/swagger-codegen-cli/2.4.19/swagger-codegen-cli-2.4.19.jar -O /home/$USER/swagger-codegen-cli.jar
18-
1920
USER $USER
20-
ENV PATH "$PATH:/usr/bin:/home/$USER/.local/bin"
21+
22+
RUN wget https://repo1.maven.org/maven2/io/swagger/swagger-codegen-cli/2.4.19/swagger-codegen-cli-2.4.19.jar \
23+
-O /home/$USER/.local/bin/swagger-codegen-cli.jar
24+
25+
ENV PATH "/home/$USER/.local/bin:$PATH"
2126
WORKDIR /home/$USER/toggl-py
2227

2328
RUN python -m pip install --upgrade pip && \
2429
pip install black pre-commit
2530

31+
COPY LICENSE LICENSE
32+
COPY codegen/ codegen/
33+
2634
CMD ["sleep", "infinity"]

.devcontainer/devcontainer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"service": "dev",
66
"runServices": ["dev"],
77
"workspaceFolder": "/home/compiler/toggl-py",
8+
"postAttachCommand": ["/bin/bash", ".devcontainer/postAttach.sh"],
89
"customizations": {
910
"vscode": {
1011
// Set *default* container specific settings.json values on container create.

.devcontainer/postAttach.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env bash
2+
set -eux
3+
4+
pip install -e toggl_py/

.env.sample

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CODEGEN_DIR=codegen
2+
CODEGEN_JAR=/home/compiler/.local/bin/swagger-codegen-cli.jar
3+
CODEGEN_LANG=TogglPythonClientCodegen
4+
CODEGEN_TARGET=/toggl_py

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
*.egg-info
22
.classpath
3+
.env
34
.project
45
.settings/
56
target/

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
# `toggl-py`
22

3-
Python implementation of the Toggl API, autogenerated from their
3+
Python implementation of the Toggl API v9, autogenerated from their
44
[OpenAPI (Swagger) specification](https://engineering.toggl.com/docs/openapi).
5+
6+
## [`codegen`](./codegen/)
7+
8+
This directory contains a [custom Swagger codegen module](https://github.com/swagger-api/swagger-codegen?tab=readme-ov-file#making-your-own-codegen-modules) for generating the `toggl-py` package.
9+
10+
## [`toggl_py`](./toggl_py/)
11+
12+
This directory contains the generated Python library and docs.

codegen/README.md

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,63 @@
1-
# `toggl-py` codegen
1+
# toggl-py codegen
22

33
This folder contains configuration and scripts for generating the `toggl-py` source from
44
[Toggl's OpenApi (Swagger) spec](./spec.json).
55

6-
Code generation uses the Docker image wrapping [`swagger-codegen`](https://github.com/swagger-api/swagger-codegen.git).
6+
Code generation uses a custom Docker image wrapping the
7+
[`swagger-codegen-cli`](https://github.com/swagger-api/swagger-codegen.git).
8+
9+
## Setup
10+
11+
From the root of this repository
12+
13+
1. Create an environment file from the sample (defaults are good for now)
14+
15+
```bash
16+
cp .env.sample .env
17+
```
18+
19+
2. Then build the main Docker image:
20+
21+
```bash
22+
docker compose build
23+
```
24+
25+
## Open a devcontainer
26+
27+
Open this repository in VS Code, then `Rebuild and Reopen in Container`.
728

829
## Generate the code
930

10-
From the root of this repository, run the generate script:
31+
(Outside a devcontainer), run the Docker Compose `codegen` service:
1132

1233
```bash
13-
codegen/bin/run.sh
34+
docker compose run codegen
1435
```
1536

16-
_Requires Docker_
37+
(Inside the devcontainer), run the codegen script directly:
38+
39+
```bash
40+
codegen/bin/run.sh
41+
```
1742

1843
## Edit configuration
1944

2045
Some configuration values can be changed in [`config.json`](./config.json).
2146

2247
Other flags for `swagger-codegen` can be modified in the [`run.sh`](./bin/run.sh) script.
2348

24-
Use the [`help.sh`](./bin/help.sh) script to find out more about `swagger-codegen` configuration options:
49+
Use the [`help.sh`](./bin/help.sh) script to find out more about `swagger-codegen-cli` configuration options:
2550

2651
```bash
2752
codegen/bin/help.sh
2853
```
2954

30-
_Requires Docker_
31-
3255
## Edit templates
3356

34-
The [`templates`](./templates/) directory contains [Mustache](https://mustache.github.io/) templates for the generated files.
57+
Custom [Mustache](https://mustache.github.io/) templates used to generate the files live under [`./TogglPythonClientCodegen/src/main/resources/TogglPythonClientCodegen/`](./TogglPythonClientCodegen/src/main/resources/TogglPythonClientCodegen/).
58+
59+
## Edit the generator class
60+
61+
[`TogglPythonClientCodegen`](./TogglPythonClientCodegen/src/main/java/la/compiler/codegen/TogglPythonClientCodegen.java) is the
62+
custom Java class, inheriting from and overriding some behavior of Swagger's [`PythonClientCodegen`](https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PythonClientCodegen.java),
63+
serving as the "language" implementation that `swagger-codegen-cli` uses to generate `toggl-py`.

codegen/TogglPythonClientCodegen/src/main/resources/TogglPythonClientCodegen/partial_header.mustache

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,5 @@ This file is auto generated by the swagger code generator program.
77
Do not edit this file manually.
88

99
{{#version}}OpenAPI spec version: {{{version}}}{{/version}}
10-
Last changed: {{generatedDate}}
11-
Generated by: https://github.com/compilerla/toggl-py/tree/main/codegen
10+
Generated by: https://github.com/compilerla/toggl-py
1211
"""

codegen/bin/help.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env bash
22
set -eux
33

4-
java -cp ~/swagger-codegen-cli.jar:codegen/TogglPythonClientCodegen/target/* io.swagger.codegen.SwaggerCodegen help generate
5-
java -cp ~/swagger-codegen-cli.jar:codegen/TogglPythonClientCodegen/target/* io.swagger.codegen.SwaggerCodegen config-help -l TogglPythonClientCodegen
4+
java -cp $CODEGEN_JAR:$CODEGEN_DIR/$CODEGEN_LANG/target/* io.swagger.codegen.SwaggerCodegen help generate
5+
java -cp $CODEGEN_JAR:$CODEGEN_DIR/$CODEGEN_LANG/target/* io.swagger.codegen.SwaggerCodegen config-help -l $CODEGEN_LANG

codegen/bin/meta.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env bash
22
set -eux
33

4-
docker run --rm -u 1000:1000 -v ${PWD}:/dist -w /dist swaggerapi/swagger-codegen-cli meta \
5-
--name TogglPythonClientCodegen \
6-
--output codegen/TogglPythonClientCodegen \
4+
java -jar $CODEGEN_JAR meta \
5+
--name $CODEGEN_LANG \
6+
--output $CODEGEN_DIR/$CODEGEN_LANG \
77
--package la.compiler.codegen

0 commit comments

Comments
 (0)