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

feat(builtin): support for substitutions #1664

Merged
merged 3 commits into from
Aug 31, 2020
Merged

feat(builtin): support for substitutions #1664

merged 3 commits into from
Aug 31, 2020

Conversation

thesayyn
Copy link
Collaborator

PR Checklist

Please check if your PR fulfills the following requirements:

  • [ X] Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • [ X ] Feature (please, look at the "Scope of the project" section in the README.md file)
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • Other... Please describe:

What is the current behavior?

Issue Number: #1621

What is the new behavior?

Does this PR introduce a breaking change?

  • Yes
  • [ X ] No

Other information

@thesayyn
Copy link
Collaborator Author

//internal/pkg_web/test3:test is failing. I'm gonna fix it asap.

Copy link
Collaborator

@alexeagle alexeagle left a comment

Choose a reason for hiding this comment

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

Thanks for contributing!

@@ -0,0 +1,26 @@
load("@build_bazel_rules_nodejs//:index.bzl", "pkg_web")
Copy link
Collaborator

Choose a reason for hiding this comment

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

"test3" isn't a descriptive name, can you make it "substitutions" or similar?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done ✅

describe('pkg_web', () => {
it('should match the golden file', () => {
const output = 'build_bazel_rules_nodejs/internal/pkg_web/test/pkg/index.html';
const golden = 'build_bazel_rules_nodejs/internal/pkg_web/test/index_golden.html_';
Copy link
Collaborator

Choose a reason for hiding this comment

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

use the golden_file_test rule instead

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done ✅

@@ -48,12 +51,15 @@ def move_files(output_name, files, action_factory, var, assembler, root_paths):
www_dir = action_factory.declare_directory(output_name)
args = action_factory.args()
args.add(www_dir.path)
args.add(version_file.path)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this needs some conditional logic because the version_file isn't always present?

I think we need to detect use of --stamp like pkg_npm does stamp = ctx.attr.node_context_data[NodeContextInfo].stamp

Copy link
Collaborator Author

@thesayyn thesayyn Feb 28, 2020

Choose a reason for hiding this comment

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

as far as I know, the version file is always present but the content changes regarding to the --workspace_status_command. Since we have a logic in bundler which checks for the presence of the keys, we'll be okay I guess.

Copy link
Collaborator Author

@thesayyn thesayyn Mar 22, 2020

Choose a reason for hiding this comment

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

As far as I understand the rule has to obey the --stamp parameter which is stored in the node_context helper rule. I should make this work that way.


it('should replace contents with dynamic text from volatile file', () => {
assembler.main(
[outdir, volatilePath, '{"content":"{TEST_KEY}"}', '--assets', 'path/to/thing1.txt']);
Copy link
Collaborator

Choose a reason for hiding this comment

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

we should make the API match with pkg_npm. Over there we don't let you grab arbitrary keys from the version file, instead we know the key that indicates the version and the replace_with_version attribute which says what placeholder is used.

I suspect you prefer this more general implementation? but I think it's more important to be consistent.

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. I think we should permit the users to replace the contents with the arbitrary keys from the version file. It would be more useful this way. Since we have a key check in bundler, the user would get an error if the key doesn't exist in the version file.

Copy link
Collaborator Author

@thesayyn thesayyn Mar 20, 2020

Choose a reason for hiding this comment

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

In addition, this approach is more general and gives you the ability to replace "placeholder texts" with arbitrary keys from the version file.

For instance, In one of our projects, we are replacing "BUILD_SCM_HASH" with the real hash along with the "BUILD_SCM_VERSION" key and it is not possible to accomplish the same thing with the pkg_npm rule.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Perhaps provide both APIs? The replace_with_version attr on pkg_web would provide a consistent API surface for both rules, and pkg_npm would change to support substitutions from workspace_status?

Copy link
Collaborator

Choose a reason for hiding this comment

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

update, I'm deprecating replace_with_version so let's just do it your way :)

if (substituteWith.match(/^{.*?}$/)) {
substituteWith = substituteWith.replace(/^{(.*?)}$/, '$1');
if (!stampMap[substituteWith]) {
throw new Error(`Could not find ${substituteWith} key in volatile-status file.`);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure about throwing this error here - if the user is replacing 0.0.0-PLACEHOLDER as the version string for regular builds with a key from --workspace_status_command which is tied to --stamp, this will throw when not running a stamped build

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

When I tie this with the version file from node context, which only appears when --stamp option is present, this would be long gone. So I thought it would be better if we explicitly check for the keys when the user trying to stamp builds with --stamp option present.

@@ -0,0 +1,9 @@
#!/bin/bash
Copy link
Collaborator

Choose a reason for hiding this comment

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

Revert this file?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This happened automatically when I was trying to commit the changes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sure, I'll try to revert it.


it('should replace contents with dynamic text from volatile file', () => {
assembler.main(
[outdir, volatilePath, '{"content":"{TEST_KEY}"}', '--assets', 'path/to/thing1.txt']);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Perhaps provide both APIs? The replace_with_version attr on pkg_web would provide a consistent API surface for both rules, and pkg_npm would change to support substitutions from workspace_status?

"index.js",
],
substitutions = {
"THIS_SHOULD_BE_REPLACED": "THIS_SHOULD_BE_REPLACED_WITH_THIS",
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: I know this is a substitutions, but at first I didn't see the different between the strings after the build as only the suffix changed 😄
Perhaps provide a real world example of a replacement here, as this is mostly centered around version replacement.

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 agree, a bit confusing. we can add an example that replaces with an arbitrary key from the volatile file therefore we would have tests and examples for both use cases.

@alexeagle
Copy link
Collaborator

Hey I'm finally picking up stamping again, I'll get this merged shortly...

@thesayyn
Copy link
Collaborator Author

I had no time to work on this and there is still some work to do. Maybe I could get them done by Sunday.

@alexeagle
Copy link
Collaborator

no worries @thesayyn I have time today and want to make this consistent with other stamping changes I just landed. I'll push a fixup commit

@alexeagle alexeagle changed the base branch from master to stable August 31, 2020 20:53
@googlebot
Copy link

All (the pull request submitter and all commit authors) CLAs are signed, but one or more commits were authored or co-authored by someone other than the pull request submitter.

We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that by leaving a comment that contains only @googlebot I consent. in this pull request.

Note to project maintainer: There may be cases where the author cannot leave a comment, or the comment is not properly detected as consent. In those cases, you can manually confirm consent of the commit author(s), and set the cla label to yes (if enabled on your project).

ℹ️ Googlers: Go here for more info.

@alexeagle
Copy link
Collaborator

@googlebot I consent.

@googlebot
Copy link

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

@alexeagle alexeagle merged commit 421280a into bazel-contrib:stable Aug 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants