-
Notifications
You must be signed in to change notification settings - Fork 236
/
generate-code.py
119 lines (93 loc) · 3.57 KB
/
generate-code.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import os
import subprocess
import sys
def run_command(command):
print(command)
env = os.environ.copy()
env["GO_POST_PROCESS_FILE"] = "./postprocess-file.sh"
proc = subprocess.run(command, shell=True, text=True, capture_output=True, env=env)
if len(proc.stdout) != 0:
print("\n\nSTDOUT:\n\n")
print(proc.stdout)
if len(proc.stderr) != 0:
print("\n\nSTDERR:\n\n")
print(proc.stderr)
print("\n\n")
if proc.returncode != 0:
print(f"\n\nCommand '{command}' returned non-zero exit status {proc.returncode}.")
sys.exit(1)
return proc.stdout.strip()
def read_files_from_openapi_generator(directory):
file_path = os.path.join(directory, ".openapi-generator", "FILES")
if not os.path.exists(file_path):
print(f"Error: File {file_path} does not exist.")
return []
try:
with open(file_path, 'r') as f:
content = f.read()
except Exception as e:
print(f"Error reading file: {e}")
return []
file_list = content.strip().split("\n")
return file_list
def generate_clients():
components = [
"shop.yml",
"channel-access-token.yml",
"insight.yml",
"liff.yml",
"manage-audience.yml",
"module-attach.yml",
"module.yml",
"messaging-api.yml",
]
for sourceYaml in components:
output_path = 'linebot/' + sourceYaml.replace('.yml', '').replace('-', '_')
orig_files = read_files_from_openapi_generator(output_path)
for f in set(orig_files):
run_command(f'rm -rf {f}')
command = f'''java \\
-cp ./tools/openapi-generator-cli.jar:./generator/target/line-bot-sdk-go-generator-openapi-generator-1.0.0.jar \\
org.openapitools.codegen.OpenAPIGenerator \\
generate \\
-g line-bot-sdk-go-generator \\
-e pebble \\
--model-package model \\
--api-package api \\
--package-name {sourceYaml.replace('.yml', '').replace('-', '_')} \\
--enable-post-process-file \\
-o {output_path.replace("-", "_")} \\
-i line-openapi/{sourceYaml} \\
'''
run_command(command)
def generate_webhook():
source_yaml = "webhook.yml"
output_path = 'linebot/webhook'
orig_files = read_files_from_openapi_generator(output_path)
for f in set(orig_files):
run_command(f'rm -rf {f}')
command = f'''java \\
-cp ./tools/openapi-generator-cli.jar:./generator/target/line-bot-sdk-go-generator-openapi-generator-1.0.0.jar \\
org.openapitools.codegen.OpenAPIGenerator \\
generate \\
--global-property apiTest=false,modelDocs=false,apiDocs=false \\
--template-dir ./generator/src/main/resources \\
--enable-post-process-file \\
-e pebble \\
--model-package model \\
--api-package api \\
--package-name webhook \\
-g line-bot-sdk-go-generator \\
-o {output_path} \\
-i line-openapi/{source_yaml} \\
'''
run_command(command)
def main():
os.chdir(os.path.dirname(os.path.abspath(__file__)))
os.chdir("generator")
run_command('mvn package -DskipTests=true')
os.chdir("..")
generate_clients()
generate_webhook()
if __name__ == "__main__":
main()