-
Notifications
You must be signed in to change notification settings - Fork 399
refactor: Concentrate knowledge of directory locations #13998
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…ayout This removes a lot of scattered knowledge of where apis.json, googleapis etc are - but still scatters a lot of calls to RootLayout.ForCurrentDirectory(). More refactoring coming.
- Removed parameterless ApiCatalog.Load method: we always pass in a RootLayout not - Provide a RootLayout property in CommandBase, to *vastly* reduce the number of calls to RootLayout.ForCurrentDirectory() (This will make it much easier to run commands in a different context if we ever need to.)
7f4b1dd to
439e276
Compare
|
Fixes b/383482997 |
amanda-tarafa
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great!
All of my comments are about more refactoring on the same line, i.e. moving more paths into RootLayout or ApiLayout. I'm on the fence about some of them as it would require they layout types to know about special folders and files, and even default values for some of those. I think that's fine, and I think having the layout types and only the layout types know everything about folders and files is fine. But I'm also fine with the not knowing about many specific things.
Any changes you want to make, happy to have those in other PRs.
| /// </summary> | ||
| public string RepositoryRoot => FailIfAbsent(_repositoryRoot); | ||
|
|
||
| private static string FailIfAbsent(string value, [CallerMemberName] string caller = null) => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh this is good!
| var packageSource = Path.Combine(layout.SourceDirectory, api.Id); | ||
| var rootLayout = RootLayout.ForCurrentDirectory(); | ||
| var apiLayout = rootLayout.CreateApiLayout(api); | ||
| var packageSource = Path.Combine(apiLayout.SourceDirectory, api.Id); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't this one be brought into ApiLayout? It already knows the api.Id, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. (Or at least, it does at build time.) I think we've got this in multiple places, in fact - certainly in generation. Will do as a follow-up. I'll try finding every occurrence of Path.Combine to see whether there are other similar patterns, too.
|
|
||
| DirectoryLayout layout = api == "help" ? DirectoryLayout.ForHelpSnippets() : DirectoryLayout.ForApi(api); | ||
| var rootLayout = RootLayout.ForCurrentDirectory(); | ||
| var apiLayout = api == "help" ? ApiLayout.ForHelpSnippets(rootLayout) : rootLayout.CreateApiLayout(api); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move the question to RootLayout? I'm on the fence about this one btw.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I strongly suspect this is the only occurrence of this - we may want to move the logic into this file, if we can.
| DeleteGeneratedSource(".Snippets"); | ||
| DeleteGeneratedSource(".GeneratedSnippets"); | ||
| var generatedSnippetsDirectory = Path.Combine(layout.SourceDirectory, $"{api.Id}.GeneratedSnippets"); | ||
| var generatedSnippetsDirectory = Path.Combine(apiLayout.SourceDirectory, $"{api.Id}.GeneratedSnippets"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move to ApiLayout?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm... will have a think. (Again, I'll see how many places we use Path.Combine for this.)
| DeleteAll(Directory.EnumerateFiles(generatedSnippetsDirectory, "*.json")); | ||
| } | ||
| File.Delete(Path.Combine(layout.SourceDirectory, "gapic_metadata.json")); | ||
| File.Delete(Path.Combine(apiLayout.SourceDirectory, "gapic_metadata.json")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move as well? Maybe with a ApiLayout.CombineWithSourceDirectory(string fileName) if you prefer not know about specific files in ApiLayout?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ooh, a method like that would be nice, yes.
| var directory = Path.Combine(apiLayout.SourceDirectory, $"{api.Id}{directorySuffix}"); | ||
| if (Directory.Exists(directory)) | ||
| { | ||
| DeleteAll(Directory.EnumerateFiles(directory, "*.g.cs", SearchOption.AllDirectories)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about ApiLayout knows about the suffixes, etc. and just gives you the collection of generated files?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unsure at the moment. Bear in mind that quite a bit of that is gapic-specific.
| Environment.SetEnvironmentVariable(GenerateApisCommand.GeneratorInputDirectoryEnvironmentVariable, generatorInput ?? "/app/generator-input"); | ||
| Environment.SetEnvironmentVariable(GenerateApisCommand.GeneratorOutputDirectoryEnvironmentVariable, output); | ||
| Environment.SetEnvironmentVariable(GenerateApisCommand.GoogleApisDirectoryEnvironmentVariable, apiRoot); | ||
| var rootLayout = RootLayout.ForGeneration(generatorInput ?? "/app/generator-input", output, apiRoot); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move this into RootLayout?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so in this case - it's a really special case of wanting to use /app, which is very specific to the container.
| var layout = DirectoryLayout.ForApi(api.Id, generatorOutputDirectory, generatorInputDirectory); | ||
| string productionDirectory = Path.Combine(layout.SourceDirectory, api.Id); | ||
| var apiLayout = _rootLayout.CreateApiLayout(api); | ||
| string productionDirectory = Path.Combine(apiLayout.SourceDirectory, api.Id); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is the same as a built package directory I flagged for moving into ApiLayout a few comments up. If it's here again, maybe it does make sense to move it to ApiLayout.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, exactly what I was thinking :)
| var layout = DirectoryLayout.ForApi(api.Id, generatorOutputDirectory, generatorInputDirectory); | ||
| string productionDirectory = Path.Combine(layout.SourceDirectory, api.Id); | ||
| var apiLayout = _rootLayout.CreateApiLayout(api); | ||
| string productionDirectory = Path.Combine(apiLayout.SourceDirectory, api.Id); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And here again, doesn't ApiLayout already knows about the API ID.
|
Merging, but will do a follow-up on Monday. |
No description provided.