Skip to content

Conversation

@jskeet
Copy link
Collaborator

@jskeet jskeet commented Dec 13, 2024

No description provided.

@jskeet jskeet requested a review from a team as a code owner December 13, 2024 11:13
…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.)
@jskeet jskeet force-pushed the refactor-directory-layout branch from 7f4b1dd to 439e276 Compare December 13, 2024 11:17
@jskeet
Copy link
Collaborator Author

jskeet commented Dec 13, 2024

Fixes b/383482997

Copy link
Contributor

@amanda-tarafa amanda-tarafa left a 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) =>
Copy link
Contributor

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);
Copy link
Contributor

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?

Copy link
Collaborator Author

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);
Copy link
Contributor

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.

Copy link
Collaborator Author

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");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move to ApiLayout?

Copy link
Collaborator Author

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"));
Copy link
Contributor

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?

Copy link
Collaborator Author

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.

Comment on lines +101 to 104
var directory = Path.Combine(apiLayout.SourceDirectory, $"{api.Id}{directorySuffix}");
if (Directory.Exists(directory))
{
DeleteAll(Directory.EnumerateFiles(directory, "*.g.cs", SearchOption.AllDirectories));
Copy link
Contributor

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?

Copy link
Collaborator Author

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this into RootLayout?

Copy link
Collaborator Author

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);
Copy link
Contributor

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.

Copy link
Collaborator Author

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);
Copy link
Contributor

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.

@jskeet
Copy link
Collaborator Author

jskeet commented Dec 13, 2024

Merging, but will do a follow-up on Monday.

@jskeet jskeet merged commit d9d7a4f into googleapis:main Dec 13, 2024
9 checks passed
@jskeet jskeet deleted the refactor-directory-layout branch December 13, 2024 18:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants