Skip to content
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

fix: add license and generated code headers to generated resource files #696

Merged
merged 2 commits into from
Mar 26, 2024

Conversation

Hectorhammett
Copy link
Contributor

Change the ResourcesGenerator class to make use of the AST::file() method to add the required comments. I also added some comments for discussion on this PR to add them in a different PR if we come with a more elegant solution.

Fixes #643

…utogenerated code warning to generated files
->withApacheLicense($currentYear)
->withGeneratedCodeWarning()
->withBlock($codeBlock)
->toCode() . ";";
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am adding the ; manually here but I am not particularly fond of this.

My first attempt was to add this to the return method on AST but that function is used in different places. I am using the PhpFile class which has the ability to use a codeblock but any PhpBlock class implements the ShouldNotApplySemicolonInterface which the AST::toPhp method checks to decide wether or not add a semicolon.

We could create a EndPhpBlock or FinalPhpBlock class that does not implements the interface perhaps?

AST::file(null)
    ->withBlock(new EndPhpBlock($string))

Add a method that internally forces a semicolon while calling the toPhp method?

AST::file(null)
    ->withBlock(new PhpBlock($string))
    ->withSemiColon()

perhaps a method on the PhpClass to "finalize" the class? But perhaps the word for "Final" might be confused by a Final class. Ready perhaps? that adds a ; to the end?

AST::file(null)
    ->withBlock(new PhpBlock())
    ->ready()

Copy link
Collaborator

Choose a reason for hiding this comment

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

Interesting...I don't want to mess with PhpBlock too much, especially if it is already opinionated about semicolons. Given how choosy the AST is about semicolons altogether, I think this might be a reasonable solution to keep it how it is...or do what you suggested with a new EndPhpBlock...maybe LastPhpBlock? It just wraps a Block and tacks on the ; in toCode?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I poked around a bit more today to see if adding the extra PHP class would help but after debugging and poking further turns out that in this case, it is actually not using the phpBlock class, but just a regular AST class and the toPhp method just calls toCode on an AST instance:

elseif ($x instanceof AST) {
    return $x->toCode();
}

So the semicolon interface does nothing in this case lol. I think it would be best to have a way to add a semicolon codewise but for now let's keep it like it is.

Copy link
Collaborator

Choose a reason for hiding this comment

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

So the semicolon interface does nothing in this case lol. I think it would be best to have a way to add a semicolon codewise but for now let's keep it like it is.

Nice code spelunking! I agree, simple enough for now. Maybe file a priority: p3 type: cleanup bug to look at this?

@Hectorhammett Hectorhammett changed the title Edit the ResourcesGenerator class to add the apache license and the autogenerated code warning to generated files Chore: Edit the ResourcesGenerator class to add the apache license and the autogenerated code warning to generated files Mar 22, 2024
@noahdietz noahdietz changed the title Chore: Edit the ResourcesGenerator class to add the apache license and the autogenerated code warning to generated files fix: add license and generated code headers to generated resource files Mar 23, 2024
@noahdietz
Copy link
Collaborator

Note: I changed the title to fix so that with automerge (for merge-on-green bot), the commits are squashed and the PR title is used as the main commit message. We want this to be fix b.c it changes the generated code, but not feat because it doesn't impact the end-user surface or functionality. This is my interpretation of the Conventional Commit spec that acts as the contract between our commit types and release-please semver versioning

Copy link
Collaborator

@noahdietz noahdietz left a comment

Choose a reason for hiding this comment

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

You will also need to update the Bazel integration test Golden files. Those can be updated locally via the instructions in DEVELOPMENT.md

@noahdietz
Copy link
Collaborator

When updating the integration test golden files you will need to rotate the bazel cache key in the secrets. It isn't really a "secret" just where this value is stored, which indicates we want a full rebuild of the bazel workspace.

uuidgen | gh secret set CACHE_VERSION -r https://github.com/googleapis/gapic-generator-php

This is documented here: https://github.com/googleapis/gapic-generator-php/blob/main/DEVELOPMENT.md#rotating-the-bazel-cache-key. You may or may not have perms for this, lmk if you don't.

I've rotated and rerun the build for you.

Copy link
Collaborator

@noahdietz noahdietz left a comment

Choose a reason for hiding this comment

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

Please remove the MODULE bazel files - these are created by buildifier or something when you open a WORKSPACE or BUILD.bazel file. We don't support bzlmod yet.

Maybe we should add these to the .gitignore....

MODULE.bazel.lock Outdated Show resolved Hide resolved
MODULE.bazel Outdated Show resolved Hide resolved
@Hectorhammett
Copy link
Contributor Author

Hectorhammett commented Mar 26, 2024

Please remove the MODULE bazel files - these are created by buildifier or something when you open a WORKSPACE or BUILD.bazel file. We don't support bzlmod yet.

Maybe we should add these to the .gitignore....

@noahdietz
I removed these. Is there a reason why NOT to add them to the .gitignore? If not I could just add them rn unless there is a specific reason not to.

@noahdietz
Copy link
Collaborator

I removed these. Is there a reason why NOT to add them to the .gitignore? If not I could just add them rn unless there is a specific reason not to.

Not really, unless we worry about folks forgetting that we are ignoring them if we eventually add bzlmod support haha.

Let's just add it! This PR or another is fine.

@Hectorhammett
Copy link
Contributor Author

I removed these. Is there a reason why NOT to add them to the .gitignore? If not I could just add them rn unless there is a specific reason not to.

Not really, unless we worry about folks forgetting that we are ignoring them if we eventually add bzlmod support haha.

Let's just add it! This PR or another is fine.

@noahdietz
Let's do it in a different PR. This is getting too mangled for what it is. Just a small code change and 114 files change feels like when you have a play dough getting mixed with different colours and ends up gray 🤣

@noahdietz noahdietz merged commit 8409511 into main Mar 26, 2024
5 checks passed
@noahdietz noahdietz deleted the resource-php-file branch March 26, 2024 17:44
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.

Ensure all generated files are labeled as generated in the comments
2 participants