diff --git a/WORKSPACE b/WORKSPACE index d01dc2c32..fa678e3dd 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -39,7 +39,7 @@ load("@io_bazel_rules_scala//:scala_config.bzl", "scala_config") scala_config(enable_compiler_dependency_tracking = True) -load("//scala:scala.bzl", "scala_toolchains") +load("//scala:toolchains.bzl", "scala_toolchains") scala_toolchains( fetch_sources = True, diff --git a/docs/testing.md b/docs/testing.md index e620a45b8..20a0909b7 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -31,7 +31,10 @@ Test framework dependencies are configured via testing toolchain. For convenienc `setup_scala_testing_toolchain` can be used to define such toolchains. ```starlark -load("@io_bazel_rules_scala//scala:scala.bzl", "setup_scala_testing_toolchain") +load( + "@io_bazel_rules_scala//testing:testing.bzl", + "setup_scala_testing_toolchain", +) ``` Attributes @@ -53,7 +56,10 @@ Examples (assumes maven deps are managed with rules_jvm_external): ```starlark # BUILD -load("@io_bazel_rules_scala//scala:scala.bzl", "setup_scala_testing_toolchain") +load( + "@io_bazel_rules_scala//testing:testing.bzl", + "setup_scala_testing_toolchain", +) setup_scala_testing_toolchain( name = "scalatest_toolchain", @@ -77,7 +83,10 @@ register_toolchains('//:scalatest_toolchain') #### JUnit 4 ```starlark # BUILD -load("@io_bazel_rules_scala//scala:scala.bzl", "setup_scala_testing_toolchain") +load( + "@io_bazel_rules_scala//testing:testing.bzl", + "setup_scala_testing_toolchain", +) setup_scala_testing_toolchain( name = "junit_toolchain", @@ -98,7 +107,10 @@ For Specs2 rules to work, `junit_classpath`, `specs2_junit_classpath` and `specs be configured. ```starlark # BUILD -load("@io_bazel_rules_scala//scala:scala.bzl", "setup_scala_testing_toolchain") +load( + "@io_bazel_rules_scala//testing:testing.bzl", + "setup_scala_testing_toolchain", +) setup_scala_testing_toolchain( name = "specs2_toolchain", @@ -122,4 +134,4 @@ Register the toolchain ```starlark # WORKSPACE register_toolchains('//:specs2_toolchain') -``` \ No newline at end of file +``` diff --git a/dt_patches/test_dt_patches/WORKSPACE b/dt_patches/test_dt_patches/WORKSPACE index cac85b0d6..a3b7df676 100644 --- a/dt_patches/test_dt_patches/WORKSPACE +++ b/dt_patches/test_dt_patches/WORKSPACE @@ -31,7 +31,7 @@ load("@io_bazel_rules_scala//:scala_config.bzl", "scala_config") scala_config(enable_compiler_dependency_tracking = True) -load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains") +load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_toolchains") load( "@io_bazel_rules_scala//scala:scala_cross_version.bzl", "default_maven_server_urls", diff --git a/dt_patches/test_dt_patches_user_srcjar/WORKSPACE b/dt_patches/test_dt_patches_user_srcjar/WORKSPACE index 278e44a5d..f587be3f2 100644 --- a/dt_patches/test_dt_patches_user_srcjar/WORKSPACE +++ b/dt_patches/test_dt_patches_user_srcjar/WORKSPACE @@ -31,7 +31,7 @@ load("@io_bazel_rules_scala//:scala_config.bzl", "scala_config") scala_config(enable_compiler_dependency_tracking = True) -load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains") +load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_toolchains") load( "@io_bazel_rules_scala//scala:scala_cross_version.bzl", "default_maven_server_urls", diff --git a/examples/crossbuild/WORKSPACE b/examples/crossbuild/WORKSPACE index d61c31e81..182bc8837 100644 --- a/examples/crossbuild/WORKSPACE +++ b/examples/crossbuild/WORKSPACE @@ -38,7 +38,7 @@ scala_config( ], ) -load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains") +load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_toolchains") scala_toolchains(scalatest = True) diff --git a/examples/scala3/WORKSPACE b/examples/scala3/WORKSPACE index a98255c50..fd362c89a 100644 --- a/examples/scala3/WORKSPACE +++ b/examples/scala3/WORKSPACE @@ -31,7 +31,7 @@ load("@io_bazel_rules_scala//:scala_config.bzl", "scala_config") scala_config(scala_version = "3.5.2") -load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains") +load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_toolchains") scala_toolchains(fetch_sources = True) diff --git a/examples/semanticdb/WORKSPACE b/examples/semanticdb/WORKSPACE index 72ae37bd5..0e99dfe95 100644 --- a/examples/semanticdb/WORKSPACE +++ b/examples/semanticdb/WORKSPACE @@ -34,7 +34,7 @@ load("@io_bazel_rules_scala//:scala_config.bzl", "scala_config") scala_config(scala_version = "2.13.15") -load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains") +load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_toolchains") scala_toolchains(fetch_sources = True) diff --git a/examples/testing/multi_frameworks_toolchain/BUILD b/examples/testing/multi_frameworks_toolchain/BUILD index b948c8ee2..27eedcf7e 100644 --- a/examples/testing/multi_frameworks_toolchain/BUILD +++ b/examples/testing/multi_frameworks_toolchain/BUILD @@ -1,4 +1,7 @@ -load("@io_bazel_rules_scala//scala:scala.bzl", "setup_scala_testing_toolchain") +load( + "@io_bazel_rules_scala//testing:testing.bzl", + "setup_scala_testing_toolchain", +) setup_scala_testing_toolchain( name = "testing_toolchain", diff --git a/examples/testing/multi_frameworks_toolchain/WORKSPACE b/examples/testing/multi_frameworks_toolchain/WORKSPACE index e0e72888d..631722cac 100644 --- a/examples/testing/multi_frameworks_toolchain/WORKSPACE +++ b/examples/testing/multi_frameworks_toolchain/WORKSPACE @@ -31,7 +31,7 @@ load("@io_bazel_rules_scala//:scala_config.bzl", "scala_config") scala_config() -load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains") +load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_toolchains") scala_toolchains( fetch_sources = True, diff --git a/examples/testing/scalatest_repositories/WORKSPACE b/examples/testing/scalatest_repositories/WORKSPACE index 648fa7f61..29b598730 100644 --- a/examples/testing/scalatest_repositories/WORKSPACE +++ b/examples/testing/scalatest_repositories/WORKSPACE @@ -31,7 +31,7 @@ load("@io_bazel_rules_scala//:scala_config.bzl", "scala_config") scala_config() -load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains") +load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_toolchains") scala_toolchains( fetch_sources = True, diff --git a/examples/testing/specs2_junit_repositories/WORKSPACE b/examples/testing/specs2_junit_repositories/WORKSPACE index 7a4ec73ee..5a1c4012c 100644 --- a/examples/testing/specs2_junit_repositories/WORKSPACE +++ b/examples/testing/specs2_junit_repositories/WORKSPACE @@ -31,7 +31,7 @@ load("@io_bazel_rules_scala//:scala_config.bzl", "scala_config") scala_config() -load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains") +load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_toolchains") scala_toolchains( fetch_sources = True, diff --git a/scala/private/macros/toolchains.bzl b/scala/private/macros/toolchains.bzl deleted file mode 100644 index 3a851ae22..000000000 --- a/scala/private/macros/toolchains.bzl +++ /dev/null @@ -1,128 +0,0 @@ -"""Macro to instantiate @io_bazel_rules_scala_toolchains""" - -load(":macros/toolchains_repo.bzl", "scala_toolchains_repo") -load("//junit:junit.bzl", "junit_artifact_ids") -load("//scala/private:macros/scala_repositories.bzl", "scala_repositories") -load("//scala:scala_cross_version.bzl", "default_maven_server_urls") -load("//scalatest:scalatest.bzl", "scalatest_artifact_ids") -load("//specs2:specs2.bzl", "specs2_artifact_ids") -load("//specs2:specs2_junit.bzl", "specs2_junit_artifact_ids") -load("//third_party/repositories:repositories.bzl", "repositories") -load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSIONS") - -def scala_toolchains( - maven_servers = default_maven_server_urls(), - overridden_artifacts = {}, - load_rules_scala_dependencies = True, - load_scala_toolchain_dependencies = True, - fetch_sources = False, - validate_scala_version = True, - scala_compiler_srcjars = {}, - scalatest = False, - junit = False, - specs2 = False, - testing = False): - """Instantiates @io_bazel_rules_scala_toolchains and all its dependencies. - - Provides a unified interface to configuring rules_scala both directly in a - `WORKSPACE` file and in a Bazel module extension. - - Instantiates the `@io_bazel_rules_scala_toolchains` repository. Under - `WORKSPACE`, you will need to call `register_toolchains` at some point. - Under Bzlmod, rules_scala does this automatically. - - ```starlark - register_toolchains("@io_bazel_rules_scala_toolchains//...:all") - ``` - - All arguments are optional. - - Args: - maven_servers: Maven servers used to fetch dependency jar files - overridden_artifacts: specific dependency jar files to use instead of - those from `maven_servers`, in the format: - ```starlark - "repo_name": { - "artifact": "", - "sha256": "", - "deps": [ - "repository_names_of_dependencies", - ], - } - ``` - load_rules_scala_dependencies: whether load rules_scala repository - dependencies - load_scala_toolchain_dependencies: whether to load repository - dependencies of the core Scala language toolchain - fetch_sources: whether to download dependency source jars - validate_scala_version: whether to check if the configured Scala version - matches the default version supported by rules_scala - scala_compiler_srcjars: optional dictionary of Scala version string to - compiler srcjar metadata dictionaries containing: - - exactly one "label", "url", or "urls" key - - optional "integrity" or "sha256" keys - scalatest: whether to instantiate the Scalatest toolchain - junit: whether to instantiate the JUnit toolchain - specs2: whether to instantiate the Specs2 JUnit toolchain - testing: whether to instantiate the Scalatest, JUnit, and Specs2 JUnit - toolchains combined - """ - scala_repositories( - maven_servers = maven_servers, - # Note the internal macro parameter misspells "overriden". - overriden_artifacts = overridden_artifacts, - load_dep_rules = load_rules_scala_dependencies, - load_jar_deps = load_scala_toolchain_dependencies, - fetch_sources = fetch_sources, - validate_scala_version = validate_scala_version, - scala_compiler_srcjars = scala_compiler_srcjars, - ) - - if testing: - scalatest = True - junit = True - specs2 = True - if specs2: - junit = True - - artifact_ids_to_fetch_sources = {} - - if scalatest: - artifact_ids_to_fetch_sources.update({ - id: True - for id in scalatest_artifact_ids() - }) - if junit: - artifact_ids_to_fetch_sources.update({ - id: True - for id in junit_artifact_ids() - }) - if specs2: - artifact_ids_to_fetch_sources.update({ - id: True - for id in specs2_artifact_ids() + specs2_junit_artifact_ids() - }) - - for scala_version in SCALA_VERSIONS: - version_specific_artifact_ids = {} - - all_artifacts = ( - artifact_ids_to_fetch_sources | version_specific_artifact_ids - ) - - repositories( - scala_version = scala_version, - for_artifact_ids = all_artifacts.keys(), - maven_servers = maven_servers, - fetch_sources = fetch_sources, - fetch_sources_by_id = all_artifacts, - overriden_artifacts = overridden_artifacts, - validate_scala_version = validate_scala_version, - ) - - scala_toolchains_repo( - scalatest = scalatest, - junit = junit, - specs2 = specs2, - testing = testing, - ) diff --git a/scala/private/macros/toolchains_repo.bzl b/scala/private/macros/toolchains_repo.bzl index 6c977dcf7..8234d5c94 100644 --- a/scala/private/macros/toolchains_repo.bzl +++ b/scala/private/macros/toolchains_repo.bzl @@ -120,12 +120,15 @@ load( """ _TESTING_TOOLCHAIN_BUILD = """ -load("@@{rules_scala_repo}//scala:scala.bzl", "setup_scala_testing_toolchain") load("@@{rules_scala_repo}//scala:scala_cross_version.bzl", "version_suffix") load( "@@{rules_scala_repo}//testing:deps.bzl", "{deps_symbols}", ) +load( + "@@{rules_scala_repo}//testing:testing.bzl", + "setup_scala_testing_toolchain", +) load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSIONS") [ diff --git a/scala/scala.bzl b/scala/scala.bzl index 54588ecc8..3e8d5187e 100644 --- a/scala/scala.bzl +++ b/scala/scala.bzl @@ -2,10 +2,6 @@ load( "//specs2:specs2_junit.bzl", _specs2_junit_dependencies = "specs2_junit_dependencies", ) -load( - "//scala/private:macros/toolchains.bzl", - _scala_toolchains = "scala_toolchains", -) load( "//scala/private:macros/toolchains_repo.bzl", _scala_toolchains_repo = "scala_toolchains_repo", @@ -50,10 +46,6 @@ load( _scala_test = "scala_test", _scala_test_suite = "scala_test_suite", ) -load( - "//testing:testing.bzl", - _setup_scala_testing_toolchain = "setup_scala_testing_toolchain", -) def scala_specs2_junit_test(name, **kwargs): _scala_junit_test( @@ -87,7 +79,5 @@ rules_scala_setup = _rules_scala_setup rules_scala_toolchain_deps_repositories = _rules_scala_toolchain_deps_repositories scala_test = _scala_test scala_test_suite = _scala_test_suite -setup_scala_testing_toolchain = _setup_scala_testing_toolchain setup_scala_toolchain = _setup_scala_toolchain -scala_toolchains = _scala_toolchains scala_toolchains_repo = _scala_toolchains_repo diff --git a/scala/toolchains.bzl b/scala/toolchains.bzl index eebdb7453..a3a14e1e9 100644 --- a/scala/toolchains.bzl +++ b/scala/toolchains.bzl @@ -1,3 +1,132 @@ +"""Macros to instantiate and register @io_bazel_rules_scala_toolchains""" + +load("//junit:junit.bzl", "junit_artifact_ids") +load("//scala/private:macros/scala_repositories.bzl", "scala_repositories") +load("//scala/private:macros/toolchains_repo.bzl", "scala_toolchains_repo") +load("//scala:scala_cross_version.bzl", "default_maven_server_urls") +load("//scalatest:scalatest.bzl", "scalatest_artifact_ids") +load("//specs2:specs2.bzl", "specs2_artifact_ids") +load("//specs2:specs2_junit.bzl", "specs2_junit_artifact_ids") +load("//third_party/repositories:repositories.bzl", "repositories") +load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSIONS") + +def scala_toolchains( + maven_servers = default_maven_server_urls(), + overridden_artifacts = {}, + load_rules_scala_dependencies = True, + load_scala_toolchain_dependencies = True, + fetch_sources = False, + validate_scala_version = True, + scala_compiler_srcjars = {}, + scalatest = False, + junit = False, + specs2 = False, + testing = False): + """Instantiates @io_bazel_rules_scala_toolchains and all its dependencies. + + Provides a unified interface to configuring rules_scala both directly in a + `WORKSPACE` file and in a Bazel module extension. + + Instantiates the `@io_bazel_rules_scala_toolchains` repository. Under + `WORKSPACE`, you will need to call `register_toolchains` at some point. + Under Bzlmod, rules_scala does this automatically. + + ```starlark + register_toolchains("@io_bazel_rules_scala_toolchains//...:all") + ``` + + All arguments are optional. + + Args: + maven_servers: Maven servers used to fetch dependency jar files + overridden_artifacts: specific dependency jar files to use instead of + those from `maven_servers`, in the format: + ```starlark + "repo_name": { + "artifact": "", + "sha256": "", + "deps": [ + "repository_names_of_dependencies", + ], + } + ``` + load_rules_scala_dependencies: whether load rules_scala repository + dependencies + load_scala_toolchain_dependencies: whether to load repository + dependencies of the core Scala language toolchain + fetch_sources: whether to download dependency source jars + validate_scala_version: whether to check if the configured Scala version + matches the default version supported by rules_scala + scala_compiler_srcjars: optional dictionary of Scala version string to + compiler srcjar metadata dictionaries containing: + - exactly one "label", "url", or "urls" key + - optional "integrity" or "sha256" keys + scalatest: whether to instantiate the Scalatest toolchain + junit: whether to instantiate the JUnit toolchain + specs2: whether to instantiate the Specs2 JUnit toolchain + testing: whether to instantiate the Scalatest, JUnit, and Specs2 JUnit + toolchains combined + """ + scala_repositories( + maven_servers = maven_servers, + # Note the internal macro parameter misspells "overriden". + overriden_artifacts = overridden_artifacts, + load_dep_rules = load_rules_scala_dependencies, + load_jar_deps = load_scala_toolchain_dependencies, + fetch_sources = fetch_sources, + validate_scala_version = validate_scala_version, + scala_compiler_srcjars = scala_compiler_srcjars, + ) + + if testing: + scalatest = True + junit = True + specs2 = True + if specs2: + junit = True + + artifact_ids_to_fetch_sources = {} + + if scalatest: + artifact_ids_to_fetch_sources.update({ + id: True + for id in scalatest_artifact_ids() + }) + if junit: + artifact_ids_to_fetch_sources.update({ + id: True + for id in junit_artifact_ids() + }) + if specs2: + artifact_ids_to_fetch_sources.update({ + id: True + for id in specs2_artifact_ids() + specs2_junit_artifact_ids() + }) + + for scala_version in SCALA_VERSIONS: + version_specific_artifact_ids = {} + + all_artifacts = ( + artifact_ids_to_fetch_sources | version_specific_artifact_ids + ) + + repositories( + scala_version = scala_version, + for_artifact_ids = all_artifacts.keys(), + maven_servers = maven_servers, + fetch_sources = fetch_sources, + fetch_sources_by_id = all_artifacts, + overriden_artifacts = overridden_artifacts, + validate_scala_version = validate_scala_version, + ) + + scala_toolchains_repo( + scalatest = scalatest, + junit = junit, + specs2 = specs2, + testing = testing, + ) + def scala_register_toolchains(): native.register_toolchains("@io_bazel_rules_scala_toolchains//...:all") diff --git a/test_cross_build/WORKSPACE b/test_cross_build/WORKSPACE index 6aa0c6473..c21b83d0c 100644 --- a/test_cross_build/WORKSPACE +++ b/test_cross_build/WORKSPACE @@ -72,7 +72,7 @@ scala_config( ) # loads other rules Rules Scala depends on -load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains") +load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_toolchains") scala_toolchains( scalatest = True, diff --git a/test_version/WORKSPACE.template b/test_version/WORKSPACE.template index c16c9ff68..eaeff1ba0 100644 --- a/test_version/WORKSPACE.template +++ b/test_version/WORKSPACE.template @@ -59,7 +59,7 @@ scala_config(enable_compiler_dependency_tracking = True) load("@io_bazel_rules_scala//scala:scala_cross_version.bzl", "extract_major_version") -load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains") +load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_toolchains") scala_toolchains( fetch_sources = True, diff --git a/third_party/test/example_external_workspace/WORKSPACE b/third_party/test/example_external_workspace/WORKSPACE index 03b524665..58402d1bf 100644 --- a/third_party/test/example_external_workspace/WORKSPACE +++ b/third_party/test/example_external_workspace/WORKSPACE @@ -31,7 +31,7 @@ load("@io_bazel_rules_scala//:scala_config.bzl", "scala_config") scala_config() -load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains") +load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_toolchains") scala_toolchains( fetch_sources = True, diff --git a/third_party/test/proto/WORKSPACE b/third_party/test/proto/WORKSPACE index 12cb91cb3..332d8f446 100644 --- a/third_party/test/proto/WORKSPACE +++ b/third_party/test/proto/WORKSPACE @@ -31,7 +31,7 @@ load("@io_bazel_rules_scala//:scala_config.bzl", "scala_config") scala_config() -load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains") +load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_toolchains") scala_toolchains()