Skip to content

Commit f7708d8

Browse files
authored
fix: Allow names other than rules_playwright (#13)
Within the repository rule there were a number of places where rules_playwright was hard coded so that build rules and platform constraints from this repo could be referenced within the generated repositories. With this change, those hard codings are replaced with a command line flag to be passed to the CLI. For WORKSPACE dependencies the rules_playwright_cannonical_name will match the name used to download rules_playwright in the first place. For bzlmod the rules_playwright_cannonical_name is derived using bazel using `Label("rules_playwright").repo_name` Fixes #10
1 parent c1c0560 commit f7708d8

17 files changed

+75
-41
lines changed

examples/rules_js/.bazelrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
startup --host_jvm_args=-DBAZEL_TRACK_SOURCE_DIRECTORIES=1
22
common --enable_bzlmod
3-
common --@rules_playwright//:macos_version=14
3+
common --@mrmeku_rules_playwright//:macos_version=14
44
common --symlink_prefix=/

examples/rules_js/MODULE.bazel

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
bazel_dep(name = "rules_playwright", version = "0.0.0")
1+
bazel_dep(name = "rules_playwright", version = "0.0.0", repo_name = "mrmeku_rules_playwright")
22
local_path_override(
33
module_name = "rules_playwright",
44
path = "../..",
55
)
66

7-
playwright = use_extension("@rules_playwright//playwright:extensions.bzl", "playwright")
7+
playwright = use_extension("@mrmeku_rules_playwright//playwright:extensions.bzl", "playwright")
88
playwright.repo(
99
name = "playwright",
10+
browsers_json = "//:browsers.1.50.1.json",
1011
integrity_map = {
1112
"firefox-mac14-arm64": "sha256-+FLEjmC1ypnAI/DQY4uJhtx3kQPN4mX8LC0TWEt9x9Y=",
1213
"chromium-headless-shell-mac14-arm64": "sha256-pJnv5Nfost7BtlSlG1JPzpQHgzXlxerZHyDoV3/1NM0=",
1314
"webkit-mac14-arm64": "sha256-RiHUDBPVDRlnkgHXzaoeQeAxeSvMiWNOphl/GlEj5aA=",
1415
},
15-
browsers_json = "//:browsers.1.50.1.json",
1616
)
1717
use_repo(playwright, "playwright")
1818

examples/workspace/WORKSPACE.bazel

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
workspace(name = "workspace_example")
22

3-
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive", "http_file")
3+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
44

55
http_archive(
66
name = "aspect_rules_js",
@@ -29,7 +29,7 @@ load("@npm//:repositories.bzl", "npm_repositories")
2929
npm_repositories()
3030

3131
local_repository(
32-
name = "rules_playwright",
32+
name = "mrmeku_rules_playwright",
3333
path = "../..",
3434
)
3535

@@ -45,7 +45,7 @@ exports_files([
4545
url = "https://registry.npmjs.org/playwright-core/-/playwright-core-1.51.0.tgz",
4646
)
4747

48-
load("@rules_playwright//playwright:repositories.bzl", "define_browsers", "playwright_repository")
48+
load("@mrmeku_rules_playwright//playwright:repositories.bzl", "define_browsers", "playwright_repository")
4949

5050
define_browsers(
5151
name = "browsers_index",
@@ -58,8 +58,9 @@ fetch_browsers()
5858

5959
playwright_repository(
6060
name = "playwright",
61+
browsers_workspace_name_prefix = "browsers_index",
6162
playwright_version = "1.51.0",
62-
user_workspace_name = "browsers_index",
63+
rules_playwright_cannonical_name = "mrmeku_rules_playwright",
6364
)
6465

6566
# ###########################################################
@@ -68,6 +69,7 @@ playwright_repository(
6869

6970
playwright_repository(
7071
name = "playwright_2",
72+
browsers_workspace_name_prefix = "browsers_index",
7173
playwright_version_from = "//:package.json",
72-
user_workspace_name = "browsers_index",
74+
rules_playwright_cannonical_name = "mrmeku_rules_playwright",
7375
)

playwright/extensions.bzl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ def _extension_impl(module_ctx):
6060
"http-files",
6161
"--browser-json-path",
6262
get_browsers_json_path(module_ctx, repo.playwright_version, repo.browsers_json),
63-
"--workspace-name",
64-
name,
63+
"--browsers-workspace-name-prefix",
64+
repo.name,
6565
],
6666
)
6767
if result.return_code != 0:
@@ -91,7 +91,10 @@ def _extension_impl(module_ctx):
9191
name = name,
9292
playwright_version = repo.playwright_version,
9393
browsers_json = repo.browsers_json,
94-
user_workspace_name = name,
94+
browsers_workspace_name_prefix = name,
95+
# Map the apparant name of the module to the cannonical name
96+
# See https://bazel.build/external/module
97+
rules_playwright_cannonical_name = "@" + Label("rules_playwright").repo_name,
9598
)
9699

97100
playwright = module_extension(

playwright/private/cli/src/browser_targets.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ impl From<BrowserTarget> for HttpFile {
3434
}
3535

3636
pub fn get_browser_rules(
37-
workspace_name: &str,
37+
browsers_workspace_name_prefix: &str,
3838
browser_json_path: &PathBuf,
3939
) -> std::io::Result<Vec<BrowserTarget>> {
4040
let browsers_json = std::fs::read_to_string(browser_json_path)?;
@@ -97,7 +97,7 @@ pub fn get_browser_rules(
9797

9898
Some(BrowserTarget {
9999
http_file_workspace_name: format!(
100-
"{workspace_name}-{browser_name}-{platform_str}"
100+
"{browsers_workspace_name_prefix}-{browser_name}-{platform_str}"
101101
),
102102
http_file_path: template.replace("%s", revision),
103103
label: format!("{browser_name}-{platform_str}"),

playwright/private/cli/src/main.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ mod flags {
1717
cmd rules-playwright {
1818
cmd workspace {
1919
required --browser-json-path browser_json_path: PathBuf
20-
required --workspace-name workspace_name: String
20+
required --browsers-workspace-name-prefix browsers_workspace_name_prefix: String
21+
required --rules-playwright-cannonical-name rules_playwright_cannonical_name: String
2122
}
2223
cmd http-files {
2324
required --browser-json-path browser_json_path: PathBuf
24-
required --workspace-name workspace_name: String
25+
required --browsers-workspace-name-prefix browsers_workspace_name_prefix: String
2526
}
2627
cmd unzip {
2728
required --input-path input_path: PathBuf
@@ -46,12 +47,13 @@ pub fn main() -> std::io::Result<()> {
4647
flags::RulesPlaywrightCmd::Workspace(cmd) => {
4748
templates::write_workspace(
4849
&out_dir,
49-
get_browser_rules(&cmd.workspace_name, &cmd.browser_json_path)?,
50+
get_browser_rules(&cmd.browsers_workspace_name_prefix, &cmd.browser_json_path)?,
51+
&cmd.rules_playwright_cannonical_name,
5052
)?;
5153
}
5254
flags::RulesPlaywrightCmd::HttpFiles(cmd) => {
5355
let http_files: Vec<HttpFile> =
54-
get_browser_rules(&cmd.workspace_name, &cmd.browser_json_path)?
56+
get_browser_rules(&cmd.browsers_workspace_name_prefix, &cmd.browser_json_path)?
5557
.into_iter()
5658
.map(|b| b.into())
5759
.collect();

playwright/private/cli/src/templates/aliases.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ struct AliasTargetSelect {
4040
pub fn write_build_file(
4141
out_dir: &Path,
4242
root_targets: &HashMap<String, RootTarget>,
43+
rules_playwright_cannonical_name: &str,
4344
) -> io::Result<()> {
4445
let template = AliasBuildFileTemplate {
4546
alias_targets: root_targets
@@ -55,7 +56,7 @@ pub fn write_build_file(
5556
.iter()
5657
.map(|(platform, platform_target)| AliasTargetSelect {
5758
key: format!(
58-
"@rules_playwright//tools/platforms:{}",
59+
"@{rules_playwright_cannonical_name}//tools/platforms:{}",
5960
platform.base_name()
6061
),
6162
value: format!("//browsers:{}", platform_target.name),

playwright/private/cli/src/templates/browsers.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::browser_targets::BrowserTarget;
77
#[derive(Template)]
88
#[template(
99
source = r#"
10-
load("@rules_playwright//playwright:unzip_browser.bzl", "unzip_browser")
10+
load("@{{ rules_playwright_cannonical_name }}//playwright:unzip_browser.bzl", "unzip_browser")
1111
1212
package(default_visibility = ["//visibility:public"])
1313
@@ -24,15 +24,23 @@ unzip_browser(
2424
)]
2525
struct BrowsersBuildFileTemplate<'a> {
2626
browser_targets: &'a Vec<BrowserTarget>,
27+
rules_playwright_cannonical_name: &'a str,
2728
}
2829

29-
pub fn write_build_file(out_dir: &Path, browser_targets: &Vec<BrowserTarget>) -> io::Result<()> {
30+
pub fn write_build_file(
31+
out_dir: &Path,
32+
browser_targets: &Vec<BrowserTarget>,
33+
rules_playwright_cannonical_name: &str,
34+
) -> io::Result<()> {
3035
let browsers_dir = out_dir.join("browsers");
3136
fs::create_dir(browsers_dir)?;
3237
fs::write(
3338
out_dir.join("browsers/BUILD.bazel"),
34-
BrowsersBuildFileTemplate { browser_targets }
35-
.render()
36-
.unwrap(),
39+
BrowsersBuildFileTemplate {
40+
browser_targets,
41+
rules_playwright_cannonical_name,
42+
}
43+
.render()
44+
.unwrap(),
3745
)
3846
}

playwright/private/cli/src/templates/mod.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ mod aliases;
88
mod browsers;
99
mod root;
1010

11-
pub fn write_workspace(out_dir: &Path, browser_targets: Vec<BrowserTarget>) -> io::Result<()> {
12-
browsers::write_build_file(out_dir, &browser_targets)?;
11+
pub fn write_workspace(
12+
out_dir: &Path,
13+
browser_targets: Vec<BrowserTarget>,
14+
rules_playwright_cannonical_name: &str,
15+
) -> io::Result<()> {
16+
browsers::write_build_file(out_dir, &browser_targets, rules_playwright_cannonical_name)?;
1317

1418
let mut root_targets: HashMap<String, RootTarget> = HashMap::new();
1519
for target in browser_targets {
@@ -39,8 +43,8 @@ pub fn write_workspace(out_dir: &Path, browser_targets: Vec<BrowserTarget>) -> i
3943
);
4044
}
4145

42-
root::write_build_file(out_dir, &root_targets)?;
43-
aliases::write_build_file(out_dir, &root_targets)
46+
root::write_build_file(out_dir, &root_targets, rules_playwright_cannonical_name)?;
47+
aliases::write_build_file(out_dir, &root_targets, rules_playwright_cannonical_name)
4448
}
4549

4650
struct RootTarget {

playwright/private/cli/src/templates/root.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use super::RootTarget;
66
#[derive(Template)]
77
#[template(
88
source = r#"
9-
load("@rules_playwright//playwright:select_exec.bzl", "select_exec")
9+
load("@{{ rules_playwright_cannonical_name }}//playwright:select_exec.bzl", "select_exec")
1010
1111
package(default_visibility = ["//visibility:public"])
1212
{% for target in select_targets %}
@@ -25,6 +25,7 @@ select_exec(
2525
ext = "txt"
2626
)]
2727
struct RootBuildFileTemplate<'a> {
28+
rules_playwright_cannonical_name: &'a str,
2829
select_targets: &'a [SelectTarget],
2930
}
3031

@@ -41,6 +42,7 @@ struct SelectPlatformGroup {
4142
pub fn write_build_file(
4243
out_dir: &Path,
4344
root_targets: &HashMap<String, RootTarget>,
45+
rules_playwright_cannonical_name: &str,
4446
) -> io::Result<()> {
4547
let select_targets: Vec<SelectTarget> = root_targets
4648
.iter()
@@ -53,7 +55,10 @@ pub fn write_build_file(
5355
let name_label: String = platform_group.clone().into();
5456

5557
SelectPlatformGroup {
56-
name: format!("@rules_playwright//tools/platforms:{}", name_label),
58+
name: format!(
59+
"@{rules_playwright_cannonical_name}//tools/platforms:{}",
60+
name_label
61+
),
5762
alias_label: format!(
5863
"//aliases:{}_{}",
5964
root_target.name, platform_group_target.name
@@ -68,6 +73,7 @@ pub fn write_build_file(
6873
out_dir.join("BUILD.bazel"),
6974
RootBuildFileTemplate {
7075
select_targets: &select_targets,
76+
rules_playwright_cannonical_name,
7177
}
7278
.render()
7379
.unwrap(),

playwright/repositories.bzl

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,15 @@ def _playwright_repo_impl(ctx):
4848
"workspace",
4949
"--browser-json-path",
5050
get_browsers_json_path(ctx, playwright_version, ctx.attr.browsers_json),
51-
"--workspace-name",
52-
ctx.attr.user_workspace_name,
51+
"--browsers-workspace-name-prefix",
52+
ctx.attr.browsers_workspace_name_prefix,
53+
"--rules-playwright-cannonical-name",
54+
ctx.attr.rules_playwright_cannonical_name,
5355
],
5456
)
5557

5658
if result.return_code != 0:
57-
fail(ctx.attr.user_workspace_name, "workspace command failed", result.stdout, result.stderr)
59+
fail(ctx.attr.name, "workspace command failed", result.stdout, result.stderr)
5860

5961
playwright_repository = repository_rule(
6062
_playwright_repo_impl,
@@ -73,7 +75,14 @@ playwright_repository = repository_rule(
7375
allow_single_file = True,
7476
doc = "The browsers.json file to use. For example https://unpkg.com/playwright-core@1.51.0/browsers.json",
7577
),
76-
"user_workspace_name": attr.string(mandatory = True),
78+
"browsers_workspace_name_prefix": attr.string(
79+
mandatory = True,
80+
doc = "The namespace prefix used when defining browser workspace repositories.",
81+
),
82+
"rules_playwright_cannonical_name": attr.string(
83+
mandatory = True,
84+
doc = "The cannonical name given to the rules_playwright repository. See https://bazel.build/external/module",
85+
),
7786
},
7887
)
7988

@@ -84,8 +93,8 @@ def _define_browsers_impl(rctx):
8493
"http-files",
8594
"--browser-json-path",
8695
rctx.path(rctx.attr.browsers_json),
87-
"--workspace-name",
88-
rctx.name,
96+
"--browsers-workspace-name-prefix",
97+
rctx.attr.name,
8998
],
9099
)
91100
if result.return_code != 0:

run_gen.sh

Lines changed: 0 additions & 1 deletion
This file was deleted.

tools/release/BUILD.bazel

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ native_binary(
5959
name = "cli",
6060
src = select(
6161
{
62-
"@rules_playwright//tools/platforms:macos_x86_64": ":artifacts/cli-x86_64-apple-darwin",
63-
"@rules_playwright//tools/platforms:macos_arm64": ":artifacts/cli-arm64-apple-darwin",
64-
"@rules_playwright//tools/platforms:linux_x86_64": ":artifacts/cli-x86_64-unknown-linux-musl",
65-
"@rules_playwright//tools/platforms:linux_arm64": ":artifacts/cli-arm64-unknown-linux-musl",
62+
"//tools/platforms:macos_x86_64": ":artifacts/cli-x86_64-apple-darwin",
63+
"//tools/platforms:macos_arm64": ":artifacts/cli-arm64-apple-darwin",
64+
"//tools/platforms:linux_x86_64": ":artifacts/cli-x86_64-unknown-linux-musl",
65+
"//tools/platforms:linux_arm64": ":artifacts/cli-arm64-unknown-linux-musl",
6666
},
6767
),
6868
out = "cli",
0 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)