diff --git a/MODULE.bazel b/MODULE.bazel index ccfd0ab4..7d362b9d 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -76,7 +76,8 @@ scala_deps.toolchains( ) ##### Browsers dependencies ##### -browser_repositories = use_extension("//web/versioned:browsers-0.3.3.bzl", "browser_repositories_extension") +browser_repositories = use_extension("//web:extension.bzl", "browser_repositories_extension") +browser_repositories.version(version = "0.3.3") use_repo( browser_repositories, "com_saucelabs_sauce_connect_linux_x64", @@ -87,7 +88,7 @@ use_repo( "org_chromium_chromedriver_macos_x64", "org_chromium_chromedriver_windows_x64", "org_chromium_chromium_linux_x64", - "org_chromium_chromium_macos_arm64", + "org_chromium_chromium_macos_arm64", # Only available in 0.3.3 "org_chromium_chromium_macos_x64", "org_chromium_chromium_windows_x64", "org_mozilla_firefox_linux_x64", diff --git a/web/extension.bzl b/web/extension.bzl new file mode 100644 index 00000000..b5c4b020 --- /dev/null +++ b/web/extension.bzl @@ -0,0 +1,84 @@ +# Copyright 2024 Google Inc. +# +# 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. +"""Module extension for introducing browsers dependencies.""" + +load("//web/versioned:browsers-0.3.1.bzl", browser_repositories_0_3_1 = "browser_repositories") +load("//web/versioned:browsers-0.3.2.bzl", browser_repositories_0_3_2 = "browser_repositories") +load("//web/versioned:browsers-0.3.3.bzl", browser_repositories_0_3_3 = "browser_repositories") +load("//web/versioned:browsers-0.3.4.bzl", browser_repositories_0_3_4 = "browser_repositories") + +browser_versions = { + "0.3.1": browser_repositories_0_3_1, + "0.3.2": browser_repositories_0_3_2, + "0.3.3": browser_repositories_0_3_3, + "0.3.4": browser_repositories_0_3_4, +} + +def parse_version(version): + if not version: + return [0, 0, 0] + return [int(s) for s in version.split(".")] + +def _browser_repositories_extension(ctx): + version = None + + # Get the override version from the root module if specified. + root_module = ctx.modules[0] + if len(root_module.tags.override_version) > 1: + fail("Only one override_version tag is allowed in the root module.") + override_version = root_module.tags.override_version[0].version if root_module.tags.override_version else None + + if override_version: + version = override_version + else: + # Go through modules in the dependency graph and choose the highest version. + for mod in ctx.modules: + for tag in mod.tags.version: + if parse_version(tag.version) > parse_version(version): + version = tag.version + + if not version: + fail("No version of browser_repositories is specified in the dependency graph, supported versions are %s." % ', '.join(browser_versions.keys())) + + browser_repositories = browser_versions.get(version) + if not browser_repositories: + fail("Unsupported version %s of browser_repositories, supported versions are %s." % (version, ', '.join(browser_versions.keys()))) + + browser_repositories() + + # Mark this module extension as reproducible, so it doesn't appear in lockfile. + return ctx.extension_metadata(reproducible = True) + +# The `version` tag can be used by any module to specify the version of browsers version. +# If multiple versions are specified, the highest one is chosen. +version = tag_class( + attrs = { + "version": attr.string(mandatory = True), + }, +) + +# The `override_version` tag can be used by root module to override the version of browsers version. +override_version = tag_class( + attrs = { + "version": attr.string(mandatory = True), + }, +) + +browser_repositories_extension = module_extension( + implementation = _browser_repositories_extension, + tag_classes = { + "version": version, + "override_version": override_version, + } +) diff --git a/web/versioned/browsers-0.3.1.bzl b/web/versioned/browsers-0.3.1.bzl index 9c7fca20..a9788491 100644 --- a/web/versioned/browsers-0.3.1.bzl +++ b/web/versioned/browsers-0.3.1.bzl @@ -15,22 +15,13 @@ load("//web:web.bzl", "platform_archive") -def browser_repositories(firefox = False, chromium = False, sauce = False): - """Sets up repositories for browsers defined in //browsers/.... - - Args: - firefox: Configure repositories for //browsers:firefox-native. - chromium: Configure repositories for //browsers:chromium-native. - sauce: Configure repositories for //browser/sauce:chrome-win10-connect. - """ - if chromium: - org_chromium_chromedriver() - org_chromium_chromium() - if firefox: - org_mozilla_firefox() - org_mozilla_geckodriver() - if sauce: - com_saucelabs_sauce_connect() +def browser_repositories(): + """Sets up repositories for browsers defined in //browsers/.... """ + org_chromium_chromedriver() + org_chromium_chromium() + org_mozilla_firefox() + org_mozilla_geckodriver() + com_saucelabs_sauce_connect() def com_saucelabs_sauce_connect(): platform_archive( @@ -203,10 +194,3 @@ def org_mozilla_geckodriver(): "GECKODRIVER": "geckodriver", }, ) - -def _browser_repositories_extension(ctx): - # TODO: This can be configured in MODULE.bazel via module extension tags. - browser_repositories(firefox = True, chromium = True, sauce = True) - return ctx.extension_metadata(reproducible = True) - -browser_repositories_extension = module_extension(implementation = _browser_repositories_extension) diff --git a/web/versioned/browsers-0.3.2.bzl b/web/versioned/browsers-0.3.2.bzl index 9555e588..7ce70921 100644 --- a/web/versioned/browsers-0.3.2.bzl +++ b/web/versioned/browsers-0.3.2.bzl @@ -15,22 +15,13 @@ load("//web:web.bzl", "platform_archive") -def browser_repositories(firefox = False, chromium = False, sauce = False): - """Sets up repositories for browsers defined in //browsers/.... - - Args: - firefox: Configure repositories for //browsers:firefox-native. - chromium: Configure repositories for //browsers:chromium-native. - sauce: Configure repositories for //browser/sauce:chrome-win10-connect. - """ - if chromium: - org_chromium_chromedriver() - org_chromium_chromium() - if firefox: - org_mozilla_firefox() - org_mozilla_geckodriver() - if sauce: - com_saucelabs_sauce_connect() +def browser_repositories(): + """Sets up repositories for browsers defined in //browsers/.... """ + org_chromium_chromedriver() + org_chromium_chromium() + org_mozilla_firefox() + org_mozilla_geckodriver() + com_saucelabs_sauce_connect() def com_saucelabs_sauce_connect(): platform_archive( @@ -207,10 +198,3 @@ def org_mozilla_geckodriver(): "GECKODRIVER": "geckodriver", }, ) - -def _browser_repositories_extension(ctx): - # TODO: This can be configured in MODULE.bazel via module extension tags. - browser_repositories(firefox = True, chromium = True, sauce = True) - return ctx.extension_metadata(reproducible = True) - -browser_repositories_extension = module_extension(implementation = _browser_repositories_extension) diff --git a/web/versioned/browsers-0.3.3.bzl b/web/versioned/browsers-0.3.3.bzl index a05cf0e1..389e1df8 100644 --- a/web/versioned/browsers-0.3.3.bzl +++ b/web/versioned/browsers-0.3.3.bzl @@ -15,22 +15,13 @@ load("//web:web.bzl", "platform_archive") -def browser_repositories(firefox = False, chromium = False, sauce = False): - """Sets up repositories for browsers defined in //browsers/.... - - Args: - firefox: Configure repositories for //browsers:firefox-native. - chromium: Configure repositories for //browsers:chromium-native. - sauce: Configure repositories for //browser/sauce:chrome-win10-connect. - """ - if chromium: - org_chromium_chromedriver() - org_chromium_chromium() - if firefox: - org_mozilla_firefox() - org_mozilla_geckodriver() - if sauce: - com_saucelabs_sauce_connect() +def browser_repositories(): + """Sets up repositories for browsers defined in //browsers/.... """ + org_chromium_chromedriver() + org_chromium_chromium() + org_mozilla_firefox() + org_mozilla_geckodriver() + com_saucelabs_sauce_connect() def com_saucelabs_sauce_connect(): platform_archive( @@ -276,10 +267,3 @@ def org_mozilla_geckodriver(): "GECKODRIVER": "geckodriver", }, ) - -def _browser_repositories_extension(ctx): - # TODO: This can be configured in MODULE.bazel via module extension tags. - browser_repositories(firefox = True, chromium = True, sauce = True) - return ctx.extension_metadata(reproducible = True) - -browser_repositories_extension = module_extension(implementation = _browser_repositories_extension) diff --git a/web/versioned/browsers-0.3.4.bzl b/web/versioned/browsers-0.3.4.bzl index e3ada859..b409ed6b 100644 --- a/web/versioned/browsers-0.3.4.bzl +++ b/web/versioned/browsers-0.3.4.bzl @@ -15,22 +15,13 @@ load("//web:web.bzl", "platform_archive") -def browser_repositories(firefox = False, chromium = False, sauce = False): - """Sets up repositories for browsers defined in //browsers/.... - - Args: - firefox: Configure repositories for //browsers:firefox-native. - chromium: Configure repositories for //browsers:chromium-native. - sauce: Configure repositories for //browser/sauce:chrome-win10-connect. - """ - if chromium: - org_chromium_chromedriver() - org_chromium_chromium() - if firefox: - org_mozilla_firefox() - org_mozilla_geckodriver() - if sauce: - com_saucelabs_sauce_connect() +def browser_repositories(): + """Sets up repositories for browsers defined in //browsers/.... """ + org_chromium_chromedriver() + org_chromium_chromium() + org_mozilla_firefox() + org_mozilla_geckodriver() + com_saucelabs_sauce_connect() def com_saucelabs_sauce_connect(): platform_archive( @@ -250,10 +241,3 @@ def org_mozilla_geckodriver(): "GECKODRIVER": "geckodriver", }, ) - -def _browser_repositories_extension(ctx): - # TODO: This can be configured in MODULE.bazel via module extension tags. - browser_repositories(firefox = True, chromium = True, sauce = True) - return ctx.extension_metadata(reproducible = True) - -browser_repositories_extension = module_extension(implementation = _browser_repositories_extension)