Changeset View
Changeset View
Standalone View
Standalone View
data/generators/generate-cmake-syntax.py
Show All 27 Lines | 26 | _PROPERTY_KEYS = [ | |||
---|---|---|---|---|---|
28 | , 'directory-properties' | 28 | , 'directory-properties' | ||
29 | , 'target-properties' | 29 | , 'target-properties' | ||
30 | , 'source-properties' | 30 | , 'source-properties' | ||
31 | , 'test-properties' | 31 | , 'test-properties' | ||
32 | , 'cache-properties' | 32 | , 'cache-properties' | ||
33 | , 'install-properties' | 33 | , 'install-properties' | ||
34 | ] | 34 | ] | ||
35 | _KW_RE_LIST = ['kw', 're'] | 35 | _KW_RE_LIST = ['kw', 're'] | ||
36 | _VAR_KIND_LIST = ['variables', 'environment-variables'] | ||||
36 | 37 | | |||
37 | 38 | | |||
38 | def try_transform_placeholder_string_to_regex(name): | 39 | def try_transform_placeholder_string_to_regex(name): | ||
39 | ''' | 40 | ''' | ||
40 | NOTE Some placeholders are not IDs, but numbers... | 41 | NOTE Some placeholders are not IDs, but numbers... | ||
41 | `CMAKE_MATCH_<N>` 4 example | 42 | `CMAKE_MATCH_<N>` 4 example | ||
42 | ''' | 43 | ''' | ||
43 | m = _TEMPLATED_NAME.split(name) | 44 | m = _TEMPLATED_NAME.split(name) | ||
44 | if 'CMAKE_MATCH_' in m: | 45 | if 'CMAKE_MATCH_' in m: | ||
45 | return '\\bCMAKE_MATCH_[0-9]+\\b' | 46 | return '\\bCMAKE_MATCH_[0-9]+\\b' | ||
46 | 47 | | |||
48 | if 'CMAKE_ARGV' in m: | ||||
49 | return '\\bCMAKE_ARGV[0-9]+\\b' | ||||
50 | | ||||
47 | return '\\b{}\\b'.format('&id_re;'.join(list(m))) if 1 < len(m) else name | 51 | return '\\b{}\\b'.format('&id_re;'.join(list(m))) if 1 < len(m) else name | ||
48 | 52 | | |||
49 | 53 | | |||
50 | def partition_iterable(fn, iterable): | 54 | def partition_iterable(fn, iterable): | ||
51 | true, false = [], [] | 55 | true, false = [], [] | ||
52 | for i in iterable: | 56 | for i in iterable: | ||
53 | (false, true)[int(fn(i))].append(i) | 57 | (false, true)[int(fn(i))].append(i) | ||
54 | return true, false | 58 | return true, false | ||
▲ Show 20 Lines • Show All 49 Lines • ▼ Show 20 Line(s) | |||||
104 | 108 | | |||
105 | 109 | | |||
106 | @click.command() | 110 | @click.command() | ||
107 | @click.argument('input_yaml', type=click.File('r')) | 111 | @click.argument('input_yaml', type=click.File('r')) | ||
108 | @click.argument('template', type=click.File('r'), default='./cmake.xml.tpl') | 112 | @click.argument('template', type=click.File('r'), default='./cmake.xml.tpl') | ||
109 | def cli(input_yaml, template): | 113 | def cli(input_yaml, template): | ||
110 | data = yaml.load(input_yaml) | 114 | data = yaml.load(input_yaml) | ||
111 | 115 | | |||
112 | # Partition `variables` list into "pure" words and regexes to match | 116 | # Partition `variables` and `environment-variables` lists into "pure" (key)words and regexes to match | ||
113 | data['variables'] = { | 117 | for var_key in _VAR_KIND_LIST: | ||
114 | k: sorted(set(v)) for k, v in zip(_KW_RE_LIST, [*partition_iterable(lambda x: _TEMPLATED_NAME.search(x) is None, data['variables'])]) | 118 | data[var_key] = { | ||
115 | } | 119 | k: sorted(set(v)) for k, v in zip( | ||
116 | data['variables']['re'] = [*map(lambda x: try_transform_placeholder_string_to_regex(x), data['variables']['re'])] | 120 | _KW_RE_LIST | ||
117 | 121 | , [*partition_iterable(lambda x: _TEMPLATED_NAME.search(x) is None, data[var_key])] | |||
118 | # Partition `environment-variables` list into "pure" words and regexes to match | 122 | ) | ||
119 | data['environment-variables'] = { | 123 | } | ||
120 | k: sorted(set(v)) for k, v in zip(_KW_RE_LIST, [*partition_iterable(lambda x: _TEMPLATED_NAME.search(x) is None, data['environment-variables'])]) | 124 | data[var_key]['re'] = [ | ||
121 | } | 125 | *map( | ||
122 | data['environment-variables']['re'] = [*map(lambda x: try_transform_placeholder_string_to_regex(x), data['environment-variables']['re'])] | 126 | lambda x: try_transform_placeholder_string_to_regex(x) | ||
123 | 127 | , data[var_key]['re'] | |||
128 | ) | ||||
129 | ] | ||||
124 | 130 | | |||
125 | # Transform properties and make all-properties list | 131 | # Transform properties and make all-properties list | ||
126 | data['properties'] = {} | 132 | data['properties'] = {} | ||
127 | for prop in _PROPERTY_KEYS: | 133 | for prop in _PROPERTY_KEYS: | ||
128 | python_prop_list_name = prop.replace('-', '_') | 134 | python_prop_list_name = prop.replace('-', '_') | ||
129 | props, props_re = partition_iterable(lambda x: _TEMPLATED_NAME.search(x) is None, data[prop]) | 135 | props, props_re = partition_iterable(lambda x: _TEMPLATED_NAME.search(x) is None, data[prop]) | ||
130 | del data[prop] | 136 | del data[prop] | ||
131 | 137 | | |||
132 | data['properties'][python_prop_list_name] = {k: sorted(set(v)) for k, v in zip(_KW_RE_LIST, [props, props_re])} | 138 | data['properties'][python_prop_list_name] = { | ||
133 | data['properties'][python_prop_list_name]['re'] = [*map(lambda x: try_transform_placeholder_string_to_regex(x), props_re)] | 139 | k: sorted(set(v)) for k, v in zip(_KW_RE_LIST, [props, props_re]) | ||
134 | 140 | } | |||
141 | data['properties'][python_prop_list_name]['re'] = [ | ||||
142 | *map(lambda x: try_transform_placeholder_string_to_regex(x), props_re) | ||||
143 | ] | ||||
135 | 144 | | |||
136 | data['properties']['kinds'] = [*map(lambda name: name.replace('-', '_'), _PROPERTY_KEYS)] | 145 | data['properties']['kinds'] = [*map(lambda name: name.replace('-', '_'), _PROPERTY_KEYS)] | ||
137 | 146 | | |||
138 | # Make all commands list | 147 | # Make all commands list | ||
139 | data['commands'] = [*map(lambda cmd: transform_command(cmd), data['scripting-commands'] + data['project-commands'] + data['ctest-commands'])] | 148 | data['commands'] = [ | ||
149 | *map( | ||||
150 | lambda cmd: transform_command(cmd) | ||||
151 | , data['scripting-commands'] + data['project-commands'] + data['ctest-commands']) | ||||
152 | ] | ||||
140 | 153 | | |||
141 | # Fix node names to be accessible from Jinja template | 154 | # Fix node names to be accessible from Jinja template | ||
142 | data['generator_expressions'] = data['generator-expressions'] | 155 | data['generator_expressions'] = data['generator-expressions'] | ||
143 | data['environment_variables'] = data['environment-variables'] | 156 | data['environment_variables'] = data['environment-variables'] | ||
144 | del data['generator-expressions'] | 157 | del data['generator-expressions'] | ||
145 | del data['environment-variables'] | 158 | del data['environment-variables'] | ||
146 | 159 | | |||
147 | env = jinja2.Environment( | 160 | env = jinja2.Environment( | ||
Show All 14 Lines |