Skip to content

Commit c647485

Browse files
authored
LocalPackages config to _generated_local (no CI needed for gen code) (#20504)
* LocalPackages config to _generated_local (no check-in needed for generated code)) Updates to BuildConfigGen to support unverified writes to _generated_local Fixes for globalversion.txt updates
1 parent cf6f574 commit c647485

File tree

7 files changed

+390
-118
lines changed

7 files changed

+390
-118
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,4 @@ _dotnetsdk/
222222
# BuildConfigGen files
223223
FilesOverriddenForConfigGoHereREADME.txt
224224

225+
_generated_local/

BuildConfigGen/EnsureUpdateModeVerifier.cs

+150-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.ComponentModel.Design;
22
using System.Diagnostics;
3+
using System.IO;
34
using System.Security.AccessControl;
45

56
namespace BuildConfigGen
@@ -10,15 +11,17 @@ internal class EnsureUpdateModeVerifier
1011
// if any changes would be made to output, verification should fail
1112
// check the contents of VerifyErrors for verification errors
1213

13-
private readonly bool verifyOnly;
14+
private bool verifyState;
15+
private readonly bool verifyFromConstructor;
1416
private List<string> VerifyErrors = new List<string>();
1517
internal Dictionary<string, string> CopiedFilesToCheck = new Dictionary<string, string>();
1618
internal Dictionary<string, string> RedirectedToTempl = new Dictionary<string, string>();
1719
private HashSet<string> tempsToKeep = new HashSet<string>();
1820

1921
public EnsureUpdateModeVerifier(bool verifyOnly)
2022
{
21-
this.verifyOnly = verifyOnly;
23+
this.verifyState = verifyOnly;
24+
this.verifyFromConstructor = verifyOnly;
2225
}
2326

2427
public IEnumerable<string> GetVerifyErrors(bool skipContentCheck)
@@ -30,7 +33,6 @@ public IEnumerable<string> GetVerifyErrors(bool skipContentCheck)
3033

3134
if (!skipContentCheck)
3235
{
33-
3436
foreach (var r in CopiedFilesToCheck)
3537
{
3638
string? sourceFile;
@@ -90,9 +92,9 @@ public void CleanupTempFiles()
9092
}
9193
}
9294

93-
if (count > 0 && !verifyOnly)
95+
if (count > 0 && !verifyFromConstructor)
9496
{
95-
throw new Exception("Expected RedirectedToTemp to be empty when !verifyOnly");
97+
throw new Exception("Expected RedirectedToTemp to be empty when !verifyFromConstructor");
9698
}
9799
}
98100
finally
@@ -105,6 +107,8 @@ public void CleanupTempFiles()
105107

