11/** Adapted from https://github.com/sbt/sbt/blob/0.13/compile/interface/src/test/scala/xsbt/ScalaCompilerForUnitTesting.scala */
22package xsbt
33
4- import xsbti .compile .SingleOutput
4+ import xsbti .compile .{ CompileProgress , SingleOutput }
55import java .io .File
66import xsbti ._
77import sbt .io .IO
88import xsbti .api .{ ClassLike , Def , DependencyContext }
99import DependencyContext ._
1010import xsbt .api .SameAPI
1111import sbt .internal .util .ConsoleLogger
12+ import dotty .tools .io .PlainFile .toPlainFile
13+ import dotty .tools .xsbt .CompilerBridge
1214
1315import TestCallback .ExtractedClassDependencies
1416
@@ -32,8 +34,8 @@ class ScalaCompilerForUnitTesting {
3234 * Compiles given source code using Scala compiler and returns API representation
3335 * extracted by ExtractAPI class.
3436 */
35- def extractApisFromSrcs (reuseCompilerInstance : Boolean )( srcs : List [String ]* ): Seq [Seq [ClassLike ]] = {
36- val (tempSrcFiles, analysisCallback) = compileSrcs(srcs.toList, reuseCompilerInstance )
37+ def extractApisFromSrcs (srcs : List [String ]* ): Seq [Seq [ClassLike ]] = {
38+ val (tempSrcFiles, analysisCallback) = compileSrcs(srcs.toList)
3739 tempSrcFiles.map(analysisCallback.apis)
3840 }
3941
@@ -91,7 +93,7 @@ class ScalaCompilerForUnitTesting {
9193 * file system-independent way of testing dependencies between source code "files".
9294 */
9395 def extractDependenciesFromSrcs (srcs : List [List [String ]]): ExtractedClassDependencies = {
94- val (_, testCallback) = compileSrcs(srcs, reuseCompilerInstance = true )
96+ val (_, testCallback) = compileSrcs(srcs)
9597
9698 val memberRefDeps = testCallback.classDependencies collect {
9799 case (target, src, DependencyByMemberRef ) => (src, target)
@@ -117,79 +119,53 @@ class ScalaCompilerForUnitTesting {
117119 * useful to compile macros, which cannot be used in the same compilation run that
118120 * defines them.
119121 *
120- * The `reuseCompilerInstance` parameter controls whether the same Scala compiler instance
121- * is reused between compiling source groups. Separate compiler instances can be used to
122- * test stability of API representation (with respect to pickling) or to test handling of
123- * binary dependencies.
124- *
125122 * The sequence of temporary files corresponding to passed snippets and analysis
126123 * callback is returned as a result.
127124 */
128- def compileSrcs (groupedSrcs : List [List [String ]],
129- reuseCompilerInstance : Boolean ): (Seq [File ], TestCallback ) = {
130- // withTemporaryDirectory { temp =>
131- {
125+ def compileSrcs (groupedSrcs : List [List [String ]]): (Seq [File ], TestCallback ) = {
132126 val temp = IO .createTemporaryDirectory
133127 val analysisCallback = new TestCallback
134128 val classesDir = new File (temp, " classes" )
135129 classesDir.mkdir()
136130
137- lazy val commonCompilerInstanceAndCtx = prepareCompiler(classesDir, analysisCallback, classesDir.toString)
131+ val bridge = new CompilerBridge
138132
139133 val files = for ((compilationUnit, unitId) <- groupedSrcs.zipWithIndex) yield {
140- // use a separate instance of the compiler for each group of sources to
141- // have an ability to test for bugs in instability between source and pickled
142- // representation of types
143- val (compiler, ctx) = if (reuseCompilerInstance) commonCompilerInstanceAndCtx else
144- prepareCompiler(classesDir, analysisCallback, classesDir.toString)
145- val run = compiler.newRun(ctx)
146- val srcFiles = compilationUnit.toSeq.zipWithIndex map {
147- case (src, i) =>
134+ val srcFiles = compilationUnit.toSeq.zipWithIndex.map {
135+ (src, i) =>
148136 val fileName = s " Test- $unitId- $i.scala "
149137 prepareSrcFile(temp, fileName, src)
150138 }
151- val srcFilePaths = srcFiles.map(srcFile => srcFile.getAbsolutePath).toList
152139
153- run.compile(srcFilePaths)
140+ val virtualSrcFiles = srcFiles.map(file => TestVirtualFile (file.toPath)).toArray
141+ val classesDirPath = classesDir.getAbsolutePath.toString
142+ val output = new SingleOutput :
143+ def getOutputDirectory () = classesDir
144+
145+ bridge.run(
146+ virtualSrcFiles.toArray,
147+ new TestDependencyChanges ,
148+ Array (" -Yforce-sbt-phases" , " -classpath" , classesDirPath, " -usejavacp" , " -d" , classesDirPath),
149+ output,
150+ analysisCallback,
151+ new TestReporter ,
152+ new CompileProgress {},
153+ new TestLogger
154+ )
154155
155- // srcFilePaths.foreach(f => new File(f).delete)
156156 srcFiles
157157 }
158158 (files.flatten.toSeq, analysisCallback)
159- }
160159 }
161160
162161 def compileSrcs (srcs : String * ): (Seq [File ], TestCallback ) = {
163- compileSrcs(List (srcs.toList), reuseCompilerInstance = true )
162+ compileSrcs(List (srcs.toList))
164163 }
165164
166165 private def prepareSrcFile (baseDir : File , fileName : String , src : String ): File = {
167166 val srcFile = new File (baseDir, fileName)
168167 IO .write(srcFile, src)
169168 srcFile
170169 }
171-
172- private def prepareCompiler (outputDir : File , analysisCallback : AnalysisCallback , classpath : String = " ." ) = {
173- val args = Array .empty[String ]
174-
175- import dotty .tools .dotc .{Compiler , Driver }
176- import dotty .tools .dotc .core .Contexts ._
177-
178- val driver = new TestDriver
179- val ctx = (new ContextBase ).initialCtx.fresh.setSbtCallback(analysisCallback)
180- driver.getCompiler(Array (" -classpath" , classpath, " -usejavacp" , " -d" , outputDir.getAbsolutePath), ctx)
181- }
182-
183- private object ConsoleReporter extends Reporter {
184- def reset (): Unit = ()
185- def hasErrors : Boolean = false
186- def hasWarnings : Boolean = false
187- def printWarnings (): Unit = ()
188- def problems (): Array [xsbti.Problem ] = Array .empty
189- def log (problem : xsbti.Problem ): Unit = println(problem.message)
190- def comment (pos : Position , msg : String ): Unit = ()
191- def printSummary (): Unit = ()
192- }
193-
194170}
195171
0 commit comments