@@ -4,13 +4,14 @@ import sbt.*
4
4
import sbt .Keys .*
5
5
import scala .jdk .CollectionConverters .*
6
6
import java .nio .file .Files
7
+ import xsbti .VirtualFileRef
8
+ import sbt .internal .inc .Stamper
7
9
8
10
object ScalaLibraryPlugin extends AutoPlugin {
9
11
10
12
override def trigger = noTrigger
11
13
12
14
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")
14
15
15
16
override def projectSettings = Seq (
16
17
fetchScala2ClassFiles := {
@@ -37,17 +38,29 @@ object ScalaLibraryPlugin extends AutoPlugin {
37
38
} (Set (scalaLibraryBinaryJar)), target)
38
39
39
40
},
40
- (Compile / compile ) := {
41
+ (Compile / manipulateBytecode ) := {
41
42
val stream = streams.value
42
43
val target = (Compile / classDirectory).value
43
44
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()))
51
64
}
52
65
53
66
val overwrittenBinaries = Files .walk((Compile / classDirectory).value.toPath())
@@ -56,13 +69,18 @@ object ScalaLibraryPlugin extends AutoPlugin {
56
69
.map(_.toFile)
57
70
.map(_.relativeTo((Compile / classDirectory).value).get)
58
71
.toSet
72
+
59
73
val diff = files.filterNot(_.relativeTo(reference).exists(overwrittenBinaries))
60
74
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
+ }
64
80
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
66
84
}
67
85
)
68
86
0 commit comments