Skip to content

Commit fe2262b

Browse files
committed
NO-ISSUE: migrate code generation from Mustache to Pebble templates
1 parent 7d796fb commit fe2262b

26 files changed

Lines changed: 3076 additions & 859 deletions

.gitignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ vendor/
88
phpDocumentor.phar
99
.phpunit.result.cache
1010

11-
## OpenAPI Generator
12-
generator/target/
13-
1411
## Generated by OpenAPI
1512
src/clients/channel-access-token/*
1613
!src/clients/channel-access-token/lib/

CONTRIBUTING.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ The project structure is as follows:
2121
- `src`: The main library code.
2222
- `test`: Test code.
2323
- `examples`: Example projects that use the library.
24-
- `tools`: Development tools including code generation scripts.
25-
- `generator`: Helper project used to fetch the OpenAPI Generator CLI for local code generation.
24+
- `tools`: Development tools including copyright checking scripts.
25+
- `generator`: Custom OpenAPI Generator codegen with Pebble templates for code generation.
2626
- `docs`: Auto-generated [Documentation](https://line.github.io/line-bot-sdk-php/) files by phpDocumentor.
2727

2828
### Edit OpenAPI templates
2929

3030
Almost all code is generated with OpenAPI Generator based on [line-openapi](https://github.com/line/line-openapi)'s YAML files.
3131
Thus, you cannot edit almost all code under `src/clients/` and `src/webhook/` directly.
3232

33-
You need to edit the custom templates under [tools/custom-template](tools/custom-template) instead.
33+
You need to edit the Pebble templates under [generator/src/main/resources/line-bot-sdk-php-generator](generator/src/main/resources/line-bot-sdk-php-generator) instead.
3434

3535
After editing the templates, run `python generate-code.py` to generate the code, and then commit all affected files.
3636
If not, CI status will fail.

generate-code.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import subprocess
33
import sys
44

5-
TEMPLATE_DIR = "tools/custom-template"
65
COMPONENTS = [
76
{"sourceYaml": "channel-access-token.yml", "invokerPackage": "LINE\\Clients\\ChannelAccessToken"},
87
{"sourceYaml": "insight.yml", "invokerPackage": "LINE\\Clients\\Insight"},
@@ -11,6 +10,11 @@
1110
{"sourceYaml": "liff.yml", "invokerPackage": "LINE\\Clients\\Liff"},
1211
]
1312

13+
# Pin the copyright year so generated code is byte-for-byte reproducible across
14+
# runs. Bumping this is an explicit choice, not a side effect of running the
15+
# generator on a new year.
16+
COPYRIGHT_YEAR = "2026"
17+
1418

1519
def run_command(command):
1620
print(command)
@@ -36,51 +40,53 @@ def generate_clients(jar_path):
3640

3741
run_command(f"rm -rf {output_path}")
3842
run_command(f"mkdir {output_path}")
39-
run_command(f"cp tools/.openapi-generator-ignore {output_path}/")
4043

41-
command = f"""java -jar {jar_path} generate \
44+
command = f"""java \
45+
-cp {jar_path} \
46+
org.openapitools.codegen.OpenAPIGenerator generate \
4247
-i line-openapi/{source_yaml} \
43-
-g php \
48+
-e pebble \
49+
-g line-bot-sdk-php-generator \
4450
-o {output_path} \
45-
--template-dir {TEMPLATE_DIR} \
4651
--http-user-agent LINE-BotSDK-PHP/11 \
4752
--additional-properties="invokerPackage={component['invokerPackage']}" \
48-
--additional-properties="variableNamingConvention=camelCase"
53+
--additional-properties="variableNamingConvention=camelCase" \
54+
--additional-properties="copyrightYear={COPYRIGHT_YEAR}"
4955
"""
5056
run_command(command)
5157

5258

5359
def generate_webhook(jar_path):
5460
output_path = "src/webhook"
5561

56-
run_command(f"rm -rf {output_path}")
62+
run_command(f"rm -rf {output_path}/lib")
63+
run_command(f"rm -rf {output_path}/.openapi-generator")
64+
run_command(f"rm -f {output_path}/.openapi-generator-ignore")
5765

58-
command = f"""java -jar {jar_path} generate \
66+
command = f"""java \
67+
-cp {jar_path} \
68+
org.openapitools.codegen.OpenAPIGenerator generate \
5969
-i line-openapi/webhook.yml \
60-
-g php \
70+
-e pebble \
71+
-g line-bot-sdk-php-generator \
6172
-o {output_path} \
62-
--template-dir {TEMPLATE_DIR} \
6373
--additional-properties="invokerPackage=LINE\\Webhook" \
64-
--additional-properties="variableNamingConvention=camelCase"
74+
--additional-properties="variableNamingConvention=camelCase" \
75+
--additional-properties="copyrightYear={COPYRIGHT_YEAR}"
6576
"""
6677
run_command(command)
6778

6879

69-
def post_process():
70-
run_command("php tools/patch-gen-oas-client.php")
71-
72-
7380
def main():
7481
os.chdir(os.path.dirname(os.path.abspath(__file__)))
7582

7683
os.chdir("generator")
7784
run_command('mvn package -DskipTests=true')
7885
os.chdir("..")
7986

80-
jar_path = "generator/target/openapi-generator-cli.jar"
87+
jar_path = "generator/target/line-bot-sdk-php-generator-1.0.0.jar"
8188
generate_clients(jar_path)
8289
generate_webhook(jar_path)
83-
post_process()
8490

8591

8692
if __name__ == "__main__":

generator/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
target/
2+
/out/
3+
/dependency-reduced-pom.xml

generator/pom.xml

Lines changed: 148 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,174 @@
11
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
22
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
33
<modelVersion>4.0.0</modelVersion>
4-
<groupId>com.linecorp.bot</groupId>
4+
<groupId>org.openapitools</groupId>
55
<artifactId>line-bot-sdk-php-generator</artifactId>
6-
<packaging>pom</packaging>
6+
<packaging>jar</packaging>
77
<name>line-bot-sdk-php-generator</name>
88
<version>1.0.0</version>
99
<build>
1010
<plugins>
1111
<plugin>
1212
<groupId>org.apache.maven.plugins</groupId>
13-
<artifactId>maven-dependency-plugin</artifactId>
14-
<version>3.11.0</version>
13+
<artifactId>maven-enforcer-plugin</artifactId>
14+
<version>3.6.3</version>
15+
<executions>
16+
<execution>
17+
<id>enforce-maven</id>
18+
<goals>
19+
<goal>enforce</goal>
20+
</goals>
21+
<configuration>
22+
<rules>
23+
<requireMavenVersion>
24+
<version>2.2.0</version>
25+
</requireMavenVersion>
26+
</rules>
27+
</configuration>
28+
</execution>
29+
</executions>
30+
</plugin>
31+
<plugin>
32+
<groupId>org.apache.maven.plugins</groupId>
33+
<artifactId>maven-surefire-plugin</artifactId>
34+
<version>3.5.6</version>
35+
<configuration>
36+
<systemProperties>
37+
<property>
38+
<name>loggerPath</name>
39+
<value>conf/log4j.properties</value>
40+
</property>
41+
</systemProperties>
42+
<argLine>-Xms512m -Xmx1500m</argLine>
43+
<parallel>methods</parallel>
44+
<forkMode>pertest</forkMode>
45+
</configuration>
46+
</plugin>
47+
48+
<plugin>
49+
<groupId>org.apache.maven.plugins</groupId>
50+
<artifactId>maven-jar-plugin</artifactId>
51+
<version>3.5.0</version>
52+
<executions>
53+
<execution>
54+
<id>default-jar</id>
55+
<goals>
56+
<goal>jar</goal>
57+
</goals>
58+
</execution>
59+
</executions>
60+
<configuration>
61+
</configuration>
62+
</plugin>
63+
64+
<plugin>
65+
<groupId>org.codehaus.mojo</groupId>
66+
<artifactId>build-helper-maven-plugin</artifactId>
67+
<version>3.6.1</version>
68+
<executions>
69+
<execution>
70+
<id>add_sources</id>
71+
<phase>generate-sources</phase>
72+
<goals>
73+
<goal>add-source</goal>
74+
</goals>
75+
<configuration>
76+
<sources>
77+
<source>
78+
src/main/java
79+
</source>
80+
</sources>
81+
</configuration>
82+
</execution>
83+
<execution>
84+
<id>add_test_sources</id>
85+
<phase>generate-test-sources</phase>
86+
<goals>
87+
<goal>add-test-source</goal>
88+
</goals>
89+
<configuration>
90+
<sources>
91+
<source>
92+
src/test/java
93+
</source>
94+
</sources>
95+
</configuration>
96+
</execution>
97+
</executions>
98+
</plugin>
99+
<plugin>
100+
<groupId>org.apache.maven.plugins</groupId>
101+
<artifactId>maven-compiler-plugin</artifactId>
102+
<version>3.15.0</version>
103+
<configuration>
104+
<source>1.8</source>
105+
<target>1.8</target>
106+
</configuration>
107+
</plugin>
108+
<plugin>
109+
<groupId>org.apache.maven.plugins</groupId>
110+
<artifactId>maven-shade-plugin</artifactId>
111+
<version>3.6.2</version>
15112
<executions>
16113
<execution>
17-
<id>copy-openapi-generator-cli</id>
18114
<phase>package</phase>
19115
<goals>
20-
<goal>copy</goal>
116+
<goal>shade</goal>
21117
</goals>
22118
<configuration>
23-
<artifactItems>
24-
<artifactItem>
25-
<groupId>org.openapitools</groupId>
26-
<artifactId>openapi-generator-cli</artifactId>
27-
<version>${openapi-generator-version}</version>
28-
<type>jar</type>
29-
<destFileName>openapi-generator-cli.jar</destFileName>
30-
<outputDirectory>${project.build.directory}</outputDirectory>
31-
</artifactItem>
32-
</artifactItems>
119+
<createDependencyReducedPom>false</createDependencyReducedPom>
120+
<transformers>
121+
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
122+
</transformers>
123+
<filters>
124+
<filter>
125+
<artifact>*:*</artifact>
126+
<excludes>
127+
<exclude>META-INF/*.SF</exclude>
128+
<exclude>META-INF/*.DSA</exclude>
129+
<exclude>META-INF/*.RSA</exclude>
130+
</excludes>
131+
</filter>
132+
</filters>
33133
</configuration>
34134
</execution>
35135
</executions>
36136
</plugin>
37137
</plugins>
38138
</build>
139+
<dependencies>
140+
<dependency>
141+
<groupId>org.openapitools</groupId>
142+
<artifactId>openapi-generator</artifactId>
143+
<version>${openapi-generator-version}</version>
144+
<scope>provided</scope>
145+
</dependency>
146+
<dependency>
147+
<groupId>org.openapitools</groupId>
148+
<artifactId>openapi-generator-cli</artifactId>
149+
<version>${openapi-generator-version}</version>
150+
</dependency>
151+
<dependency>
152+
<groupId>io.pebbletemplates</groupId>
153+
<artifactId>pebble</artifactId>
154+
<version>4.1.2</version>
155+
</dependency>
156+
<dependency>
157+
<groupId>org.junit.jupiter</groupId>
158+
<artifactId>junit-jupiter-api</artifactId>
159+
<version>6.1.0</version>
160+
<scope>test</scope>
161+
</dependency>
162+
<dependency>
163+
<groupId>org.junit.jupiter</groupId>
164+
<artifactId>junit-jupiter-engine</artifactId>
165+
<version>6.1.0</version>
166+
<scope>test</scope>
167+
</dependency>
168+
</dependencies>
39169
<properties>
40170
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
41-
<openapi-generator-version>7.11.0</openapi-generator-version>
171+
<openapi-generator-version>7.23.0</openapi-generator-version>
172+
<maven-plugin-version>1.0.0</maven-plugin-version>
42173
</properties>
43174
</project>

0 commit comments

Comments
 (0)