Skip to content

Commit

Permalink
fix: Make the clean container command more selective about what it cl…
Browse files Browse the repository at this point in the history
…eans

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.
  • Loading branch information
jskeet committed Dec 10, 2024
1 parent db578fc commit 79d764a
Showing 1 changed file with 44 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,50 +36,73 @@ public int Execute(Dictionary<string, string> options)
var generatorInput = Path.Combine(repoRoot, DirectoryLayout.GeneratorInput);
var catalog = ApiCatalog.LoadFromGeneratorInput(generatorInput);

List<string> ids;
List<ApiMetadata> apis;
if (apiPath is not null)
{
ids = new List<string>();
apis = new List<ApiMetadata>();
var targetApi = catalog.Apis.SingleOrDefault(api => api.ProtoPath == apiPath);
if (targetApi is null)
{
Console.WriteLine($"API path '{apiPath}' is not configured for any API. Ignoring.");
}
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}");
DeleteAll(Directory.EnumerateFiles(directory, "*.g.cs", SearchOption.AllDirectories));
}
}
Console.WriteLine($"Cleaning non-API-specific files");
nonSourceGenerator.CleanNonApiFiles();
return 0;
}

private static void DeleteAll(IEnumerable<string> files)
Expand Down

0 comments on commit 79d764a

Please sign in to comment.