@@ -14,76 +14,47 @@ object ScalaLibraryPlugin extends AutoPlugin {
14
14
15
15
private val scala2Version = " 2.13.16"
16
16
17
- val fetchScala2ClassFiles = taskKey[(Set [File ], File )](" Fetch the files to use that were compiled with Scala 2" )
18
- val fetchScala2SJSIR = taskKey[(Set [File ], File )](" Fetch the .sjsir to use from Scala 2" )
17
+ object autoImport {
18
+ val keepSJSIR = settingKey[Boolean ](" Should we patch .sjsir too?" )
19
+ }
19
20
20
- override def projectSettings = Seq (
21
- fetchScala2ClassFiles := {
22
- val stream = streams.value
23
- val cache = stream.cacheDirectory
24
- val target = cache / " scala-library-classes"
25
- val report = update.value
26
-
27
- val scalaLibraryBinaryJar = report.select(
28
- configuration = configurationFilter(),
29
- module = (_ : ModuleID ).name == " scala-library" ,
30
- artifact = artifactFilter(`type` = " jar" )).headOption.getOrElse {
31
- sys.error(s " Could not fetch scala-library binary JAR " )
32
- }
21
+ import autoImport ._
33
22
34
- if (! target.exists()) {
35
- IO .createDirectory(target)
36
- }
37
-
38
- (FileFunction .cached(cache / " fetch-scala-library-classes" , FilesInfo .lastModified, FilesInfo .exists) { _ =>
39
- stream.log.info(s " Unpacking scala-library binaries to persistent directory: ${target.getAbsolutePath}" )
40
- IO .unzip(scalaLibraryBinaryJar, target)
41
- (target ** " *.class" ).get.toSet
42
- } (Set (scalaLibraryBinaryJar)), target)
43
-
44
- },
45
- fetchScala2SJSIR := {
23
+ override def projectSettings = Seq (
24
+ (Compile / manipulateBytecode) := {
46
25
val stream = streams.value
26
+ val target = (Compile / classDirectory).value
47
27
val lm = dependencyResolution.value
48
28
val log = stream.log
49
29
val cache = stream.cacheDirectory
50
30
val retrieveDir = cache / " scalajs-scalalib" / scalaVersion.value
31
+
51
32
val comp = lm.retrieve(" org.scala-js" % " scalajs-scalalib_2.13" % s " $scala2Version+ $scalaJSVersion" , scalaModuleInfo = None , retrieveDir, log)
52
33
.fold(w => throw w.resolveException, identity)
34
+ .filterNot(_.getPath().contains(" javalib" ))
35
+ .filter(! _.getPath().contains(" scalajs-scalalib_2.13" ) || keepSJSIR.value)
36
+ .distinct
53
37
54
- println(comp(0 ))
55
-
56
- val target = cache / " scala-library-sjsir"
57
-
58
-
59
- if (! target.exists()) {
60
- IO .createDirectory(target)
61
- }
62
-
63
- (FileFunction .cached(cache / " fetch-scala-library-sjsir" , FilesInfo .lastModified, FilesInfo .exists) { _ =>
64
- stream.log.info(s " Unpacking scalajs-scalalib binaries to persistent directory: ${target.getAbsolutePath}" )
65
- IO .unzip(comp(0 ), target)
66
- (target ** " *.sjsir" ).get.toSet
67
- } (Set (comp(0 ))), target)
38
+ // Fetch classfiles and sjsir files
39
+ val patches : Seq [(Set [File ], File )] = comp.map(fetch(stream, _))
68
40
69
- },
70
- (Compile / manipulateBytecode) := {
71
- val stream = streams.value
72
- val target = (Compile / classDirectory).value
73
- val (files, reference) = fetchScala2ClassFiles.value;
74
41
val previous = (Compile / manipulateBytecode).value
42
+
75
43
val analysis = previous.analysis match {
76
44
case analysis : sbt.internal.inc.Analysis => analysis
77
45
case _ => sys.error(" Unexpected analysis type" )
78
46
}
79
-
80
47
var stamps = analysis.stamps
81
- for (file <- files;
82
- id <- file.relativeTo(reference);
83
- if filesToCopy(id.toString().replace(" \\ " , " /" )); // Only Override Some Very Specific Files
84
- dest = target / (id.toString);
85
- ref <- dest.relativeTo((LocalRootProject / baseDirectory).value)
86
- ) {
48
+
49
+ // Patch the files that are in the list
50
+ for {
51
+ (files, reference) <- patches
52
+ file <- files
53
+ id <- file.relativeTo(reference)
54
+ if filesToCopy(id.toString().replace(" \\ " , " /" )) // Only Override Some Very Specific Files
55
+ dest = target / (id.toString)
56
+ ref <- dest.relativeTo((LocalRootProject / baseDirectory).value)
57
+ } {
87
58
// Copy the files to the classDirectory
88
59
IO .copyFile(file, dest)
89
60
// Update the timestamp in the analysis
@@ -92,64 +63,82 @@ object ScalaLibraryPlugin extends AutoPlugin {
92
63
Stamper .forFarmHashP(dest.toPath()))
93
64
}
94
65
66
+
95
67
val overwrittenBinaries = Files .walk((Compile / classDirectory).value.toPath())
96
68
.iterator()
97
69
.asScala
98
70
.map(_.toFile)
99
71
.map(_.relativeTo((Compile / classDirectory).value).get)
100
72
.toSet
101
73
102
- val diff = files.filterNot(_.relativeTo(reference).exists(overwrittenBinaries))
103
-
104
- // Copy all the specialized classes in the stdlib
105
- // no need to update any stamps as these classes exist nowhere in the analysis
106
- for (orig <- diff; dest <- orig.relativeTo(reference)) {
107
- IO .copyFile(orig, ((Compile / classDirectory).value / dest.toString()))
74
+ for ((files, reference) <- patches) {
75
+ val diff = files.filterNot(file => overwrittenBinaries.contains(file.relativeTo(reference).get))
76
+ // Copy all the specialized classes in the stdlib
77
+ // no need to update any stamps as these classes exist nowhere in the analysis
78
+ for (orig <- diff; dest <- orig.relativeTo(reference)) {
79
+ IO .copyFile(orig, ((Compile / classDirectory).value / dest.toString()))
80
+ }
108
81
}
109
82
110
83
previous
111
84
.withAnalysis(analysis.copy(stamps = stamps)) // update the analysis with the correct stamps
112
85
.withHasModified(true ) // mark it as updated for sbt to update its caches
86
+
113
87
}
114
88
)
115
89
90
+ def fetch (stream : TaskStreams , jar : File ) = {
91
+ val cache = stream.cacheDirectory
92
+ val target = cache / jar.getName()
93
+
94
+ if (! target.exists()) {
95
+ IO .createDirectory(target)
96
+ }
97
+
98
+ (FileFunction .cached(cache / " fetch-scala-library-classes" , FilesInfo .lastModified, FilesInfo .exists) { _ =>
99
+ stream.log.info(s " Unpacking scala-library binaries to persistent directory: ${target.getAbsolutePath}" )
100
+ IO .unzip(jar, target)
101
+ (target ** " *.class" ).get.toSet ++ (target ** " *.sjsir" ).get.toSet
102
+ } (Set (jar)), target)
103
+ }
104
+
116
105
private lazy val filesToCopy = Set (
117
- " scala/Tuple1.class " ,
118
- " scala/Tuple2.class " ,
119
- " scala/collection/DoubleStepper.class " ,
120
- " scala/collection/IntStepper.class " ,
121
- " scala/collection/LongStepper.class " ,
122
- " scala/collection/immutable/DoubleVectorStepper.class " ,
123
- " scala/collection/immutable/IntVectorStepper.class " ,
124
- " scala/collection/immutable/LongVectorStepper.class " ,
125
- " scala/jdk/DoubleAccumulator.class " ,
126
- " scala/jdk/IntAccumulator.class " ,
127
- " scala/jdk/LongAccumulator.class " ,
128
- " scala/jdk/FunctionWrappers$FromJavaDoubleBinaryOperator.class " ,
129
- " scala/jdk/FunctionWrappers$FromJavaBooleanSupplier.class " ,
130
- " scala/jdk/FunctionWrappers$FromJavaDoubleConsumer.class " ,
131
- " scala/jdk/FunctionWrappers$FromJavaDoublePredicate.class " ,
132
- " scala/jdk/FunctionWrappers$FromJavaDoubleSupplier.class " ,
133
- " scala/jdk/FunctionWrappers$FromJavaDoubleToIntFunction.class " ,
134
- " scala/jdk/FunctionWrappers$FromJavaDoubleToLongFunction.class " ,
135
- " scala/jdk/FunctionWrappers$FromJavaIntBinaryOperator.class " ,
136
- " scala/jdk/FunctionWrappers$FromJavaDoubleUnaryOperator.class " ,
137
- " scala/jdk/FunctionWrappers$FromJavaIntPredicate.class " ,
138
- " scala/jdk/FunctionWrappers$FromJavaIntConsumer.class " ,
139
- " scala/jdk/FunctionWrappers$FromJavaIntSupplier.class " ,
140
- " scala/jdk/FunctionWrappers$FromJavaIntToDoubleFunction.class " ,
141
- " scala/jdk/FunctionWrappers$FromJavaIntToLongFunction.class " ,
142
- " scala/jdk/FunctionWrappers$FromJavaIntUnaryOperator.class " ,
143
- " scala/jdk/FunctionWrappers$FromJavaLongBinaryOperator.class " ,
144
- " scala/jdk/FunctionWrappers$FromJavaLongConsumer.class " ,
145
- " scala/jdk/FunctionWrappers$FromJavaLongPredicate.class " ,
146
- " scala/jdk/FunctionWrappers$FromJavaLongSupplier.class " ,
147
- " scala/jdk/FunctionWrappers$FromJavaLongToDoubleFunction.class " ,
148
- " scala/jdk/FunctionWrappers$FromJavaLongToIntFunction.class " ,
149
- " scala/jdk/FunctionWrappers$FromJavaLongUnaryOperator.class " ,
150
- " scala/collection/ArrayOps$ReverseIterator.class " ,
151
- " scala/runtime/NonLocalReturnControl.class " ,
152
- " scala/util/Sorting.class " , " scala/util/Sorting$.class " , // Contains @specialized annotation
153
- )
106
+ " scala/Tuple1" ,
107
+ " scala/Tuple2" ,
108
+ " scala/collection/DoubleStepper" ,
109
+ " scala/collection/IntStepper" ,
110
+ " scala/collection/LongStepper" ,
111
+ " scala/collection/immutable/DoubleVectorStepper" ,
112
+ " scala/collection/immutable/IntVectorStepper" ,
113
+ " scala/collection/immutable/LongVectorStepper" ,
114
+ " scala/jdk/DoubleAccumulator" ,
115
+ " scala/jdk/IntAccumulator" ,
116
+ " scala/jdk/LongAccumulator" ,
117
+ " scala/jdk/FunctionWrappers$FromJavaDoubleBinaryOperator" ,
118
+ " scala/jdk/FunctionWrappers$FromJavaBooleanSupplier" ,
119
+ " scala/jdk/FunctionWrappers$FromJavaDoubleConsumer" ,
120
+ " scala/jdk/FunctionWrappers$FromJavaDoublePredicate" ,
121
+ " scala/jdk/FunctionWrappers$FromJavaDoubleSupplier" ,
122
+ " scala/jdk/FunctionWrappers$FromJavaDoubleToIntFunction" ,
123
+ " scala/jdk/FunctionWrappers$FromJavaDoubleToLongFunction" ,
124
+ " scala/jdk/FunctionWrappers$FromJavaIntBinaryOperator" ,
125
+ " scala/jdk/FunctionWrappers$FromJavaDoubleUnaryOperator" ,
126
+ " scala/jdk/FunctionWrappers$FromJavaIntPredicate" ,
127
+ " scala/jdk/FunctionWrappers$FromJavaIntConsumer" ,
128
+ " scala/jdk/FunctionWrappers$FromJavaIntSupplier" ,
129
+ " scala/jdk/FunctionWrappers$FromJavaIntToDoubleFunction" ,
130
+ " scala/jdk/FunctionWrappers$FromJavaIntToLongFunction" ,
131
+ " scala/jdk/FunctionWrappers$FromJavaIntUnaryOperator" ,
132
+ " scala/jdk/FunctionWrappers$FromJavaLongBinaryOperator" ,
133
+ " scala/jdk/FunctionWrappers$FromJavaLongConsumer" ,
134
+ " scala/jdk/FunctionWrappers$FromJavaLongPredicate" ,
135
+ " scala/jdk/FunctionWrappers$FromJavaLongSupplier" ,
136
+ " scala/jdk/FunctionWrappers$FromJavaLongToDoubleFunction" ,
137
+ " scala/jdk/FunctionWrappers$FromJavaLongToIntFunction" ,
138
+ " scala/jdk/FunctionWrappers$FromJavaLongUnaryOperator" ,
139
+ " scala/collection/ArrayOps$ReverseIterator" ,
140
+ " scala/runtime/NonLocalReturnControl" ,
141
+ " scala/util/Sorting" , " scala/util/Sorting$" ,
142
+ ).flatMap(f => Seq ( s " $f .class " , s " $f .sjsir " ))
154
143
155
144
}
0 commit comments