Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion common/repositories.bzl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# This file has been generated using `bazel run scripts:pinned_browsers`
# This file is generated by pinned_browsers.py. DO NOT EDIT!
# Regenerate with: bazel run //scripts:pinned_browsers

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("//common/private:deb_archive.bzl", "deb_archive")
Expand Down
5 changes: 5 additions & 0 deletions java/src/org/openqa/selenium/devtools/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ java_library(
java_binary(
name = "cdp-client-generator",
srcs = GENERATOR_SOURCES,
data = [
"//scripts:generated_note_template.txt",
"//scripts:license_header.txt",
],
main_class = "org.openqa.selenium.devtools.CdpClientGenerator",
visibility = [
"//java/src/org/openqa/selenium/devtools:__subpackages__",
Expand All @@ -92,6 +96,7 @@ java_binary(
"//java/src/org/openqa/selenium:core",
"//java/src/org/openqa/selenium/devtools:devtools-prototypes",
"//java/src/org/openqa/selenium/json",
"@rules_java//java/runfiles",
artifact("com.github.javaparser:javaparser-core"),
artifact("com.google.guava:guava"),
artifact("org.jspecify:jspecify"),
Expand Down
37 changes: 34 additions & 3 deletions java/src/org/openqa/selenium/devtools/CdpClientGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.body.TypeDeclaration;
import com.github.javaparser.ast.stmt.BlockStmt;
import com.google.devtools.build.runfiles.Runfiles;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand Down Expand Up @@ -59,6 +60,36 @@

public class CdpClientGenerator {

// Shared license + note text (see scripts/*.txt); prepended as a raw string because
// JavaParser's CompilationUnit#toString() can't carry a file-level leading comment.
private static final String FILE_HEADER = loadFileHeader();

private static String loadFileHeader() {
try {
Runfiles runfiles = Runfiles.preload().withSourceRepository("");
String license = readCommented(runfiles, "_main/scripts/license_header.txt", "// ");
String note =
readCommented(runfiles, "_main/scripts/generated_note_template.txt", "// ")
.replace("{generator}", "CdpClientGenerator.java")
Comment thread
qodo-code-review[bot] marked this conversation as resolved.
Outdated
.replace("{command}", "bazel build //java/src/org/openqa/selenium/devtools/...");
return license + "\n\n" + note + "\n\n";
Comment thread
titusfortner marked this conversation as resolved.
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

private static String readCommented(Runfiles runfiles, String rlocation, String prefix)
throws IOException {
String resolved = runfiles.rlocation(rlocation);
if (resolved == null) {
throw new IOException("Could not resolve runfile " + rlocation);
}
Path file = Paths.get(resolved);
return Files.readAllLines(file, UTF_8).stream()
.map(line -> line.isEmpty() ? prefix.strip() : prefix + line)
.collect(joining("\n"));
}
Comment thread
titusfortner marked this conversation as resolved.

public static void main(String[] args) throws IOException {
Path browserProtocol = Paths.get(args[0]);
Path jsProtocol = Paths.get(args[1]);
Expand Down Expand Up @@ -347,7 +378,7 @@ private void dumpMainClass(Path target) {
ensureFileDoesNotExists(commandFile);

try {
Files.write(commandFile, unit.toString().getBytes(UTF_8));
Files.write(commandFile, (FILE_HEADER + unit).getBytes(UTF_8));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Expand Down Expand Up @@ -430,7 +461,7 @@ public void dumpTo(Path target) {
ensureFileDoesNotExists(eventFile);

try {
Files.write(eventFile, unit.toString().getBytes(UTF_8));
Files.write(eventFile, (FILE_HEADER + unit).getBytes(UTF_8));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Expand Down Expand Up @@ -544,7 +575,7 @@ public void dumpTo(Path target) {
ensureFileDoesNotExists(typeFile);

try {
Files.write(typeFile, unit.toString().getBytes(UTF_8));
Files.write(typeFile, (FILE_HEADER + unit).getBytes(UTF_8));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Expand Down
9 changes: 9 additions & 0 deletions javascript/private/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
load("@rules_python//python:defs.bzl", "py_binary")
load("//common:defs.bzl", "copy_file")

copy_file(
name = "license_header_local",
src = "//scripts:license_header.txt",
out = "license_header.txt",
)

py_binary(
name = "gen_file",
srcs = [
"gen_file.py",
],
data = [":license_header_local"],
visibility = [
"//visibility:public",
],
deps = ["//scripts:generated_note"],
)

py_binary(
Expand Down
36 changes: 17 additions & 19 deletions javascript/private/gen_file.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import os
import sys
from pathlib import Path

_copyright = """/*
* Copyright 2011-2014 Software Freedom Conservancy
*
* Licensed under the Apache License, Version 2.0 (the \"License\");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an \"AS IS\" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
from scripts.generated_note import generated_note

# Shared license body, copied next to this file by BUILD.bazel β€” see scripts/license_header.txt.
_LICENSE_NOTICE = (Path(__file__).parent / "license_header.txt").read_text(encoding="utf-8").rstrip("\n")
_LICENSE_BLOCK = "/*\n" + "\n".join(f" * {line}".rstrip() for line in _LICENSE_NOTICE.split("\n")) + "\n */"

_copyright = f"""{_LICENSE_BLOCK}
"""

# One C-style block comment serves every output language (C++ and Java both accept it).
_REGEN_HINT = "bazel build the closure_lang_file target for this file, e.g. //javascript/chrome-driver:source"
_GENERATED_NOTE_RAW = generated_note("", "gen_file.py", _REGEN_HINT)
_GENERATED_NOTE_BLOCK = "/*\n" + "\n".join(f" * {line}" for line in _GENERATED_NOTE_RAW.split("\n")) + "\n */"
Comment thread
titusfortner marked this conversation as resolved.
Outdated


def get_atom_name(name):
# We had a todo here to convert camelCase and snake_case to BIG_SNAKE_CASE, but this code
Expand Down Expand Up @@ -81,7 +79,7 @@ def generate_header(file_name, out, js_map, just_declare, utf8):
out.write(
f"""{_copyright}

/* AUTO GENERATED - DO NOT EDIT BY HAND */
{_GENERATED_NOTE_BLOCK}
#ifndef {define_guard}
#define {define_guard}
{include_stddef}
Expand Down Expand Up @@ -125,7 +123,7 @@ def generate_cc_source(out, js_map, utf8):
out.write(
f"""{_copyright}

/* AUTO GENERATED - DO NOT EDIT BY HAND */
{_GENERATED_NOTE_BLOCK}

#include <stddef.h> // For NULL.
#include "atoms.h"
Expand Down Expand Up @@ -154,13 +152,13 @@ def generate_java_source(file_name, out, preamble, js_map):

out.write(_copyright)
out.write("\n")
out.write(_GENERATED_NOTE_BLOCK)
out.write("\n\n")
out.write(preamble)
out.write("")
out.write(
f"""
public enum {class_name} {{

// AUTO GENERATED - DO NOT EDIT BY HAND
"""
)

Expand Down
15 changes: 15 additions & 0 deletions javascript/selenium-webdriver/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,26 @@ load("//javascript/selenium-webdriver/private:generate_bidi.bzl", "generate_bidi

npm_link_all_packages(name = "node_modules")

# Copy the shared license + note next to the generator so it can read them as siblings.
copy_file(
name = "license_header_local",
src = "//scripts:license_header.txt",
out = "license_header.txt",
)

copy_file(
name = "generated_note_local",
src = "//scripts:generated_note_template.txt",
out = "generated_note_template.txt",
)

# Generator script that reads the merged CDDL spec and produces one .ts file per BiDi domain.
js_binary(
name = "generate_bidi_script",
data = [
"generate_bidi.mjs",
":generated_note_local",
":license_header_local",
":node_modules/cddl",
":node_modules/cddl2ts",
],
Expand Down
40 changes: 20 additions & 20 deletions javascript/selenium-webdriver/generate_bidi.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
import { parse } from 'cddl'
import { transform } from 'cddl2ts'
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
import { join, resolve } from 'node:path'
import { dirname, join, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { parseArgs } from 'node:util'

// ============================================================
Expand Down Expand Up @@ -708,23 +709,22 @@ function modelToEvents(model) {
// Code generation
// ============================================================

const LICENSE_HEADER = `\
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.`
// Shared license + note text, copied next to this script by BUILD.bazel β€” see scripts/*.txt.
const GENERATOR_DIR = dirname(fileURLToPath(import.meta.url))
const commentLines = (text) =>
text
.split('\n')
.map((line) => `// ${line}`.trimEnd())
.join('\n')

const LICENSE_HEADER = commentLines(readFileSync(join(GENERATOR_DIR, 'license_header.txt'), 'utf8').replace(/\n$/, ''))

const GENERATED_NOTE = commentLines(
readFileSync(join(GENERATOR_DIR, 'generated_note_template.txt'), 'utf8')
.replace('{generator}', 'generate_bidi.mjs')
.replace('{command}', 'bazel build //javascript/selenium-webdriver:create-bidi-src')
.trim(),
)

// ============================================================
// Type-map helpers for cross-domain import generation
Expand Down Expand Up @@ -791,9 +791,9 @@ function generateDomainFile({
specVersion,
typeNameToDomain,
}) {
const parts = [LICENSE_HEADER, '']
const parts = [LICENSE_HEADER, '', GENERATED_NOTE]

parts.push(`// Auto-generated from WebDriver BiDi CDDL spec (v${specVersion}) β€” DO NOT EDIT MANUALLY`)
parts.push(`// Built from the WebDriver BiDi CDDL spec (v${specVersion}).`)
parts.push(`// Source: https://github.com/w3c/webref/tree/main/ed/cddl`)
parts.push('')

Expand Down
18 changes: 17 additions & 1 deletion javascript/selenium-webdriver/lib/atoms/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
load("@aspect_rules_js//js:defs.bzl", "js_binary", "js_run_binary")
load("//common:defs.bzl", "copy_file")

copy_file(
name = "license_header_local",
src = "//scripts:license_header.txt",
out = "license_header.txt",
)

copy_file(
name = "generated_note_local",
src = "//scripts:generated_note_template.txt",
out = "generated_note_template.txt",
)

js_binary(
name = "make_atoms_module",
data = ["make-atoms-module.js"],
data = [
"make-atoms-module.js",
":generated_note_local",
":license_header_local",
],
entry_point = ":make-atoms-module.js",
)

Expand Down
21 changes: 20 additions & 1 deletion javascript/selenium-webdriver/lib/atoms/make-atoms-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,28 @@ if (process.argv.length < 3) {

const buffer = fs.readFileSync(process.argv[2])

// Shared license + note text, copied next to this file by BUILD.bazel β€” see scripts/*.txt.
const commentLines = (text) =>
text
.split('\n')
.map((line) => `// ${line}`.trimEnd())
.join('\n')
const LICENSE_HEADER = commentLines(
fs.readFileSync(path.join(__dirname, 'license_header.txt'), 'utf8').replace(/\n$/, ''),
)
const GENERATED_NOTE = commentLines(
fs
.readFileSync(path.join(__dirname, 'generated_note_template.txt'), 'utf8')
.replace('{generator}', 'make-atoms-module.js')
.replace('{command}', 'bazel build //javascript/selenium-webdriver/lib/atoms:all')
.trim(),
)

fs.writeFileSync(
process.argv[3],
`// GENERATED CODE - DO NOT EDIT
`${LICENSE_HEADER}

${GENERATED_NOTE}
module.exports = ${buffer.toString('utf8').trim()};
`,
)
8 changes: 8 additions & 0 deletions py/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -703,10 +703,18 @@ py_binary(
deps = [requirement("inflection")],
)

copy_file(
name = "license_header_local",
src = "//scripts:license_header.txt",
out = "license_header.txt",
)

py_binary(
name = "generate_bidi",
srcs = ["generate_bidi.py"],
data = [":license_header_local"],
srcs_version = "PY3",
deps = ["//scripts:generated_note"],
)

[generate_devtools(
Expand Down
Loading