|
| 1 | +package io.bazel.rules_scala.dottyijar |
| 2 | + |
| 3 | +import io.bazel.rules_scala.dottyijar.tasty.Tasty |
| 4 | +import io.bazel.rules_scala.dottyijar.tasty.format.{TastyFormat, TastyReader, TastyWriter} |
| 5 | +import java.io.FileOutputStream |
| 6 | +import java.nio.file.{Path, Paths} |
| 7 | +import java.nio.file.attribute.FileTime |
| 8 | +import java.util.zip.{ZipEntry, ZipFile, ZipOutputStream} |
| 9 | +import scala.jdk.CollectionConverters.* |
| 10 | + |
| 11 | +object DottyIjar { |
| 12 | + private def writeInterfaceJar(inputJar: ZipFile, outputStream: ZipOutputStream): Unit = { |
| 13 | + def copyEntryWithContent(entry: ZipEntry, content: Array[Byte]): Unit = { |
| 14 | + val newEntry = new ZipEntry(entry.getName) |
| 15 | + |
| 16 | + newEntry.setCreationTime(FileTime.fromMillis(0)) |
| 17 | + newEntry.setLastAccessTime(FileTime.fromMillis(0)) |
| 18 | + newEntry.setLastModifiedTime(FileTime.fromMillis(0)) |
| 19 | + |
| 20 | + outputStream.putNextEntry(newEntry) |
| 21 | + outputStream.write(content, 0, content.length) |
| 22 | + } |
| 23 | + |
| 24 | + def copyEntry(entry: ZipEntry): Unit = copyEntryWithContent(entry, inputJar.getInputStream(entry).readAllBytes()) |
| 25 | + |
| 26 | + outputStream.setComment(inputJar.getComment) |
| 27 | + outputStream.setLevel(0) |
| 28 | + |
| 29 | + val entryNames = inputJar.entries.asScala.map(_.getName).toSet |
| 30 | + |
| 31 | + inputJar.entries.asScala.foreach { |
| 32 | + case entry if entry.getName.startsWith("META-INF/") => copyEntry(entry) |
| 33 | + case entry if entry.getName.endsWith(".class") => |
| 34 | + val i = entry.getName.lastIndexOf('/') |
| 35 | + val directory = entry.getName.slice(0, i) |
| 36 | + val filename = entry.getName.slice(i + 1, entry.getName.length) |
| 37 | + val j = filename.indexOf("$") |
| 38 | + val tastyFileBaseName = if (j == -1) filename.stripSuffix(".class") else filename.slice(0, j) |
| 39 | + |
| 40 | + if (!entryNames(s"$directory/$tastyFileBaseName.tasty")) { |
| 41 | + copyEntry(entry) |
| 42 | + } |
| 43 | + |
| 44 | + case entry if entry.getName.endsWith(".tasty") => |
| 45 | + val content = inputJar.getInputStream(entry).readAllBytes() |
| 46 | + val updatedContent = TastyUpdater.updateTastyFile(content) |
| 47 | + |
| 48 | + copyEntryWithContent(entry, updatedContent) |
| 49 | + |
| 50 | + case entry => copyEntry(entry) |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + def main(arguments: Array[String]): Unit = Arguments |
| 55 | + .parseArguments(arguments) |
| 56 | + .fold( |
| 57 | + println, |
| 58 | + arguments => { |
| 59 | + val inputJar = new ZipFile(arguments.inputPath.toFile) |
| 60 | + |
| 61 | + try { |
| 62 | + val outputStream = new ZipOutputStream(new FileOutputStream(arguments.outputPath.toFile)) |
| 63 | + |
| 64 | + try { |
| 65 | + writeInterfaceJar(inputJar, outputStream) |
| 66 | + } finally { |
| 67 | + outputStream.close() |
| 68 | + } |
| 69 | + } finally { |
| 70 | + inputJar.close() |
| 71 | + } |
| 72 | + }, |
| 73 | + ) |
| 74 | +} |
| 75 | + |
| 76 | +private case class Arguments(inputPath: Path, outputPath: Path) |
| 77 | + |
| 78 | +object Arguments { |
| 79 | + def parseArguments(arguments: Array[String]): Either[String, Arguments] = arguments |
| 80 | + .foldLeft[Either[String, UnvalidatedArguments]](Right(UnvalidatedArguments())) { |
| 81 | + case (unvalidatedArguments, argument) => |
| 82 | + unvalidatedArguments.flatMap { unvalidatedArguments => |
| 83 | + argument match { |
| 84 | + case "-h" | "--help" => |
| 85 | + Left( |
| 86 | + """dottyijar removes information from Scala 3 JARs that aren't needed for compilation. |
| 87 | + | |
| 88 | + |Usage: |
| 89 | + | dottyijar <input> <output> |
| 90 | + | dottyijar -h | --help |
| 91 | + | |
| 92 | + |Options: |
| 93 | + | -h --help Show this screen.""".stripMargin, |
| 94 | + ) |
| 95 | + |
| 96 | + case _ => |
| 97 | + lazy val path = Paths.get(argument) |
| 98 | + |
| 99 | + if (unvalidatedArguments.inputPath.isEmpty) { |
| 100 | + Right(unvalidatedArguments.copy(inputPath = Some(path))) |
| 101 | + } else if (unvalidatedArguments.outputPath.isEmpty) { |
| 102 | + Right(unvalidatedArguments.copy(outputPath = Some(path))) |
| 103 | + } else { |
| 104 | + Left(s"Unexpected argument: $argument") |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + .flatMap(_.validate) |
| 110 | +} |
| 111 | + |
| 112 | +private case class UnvalidatedArguments(inputPath: Option[Path] = None, outputPath: Option[Path] = None) { |
| 113 | + def validate: Either[String, Arguments] = ( |
| 114 | + for { |
| 115 | + inputPath <- inputPath |
| 116 | + outputPath <- outputPath |
| 117 | + } yield Arguments(inputPath, outputPath) |
| 118 | + ).toRight("Please provide paths to the input and output JARs.") |
| 119 | +} |
0 commit comments