Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
.
  • Loading branch information
lihaoyi committed Sep 9, 2025
commit 94e599a971e1a57c88b5b11ca8a0a272587106bb
111 changes: 36 additions & 75 deletions project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -697,87 +697,48 @@ object Build {
FilesInfo.lastModified, FilesInfo.exists) { _ =>
s.log.info(s"Downloading and processing shaded sources to $dest...")

val destPath = dest.toPath
if (Files.exists(destPath)) {
Files.walk(destPath)
.sorted(java.util.Comparator.reverseOrder()) // delete children before parents
.forEach(p => Files.delete(p));
if (dest.exists) {
IO.delete(dest)
}
Files.createDirectories(destPath)
IO.createDirectory(dest)

for(url <- downloads) {
import java.io._
import java.net.{HttpURLConnection, URL}
import java.nio.file._
import java.nio.file.attribute.FileTime
import java.util.zip.{ZipEntry, ZipInputStream}

val conn = new URL(url).openConnection().asInstanceOf[HttpURLConnection]
conn.setInstanceFollowRedirects(true)
conn.setConnectTimeout(15000)
conn.setReadTimeout(60000)
conn.setRequestMethod("GET")

var in: InputStream = null
var zis: ZipInputStream = null
try {
in = new BufferedInputStream(conn.getInputStream)
zis = new ZipInputStream(in)

var entry: ZipEntry = zis.getNextEntry
val buffer = new Array[Byte](8192)

while (entry != null) {
val target = destPath.resolve(entry.getName).normalize()
if (entry.isDirectory) Files.createDirectories(target)
else {
Files.createDirectories(target.getParent)
var out: OutputStream = null
try {
out = new BufferedOutputStream(Files.newOutputStream(target, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING))
var n = zis.read(buffer)
while (n != -1) {
out.write(buffer, 0, n)
n = zis.read(buffer)
}
} finally if (out != null) out.close()
}
import java.net.URL

zis.closeEntry()
entry = zis.getNextEntry
}
} finally {
if (zis != null) zis.close()
if (in != null) in.close()
conn.disconnect()
}
}
// Download jar to a temporary file
val jarName = url.substring(url.lastIndexOf('/') + 1)
val tempJar = cacheDir / jarName

import collection.JavaConverters._
Files.walk(destPath)
.filter(p => p.toString().endsWith(".scala"))
.map[java.io.File] { (file: java.nio.file.Path) =>
val text = new String(Files.readAllBytes(file), java.nio.charset.StandardCharsets.UTF_8)
if (!file.getFileName().toString().equals("CollectionName.scala")) Files.write(
file,
("package dotty.shaded\n" +
text
.replace("import scala", "import _root_.scala")
.replace(" scala.collection.", " _root_.scala.collection.")
.replace("_root_.pprint", "_root_.dotty.shaded.pprint")
.replace("_root_.fansi", "_root_.dotty.shaded.fansi")
.replace("def apply(c: Char): Trie[T]", "def apply(c: Char): Trie[T] | Null")
.replace("var head: Iterator[T] = null", "var head: Iterator[T] | Null = null")
.replace("if (head != null && head.hasNext) true", "if (head != null && head.nn.hasNext) true")
.replace("head.next()", "head.nn.next()")
.replace("abstract class Walker", "@scala.annotation.nowarn abstract class Walker")
.replace("object TPrintLowPri", "@scala.annotation.nowarn object TPrintLowPri")
.replace("x.toString match{", "scala.runtime.ScalaRunTime.stringOf(x) match{")).getBytes
)
file.toFile
s.log.info(s"Downloading $jarName...")
IO.transfer(new URL(url).openStream(), tempJar)

}
.collect(java.util.stream.Collectors.toList()).asScala.toSet
// Extract the jar using SBT's IO.unzip
s.log.info(s"Extracting $jarName...")
IO.unzip(tempJar, dest)
}

val scalaFiles = (dest ** "*.scala").get
scalaFiles.foreach { file =>
val text = IO.read(file)
if (!file.getName.equals("CollectionName.scala")) {
val processedText = "package dotty.shaded\n" +
text
.replace("import scala", "import _root_.scala")
.replace(" scala.collection.", " _root_.scala.collection.")
.replace("_root_.pprint", "_root_.dotty.shaded.pprint")
.replace("_root_.fansi", "_root_.dotty.shaded.fansi")
.replace("def apply(c: Char): Trie[T]", "def apply(c: Char): Trie[T] | Null")
.replace("var head: Iterator[T] = null", "var head: Iterator[T] | Null = null")
.replace("if (head != null && head.hasNext) true", "if (head != null && head.nn.hasNext) true")
.replace("head.next()", "head.nn.next()")
.replace("abstract class Walker", "@scala.annotation.nowarn abstract class Walker")
.replace("object TPrintLowPri", "@scala.annotation.nowarn object TPrintLowPri")
.replace("x.toString match{", "scala.runtime.ScalaRunTime.stringOf(x) match{")

IO.write(file, processedText)
}
}
scalaFiles.toSet
} (Set(markerFile)).toSeq

}.taskValue
Expand Down
Loading