-
Notifications
You must be signed in to change notification settings - Fork 36
/
symbolProcessors.kt
200 lines (178 loc) · 6.47 KB
/
symbolProcessors.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* Copyright (C) 2021 Zac Sweers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.tschuchort.compiletesting
import com.google.devtools.ksp.AbstractKotlinSymbolProcessingExtension
import com.google.devtools.ksp.KspOptions
import com.google.devtools.ksp.processing.KSPLogger
import com.google.devtools.ksp.processing.SymbolProcessorProvider
import com.google.devtools.ksp.processing.impl.MessageCollectorBasedKSPLogger
import org.jetbrains.kotlin.cli.common.messages.MessageRenderer
import org.jetbrains.kotlin.cli.common.messages.PrintingMessageCollector
import org.jetbrains.kotlin.com.intellij.mock.MockProject
import org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.resolve.jvm.extensions.AnalysisHandlerExtension
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
import java.io.File
/**
* The list of symbol processors for the kotlin compilation.
* https://goo.gle/ksp
*/
var KotlinCompilation.symbolProcessorProviders: List<SymbolProcessorProvider>
get() = getKspRegistrar().processorProviders
set(value) {
val registrar = getKspRegistrar()
registrar.processorProviders = value
}
/**
* The directory where generated KSP sources are written
*/
val KotlinCompilation.kspSourcesDir: File
get() = kspWorkingDir.resolve("sources")
/**
* Arbitrary arguments to be passed to ksp
*/
var KotlinCompilation.kspArgs: MutableMap<String, String>
get() = getKspRegistrar().options
set(value) {
val registrar = getKspRegistrar()
registrar.options = value
}
/**
* Controls for enabling incremental processing in KSP.
*/
var KotlinCompilation.kspIncremental: Boolean
get() = getKspRegistrar().incremental
set(value) {
val registrar = getKspRegistrar()
registrar.incremental = value
}
/**
* Controls for enabling incremental processing logs in KSP.
*/
var KotlinCompilation.kspIncrementalLog: Boolean
get() = getKspRegistrar().incrementalLog
set(value) {
val registrar = getKspRegistrar()
registrar.incrementalLog = value
}
private val KotlinCompilation.kspJavaSourceDir: File
get() = kspSourcesDir.resolve("java")
private val KotlinCompilation.kspKotlinSourceDir: File
get() = kspSourcesDir.resolve("kotlin")
private val KotlinCompilation.kspResources: File
get() = kspSourcesDir.resolve("resources")
/**
* The working directory for KSP
*/
private val KotlinCompilation.kspWorkingDir: File
get() = workingDir.resolve("ksp")
/**
* The directory where compiled KSP classes are written
*/
// TODO this seems to be ignored by KSP and it is putting classes into regular classes directory
// but we still need to provide it in the KSP options builder as it is required
// once it works, we should make the property public.
private val KotlinCompilation.kspClassesDir: File
get() = kspWorkingDir.resolve("classes")
/**
* The directory where compiled KSP caches are written
*/
private val KotlinCompilation.kspCachesDir: File
get() = kspWorkingDir.resolve("caches")
/**
* Custom subclass of [AbstractKotlinSymbolProcessingExtension] where processors are pre-defined instead of being
* loaded via ServiceLocator.
*/
private class KspTestExtension(
options: KspOptions,
private val testProcessorProviders: List<SymbolProcessorProvider>,
logger: KSPLogger
) : AbstractKotlinSymbolProcessingExtension(
options = options,
logger = logger,
testMode = false
) {
override fun loadProviders() = testProcessorProviders
}
/**
* Registers the [KspTestExtension] to load the given list of processors.
*/
private class KspCompileTestingComponentRegistrar(
private val compilation: KotlinCompilation
) : ComponentRegistrar {
var processorProviders = emptyList<SymbolProcessorProvider>()
var options: MutableMap<String, String> = mutableMapOf()
var incremental: Boolean = false
var incrementalLog: Boolean = false
override fun registerProjectComponents(project: MockProject, configuration: CompilerConfiguration) {
if (processorProviders.isEmpty()) {
return
}
val options = KspOptions.Builder().apply {
this.projectBaseDir = compilation.kspWorkingDir
this.processingOptions.putAll(compilation.kspArgs)
this.incremental = this@KspCompileTestingComponentRegistrar.incremental
this.incrementalLog = this@KspCompileTestingComponentRegistrar.incrementalLog
this.cachesDir = compilation.kspCachesDir.also {
it.deleteRecursively()
it.mkdirs()
}
this.kspOutputDir = compilation.kspSourcesDir.also {
it.deleteRecursively()
it.mkdirs()
}
this.classOutputDir = compilation.kspClassesDir.also {
it.deleteRecursively()
it.mkdirs()
}
this.javaOutputDir = compilation.kspJavaSourceDir.also {
it.deleteRecursively()
it.mkdirs()
}
this.kotlinOutputDir = compilation.kspKotlinSourceDir.also {
it.deleteRecursively()
it.mkdirs()
}
this.resourceOutputDir = compilation.kspResources.also {
it.deleteRecursively()
it.mkdirs()
}
}.build()
// Temporary until friend-paths is fully supported https://youtrack.jetbrains.com/issue/KT-34102
@Suppress("invisible_member")
val messageCollectorBasedKSPLogger = MessageCollectorBasedKSPLogger(
PrintingMessageCollector(
compilation.internalMessageStreamAccess,
MessageRenderer.GRADLE_STYLE,
compilation.verbose
)
)
val registrar = KspTestExtension(options, processorProviders, messageCollectorBasedKSPLogger)
AnalysisHandlerExtension.registerExtension(project, registrar)
}
}
/**
* Gets the test registrar from the plugin list or adds if it does not exist.
*/
private fun KotlinCompilation.getKspRegistrar(): KspCompileTestingComponentRegistrar {
compilerPlugins.firstIsInstanceOrNull<KspCompileTestingComponentRegistrar>()?.let {
return it
}
val kspRegistrar = KspCompileTestingComponentRegistrar(this)
compilerPlugins = compilerPlugins + kspRegistrar
return kspRegistrar
}