-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathharness-generate
123 lines (90 loc) · 4.79 KB
/
harness-generate
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
120
121
122
123
#!/usr/bin/env python
import argparse
from dataclasses import dataclass
import enum
import functools
import operator
import pathlib
import logging
from typing import Optional
from ch_cli_tools.openapi import LIB_NAME, ClientType, generate_clients, generate_models, generate_servers, get_dependencies
from ch_cli_tools.utils import confirm
def main():
args = get_command_line_arguments()
get_dependencies()
root_path = args.path.absolute()
# Check if applications folder exists, if not, go up until it's found
while not (root_path / 'applications').exists():
if root_path == root_path.parent:
logging.error('Could not find applications folder')
return
root_path = root_path.parent
should_generate = should_generate_interactive if args.is_interactive else lambda _: True
if args.generate_models:
generate_models(root_path, should_generate)
if args.generate_servers:
generate_servers(root_path, should_generate, args.app_name)
if args.generate_clients:
assert args.client_name is not None
generate_clients(root_path, should_generate, args.app_name, args.client_name, args.client_types)
class GenerationMode(enum.Flag):
CLIENTS = enum.auto()
MODELS = enum.auto()
SERVERS = enum.auto()
@classmethod
def all(cls):
return functools.reduce(operator.or_, cls)
@dataclass(frozen=True)
class CommandLineArguments:
path: pathlib.Path
app_name: Optional[str]
is_interactive: bool
generation_mode: GenerationMode
client_name: Optional[str] = None
client_types: ClientType = ClientType.all()
@property
def generate_models(self):
return GenerationMode.MODELS in self.generation_mode
@property
def generate_servers(self):
return GenerationMode.SERVERS in self.generation_mode
@property
def generate_clients(self):
return GenerationMode.CLIENTS in self.generation_mode
def get_command_line_arguments() -> CommandLineArguments:
parser = argparse.ArgumentParser(description='Walks filesystem inside ./applications to create and update application scaffolding based on API specifications.')
common_arguments = argparse.ArgumentParser(add_help=False)
common_arguments.add_argument('path', metavar='path', nargs='?', default=pathlib.Path.cwd(), type=pathlib.Path,
help='Base path of the application.')
common_arguments.add_argument('-i', '--interactive', dest='is_interactive', action="store_true",
help='Asks before generate')
common_arguments.add_argument('-a', '--app-name', dest='app_name', action="store", default=None,
help='Generate only for a specific application')
clients_arguments = argparse.ArgumentParser(add_help=False)
clients_arguments.add_argument('-cn', '--client-name', dest='client_name', action='store', default=LIB_NAME,
help='specify client prefix name')
client_type_group = clients_arguments.add_mutually_exclusive_group(required=False)
client_type_group.add_argument('-t', '--ts-only', dest='client_types', action='store_const', const=ClientType.TS_CLIENT,
help='Generate only typescript clients')
client_type_group.add_argument('-p', '--python-only', dest='client_types', action='store_const', const=ClientType.PYTHON_CLIENT,
help='Generate only python clients')
clients_arguments.set_defaults(client_types=ClientType.all())
subparsers = parser.add_subparsers(title='generation modes', required=True)
all_parser = subparsers.add_parser('all', parents=[common_arguments, clients_arguments],
help='Generate models, server stubs and client libraries')
all_parser.set_defaults(generation_mode=GenerationMode.all())
models_parser = subparsers.add_parser('models', parents=[common_arguments],
help='Generate only model library')
models_parser.set_defaults(generation_mode=GenerationMode.MODELS)
servers_parser = subparsers.add_parser('servers', parents=[common_arguments],
help='Generate only server stubs')
servers_parser.set_defaults(generation_mode=GenerationMode.SERVERS)
clients_parser = subparsers.add_parser('clients', parents=[common_arguments, clients_arguments],
help='Generate only client libraries')
clients_parser.set_defaults(generation_mode=GenerationMode.CLIENTS)
args = parser.parse_args()
return CommandLineArguments(**args.__dict__)
def should_generate_interactive(resource: str) -> bool:
return confirm(f'Do you want to generate {resource}?')
if __name__ == "__main__":
main()