Skip to content

Commit 7118ce2

Browse files
committed
.
1 parent 0e78df2 commit 7118ce2

File tree

1 file changed

+92
-75
lines changed

1 file changed

+92
-75
lines changed

project/Build.scala

Lines changed: 92 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -676,92 +676,109 @@ object Build {
676676
}
677677

678678
val shadedSourceGenerator = (Compile / sourceGenerators) += Def.task {
679+
val s = streams.value
680+
val cacheDir = s.cacheDirectory
681+
val dest = (Compile / sourceManaged).value / "downloaded"
682+
679683
val downloads = Seq(
680684
"https://repo1.maven.org/maven2/com/lihaoyi/pprint_3/0.9.3/pprint_3-0.9.3-sources.jar",
681685
"https://repo1.maven.org/maven2/com/lihaoyi/fansi_3/0.5.1/fansi_3-0.5.1-sources.jar",
682686
"https://repo1.maven.org/maven2/com/lihaoyi/sourcecode_3/0.4.4/sourcecode_3-0.4.4-sources.jar",
683687
)
684-
val dest = ((Compile / sourceManaged).value / "downloaded").toPath
685-
if (Files.exists(dest)) {
686-
Files.walk(dest)
687-
.sorted(java.util.Comparator.reverseOrder()) // delete children before parents
688-
.forEach(p => Files.delete(p));
688+
689+
// Create a marker file that tracks the download URLs for cache invalidation
690+
val markerFile = cacheDir / "shaded-sources-marker"
691+
val markerContent = downloads.mkString("\n")
692+
if (!markerFile.exists || IO.read(markerFile) != markerContent) {
693+
IO.write(markerFile, markerContent)
689694
}
690-
Files.createDirectories(dest)
691-
692-
for(url <- downloads) {
693-
import java.io._
694-
import java.net.{HttpURLConnection, URL}
695-
import java.nio.file._
696-
import java.nio.file.attribute.FileTime
697-
import java.util.zip.{ZipEntry, ZipInputStream}
698-
699-
val conn = new URL(url).openConnection().asInstanceOf[HttpURLConnection]
700-
conn.setInstanceFollowRedirects(true)
701-
conn.setConnectTimeout(15000)
702-
conn.setReadTimeout(60000)
703-
conn.setRequestMethod("GET")
704-
705-
var in: InputStream = null
706-
var zis: ZipInputStream = null
707-
try {
708-
in = new BufferedInputStream(conn.getInputStream)
709-
zis = new ZipInputStream(in)
710-
711-
var entry: ZipEntry = zis.getNextEntry
712-
val buffer = new Array[Byte](8192)
713-
714-
while (entry != null) {
715-
val target = dest.resolve(entry.getName).normalize()
716-
if (entry.isDirectory) Files.createDirectories(target)
717-
else {
718-
Files.createDirectories(target.getParent)
719-
var out: OutputStream = null
720-
try {
721-
out = new BufferedOutputStream(Files.newOutputStream(target, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING))
722-
var n = zis.read(buffer)
723-
while (n != -1) {
724-
out.write(buffer, 0, n)
725-
n = zis.read(buffer)
726-
}
727-
} finally if (out != null) out.close()
728-
}
729695

730-
zis.closeEntry()
731-
entry = zis.getNextEntry
696+
FileFunction.cached(cacheDir / "fetchShadedSources",
697+
FilesInfo.lastModified, FilesInfo.exists) { _ =>
698+
s.log.info(s"Downloading and processing shaded sources to $dest...")
699+
700+
val destPath = dest.toPath
701+
if (Files.exists(destPath)) {
702+
Files.walk(destPath)
703+
.sorted(java.util.Comparator.reverseOrder()) // delete children before parents
704+
.forEach(p => Files.delete(p));
705+
}
706+
Files.createDirectories(destPath)
707+
708+
for(url <- downloads) {
709+
import java.io._
710+
import java.net.{HttpURLConnection, URL}
711+
import java.nio.file._
712+
import java.nio.file.attribute.FileTime
713+
import java.util.zip.{ZipEntry, ZipInputStream}
714+
715+
val conn = new URL(url).openConnection().asInstanceOf[HttpURLConnection]
716+
conn.setInstanceFollowRedirects(true)
717+
conn.setConnectTimeout(15000)
718+
conn.setReadTimeout(60000)
719+
conn.setRequestMethod("GET")
720+
721+
var in: InputStream = null
722+
var zis: ZipInputStream = null
723+
try {
724+
in = new BufferedInputStream(conn.getInputStream)
725+
zis = new ZipInputStream(in)
726+
727+
var entry: ZipEntry = zis.getNextEntry
728+
val buffer = new Array[Byte](8192)
729+
730+
while (entry != null) {
731+
val target = destPath.resolve(entry.getName).normalize()
732+
if (entry.isDirectory) Files.createDirectories(target)
733+
else {
734+
Files.createDirectories(target.getParent)
735+
var out: OutputStream = null
736+
try {
737+
out = new BufferedOutputStream(Files.newOutputStream(target, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING))
738+
var n = zis.read(buffer)
739+
while (n != -1) {
740+
out.write(buffer, 0, n)
741+
n = zis.read(buffer)
742+
}
743+
} finally if (out != null) out.close()
744+
}
745+
746+
zis.closeEntry()
747+
entry = zis.getNextEntry
748+
}
749+
} finally {
750+
if (zis != null) zis.close()
751+
if (in != null) in.close()
752+
conn.disconnect()
732753
}
733-
} finally {
734-
if (zis != null) zis.close()
735-
if (in != null) in.close()
736-
conn.disconnect()
737754
}
738-
}
739755

740-
import collection.JavaConverters._
741-
Files.walk(dest)
742-
.filter(p => p.toString().endsWith(".scala"))
743-
.map[java.io.File] { (file: java.nio.file.Path) =>
744-
val text = new String(Files.readAllBytes(file.path), java.nio.charset.StandardCharsets.UTF_8)
745-
if (!file.getFileName().toString().equals("CollectionName.scala")) Files.write(
746-
file,
747-
("package dotty.shaded\n" +
748-
text
749-
.replace("import scala", "import _root_.scala")
750-
.replace(" scala.collection.", " _root_.scala.collection.")
751-
.replace("_root_.pprint", "_root_.dotty.shaded.pprint")
752-
.replace("_root_.fansi", "_root_.dotty.shaded.fansi")
753-
.replace("def apply(c: Char): Trie[T]", "def apply(c: Char): Trie[T] | Null")
754-
.replace("var head: Iterator[T] = null", "var head: Iterator[T] | Null = null")
755-
.replace("if (head != null && head.hasNext) true", "if (head != null && head.nn.hasNext) true")
756-
.replace("head.next()", "head.nn.next()")
757-
.replace("abstract class Walker", "@scala.annotation.nowarn abstract class Walker")
758-
.replace("object TPrintLowPri", "@scala.annotation.nowarn object TPrintLowPri")
759-
.replace("x.toString match{", "scala.runtime.ScalaRunTime.stringOf(x) match{")).getBytes
760-
)
761-
file.toFile
756+
import collection.JavaConverters._
757+
Files.walk(destPath)
758+
.filter(p => p.toString().endsWith(".scala"))
759+
.map[java.io.File] { (file: java.nio.file.Path) =>
760+
val text = new String(Files.readAllBytes(file), java.nio.charset.StandardCharsets.UTF_8)
761+
if (!file.getFileName().toString().equals("CollectionName.scala")) Files.write(
762+
file,
763+
("package dotty.shaded\n" +
764+
text
765+
.replace("import scala", "import _root_.scala")
766+
.replace(" scala.collection.", " _root_.scala.collection.")
767+
.replace("_root_.pprint", "_root_.dotty.shaded.pprint")
768+
.replace("_root_.fansi", "_root_.dotty.shaded.fansi")
769+
.replace("def apply(c: Char): Trie[T]", "def apply(c: Char): Trie[T] | Null")
770+
.replace("var head: Iterator[T] = null", "var head: Iterator[T] | Null = null")
771+
.replace("if (head != null && head.hasNext) true", "if (head != null && head.nn.hasNext) true")
772+
.replace("head.next()", "head.nn.next()")
773+
.replace("abstract class Walker", "@scala.annotation.nowarn abstract class Walker")
774+
.replace("object TPrintLowPri", "@scala.annotation.nowarn object TPrintLowPri")
775+
.replace("x.toString match{", "scala.runtime.ScalaRunTime.stringOf(x) match{")).getBytes
776+
)
777+
file.toFile
762778

763-
}
764-
.collect(java.util.stream.Collectors.toList()).asScala.toSeq
779+
}
780+
.collect(java.util.stream.Collectors.toList()).asScala.toSet
781+
} (Set(markerFile)).toSeq
765782

766783
}.taskValue
767784
// Settings shared between scala3-compiler and scala3-compiler-bootstrapped

0 commit comments

Comments
 (0)