@@ -4,13 +4,14 @@ import sbt.*
44import sbt .Keys .*
55import scala .jdk .CollectionConverters .*
66import java .nio .file .Files
7+ import xsbti .VirtualFileRef
8+ import sbt .internal .inc .Stamper
79
810object ScalaLibraryPlugin extends AutoPlugin {
911
1012 override def trigger = noTrigger
1113
1214 val fetchScala2ClassFiles = taskKey[(Set [File ], File )](" Fetch the files to use that were compiled with Scala 2" )
13- // val scala2LibraryVersion = settingKey[String]("Version of the Scala 2 Standard Library")
1415
1516 override def projectSettings = Seq (
1617 fetchScala2ClassFiles := {
@@ -37,17 +38,29 @@ object ScalaLibraryPlugin extends AutoPlugin {
3738 } (Set (scalaLibraryBinaryJar)), target)
3839
3940 },
40- (Compile / compile ) := {
41+ (Compile / manipulateBytecode ) := {
4142 val stream = streams.value
4243 val target = (Compile / classDirectory).value
4344 val (files, reference) = fetchScala2ClassFiles.value;
44- val analysis = (Compile / compile).value
45- stream.log.info(s " Copying files from Scala 2 Standard Library to $target" )
46- for (file <- files; id <- file.relativeTo(reference).map(_.toString())) {
47- if (filesToCopy(id)) {
48- stream.log.debug(s " Copying file ' ${id}' to ${target / id}" )
49- IO .copyFile(file, target / id)
50- }
45+ val previous = (Compile / manipulateBytecode).value
46+ val analysis = previous.analysis match {
47+ case analysis : sbt.internal.inc.Analysis => analysis
48+ case _ => sys.error(" Unexpected analysis type" )
49+ }
50+
51+ var stamps = analysis.stamps
52+ for (file <- files;
53+ id <- file.relativeTo(reference);
54+ if filesToCopy(id.toString()); // Only Override Some Very Specific Files
55+ dest = target / (id.toString);
56+ ref <- dest.relativeTo((LocalRootProject / baseDirectory).value)
57+ ) {
58+ // Copy the files to the classDirectory
59+ IO .copyFile(file, dest)
60+ // Update the timestamp in the analysis
61+ stamps = stamps.markProduct(
62+ VirtualFileRef .of(s " $$ {BASE}/ $ref" ),
63+ Stamper .forFarmHashP(dest.toPath()))
5164 }
5265
5366 val overwrittenBinaries = Files .walk((Compile / classDirectory).value.toPath())
@@ -56,13 +69,18 @@ object ScalaLibraryPlugin extends AutoPlugin {
5669 .map(_.toFile)
5770 .map(_.relativeTo((Compile / classDirectory).value).get)
5871 .toSet
72+
5973 val diff = files.filterNot(_.relativeTo(reference).exists(overwrittenBinaries))
6074
61- IO .copy(diff.map { file =>
62- file -> (Compile / classDirectory).value / file.relativeTo(reference).get.getPath
63- })
75+ // Copy all the specialized classes in the stdlib
76+ // no need to update any stamps as these classes exist nowhere in the analysis
77+ for (orig <- diff; dest <- orig.relativeTo(reference)) {
78+ IO .copyFile(orig, ((Compile / classDirectory).value / dest.toString()))
79+ }
6480
65- analysis
81+ previous
82+ .withAnalysis(analysis.copy(stamps = stamps)) // update the analysis with the correct stamps
83+ .withHasModified(true ) // mark it as updated for sbt to update its caches
6684 }
6785 )
6886
0 commit comments