106108
internal void Copy(string sourceFileName, string destFileName, bool overwrite)
107109
{
110+
bool verifyOnly = UseVerifyOnlyForFile(destFileName);
111+
108112
if (verifyOnly)
109113
{
110114
if (File.Exists(destFileName))
@@ -138,6 +142,16 @@ internal void Copy(string sourceFileName, string destFileName, bool overwrite)
138142

139143
internal void Move(string sourceFileName, string destFileName)
140144
{
145+
bool verifyOnlySource = UseVerifyOnlyForFile(sourceFileName);
146+
bool verifyOnlyDest = UseVerifyOnlyForFile(destFileName);
147+
148+
if (verifyOnlySource != verifyOnlyDest)
149+
{
150+
throw new Exception($"BUG: both source and dest must be unconditional path or not sourceFileName={sourceFileName} destFileName={destFileName}");
151+
}
152+
153+
var verifyOnly = verifyOnlySource || verifyOnlyDest;
154+
141155
if (verifyOnly)
142156
{
143157
// verification won't pass if we encounter a move
@@ -160,6 +174,8 @@ internal void Move(string sourceFileName, string destFileName)
160174

161175
internal void WriteAllText(string path, string contents, bool suppressValidationErrorIfTargetPathDoesntExist)
162176
{
177+
bool verifyOnly = UseVerifyOnlyForFile(path);
178+
163179
if (verifyOnly)
164180
{
165181
if (File.Exists(path))
@@ -205,6 +221,8 @@ private string NormalizeFile(string file)
205221

206222
internal void DirectoryCreateDirectory(string path, bool suppressValidationErrorIfTargetPathDoesntExist)
207223
{
224+
bool verifyOnly = UseVerifyOnlyForPath(path);
225+
208226
if (verifyOnly)
209227
{
210228
if (!Directory.Exists(path))
@@ -228,6 +246,8 @@ internal void DirectoryCreateDirectory(string path, bool suppressValidationError
228246

229247
internal string FileReadAllText(string filePath)
230248
{
249+
bool verifyOnly = UseVerifyOnlyForFile(filePath);
250+
231251
if (verifyOnly)
232252
{
233253
string targetFile = ResolveFile(filePath);
@@ -240,8 +260,10 @@ internal string FileReadAllText(string filePath)
240260
}
241261
}
242262

243-
internal string [] FileReadAllLines(string filePath)
263+
internal string[] FileReadAllLines(string filePath)
244264
{
265+
bool verifyOnly = UseVerifyOnlyForFile(filePath);
266+
245267
if (verifyOnly)
246268
{
247269
string targetFile = ResolveFile(filePath);
@@ -263,7 +285,7 @@ internal bool FilesEqual(string sourcePath, string targetPath)
263285

264286
private string ResolveFile(string filePath)
265287
{
266-
if(!verifyOnly)
288+
if (!UseVerifyOnlyForFile(filePath))
267289
{
268290
return filePath;
269291
}
@@ -295,20 +317,138 @@ private string ResolveFile(string filePath)
295317

296318
internal void DeleteDirectoryRecursive(string path)
297319
{
298-
if(verifyOnly)
320+
bool verify = UseVerifyOnlyForPath(path);
321+
322+
if (verify)
299323
{
300-
if(Directory.Exists(path))
324+
if (Directory.Exists(path))
301325
{
302326
VerifyErrors.Add($"Expected directory {path} to not exist");
303327
}
304328
}
305329
else
306330
{
307-
if(Directory.Exists(path))
331+
if (Directory.Exists(path))
308332
{
309333
Directory.Delete(path, true);
310334
}
311335
}
312336
}
337+
338+
private bool UseVerifyOnlyForFile(string file)
339+
{
340+
return UseVerifyOnlyInternal(file, true);
341+
}
342+
343+
private bool UseVerifyOnlyForPath(string path)
344+
{
345+
return UseVerifyOnlyInternal(path, false);
346+
}
347+
348+
private bool UseVerifyOnlyInternal(string path, bool trueForFile)
349+
{
350+
EnsureState();
351+
352+
/*
353+
// if verifyOnly state
354+
if (verifyState)
355+
{
356+
return true;
357+
}*/
358+
359+
// if !verifyOnly was passed to constructor, unconditional writes everywhere
360+
if (!verifyFromConstructor)
361+
{
362+
return false;
363+
}
364+
365+
// if uncondo
366+
if (allowedUnconditionalPath is null)
367+
{
368+
return verifyState;
369+
}
370+
371+
if (trueForFile ? IsSubFile(allowedUnconditionalPath, path) : IsSubPath(allowedUnconditionalPath, path))
372+
{
373+
return false;
374+
}
375+
else
376+
{
377+
return true;
378+
}
379+
}
380+
381+
string? allowedUnconditionalPath;
382+
383+
internal void StartUnconditionalWrites(string allowedUnconditionalPath)
384+
{
385+
EnsureState();
386+
387+
if(verifyState != verifyFromConstructor)
388+
{
389+
throw new Exception($"BUG: expected verifyState {verifyState} == verifyFromConstructor {verifyFromConstructor}");
390+
}
391+
392+
if (!verifyFromConstructor)
393+
{
394+
return;
395+
}
396+
397+
verifyState = false;
398+
this.allowedUnconditionalPath = allowedUnconditionalPath;
399+
}
400+
401+
internal void ResumeWriteBehavior()
402+
{
403+
EnsureState();
404+
405+
if (!verifyFromConstructor)
406+
{
407+
return;
408+
}
409+
410+
verifyState = verifyFromConstructor;
411+
this.allowedUnconditionalPath = null;
412+
}
413+
414+
private void EnsureState()
415+
{
416+
if(!verifyFromConstructor && verifyState)
417+
{
418+
throw new Exception("BUG: verifyState cannot be true if verifyFromConstructor is false");
419+
}
420+
421+
if (verifyFromConstructor)
422+
{
423+
if (this.allowedUnconditionalPath is null && !verifyState)
424+
{
425+
throw new Exception($"BUG: expected allowedUnconditionalPath={allowedUnconditionalPath} to be not null when !verifyState=={!verifyState}");
426+
}
427+
}
428+
else
429+
{
430+
if (this.allowedUnconditionalPath is not null)
431+
{
432+
throw new Exception($"BUG: expected allowedUnconditionalPath={allowedUnconditionalPath} to be null");
433+
}
434+
}
435+
}
436+
437+
// generaetd by copilot 20240926
438+
static bool IsSubPath(string mainPath, string subPath)
439+
{
440+
var mainDirectory = new DirectoryInfo(mainPath).FullName;
441+
var subDirectory = new DirectoryInfo(subPath).FullName;
442+
443+
return subDirectory.StartsWith(mainDirectory, StringComparison.OrdinalIgnoreCase);
444+
}
445+
446+
static bool IsSubFile(string mainPath, string subFile)
447+
{
448+
var mainDirectory = new DirectoryInfo(mainPath).FullName;
449+
var subDirectory = new FileInfo(subFile).FullName;
450+
451+
return subDirectory.StartsWith(mainDirectory, StringComparison.OrdinalIgnoreCase);
452+
}
313453
}
314454
}

BuildConfigGen/GitUtil.cs

+44-3
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,51 @@ internal static string GetGitRootPath(string currentDir)
120120
}
121121
}
122122

123-
internal static IEnumerable<string> GetNonIgnoredFileListFromPath(string taskTarget)
123+
internal static IEnumerable<string> GetNonIgnoredFileListFromPath(string gitRoot, string taskTarget)
124124
{
125-
if(!Directory.Exists(taskTarget))
125+
string gitIgnorePathBak = Path.Combine(gitRoot, ".gitignore.bak");
126+
string gitIgnore = Path.Combine(gitRoot, ".gitignore");
127+
128+
bool needsGitIgnoreUpdate = taskTarget.Contains("/_generated_local/") || taskTarget.Contains(@"\_generated_local\");
129+
130+
string? gitIgnoreContent = null;
131+
132+
if (needsGitIgnoreUpdate)
133+
{
134+
gitIgnoreContent = File.ReadAllText(gitIgnore);
135+
const string genertedLocalPath = "_generated_local/";
136+
137+
if (!gitIgnoreContent.Contains(genertedLocalPath))
138+
{
139+
throw new Exception("Expected " + genertedLocalPath + " in " + gitIgnore);
140+
}
141+
142+
gitIgnoreContent = gitIgnoreContent.Replace(genertedLocalPath, "");
143+
144+
File.Copy(gitIgnore, gitIgnorePathBak, true);
145+
}
146+
147+
try
148+
{
149+
if (needsGitIgnoreUpdate)
150+
{
151+
File.WriteAllText(gitIgnore, gitIgnoreContent);
152+
}
153+
154+
return GetNonIgnoredFileListFromPathInner(taskTarget);
155+
}
156+
finally
157+
{
158+
if (needsGitIgnoreUpdate)
159+
{
160+
File.Move(gitIgnorePathBak, gitIgnore, true);
161+
}
162+
}
163+
}
164+
165+
private static IEnumerable<string> GetNonIgnoredFileListFromPathInner(string taskTarget)
166+
{
167+
if (!Directory.Exists(taskTarget))
126168
{
127169
throw new Exception($"{nameof(taskTarget)}=={taskTarget} doesn't exist");
128170
}
@@ -137,7 +179,6 @@ internal static IEnumerable<string> GetNonIgnoredFileListFromPath(string taskTar
137179
return paths;
138180
}
139181

140-
141182
private static IEnumerable<string> GitLsFiles(string taskTarget)
142183
{
143184
if (!Directory.Exists(taskTarget))

0 commit comments

Comments
 (0)