-
Notifications
You must be signed in to change notification settings - Fork 524
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
Conversation
//internal/pkg_web/test3:test is failing. I'm gonna fix it asap. |
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.
Thanks for contributing!
internal/pkg_web/test3/BUILD.bazel
Outdated
@@ -0,0 +1,26 @@ | |||
load("@build_bazel_rules_nodejs//:index.bzl", "pkg_web") |
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.
"test3" isn't a descriptive name, can you make it "substitutions" or similar?
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.
Done ✅
internal/pkg_web/test3/spec.js
Outdated
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_'; |
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.
use the golden_file_test
rule instead
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.
Done ✅
internal/pkg_web/pkg_web.bzl
Outdated
@@ -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) |
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 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
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.
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.
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.
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.
internal/pkg_web/assembler_spec.js
Outdated
|
||
it('should replace contents with dynamic text from volatile file', () => { | ||
assembler.main( | ||
[outdir, volatilePath, '{"content":"{TEST_KEY}"}', '--assets', 'path/to/thing1.txt']); |
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.
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.
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. 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.
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.
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.
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.
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?
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.
update, I'm deprecating replace_with_version
so let's just do it your way :)
internal/pkg_web/assembler.js
Outdated
if (substituteWith.match(/^{.*?}$/)) { | ||
substituteWith = substituteWith.replace(/^{(.*?)}$/, '$1'); | ||
if (!stampMap[substituteWith]) { | ||
throw new Error(`Could not find ${substituteWith} key in volatile-status file.`); |
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'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
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.
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.
internal/node/_node_bin/node
Outdated
@@ -0,0 +1,9 @@ | |||
#!/bin/bash |
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.
Revert this file?
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 happened automatically when I was trying to commit the changes.
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.
Sure, I'll try to revert it.
internal/pkg_web/assembler_spec.js
Outdated
|
||
it('should replace contents with dynamic text from volatile file', () => { | ||
assembler.main( | ||
[outdir, volatilePath, '{"content":"{TEST_KEY}"}', '--assets', 'path/to/thing1.txt']); |
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.
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", |
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.
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.
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 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.
Hey I'm finally picking up stamping again, I'll get this merged shortly... |
I had no time to work on this and there is still some work to do. Maybe I could get them done by Sunday. |
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 |
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 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 ℹ️ Googlers: Go here for more info. |
@googlebot I consent. |
CLAs look good, thanks! ℹ️ Googlers: Go here for more info. |
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Issue Number: #1621
What is the new behavior?
Does this PR introduce a breaking change?
Other information