-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[dotnet] Generate atoms statically #16608
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
Merged
nvborisenko
merged 12 commits into
SeleniumHQ:trunk
from
nvborisenko:dotnet-atoms-res-gen
Nov 18, 2025
+175
−126
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8aa9c86
generate partial class
nvborisenko fbe666d
clean bazel
nvborisenko 486f308
Use gen strings in runtime
nvborisenko 0ea00a1
Notice about migrate to .net 10
nvborisenko a3565f3
GetWrappedAtom in WebElement.cs
nvborisenko 974828d
Camel case property names
nvborisenko 8512fa1
Rename across all projects
nvborisenko 147c681
Provide property name per resource
nvborisenko edf6588
Meaningful resource names
nvborisenko dfdbd78
Format?
nvborisenko 9321944
Format?
nvborisenko e2cac36
Merge branch 'trunk' into dotnet-atoms-res-gen
nvborisenko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| py_binary( | ||
| name = "generate_resources_tool", | ||
| srcs = ["generate_resources_tool.py"], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| """Generate C# partial class with embedded JS resources via a Python tool.""" | ||
|
|
||
| def _generate_resource_utilities_impl(ctx): | ||
| """Invoke a Python script to generate ResourceUtilities.cs from input files. | ||
|
|
||
| The mapping from C# property name to JS file is provided explicitly via the | ||
| 'resources' attribute as a dict: { "PropertyName": label }. | ||
| """ | ||
|
|
||
| args = ctx.actions.args() | ||
| args.add("--output", ctx.outputs.out) | ||
|
|
||
| inputs = [] | ||
| for target, name in ctx.attr.resources.items(): | ||
| files = target.files.to_list() | ||
| if len(files) != 1: | ||
| fail("Each resource label must produce exactly one file, got {} for {}".format(len(files), name)) | ||
| src = files[0] | ||
| inputs.append(src) | ||
| args.add("--input") | ||
| args.add("%s=%s" % (name, src.path)) | ||
|
|
||
| ctx.actions.run( | ||
| inputs = inputs, | ||
| outputs = [ctx.outputs.out], | ||
| executable = ctx.executable._tool, | ||
| arguments = [args], | ||
| mnemonic = "GenerateResourceUtilities", | ||
| progress_message = "Generating C# ResourceUtilities partial class", | ||
| ) | ||
|
|
||
| generated_resource_utilities = rule( | ||
| implementation = _generate_resource_utilities_impl, | ||
| attrs = { | ||
| "resources": attr.label_keyed_string_dict(allow_files = True), | ||
| "out": attr.output(mandatory = True), | ||
| "_tool": attr.label( | ||
| default = Label("//dotnet/private:generate_resources_tool"), | ||
| executable = True, | ||
| cfg = "exec", | ||
nvborisenko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ), | ||
| }, | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| #!/usr/bin/env python3 | ||
| """Generate C# ResourceUtilities partial class with embedded JS resources. | ||
|
|
||
| Usage: | ||
| generate_resources_tool.py --output path/to/ResourceUtilities.g.cs \ | ||
| --input Ident1=path/to/file1.js \ | ||
| --input Ident2=path/to/file2.js ... | ||
|
|
||
| Each identifier becomes a const string in ResourceUtilities class. | ||
| The content is emitted as a C# raw string literal using 5-quotes. | ||
|
|
||
| TODO: | ||
| It would be nice to convert this small single-file utility to .NET10/C#, | ||
| so it would work like `dotnet run generate_resources.cs -- <args>`. | ||
| Meaning .NET developers can easily support it. | ||
| """ | ||
|
|
||
| import argparse | ||
| import os | ||
| import sys | ||
| from typing import List, Tuple | ||
|
|
||
|
|
||
| def parse_args(argv: List[str]) -> argparse.Namespace: | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument("--output", required=True) | ||
| parser.add_argument("--input", action="append", default=[], help="IDENT=path") | ||
| return parser.parse_args(argv) | ||
|
|
||
|
|
||
| def parse_input_spec(spec: str) -> Tuple[str, str]: | ||
| if "=" not in spec: | ||
| raise ValueError(f"Invalid --input value, expected IDENT=path, got: {spec}") | ||
| ident, path = spec.split("=", 1) | ||
| ident = ident.strip() | ||
| path = path.strip() | ||
| if not ident: | ||
| raise ValueError(f"Empty identifier in --input value: {spec}") | ||
| if not path: | ||
| raise ValueError(f"Empty path in --input value: {spec}") | ||
| return ident, path | ||
|
|
||
|
|
||
| def generate(output: str, inputs: List[Tuple[str, str]]) -> None: | ||
| props: List[str] = [] | ||
| for prop_name, path in inputs: | ||
| with open(path, "r", encoding="utf-8") as f: | ||
| content = f.read() | ||
| # Use a C# raw string literal with five quotes. For a valid raw | ||
| # literal, the content must start on a new line and the closing | ||
| # quotes must be on their own line as well. We assume the content | ||
| # does not contain a sequence of five consecutive double quotes. | ||
| # | ||
| # Resulting C# will look like: | ||
| # """"" | ||
| # <content> | ||
| # """"" | ||
| literal = '"""""\n' + content + '\n"""""' | ||
| props.append(f" internal const string {prop_name} = {literal};") | ||
|
|
||
| lines: List[str] = [] | ||
| lines.append("// <auto-generated />") | ||
| lines.append("namespace OpenQA.Selenium.Internal;") | ||
| lines.append("") | ||
| lines.append("internal static partial class ResourceUtilities") | ||
| lines.append("{") | ||
| for p in props: | ||
| lines.append(p) | ||
| lines.append("}") | ||
| lines.append("") | ||
|
|
||
| os.makedirs(os.path.dirname(output), exist_ok=True) | ||
| with open(output, "w", encoding="utf-8", newline="\n") as f: | ||
| f.write("\n".join(lines)) | ||
|
|
||
|
|
||
| def main(argv: List[str]) -> int: | ||
| args = parse_args(argv) | ||
| inputs: List[Tuple[str, str]] = [] | ||
| for spec in args.input: | ||
| ident, path = parse_input_spec(spec) | ||
| inputs.append((ident, path)) | ||
| generate(args.output, inputs) | ||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| raise SystemExit(main(sys.argv[1:])) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.