Skip to content

Commit 3310e7b

Browse files
authored
Support for auto-merging build configs to base class (#20761)
* merge base wip * mergebase implementation * fix .tmp exists error due to double-deleting _buildconfig override files * // 1. keep version of config being merged (only bump version if needed!) // 2. we'll assume that any configs needed for the 'merged' buildconfig are enabled // this means, we won't bump version even if the build config version is 'lower' than the base version * add bugs /todos * update todo * another todo * Remove unneeded #ifelse to simplify merging baseconfigs Only erase preprocessor instructions being merged.. . if X is to be collapsed, collapse all #if X ... #else ... #endif into content of #if block (erasing #else). but retain #if Y ... #else ... #endif Fix to retain default buildconfig when other configs present * disable merges (initially) verify scenerio will enforce merge for config (note) * cleanup * ensure we don't overwrite base version with a "lower" version number when merging config * bump * test
1 parent 93a10ac commit 3310e7b

File tree

3 files changed

+224
-89
lines changed

3 files changed

+224
-89
lines changed

BuildConfigGen/EnsureUpdateModeVerifier.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,30 @@ private string ResolveFile(string filePath)
351351
return targetFile;
352352
}
353353

354+
internal void DeleteFile(string targetFile, bool addVerifyErrorIfExists, out bool removed)
355+
{
356+
removed = false;
357+
bool verify = UseVerifyOnlyForFile(targetFile);
358+
359+
if (verify)
360+
{
361+
removed = true;
362+
363+
if (File.Exists(targetFile) && addVerifyErrorIfExists)
364+
{
365+
VerifyErrors.Add($"Expected file {targetFile} to not exist");
366+
}
367+
}
368+
else
369+
{
370+
if (File.Exists(targetFile))
371+
{
372+
removed = true;
373+
File.Delete(targetFile);
374+
}
375+
}
376+
}
377+
354378
internal void DeleteDirectoryRecursive(string path)
355379
{
356380
bool verify = UseVerifyOnlyForPath(path);

BuildConfigGen/Preprocessor.cs

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@ internal partial class Preprocessor
1717
private static partial Regex elseAndEndIfPreprocess();
1818

1919

20-
internal static void Preprocess(string file, IEnumerable<string> lines, ISet<string> validConfigPreprocessorVariableNames, string configName, out string processedOutput, out List<string> validationErrors, out bool madeChanges)
20+
internal static void Preprocess(string file, IEnumerable<string> lines, ISet<string> validConfigPreprocessorVariableNames, string configName, bool retainOtherPreprocessingInstructions, out string processedOutput, out List<string> validationErrors, out bool madeChanges)
2121
{
2222
const string ifCommand = "if";
23-
const string elseIfCommand = "elseif";
2423
const string endIfCommand = "endif";
2524
const string elseCommand = "else";
2625

@@ -32,6 +31,7 @@ internal static void Preprocess(string file, IEnumerable<string> lines, ISet<str
3231

3332
bool inIfBlock = false;
3433
bool inElseBlock = false;
34+
bool matchingOtherConfig = false;
3535

3636
string? currentExpression = null;
3737
bool ifBlockMatched = false;
@@ -57,9 +57,9 @@ internal static void Preprocess(string file, IEnumerable<string> lines, ISet<str
5757
validationErrors.Add($"Error {file}:{lineNumber}: # should not be preceeded by spaces");
5858
}
5959

60-
if (startPreprocessMatch.Groups["command"].Value != ifCommand && startPreprocessMatch.Groups["command"].Value != elseIfCommand)
60+
if (startPreprocessMatch.Groups["command"].Value != ifCommand)
6161
{
62-
validationErrors.Add($"Error {file}:{lineNumber}: command after # must be if or elseif (case-sensitive), when followed by a space and expression of 0 or more characters");
62+
validationErrors.Add($"Error {file}:{lineNumber}: command after # must be if when followed by a space and expression of 0 or more characters");
6363
}
6464

6565
if (startPreprocessMatch.Groups["spacesAfterHash"].Value != " ")
@@ -79,7 +79,7 @@ internal static void Preprocess(string file, IEnumerable<string> lines, ISet<str
7979

8080
if (expressions.Contains(expression))
8181
{
82-
validationErrors.Add($"Error {file}:{lineNumber}: expression already encountered in IF / ELSEIF expression={expression}");
82+
validationErrors.Add($"Error {file}:{lineNumber}: expression already encountered in IF expression={expression}");
8383
}
8484
else
8585
{
@@ -117,31 +117,25 @@ internal static void Preprocess(string file, IEnumerable<string> lines, ISet<str
117117

118118
if (inIfBlock)
119119
{
120-
validationErrors.Add($"Error {file}:{lineNumber}: nested #if block in #if or #ifelse block detected, not allowed");
120+
validationErrors.Add($"Error {file}:{lineNumber}: nested #if block in #if block detected, not allowed");
121121
}
122122

123123
inIfBlock = true;
124+
matchingOtherConfig = false;
124125

125126
if (currentExpression == configName)
126127
{
127128
ifBlockMatched = true;
128-
}
129-
break;
130-
case elseIfCommand:
131-
if (!inIfBlock)
132-
{
133-
validationErrors.Add($"Error {file}:{lineNumber}: #elseif detected without matching #if block");
134-
}
135-
136-
if (currentExpression == configName)
129+
}
130+
else if (retainOtherPreprocessingInstructions)
137131
{
138-
ifBlockMatched = true;
132+
matchingOtherConfig = true;
139133
}
140134
break;
141135
case elseCommand:
142136
if (!inIfBlock)
143137
{
144-
validationErrors.Add($"Error {file}:{lineNumber}: #else detected without matching #if or #ifelse block");
138+
validationErrors.Add($"Error {file}:{lineNumber}: #else detected without matching #if block");
145139
}
146140

147141
inIfBlock = false;
@@ -155,7 +149,7 @@ internal static void Preprocess(string file, IEnumerable<string> lines, ISet<str
155149
}
156150
else
157151
{
158-
validationErrors.Add($"Error {file}:{lineNumber}: #endif detected without matching #if, #ifelse, or #else block");
152+
validationErrors.Add($"Error {file}:{lineNumber}: #endif detected without matching #if or #else block");
159153
}
160154

161155
inIfBlock = false;
@@ -189,32 +183,25 @@ internal static void Preprocess(string file, IEnumerable<string> lines, ISet<str
189183
throw new Exception("BUG: state error: currentExpression must be null when inIfBlock is false");
190184
}
191185

192-
/*
193-
if (inIfBlock || inElseBlock)
194-
{
195-
if (ifBlockMatched is null)
196-
{
197-
throw new Exception($"BUG: state error: ifBlockMatched must be not be null if (inIfBlock={inIfBlock} or inElseBlock={inElseBlock})");
198-
}
199-
}
200-
else
201-
{
202-
if (ifBlockMatched is not null)
203-
{
204-
throw new Exception($"BUG: state error: ifBlockMatched must be null if not (inIfBlock={inIfBlock} or inElseBlock={inElseBlock})");
205-
}
206-
}
207-
*/
208-
209186
if (lineIsDirective)
210187
{
211188
madeChanges = true;
189+
190+
if (matchingOtherConfig)
191+
{
192+
output.AppendLine(line);
193+
}
194+
195+
if (command == endIfCommand)
196+
{
197+
matchingOtherConfig = false;
198+
}
212199
}
213200
else
214201
{
215202
if (inIfBlock)
216203
{
217-
if (currentExpression == configName)
204+
if (currentExpression == configName || matchingOtherConfig)
218205
{
219206
output.AppendLine(line);
220207
}
@@ -223,7 +210,7 @@ internal static void Preprocess(string file, IEnumerable<string> lines, ISet<str
223210
}
224211
else if (inElseBlock)
225212
{
226-
if (!ifBlockMatched)
213+
if (!ifBlockMatched || matchingOtherConfig)
227214
{
228215
output.AppendLine(line);
229216
}
@@ -244,7 +231,7 @@ internal static void Preprocess(string file, IEnumerable<string> lines, ISet<str
244231

245232
if (inElseBlock)
246233
{
247-
validationErrors.Add($"Error {file}:{lineNumber}: still in #ifelse block at EOF");
234+
validationErrors.Add($"Error {file}:{lineNumber}: still in #else block at EOF");
248235
}
249236

250237
//File.WriteAllText(file, output.ToString());

0 commit comments

Comments
 (0)