-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmodule.py
More file actions
175 lines (125 loc) · 5.48 KB
/
module.py
File metadata and controls
175 lines (125 loc) · 5.48 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env python
import b
import os
import sys
import textwrap
def register(command):
# Module help
command.help = 'Manage NUClear modules in the codebase'
# Module subcommands
subcommands = command.add_subparsers(dest='module_command')
# Generate module subcommand
generate_command = subcommands.add_parser('generate', help='Generate a new NUClear module based on a template')
generate_command.add_argument('path', metavar='path', help='a path to the new module (from the module directory)')
def run(path, **kwargs):
# Try to get our actual module directory from the cmake cache
if 'NUCLEAR_MODULE_DIR' in b.cmake_cache:
module_path = os.path.join(b.source_dir, b.cmake_cache['NUCLEAR_MODULE_DIR'])
else:
sys.stderr.write('Warning: the system couldn\'t find the real module directory.')
sys.stderr.write('defaulting to module\n')
module_path = 'module'
# Calculate all of our file paths
path = os.path.join(module_path, path)
src_path = os.path.join(path, 'src')
tests_path = os.path.join(path, 'tests')
config_path = os.path.join(path, 'data', 'config')
module_name = os.path.split(path)[-1]
# Check if the path already exists
if os.path.exists(path):
sys.stderr.write('The path provided already exists.\n')
sys.stderr.write('Module generation aborted.\n')
sys.exit(1)
print('Module directory', module_path)
print('Creating directories')
# Create the required directories
os.makedirs(path)
print('\t', path)
os.makedirs(src_path)
print('\t', src_path)
os.makedirs(tests_path)
print('\t', tests_path)
os.makedirs(config_path)
print('\t', config_path)
# Split our provided path
parts = ['module'] + os.path.relpath(path, module_path).split(os.sep)
print('Generating files')
# Write all of our files
with open(os.path.join(path, 'CMakeLists.txt'), "w") as output:
output.write(generate_cmake(parts))
print('\t', os.path.join(path, 'CMakeLists.txt'))
with open(os.path.join(path, 'README.md'), "w") as output:
output.write(generate_readme(parts))
print('\t', os.path.join(src_path, 'README.md'))
with open(os.path.join(src_path, '{}.h'.format(module_name)), "w") as output:
output.write(generate_header(parts))
print('\t', os.path.join(src_path, '{}.h'.format(module_name)))
with open(os.path.join(src_path, '{}.cpp'.format(module_name)), "w") as output:
output.write(generate_cpp(parts))
print('\t', os.path.join(src_path, '{}.cpp'.format(module_name)))
with open(os.path.join(tests_path, '{}.cpp'.format(module_name)), "w") as output:
output.write(generate_test(parts))
print('\t', os.path.join(tests_path, '{}.cpp'.format(module_name)))
with open(os.path.join(config_path, '{}.yaml'.format(module_name)), 'a'):
print('\t', os.path.join(config_path, '{}.yaml'.format(module_name)))
def generate_cmake(parts):
return textwrap.dedent("""\
# Build our NUClear module
NUCLEAR_MODULE()
""")
def generate_header(parts):
template = textwrap.dedent("""\
#ifndef {define}
#define {define}
#include <nuclear>
{openNamespace}
class {className} : public NUClear::Reactor {{
public:
/// @brief Called by the powerplant to build and setup the {className} reactor.
explicit {className}(std::unique_ptr<NUClear::Environment> environment);
}};
{closeNamespace}
#endif // {define}
""")
return template.format(define='{}_H'.format('_'.join([p.upper() for p in parts]))
, className=parts[-1]
, openNamespace = '\n'.join(['namespace {} {{'.format(x) for x in parts[:-1]])
, closeNamespace = '\n'.join('}' * (len(parts) - 1)))
def generate_cpp(parts):
template = textwrap.dedent("""\
#include "{className}.h"
#include "extension/Configuration.h"
{openNamespace}
using extension::Configuration;
{className}::{className}(std::unique_ptr<NUClear::Environment> environment)
: Reactor(std::move(environment)) {{
on<Configuration>("{className}.yaml").then([this] (const Configuration& config) {{
// Use configuration here from file {className}.yaml
}});
}}
{closeNamespace}
""")
return template.format(className=parts[-1]
, openNamespace = '\n'.join(['namespace {} {{'.format(x) for x in parts[:-1]])
, closeNamespace = '\n'.join(['}' for x in parts[:-1]]))
def generate_readme(parts):
template = textwrap.dedent("""\
{className}
{classNameTitle}
## Description
## Usage
## Emits
## Dependencies
""")
return template.format(className=parts[-1]
, classNameTitle = len(parts[-1]) * '='
, closeNamespace = '\n'.join(['}' for x in parts[:-1]]))
def generate_test(parts):
template = textwrap.dedent("""\
// Uncomment this line when other test files are added
//#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
//#include <catch.hpp>
// Remove this line when test files are added
int main() {{ return 0; }}
""")
return template.format()