Skip to content

Commit 94e599a

Browse files
committed
.
1 parent 7118ce2 commit 94e599a

File tree

1 file changed

+36
-75
lines changed

1 file changed

+36
-75
lines changed

project/Build.scala

Lines changed: 36 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -697,87 +697,48 @@ object Build {
697697
FilesInfo.lastModified, FilesInfo.exists) { _ =>
698698
s.log.info(s"Downloading and processing shaded sources to $dest...")
699699

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));
700+
if (dest.exists) {
701+
IO.delete(dest)
705702
}
706-
Files.createDirectories(destPath)
703+
IO.createDirectory(dest)
707704

708705
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-
}
706+
import java.net.URL
745707

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()
753-
}
754-
}
708+
// Download jar to a temporary file
709+
val jarName = url.substring(url.lastIndexOf('/') + 1)
710+
val tempJar = cacheDir / jarName
755711

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
712+
s.log.info(s"Downloading $jarName...")
713+
IO.transfer(new URL(url).openStream(), tempJar)
778714

779-
}
780-
.collect(java.util.stream.Collectors.toList()).asScala.toSet
715+
// Extract the jar using SBT's IO.unzip
716+
s.log.info(s"Extracting $jarName...")
717+
IO.unzip(tempJar, dest)
718+
}
719+
720+
val scalaFiles = (dest ** "*.scala").get
721+
scalaFiles.foreach { file =>
722+
val text = IO.read(file)
723+
if (!file.getName.equals("CollectionName.scala")) {
724+
val processedText = "package dotty.shaded\n" +
725+
text
726+
.replace("import scala", "import _root_.scala")
727+
.replace(" scala.collection.", " _root_.scala.collection.")
728+
.replace("_root_.pprint", "_root_.dotty.shaded.pprint")
729+
.replace("_root_.fansi", "_root_.dotty.shaded.fansi")
730+
.replace("def apply(c: Char): Trie[T]", "def apply(c: Char): Trie[T] | Null")
731+
.replace("var head: Iterator[T] = null", "var head: Iterator[T] | Null = null")
732+
.replace("if (head != null && head.hasNext) true", "if (head != null && head.nn.hasNext) true")
733+
.replace("head.next()", "head.nn.next()")
734+
.replace("abstract class Walker", "@scala.annotation.nowarn abstract class Walker")
735+
.replace("object TPrintLowPri", "@scala.annotation.nowarn object TPrintLowPri")
736+
.replace("x.toString match{", "scala.runtime.ScalaRunTime.stringOf(x) match{")
737+
738+
IO.write(file, processedText)
739+
}
740+
}
741+
scalaFiles.toSet
781742
} (Set(markerFile)).toSeq
782743

783744
}.taskValue

0 commit comments

Comments
 (0)