@@ -19,8 +19,6 @@ package com.tschuchort.compiletesting
19
19
import com.facebook.buck.jvm.java.javax.SynchronizedToolProvider
20
20
import io.github.classgraph.ClassGraph
21
21
import okio.Buffer
22
- import okio.buffer
23
- import okio.sink
24
22
import org.jetbrains.kotlin.base.kapt3.AptMode
25
23
import org.jetbrains.kotlin.base.kapt3.KaptFlag
26
24
import org.jetbrains.kotlin.base.kapt3.KaptOptions
@@ -33,7 +31,6 @@ import org.jetbrains.kotlin.config.JVMAssertionsMode
33
31
import org.jetbrains.kotlin.config.JvmDefaultMode
34
32
import org.jetbrains.kotlin.config.JvmTarget
35
33
import org.jetbrains.kotlin.config.Services
36
- import org.jetbrains.kotlin.incremental.isJavaFile
37
34
import java.io.*
38
35
import java.lang.RuntimeException
39
36
import java.net.URLClassLoader
@@ -63,7 +60,7 @@ class KotlinCompilation {
63
60
var classpaths: List <File > = emptyList()
64
61
65
62
/* * Source files to be compiled */
66
- var sources: List <KotlinCompilation . SourceFile > = emptyList()
63
+ var sources: List <SourceFile > = emptyList()
67
64
68
65
/* * Annotation processors to be passed to kapt */
69
66
var annotationProcessors: List <Processor > = emptyList()
@@ -129,7 +126,7 @@ class KotlinCompilation {
129
126
* Root modules to resolve in addition to the initial modules,
130
127
* or all modules on the module path if <module> is ALL-MODULE-PATH
131
128
*/
132
- var additionalJavaModules: MutableList <Path > = mutableListOf ()
129
+ var additionalJavaModules: MutableList <File > = mutableListOf ()
133
130
134
131
/* * Don't generate not-null assertions for arguments of platform types */
135
132
var noCallAssertions: Boolean = false
@@ -158,7 +155,7 @@ class KotlinCompilation {
158
155
var assertionsMode: String? = JVMAssertionsMode .DEFAULT .description
159
156
160
157
/* * Path to the .xml build file to compile */
161
- var buildFile: Path ? = null
158
+ var buildFile: File ? = null
162
159
163
160
/* * Compile multifile classes as a hierarchy of parts and facade */
164
161
var inheritMultifileParts: Boolean = false
@@ -170,7 +167,7 @@ class KotlinCompilation {
170
167
var skipRuntimeVersionCheck: Boolean = false
171
168
172
169
/* * Path to JSON file to dump Java to Kotlin declaration mappings */
173
- var declarationsOutputPath: Path ? = null
170
+ var declarationsOutputPath: File ? = null
174
171
175
172
/* * Combine modules for source files and binary dependencies into a single module */
176
173
var singleModule: Boolean = false
@@ -212,7 +209,7 @@ class KotlinCompilation {
212
209
var sanitizeParentheses: Boolean = false
213
210
214
211
/* * Paths to output directories for friend modules (whose internals should be visible) */
215
- var friendPaths: MutableList <Path > = mutableListOf ()
212
+ var friendPaths: MutableList <File > = mutableListOf ()
216
213
217
214
/* *
218
215
* Path to the kotlin-stdlib.jar
@@ -275,17 +272,17 @@ class KotlinCompilation {
275
272
}
276
273
277
274
// Directory for input source files
278
- private val sourcesDir get() = File ( workingDir, " sources" )
275
+ private val sourcesDir get() = workingDir.resolve( " sources" )
279
276
280
277
// *.class files, Jars and resources (non-temporary) that are created by the
281
278
// compilation will land here
282
- val classesDir get() = File ( workingDir, " classes" )
279
+ val classesDir get() = workingDir.resolve( " classes" )
283
280
284
281
// Base directory for kapt stuff
285
- private val kaptBaseDir get() = File ( workingDir, " kapt" )
282
+ private val kaptBaseDir get() = workingDir.resolve( " kapt" )
286
283
287
284
// Java annotation processors that are compile by kapt will put their generated files here
288
- val kaptSourceDir get() = File ( kaptBaseDir, " sources" )
285
+ val kaptSourceDir get() = kaptBaseDir.resolve( " sources" )
289
286
290
287
// Output directory for Kotlin source files generated by kapt
291
288
val kaptKotlinGeneratedDir get() = kaptArgs[OPTION_KAPT_KOTLIN_GENERATED ]
@@ -295,23 +292,8 @@ class KotlinCompilation {
295
292
}
296
293
? : File (kaptBaseDir, " kotlinGenerated" )
297
294
298
- val kaptStubsDir get() = File (kaptBaseDir, " stubs" )
299
- val kaptIncrementalDataDir get() = File (kaptBaseDir, " incrementalData" )
300
-
301
- /* * A Kotlin source file to be compiled */
302
- data class SourceFile (val name : String , val contents : String ) {
303
- /* *
304
- * Writes the source file to the location and returns the
305
- * corresponding [File] object
306
- */
307
- fun writeTo (dir : File ): File =
308
- File (dir, name).apply {
309
- parentFile.mkdirs()
310
- sink().buffer().use {
311
- it.writeUtf8(contents)
312
- }
313
- }
314
- }
295
+ val kaptStubsDir get() = kaptBaseDir.resolve(" stubs" )
296
+ val kaptIncrementalDataDir get() = kaptBaseDir.resolve(" incrementalData" )
315
297
316
298
/* * ExitCode of the entire Kotlin compilation process */
317
299
enum class ExitCode {
@@ -364,7 +346,7 @@ class KotlinCompilation {
364
346
if (javaModulePath != null )
365
347
it.javaModulePath = javaModulePath!! .toString()
366
348
367
- it.additionalJavaModules = additionalJavaModules.map(Path ::toString ).toTypedArray()
349
+ it.additionalJavaModules = additionalJavaModules.map(File ::getAbsolutePath ).toTypedArray()
368
350
it.noCallAssertions = noCallAssertions
369
351
it.noParamAssertions = noParamAssertions
370
352
it.noReceiverAssertions = noReceiverAssertions
@@ -399,7 +381,7 @@ class KotlinCompilation {
399
381
it.sanitizeParentheses = sanitizeParentheses
400
382
401
383
if (friendPaths.isNotEmpty())
402
- it.friendPaths = friendPaths.map(Path ::toString ).toTypedArray()
384
+ it.friendPaths = friendPaths.map(File ::getAbsolutePath ).toTypedArray()
403
385
404
386
if (scriptResolverEnvironment.isNotEmpty())
405
387
it.scriptResolverEnvironment = scriptResolverEnvironment.map { (key, value) -> " $key =\" $value \" " }.toTypedArray()
@@ -445,7 +427,7 @@ class KotlinCompilation {
445
427
KaptComponentRegistrar .Parameters (annotationProcessors, kaptOptions)
446
428
)
447
429
448
- val kotlinSources = sourcesDir.listFilesRecursively().filter(File ::isKotlinFile)
430
+ val kotlinSources = sourcesDir.listFilesRecursively().filter< File > (File ::isKotlinFile)
449
431
val javaSources = sourcesDir.listFilesRecursively().filter(File ::isJavaFile)
450
432
451
433
val sourcePaths = mutableListOf<File >().apply {
@@ -463,7 +445,7 @@ class KotlinCompilation {
463
445
Java files might generate Kotlin files which then need to be compiled in the
464
446
compileKotlin step before the compileJava step). So instead we trick K2JVMCompiler
465
447
by just including an empty .kt-File. */
466
- add(SourceFile (" emptyKotlinFile.kt" , " " ).writeTo (kaptBaseDir))
448
+ add(SourceFile .new (" emptyKotlinFile.kt" , " " ).writeIfNeeded (kaptBaseDir))
467
449
}
468
450
}.map(File ::getAbsolutePath).distinct()
469
451
@@ -658,7 +640,7 @@ class KotlinCompilation {
658
640
kaptKotlinGeneratedDir.mkdirs()
659
641
660
642
// write given sources to working directory
661
- sources.forEach { it.writeTo (sourcesDir) }
643
+ sources.forEach { it.writeIfNeeded (sourcesDir) }
662
644
663
645
/*
664
646
There are 4 steps to the compilation process:
@@ -738,13 +720,11 @@ class KotlinCompilation {
738
720
// TODO("check that jar file actually contains the right classes")
739
721
}
740
722
741
-
742
723
if (jarFile == null )
743
724
log(" Searched host classpaths for $simpleName and found no match" )
744
725
else
745
726
log(" Searched host classpaths for $simpleName and found ${jarFile.path} " )
746
727
747
-
748
728
return jarFile
749
729
}
750
730
0 commit comments