@@ -32,9 +32,9 @@ def __init__(self, filename: Path, folder: Path) -> None:
32
32
33
33
def parse_expands (self ) -> int :
34
34
"""
35
- Parse the expands() blocks in the VDB file.
35
+ Parse an expands() blocks in a VDB file.
36
36
37
- Updates the class attribute substitutions with the macro substitutions parsed.
37
+ Updates the class attribute ' substitutions' with the macro substitutions parsed.
38
38
Updates the class attribute text with the VDB file text with the expands()
39
39
blocks processed into MSI substitute/include statements.
40
40
@@ -50,6 +50,7 @@ def parse_expands(self) -> int:
50
50
include_path = self .folder / match [0 ]
51
51
macros = Macros (self .template_path , include_path , match [2 ])
52
52
self .includes .append (macros )
53
+ self ._normalise_macros (macros )
53
54
54
55
# replace the expands() block with the MSI directives
55
56
self .text = EXPAND .sub (macros .render_include (), self .text , 1 )
@@ -61,9 +62,32 @@ def parse_expands(self) -> int:
61
62
62
63
return len (expands )
63
64
65
+ def _normalise_macros (self , macros : Macros ):
66
+ """
67
+ Given a set of macros for a given expand block, search for all other
68
+ instances of an expand against the same template file.
69
+
70
+ Make sure that this set of macros is consistent with all other instances
71
+ by adding in a self referencing macro for any missing ones out of the list
72
+ of all macros passed by all instances of such an expansion. (OMLGG!)
73
+ """
74
+ vdb_list = self .folder .glob ("*.vdb" )
75
+ for vdb in vdb_list :
76
+ vdb_text = vdb .read_text ()
77
+ expands = EXPAND .findall (vdb_text )
78
+ for match in expands :
79
+ # match: 0=include path, 1=name, 2=macro text
80
+ if match [0 ] == macros .vdb_path .name :
81
+ other_macros = Macros (self .template_path , macros .vdb_path , match [2 ])
82
+ for macro in other_macros .macros :
83
+ if macro not in macros .macros :
84
+ print (f"adding missing { macro } to { macros .parent .name } " )
85
+ macros .macros [macro ] = f"$({ macro } )"
86
+
64
87
def process_includes (self ):
65
88
"""
66
- Process the included files for this VDB file.
89
+ Process the included files for this VDB file. Returns a generator of
90
+ tuples of the file and the text to write to the file.
67
91
"""
68
92
for include in self .includes :
69
93
if include .vdb_path .name not in Expansion .processed :
@@ -77,6 +101,9 @@ def validate_includes(cls) -> bool:
77
101
every time they are included. If not then the the replacing of macro
78
102
names with _ prefix will be inconsistent between uses of the included
79
103
templates and this approach will fail.
104
+
105
+ NOTE: with the introduction of _normalise_macros() this should not be
106
+ necessary but it is left in for now as a backup check.
80
107
"""
81
108
warning = False
82
109
index : Dict [str , Macros ] = {}
0 commit comments