-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathupdate_launch_json.py
154 lines (130 loc) · 5.05 KB
/
update_launch_json.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import json
import argparse
import pathlib
import sys
# GDB config for Microsoft C/C++ plugin
class GdbConfig:
def __init__(self, omnetpp, target, rundbg, args, cwd, debugger, setup_commands):
self.model = {
"name": "Launch {} - GDB (OMNeT++)".format(target),
"type": "cppdbg",
"request": "launch",
"program": rundbg,
"args": self.escapeArgs(args),
"stopAtEntry": False,
"cwd": cwd,
"externalConsole": False,
"MIMode": "gdb",
"miDebuggerPath": debugger,
"setupCommands": setup_commands
}
@property
def name(self):
return self.model['name']
def escapeArgs(self, args):
escaped_args = []
escape_next = False
for arg in args:
if escape_next:
escaped_args.append(arg.replace(';', '\\;'))
escape_next = False
else:
escaped_args.append(arg)
escape_next = (arg == '-n')
return escaped_args
# Config for CodeLLDB extension
class CodeLldbConfig:
def __init__(self, omnetpp, target, rundbg, args, cwd):
self.model = {
"name": "Launch {} - CodeLLDB (OMNeT++)".format(target),
"type": "lldb",
"request": "launch",
"program": rundbg,
"args": args,
"stopOnEntry": False,
"cwd": cwd,
"initCommands": []
}
formatter = pathlib.Path(
omnetpp, 'python/omnetpp/lldb/formatters/omnetpp.py')
if formatter.is_file():
cmd = "command script import {}".format(formatter.absolute())
self.model['initCommands'].append(cmd)
@property
def name(self):
return self.model['name']
def main():
parser = argparse.ArgumentParser(
description='Add OMNeT++ debug configuration to VSCode.')
parser.add_argument(
'--debug-config', type=str,
help='Config to export. Valid values are GDB and CodeLLDB.')
parser.add_argument(
'--omnetpp-root', type=str, help="OMNeT++ root folder.")
parser.add_argument(
'--gdb-command', type=str, help="GDB executable.", default="gdb")
parser.add_argument(
'--setup-commands', type=str, required=False,
help="JSON file with custom setup commands.")
parser.add_argument(
'launch_json', type=str,
help="Location of launch.json to be updated.")
args = parser.parse_args()
# Path to launch.json file
launch_file = pathlib.Path(args.launch_json)
# Load additional commands for the debugger if file is given and exists
setup_commands = []
if 'setup_commands' in args:
setup_commands_file = pathlib.Path(args.setup_commands)
if setup_commands_file.is_file():
try:
with setup_commands_file.open() as f:
setup_commands = json.load(f)
except ValueError as e:
print('Invalid setup commands in file: {}'.format(e))
configs = []
for target_config in pathlib.Path.cwd().glob('vscode-debug/*.json'):
with target_config.open("r") as f:
target = json.load(f)
target_name = target_config.stem
config = None
if args.debug_config == "GDB":
config = GdbConfig(
args.omnetpp_root, target_name, target['exec'],
target['args'], target['working_directory'],
args.gdb_command, setup_commands)
elif args.debug_config == "CodeLLDB":
config = CodeLldbConfig(
args.omnetpp_root, target_name, target['exec'],
target['args'], target['working_directory'])
if config:
configs.append(config)
launch_settings = {}
if launch_file.is_file():
with launch_file.open("r") as f:
try:
launch_settings = json.load(f)
except ValueError:
print("launch.json has invalid content, aborting.")
sys.exit(1)
# create empty configurations list if missing
if 'configurations' not in launch_settings:
launch_settings['configurations'] = []
# keep non-generated configurations
config_names = [config.name for config in configs]
launch_configs = []
for config in launch_settings['configurations']:
if not config['name'] in config_names:
launch_configs.append(config)
# append generated configurations
for config in configs:
launch_configs.append(config.model)
launch_settings['configurations'] = launch_configs
with launch_file.open("w") as f:
try:
json.dump(launch_settings, f, indent=4)
except ValueError as e:
print("Aborting, updated launch.json would be invalid: ", e)
sys.exit(1)
if __name__ == "__main__":
main()