From 55786cb273e9c7921002968fba73fad3a2948710 Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Tue, 10 Dec 2024 07:16:41 +0000 Subject: [PATCH] fix: Make the clean container command more selective about what it cleans We have some code which *is* generated code, but not generated in a regular way - e.g. conformance tests, common resource names, BigQuery resource classes. Those are all updated by running their generators manually, so shouldn't be cleaned here. --- .../ContainerCommands/CleanCommand.cs | 68 +++++++++++++------ 1 file changed, 47 insertions(+), 21 deletions(-) diff --git a/tools/Google.Cloud.Tools.ReleaseManager/ContainerCommands/CleanCommand.cs b/tools/Google.Cloud.Tools.ReleaseManager/ContainerCommands/CleanCommand.cs index be1647967ebc..75bc476e3aa4 100644 --- a/tools/Google.Cloud.Tools.ReleaseManager/ContainerCommands/CleanCommand.cs +++ b/tools/Google.Cloud.Tools.ReleaseManager/ContainerCommands/CleanCommand.cs @@ -36,10 +36,10 @@ public int Execute(Dictionary options) var generatorInput = Path.Combine(repoRoot, DirectoryLayout.GeneratorInput); var catalog = ApiCatalog.LoadFromGeneratorInput(generatorInput); - List ids; + List apis; if (apiPath is not null) { - ids = new List(); + apis = new List(); var targetApi = catalog.Apis.SingleOrDefault(api => api.ProtoPath == apiPath); if (targetApi is null) { @@ -47,39 +47,65 @@ public int Execute(Dictionary options) } else { - ids.Add(targetApi.Id); + apis.Add(targetApi); } } else { - ids = catalog.Apis.Select(api => api.Id).OrderBy(id => id, StringComparer.Ordinal).ToList(); + apis = catalog.Apis.ToList(); } var nonSourceGenerator = new NonSourceGenerator(generatorInput, repoRoot); - foreach (var id in ids) + foreach (var api in apis) { - Console.WriteLine($"Cleaning {id}"); + CleanApiFiles(api); + } + Console.WriteLine($"Cleaning non-API-specific files"); + nonSourceGenerator.CleanNonApiFiles(); + return 0; - // GAPIC files and directories (except project files, which are removed by the non-source generator). - var layout = DirectoryLayout.ForApi(id, repoRoot, generatorInput); - // Source code - DeleteAll(Directory.EnumerateFiles(layout.SourceDirectory, "*.g.cs", SearchOption.AllDirectories)); - // Snippet metadata - var generatedSnippetsDirectory = Path.Combine(layout.SourceDirectory, $"{id}.GeneratedSnippets"); - if (Directory.Exists(generatedSnippetsDirectory)) + void CleanApiFiles(ApiMetadata api) + { + Console.WriteLine($"Cleaning {api.Id}"); + var layout = DirectoryLayout.ForApi(api.Id, repoRoot, generatorInput); + + switch (api.Generator) { - DeleteAll(Directory.EnumerateFiles(generatedSnippetsDirectory, "*.json")); + case GeneratorType.Proto: + // We don't generate any snippets for proto-only APIs. + DeleteGeneratedSource(""); + break; + case GeneratorType.Micro: + DeleteGeneratedSource(""); + DeleteGeneratedSource(".Snippets"); + DeleteGeneratedSource(".GeneratedSnippets"); + var generatedSnippetsDirectory = Path.Combine(layout.SourceDirectory, $"{api.Id}.GeneratedSnippets"); + if (Directory.Exists(generatedSnippetsDirectory)) + { + DeleteAll(Directory.EnumerateFiles(generatedSnippetsDirectory, "*.json")); + } + File.Delete(Path.Combine(layout.SourceDirectory, "gapic_metadata.json")); + break; + default: + // We didn't generate any code, so we leave anything that's still there alone. + // Any project files which were generated will be deleted by nonSourceGenerator. + // Note that there may be other generated files, e.g. conformance tests, or the BigQuery + // separate generator - but they're not generated by GenerateApis. + break; } - // GAPIC metadata - File.Delete(Path.Combine(layout.SourceDirectory, "gapic_metadata.json")); - - nonSourceGenerator.CleanApiFiles(id); + nonSourceGenerator.CleanApiFiles(api.Id); PruneEmptyDirectories(layout.SourceDirectory); + + void DeleteGeneratedSource(string directorySuffix) + { + var directory = Path.Combine(layout.SourceDirectory, $"{api.Id}{directorySuffix}"); + if (Directory.Exists(directory)) + { + DeleteAll(Directory.EnumerateFiles(directory, "*.g.cs", SearchOption.AllDirectories)); + } + } } - Console.WriteLine($"Cleaning non-API-specific files"); - nonSourceGenerator.CleanNonApiFiles(); - return 0; } private static void DeleteAll(IEnumerable files)