Skip to content

Commit

Permalink
Merge branch 'master' into 9-add-coursier-deps-service
Browse files Browse the repository at this point in the history
  • Loading branch information
anna-skrodzka authored Oct 22, 2024
2 parents 4118750 + 7cc5b41 commit e8af788
Show file tree
Hide file tree
Showing 15 changed files with 192 additions and 41 deletions.
10 changes: 7 additions & 3 deletions deps/scala_deps.bzl
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
47 changes: 21 additions & 26 deletions deps/src/Deps.scala
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions deps/src/Vars.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion rules/scala/workspace.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
14 changes: 7 additions & 7 deletions rules/scala_proto/3rdparty/workspace.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
12 changes: 12 additions & 0 deletions rules/scala_proto/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
29 changes: 29 additions & 0 deletions rules/scala_proto/Dependencies.scala
Original file line number Diff line number Diff line change
@@ -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
)
13 changes: 13 additions & 0 deletions rules/scalafmt/BUILD
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
load("//deps:scala_deps.bzl", "scala_deps")
load("//rules:scala.bzl", "scala_binary")
load("//rules:scalafmt.bzl", "scala_format_test")
load(
":ext.bzl",
_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"],
Expand Down
32 changes: 32 additions & 0 deletions rules/scalafmt/Dependencies.scala
Original file line number Diff line number Diff line change
@@ -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,
)
13 changes: 13 additions & 0 deletions scala/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
40 changes: 40 additions & 0 deletions scala/Dependencies.scala
Original file line number Diff line number Diff line change
@@ -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",
)
8 changes: 8 additions & 0 deletions tests/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion tests/compat/scalacopts/BUILD
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 2 additions & 2 deletions tests/grpc/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
],
Expand Down
2 changes: 1 addition & 1 deletion tests/proto/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
Expand Down

0 comments on commit e8af788

Please sign in to comment.