Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
timothyklim committed Jul 29, 2023
1 parent ca1897d commit b2c6c69
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 41 deletions.
24 changes: 4 additions & 20 deletions deps/BazelExt.scala
Original file line number Diff line number Diff line change
@@ -1,36 +1,20 @@
package rules_scala3.deps

import scala.io.Source
import java.io.FileNotFoundException

object BazelExt:
private def loadResourceToString(path: String): String =
val resourceStream = getClass.getResourceAsStream(path)
if resourceStream == null then throw new FileNotFoundException(s"Resource not found: $path")
try Source.fromInputStream(resourceStream).mkString finally resourceStream.close

private lazy val jarArtifactCallback = loadResourceToString("/templates/jar_artifact_callback.bzl")
private lazy val jarArtifactCallback = String(getClass.getResourceAsStream("/templates/jar_artifact_callback.bzl").readAllBytes())

def apply(targets: Vector[Target]): String =
val dependencyLines: String = targets
.filterNot(_.replacement_label.isDefined)
.map { t =>
Vector(
s""" {"artifact": "${t.coordinates.toString}"""",
s""""url": "${t.url}"""",
s""""name": "${t.name}"""",
s""""actual": "${t.actual}"""",
s""""bind": "${t.bind}"},"""
).mkString(", ")
}
.mkString("\n")
.map(t => s"""{"artifact":"${t.coordinates.toString}","url":"${t.url}","name":"${t.name}","actual":"${t.actual}","bind": "${t.bind}"},""")
.mkString("\n ")

s"""# Do not edit. rules_scala3 autogenerates this file
|$jarArtifactCallback
|
|def list_dependencies():
| return [
|$dependencyLines
| $dependencyLines
| ]
|
|def maven_dependencies(callback = jar_artifact_callback):
Expand Down
20 changes: 10 additions & 10 deletions deps/Deps.scala
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
package rules_scala3.deps

import java.io.File

import sbt.librarymanagement.DependencyBuilders.OrganizationArtifactName
import sbt.librarymanagement.syntax.*

@main def Deps(args: String*): Unit =
// parse args
vars = Vars(
projectRoot = new File("/home/name_snrl/work/forks/rules_scala3"),
projectRoot = new File("/tmp/rules_scala3"),
depsDirName = "3rdparty",
bazelExtFileName = "workspace.bzl",
buildFilesDirName = "jvm",
buildFileName = "BUILD",
scalaVersion = "3.3.0",
buildFileHeader = """load("@io_bazel_rules_scala//scala:scala_import.bzl", "scala_import")"""
buildFileHeader = """load("@rules_scala3//rules:scala.bzl", "scala_import")"""
)

// 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(
"org.scala-lang" % "scala-compiler" -> "@io_bazel_rules_scala_scala_compiler//:io_bazel_rules_scala_scala_compiler",
"org.scala-lang" % "scala-library" -> "@io_bazel_rules_scala_scala_library//:io_bazel_rules_scala_scala_library",
"org.scala-lang" % "scala-reflect" -> "@io_bazel_rules_scala_scala_reflect//:io_bazel_rules_scala_scala_reflect",
"org.scala-lang.modules" % "scala-parser-combinators" -> "@io_bazel_rules_scala_scala_parser_combinators//:io_bazel_rules_scala_scala_parser_combinators",
"org.scala-lang.modules" % "scala-xml" -> "@io_bazel_rules_scala_scala_xml//:io_bazel_rules_scala_scala_xml",
val replacements = Map[OrganizationArtifactName, String](
"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",
)

val dependencies = Vector(
"org.scala-sbt" % "librarymanagement-core_3" % "2.0.0-alpha12",
"org.scala-sbt" % "librarymanagement-coursier_3" % "2.0.0-alpha6",
"org.scala-sbt" % "librarymanagement-core_3" % "2.0.0-alpha12",
"org.scala-sbt" % "librarymanagement-coursier_3" % "2.0.0-alpha6"
)

MakeTree(dependencies, replacements)
8 changes: 4 additions & 4 deletions deps/MakeTree.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package rules_scala3.deps

import java.io.File
import java.nio.file.{Files, StandardOpenOption}

import sbt.librarymanagement.{DependencyBuilders, ModuleID}, DependencyBuilders.OrganizationArtifactName
import sbt.librarymanagement.syntax.*
import java.io.{File, FileWriter}

object MakeTree:
def apply(dependencies: Vector[ModuleID], replacements: Map[OrganizationArtifactName, String]): Unit =
Expand All @@ -13,9 +15,7 @@ object MakeTree:
private def writeToFile(path: File, content: String): Unit =
val dirname = path.getParentFile
if !dirname.exists then dirname.mkdirs
val writer = new FileWriter(path)
try writer.write(content)
finally writer.close
Files.writeString(path.toPath(), content, StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING)

private def writeTree(
targets: Vector[Target],
Expand Down
15 changes: 8 additions & 7 deletions scala/common/worker/WorkerMain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ trait WorkerMain[S]:
val stdout = System.out
val stderr = System.err

System.setSecurityManager(new SecurityManager:
override def checkPermission(permission: Permission): Unit =
permission.getName match
case Exit(code) =>
stderr.println(s"ScalaCompile worker startup failure: permission=$permission, args=${args.mkString("[", ", ", "]")}")
throw ExitTrapped(code.toInt)
case _ => ()
System.setSecurityManager(
new SecurityManager:
override def checkPermission(permission: Permission): Unit =
permission.getName match
case Exit(code) =>
stderr.println(s"ScalaCompile worker startup failure: permission=$permission, args=${args.mkString("[", ", ", "]")}")
throw ExitTrapped(code.toInt)
case _ => ()
)

val outStream = ByteArrayOutputStream()
Expand Down

0 comments on commit b2c6c69

Please sign in to comment.