diff --git a/deps/scala_deps.bzl b/deps/scala_deps.bzl index 6d407d46..90ae71d2 100644 --- a/deps/scala_deps.bzl +++ b/deps/scala_deps.bzl @@ -1,22 +1,26 @@ load("@rules_scala3//rules:scala.bzl", "scala_binary") def scala_deps( - name = "scala_deps", + name, + dependencies, + src, project_root = ".", scala_version = "3.5.1-RC1", - destination = "3rdparty", targets_dir_name = "jvm", targets_file_name = "BUILD"): + destination = native.package_name() + "/3rdparty" + scala_binary( name = name, main_class = "rules_scala3.deps.src.Deps", resources = ["//deps/src/templates:jar_artifact_callback"], scala = "//scala:bootstrap_3", visibility = ["//visibility:public"], - srcs = ["//deps/src:deps"], + srcs = ["//deps/src:deps", src], args = [ "--project-root=" + project_root, "--scala-version=" + scala_version, + "--dependencies=" + dependencies, "--destination=" + destination, "--targets-dir-name=" + targets_dir_name, "--targets-file-name=" + targets_file_name, diff --git a/deps/src/Deps.scala b/deps/src/Deps.scala index bf36405b..d7433700 100644 --- a/deps/src/Deps.scala +++ b/deps/src/Deps.scala @@ -1,37 +1,32 @@ package rules_scala3.deps.src import sbt.librarymanagement.syntax.* +import sbt.librarymanagement.{DependencyBuilders, ModuleID, Resolver}, DependencyBuilders.OrganizationArtifactName +import scala.util.Try +import scala.reflect.runtime.universe._ object Deps: def main(args: Array[String]): Unit = given Vars = Vars(args.toIndexedSeq).getOrElse(sys.exit(2)) - - val jmhV = "1.37" - given DepsCfg = DepsCfg( - resolvers = Vector( - "mavencentral".at("https://repo1.maven.org/maven2/"), - "sonatype releases".at("https://oss.sonatype.org/service/local/repositories/releases/content"), - "apache staging".at("https://repository.apache.org/content/repositories/staging"), - "apache snapshots".at("https://repository.apache.org/snapshots"), - "google".at("https://maven.google.com/"), - "jitsi-maven-repository".at("https://github.com/jitsi/jitsi-maven-repository/raw/master/releases") - ), + val dependenciesClassName = summon[Vars].dependencies + val dependenciesClass = getDependenciesClass(name = dependenciesClassName) + + val resolversField = dependenciesClass.getMethod("resolvers").invoke(null).asInstanceOf[Vector[Resolver]] + val replacementsField = dependenciesClass.getMethod("replacements").invoke(null).asInstanceOf[Map[OrganizationArtifactName, String]] + val dependenciesField = dependenciesClass.getMethod("dependencies").invoke(null).asInstanceOf[Vector[ModuleID]] - // Replacements are not handled by `librarymanagement`. any Scala prefix in the name will be dropped. - // It also doesn't matter whether you use double `%` to get the Scala version or not. - replacements = Map( - "org.scala-lang" % "scala3-library" -> "@scala_library_3_3_1//jar", - "org.scala-lang" % "scala-library" -> "@scala_library_2_13_11//jar", - "org.scala-lang" % "scala-reflect" -> "@scala_reflect_2_13_11//jar" - ), - dependencies = Vector( - "org.openjdk.jmh" % "jmh-core" % jmhV, - "org.openjdk.jmh" % "jmh-generator-bytecode" % jmhV, - "org.openjdk.jmh" % "jmh-generator-reflection" % jmhV, - "org.openjdk.jmh" % "jmh-generator-asm" % jmhV, - "com.github.scopt" %% "scopt" % "4.1.0" - ) + given DepsCfg = DepsCfg( + resolvers = resolversField, + replacements = replacementsField, + dependencies = dependenciesField ) - + MakeTree() + + private def getDependenciesClass(name: String) = + val dependenciesClass = Try(Class.forName(name)) match + case scala.util.Success(clazz) => clazz + case scala.util.Failure(e) => + throw new ClassNotFoundException(s"Failed to load Dependencies class: ${e.getMessage}", e) + dependenciesClass diff --git a/deps/src/Vars.scala b/deps/src/Vars.scala index fb9e7e8c..4eafae83 100644 --- a/deps/src/Vars.scala +++ b/deps/src/Vars.scala @@ -6,6 +6,7 @@ import scopt.OParser case class Vars( projectRoot: File = new File("."), scalaVersion: String = "", + dependencies: String = "", destination: String = "3rdparty", bazelExtName: String = "workspace.bzl", targetsDirName: String = "jvm", @@ -32,6 +33,10 @@ object Vars: .required() .action((value, vars) => vars.copy(scalaVersion = value)) .text("The version of Scala used in project"), + opt[String]("dependencies") + .required() + .action((value, vars) => vars.copy(dependencies = value)) + .text("The full name of Dependencies.scala class"), opt[String]('d', "destination") .action((value, vars) => vars.copy(destination = value)) .text("""The name of the directory that will be created inside your diff --git a/rules/scala/workspace.bzl b/rules/scala/workspace.bzl index aab4cd32..c1b3bfb4 100644 --- a/rules/scala/workspace.bzl +++ b/rules/scala/workspace.bzl @@ -65,7 +65,7 @@ def scala_repositories(): ) scala2 = "2.13.15" - scala3 = "3.5.2-RC2" + scala3 = "3.5.2" scalajs = "1.17.0" direct_deps = [ diff --git a/rules/scala_proto/3rdparty/workspace.bzl b/rules/scala_proto/3rdparty/workspace.bzl index 1bb58ce3..a93ae79e 100644 --- a/rules/scala_proto/3rdparty/workspace.bzl +++ b/rules/scala_proto/3rdparty/workspace.bzl @@ -40,13 +40,13 @@ def jar_artifact_callback(hash): def list_dependencies(): return [ - {"artifact":"com.google.protobuf:protobuf-java:4.28.2","url":"https://repo1.maven.org/maven2/com/google/protobuf/protobuf-java/4.28.2/protobuf-java-4.28.2.jar","name":"com_google_protobuf_protobuf_java","actual":"@com_google_protobuf_protobuf_java//jar","bind": "jar/com/google/protobuf/protobuf_java"}, - {"artifact":"com.thesamet.scalapb:compilerplugin_3:1.0.0-alpha.1","url":"https://repo1.maven.org/maven2/com/thesamet/scalapb/compilerplugin_3/1.0.0-alpha.1/compilerplugin_3-1.0.0-alpha.1.jar","name":"com_thesamet_scalapb_compilerplugin_3","actual":"@com_thesamet_scalapb_compilerplugin_3//jar","bind": "jar/com/thesamet/scalapb/compilerplugin_3"}, - {"artifact":"com.thesamet.scalapb.grpcweb:scalapb-grpcweb-code-gen_3:0.7.0","url":"https://repo1.maven.org/maven2/com/thesamet/scalapb/grpcweb/scalapb-grpcweb-code-gen_3/0.7.0/scalapb-grpcweb-code-gen_3-0.7.0.jar","name":"com_thesamet_scalapb_grpcweb_scalapb_grpcweb_code_gen_3","actual":"@com_thesamet_scalapb_grpcweb_scalapb_grpcweb_code_gen_3//jar","bind": "jar/com/thesamet/scalapb/grpcweb/scalapb_grpcweb_code_gen_3"}, - {"artifact":"com.thesamet.scalapb:protoc-bridge_2.13:0.9.7","url":"https://repo1.maven.org/maven2/com/thesamet/scalapb/protoc-bridge_2.13/0.9.7/protoc-bridge_2.13-0.9.7.jar","name":"com_thesamet_scalapb_protoc_bridge_2_13","actual":"@com_thesamet_scalapb_protoc_bridge_2_13//jar","bind": "jar/com/thesamet/scalapb/protoc_bridge_2_13"}, - {"artifact":"com.thesamet.scalapb:protoc-gen_2.13:0.9.7","url":"https://repo1.maven.org/maven2/com/thesamet/scalapb/protoc-gen_2.13/0.9.7/protoc-gen_2.13-0.9.7.jar","name":"com_thesamet_scalapb_protoc_gen_2_13","actual":"@com_thesamet_scalapb_protoc_gen_2_13//jar","bind": "jar/com/thesamet/scalapb/protoc_gen_2_13"}, - {"artifact":"dev.dirs:directories:26","url":"https://repo1.maven.org/maven2/dev/dirs/directories/26/directories-26.jar","name":"dev_dirs_directories","actual":"@dev_dirs_directories//jar","bind": "jar/dev/dirs/directories"}, - {"artifact":"org.scala-lang.modules:scala-collection-compat_3:2.12.0","url":"https://repo1.maven.org/maven2/org/scala-lang/modules/scala-collection-compat_3/2.12.0/scala-collection-compat_3-2.12.0.jar","name":"org_scala_lang_modules_scala_collection_compat_3","actual":"@org_scala_lang_modules_scala_collection_compat_3//jar","bind": "jar/org/scala_lang/modules/scala_collection_compat_3"}, + {"artifact":"com.google.protobuf:protobuf-java:4.28.2","url":"https://oss.sonatype.org/service/local/repositories/releases/content/com/google/protobuf/protobuf-java/4.28.2/protobuf-java-4.28.2.jar","name":"com_google_protobuf_protobuf_java","actual":"@com_google_protobuf_protobuf_java//jar","bind": "jar/com/google/protobuf/protobuf_java"}, + {"artifact":"com.thesamet.scalapb:compilerplugin_3:1.0.0-alpha.1","url":"https://oss.sonatype.org/service/local/repositories/releases/content/com/thesamet/scalapb/compilerplugin_3/1.0.0-alpha.1/compilerplugin_3-1.0.0-alpha.1.jar","name":"com_thesamet_scalapb_compilerplugin_3","actual":"@com_thesamet_scalapb_compilerplugin_3//jar","bind": "jar/com/thesamet/scalapb/compilerplugin_3"}, + {"artifact":"com.thesamet.scalapb.grpcweb:scalapb-grpcweb-code-gen_3:0.7.0","url":"https://oss.sonatype.org/service/local/repositories/releases/content/com/thesamet/scalapb/grpcweb/scalapb-grpcweb-code-gen_3/0.7.0/scalapb-grpcweb-code-gen_3-0.7.0.jar","name":"com_thesamet_scalapb_grpcweb_scalapb_grpcweb_code_gen_3","actual":"@com_thesamet_scalapb_grpcweb_scalapb_grpcweb_code_gen_3//jar","bind": "jar/com/thesamet/scalapb/grpcweb/scalapb_grpcweb_code_gen_3"}, + {"artifact":"com.thesamet.scalapb:protoc-bridge_2.13:0.9.7","url":"https://oss.sonatype.org/service/local/repositories/releases/content/com/thesamet/scalapb/protoc-bridge_2.13/0.9.7/protoc-bridge_2.13-0.9.7.jar","name":"com_thesamet_scalapb_protoc_bridge_2_13","actual":"@com_thesamet_scalapb_protoc_bridge_2_13//jar","bind": "jar/com/thesamet/scalapb/protoc_bridge_2_13"}, + {"artifact":"com.thesamet.scalapb:protoc-gen_2.13:0.9.7","url":"https://oss.sonatype.org/service/local/repositories/releases/content/com/thesamet/scalapb/protoc-gen_2.13/0.9.7/protoc-gen_2.13-0.9.7.jar","name":"com_thesamet_scalapb_protoc_gen_2_13","actual":"@com_thesamet_scalapb_protoc_gen_2_13//jar","bind": "jar/com/thesamet/scalapb/protoc_gen_2_13"}, + {"artifact":"dev.dirs:directories:26","url":"https://oss.sonatype.org/service/local/repositories/releases/content/dev/dirs/directories/26/directories-26.jar","name":"dev_dirs_directories","actual":"@dev_dirs_directories//jar","bind": "jar/dev/dirs/directories"}, + {"artifact":"org.scala-lang.modules:scala-collection-compat_3:2.12.0","url":"https://oss.sonatype.org/service/local/repositories/releases/content/org/scala-lang/modules/scala-collection-compat_3/2.12.0/scala-collection-compat_3-2.12.0.jar","name":"org_scala_lang_modules_scala_collection_compat_3","actual":"@org_scala_lang_modules_scala_collection_compat_3//jar","bind": "jar/org/scala_lang/modules/scala_collection_compat_3"}, ] def maven_dependencies(callback = jar_artifact_callback): diff --git a/rules/scala_proto/BUILD b/rules/scala_proto/BUILD index 5d59b511..39cf5d35 100644 --- a/rules/scala_proto/BUILD +++ b/rules/scala_proto/BUILD @@ -7,6 +7,18 @@ toolchain_type( visibility = ["//visibility:public"], ) +filegroup( + name = "dependencies", + srcs = ["Dependencies.scala"], + visibility = ["//visibility:public"], +) + +scala_deps( + name = "scala_deps", + src = "//rules/scala_proto:dependencies", + dependencies = "rules_scala3.rules.scala_proto.Dependencies", +) + scala_binary( name = "worker", srcs = ["private/ScalaProtoWorker.scala"], diff --git a/rules/scala_proto/Dependencies.scala b/rules/scala_proto/Dependencies.scala new file mode 100644 index 00000000..06859abd --- /dev/null +++ b/rules/scala_proto/Dependencies.scala @@ -0,0 +1,29 @@ +package rules_scala3.rules.scala_proto + +import sbt.librarymanagement.syntax.* +import sbt.librarymanagement.{DependencyBuilders, ModuleID, Resolver}, DependencyBuilders.OrganizationArtifactName + +object Dependencies: + + private val scalapbV = "1.0.0-alpha.1" + private val protocBridgeV = "0.9.7" + private val grpcwebV = "0.7.0" + + val resolvers: Seq[Resolver] = Vector( + "sonatype releases".at("https://oss.sonatype.org/service/local/repositories/releases/content"), + "apache staging".at("https://repository.apache.org/content/repositories/staging"), + "apache snapshots".at("https://repository.apache.org/snapshots"), + "google".at("https://maven.google.com/"), + "jitsi-maven-repository".at("https://github.com/jitsi/jitsi-maven-repository/raw/master/releases") + ) + val replacements: Map[OrganizationArtifactName, String] = Map( + "org.scala-lang" % "scala3-library" -> "@scala3_library//jar", + "org.scala-lang" % "scala-library" -> "@scala_library_2_13//jar", + "org.scala-lang" % "scala-reflect" -> "@scala_reflect_2_13//jar" + ) + val dependencies: Seq[ModuleID] = Vector( + "com.thesamet.scalapb" %% "compilerplugin" % scalapbV, + "com.thesamet.scalapb" % "protoc-bridge_2.13" % protocBridgeV, + "com.thesamet.scalapb" % "protoc-gen_2.13" % protocBridgeV, + "com.thesamet.scalapb.grpcweb" %% "scalapb-grpcweb-code-gen" % grpcwebV + ) diff --git a/rules/scalafmt/BUILD b/rules/scalafmt/BUILD index 9fc4a931..ba05135d 100644 --- a/rules/scalafmt/BUILD +++ b/rules/scalafmt/BUILD @@ -1,3 +1,4 @@ +load("//deps:scala_deps.bzl", "scala_deps") load("//rules:scala.bzl", "scala_binary") load("//rules:scalafmt.bzl", "scala_format_test") load( @@ -5,6 +6,18 @@ load( _add_non_default_format_phase_singleton = "add_non_default_format_phase_singleton", ) +filegroup( + name = "dependencies", + srcs = ["Dependencies.scala"], + visibility = ["//visibility:public"], +) + +scala_deps( + name = "scala_deps", + src = "//rules/scalafmt:dependencies", + dependencies = "rules_scala3.rules.scalafmt.Dependencies", +) + filegroup( name = "runner", srcs = ["private/format.template.sh"], diff --git a/rules/scalafmt/Dependencies.scala b/rules/scalafmt/Dependencies.scala new file mode 100644 index 00000000..1f8a9580 --- /dev/null +++ b/rules/scalafmt/Dependencies.scala @@ -0,0 +1,32 @@ +package rules_scala3.rules.scalafmt + +import sbt.librarymanagement.syntax.* +import sbt.librarymanagement.{DependencyBuilders, ModuleID, Resolver}, DependencyBuilders.OrganizationArtifactName + +object Dependencies: + + private val scalafmtV = "3.8.3" + private val parsersV = "4.10.2" + + val resolvers: Seq[Resolver] = Vector( + "mavencentral".at("https://repo1.maven.org/maven2/"), + "sonatype releases".at("https://oss.sonatype.org/service/local/repositories/releases/content"), + "apache staging".at("https://repository.apache.org/content/repositories/staging"), + "apache snapshots".at("https://repository.apache.org/snapshots"), + "google".at("https://maven.google.com/"), + "jitsi-maven-repository".at("https://github.com/jitsi/jitsi-maven-repository/raw/master/releases") + ) + // Replacements are not handled by `librarymanagement`. any Scala prefix in the name will be dropped. + // It also doesn't matter whether you use double `%` to get the Scala version or not. + val replacements: Map[OrganizationArtifactName, String] = Map( + "org.scala-lang" % "scala3-library" -> "@scala3_library//jar", + "org.scala-lang" % "scala-library" -> "@scala_library_2_13//jar", + "org.scala-lang" % "scala-reflect" -> "@scala_reflect_2_13//jar" + ) + val dependencies: Seq[ModuleID] = Vector( + "com.geirsson" % "metaconfig-core_2.13" % "0.12.0", + "org.scalameta" % "parsers_2.13" % parsersV, + "org.scalameta" % "trees_2.13" % parsersV, + "org.scalameta" % "scalafmt-core_2.13" % scalafmtV, + "org.scalameta" % "scalafmt-sysops_2.13" % scalafmtV, + ) diff --git a/scala/BUILD b/scala/BUILD index 0e4a7cb9..9da4597e 100644 --- a/scala/BUILD +++ b/scala/BUILD @@ -4,6 +4,19 @@ load( "configure_zinc_scala", "scala_library", ) +load("//deps:scala_deps.bzl", "scala_deps") + +filegroup( + name = "dependencies", + srcs = ["Dependencies.scala"], + visibility = ["//visibility:public"], +) + +scala_deps( + name = "scala_deps", + src = "//scala:dependencies", + dependencies = "rules_scala3.scala.Dependencies", +) config_setting( name = "deps_used_off", diff --git a/scala/Dependencies.scala b/scala/Dependencies.scala new file mode 100644 index 00000000..a8614907 --- /dev/null +++ b/scala/Dependencies.scala @@ -0,0 +1,40 @@ +package rules_scala3.scala + +import sbt.librarymanagement.syntax.* +import sbt.librarymanagement.{DependencyBuilders, ModuleID, Resolver}, DependencyBuilders.OrganizationArtifactName + +object Dependencies: + + private val sbtVersion = "2.0.0-M2" + private val zincVersion = "2.0.0-alpha14" + val scalapbV = "1.0.0-alpha.1" + + val resolvers: Seq[Resolver] = Vector( + "mavencentral".at("https://repo1.maven.org/maven2/"), + "sonatype releases".at("https://oss.sonatype.org/service/local/repositories/releases/content"), + "apache staging".at("https://repository.apache.org/content/repositories/staging"), + "apache snapshots".at("https://repository.apache.org/snapshots"), + "google".at("https://maven.google.com/"), + "jitsi-maven-repository".at("https://github.com/jitsi/jitsi-maven-repository/raw/master/releases") + ) + val replacements: Map[OrganizationArtifactName, String] = Map( + "org.scala-lang" % "scala3-library" -> "@scala3_library//jar", + "org.scala-lang" % "scala-library" -> "@scala_library_2_13//jar", + "org.scala-lang" % "scala-reflect" -> "@scala_reflect_2_13//jar" + ) + val dependencies: Seq[ModuleID] = Vector( + "org.jacoco" % "org.jacoco.core" % "0.8.10", + "org.jline" % "jline-reader" % "3.24.1", + "org.scala-lang.modules" %% "scala-xml" % "2.3.0", + "org.scala-sbt" % "test-interface" % "1.0", + "org.scala-sbt" % "compiler-interface" % zincVersion, + "org.scala-sbt" % "util-interface" % sbtVersion, + "org.scala-sbt" %% "util-logging_3" % sbtVersion, + "org.scala-sbt" %% "zinc_3" % zincVersion, + "org.scala-sbt" %% "zinc-core" % zincVersion, + "org.scala-sbt" %% "zinc-classpath" % zincVersion, + "org.scala-sbt" %% "zinc-compile-core" % zincVersion, + "org.scala-sbt" %% "zinc-persist" % zincVersion, + "org.scala-sbt" %% "zinc-apiinfo" % zincVersion, + "org.scalameta" %% "munit_3" % "1.0.2", + ) \ No newline at end of file diff --git a/tests/WORKSPACE b/tests/WORKSPACE index 9d79fed3..7a2bd03e 100644 --- a/tests/WORKSPACE +++ b/tests/WORKSPACE @@ -100,6 +100,14 @@ load("//3rdparty:workspace.bzl", "maven_dependencies") maven_dependencies() +load("@rules_scala3//3rdparty:workspace.bzl", "maven_dependencies") + +maven_dependencies() + +load("@rules_scala3//scala/3rdparty:workspace.bzl", "maven_dependencies") + +maven_dependencies() + bind( name = "default_scala", actual = "@rules_scala3//scala:zinc_3_3", diff --git a/tests/compat/scalacopts/BUILD b/tests/compat/scalacopts/BUILD index f16ba9eb..096d160d 100644 --- a/tests/compat/scalacopts/BUILD +++ b/tests/compat/scalacopts/BUILD @@ -1,4 +1,4 @@ -load("@io_bazel_rules_scala//scala:scala.bzl", "scala_library") +load("@rules_scala3//rules:scala.bzl", "scala_library") scala_library( name = "default", diff --git a/tests/grpc/BUILD b/tests/grpc/BUILD index 5104336b..4dec82ec 100644 --- a/tests/grpc/BUILD +++ b/tests/grpc/BUILD @@ -44,7 +44,7 @@ scala_library( deps_used_whitelist = [ "@com_google_protobuf//java/core", ], - scala = "@rules_scala3//scala:zinc_3_3_migration", + scala = "@rules_scala3//scala:zinc_3_migration", deps = [ "//3rdparty/jvm/com/thesamet/scalapb:scalapb_runtime", "//3rdparty/jvm/com/thesamet/scalapb:scalapb_runtime_grpc", @@ -66,7 +66,7 @@ scala_library( deps_used_whitelist = [ "@com_google_protobuf//java/core", ], - scala = "@rules_scala3//scala:zinc_3_3_migration", + scala = "@rules_scala3//scala:zinc_3_migration", scalacopts = [ "-scalajs", ], diff --git a/tests/proto/BUILD b/tests/proto/BUILD index ea54baa8..75f58e6a 100644 --- a/tests/proto/BUILD +++ b/tests/proto/BUILD @@ -23,7 +23,7 @@ scala_library( deps_unused_whitelist = [ "//3rdparty/jvm/com/google/protobuf:protobuf_java", ], - scala = "@rules_scala3//scala:zinc_3_3_migration", + scala = "@rules_scala3//scala:zinc_3_migration", deps = [ "//3rdparty/jvm/com/thesamet/scalapb:scalapb_runtime", ],