-
Notifications
You must be signed in to change notification settings - Fork 250
/
KspOptions.kt
289 lines (251 loc) · 8.75 KB
/
KspOptions.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
* Copyright 2022 Google LLC
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
*
* 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
*
* http://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.google.devtools.ksp
import org.jetbrains.kotlin.compiler.plugin.AbstractCliOption
import org.jetbrains.kotlin.config.ApiVersion
import org.jetbrains.kotlin.config.LanguageVersion
import org.jetbrains.kotlin.config.toKotlinVersion
import java.io.File
class KspOptions(
val projectBaseDir: File,
val compileClasspath: List<File>,
val javaSourceRoots: List<File>,
val classOutputDir: File,
val javaOutputDir: File,
val kotlinOutputDir: File,
val resourceOutputDir: File,
val processingClasspath: List<File>,
val processors: List<String>,
val processingOptions: Map<String, String>,
val knownModified: List<File>,
val knownRemoved: List<File>,
val cachesDir: File,
val kspOutputDir: File,
val incremental: Boolean,
val incrementalLog: Boolean,
val allWarningsAsErrors: Boolean,
val withCompilation: Boolean,
val returnOkOnError: Boolean,
val changedClasses: List<String>,
val languageVersion: KotlinVersion,
val apiVersion: KotlinVersion,
val compilerVersion: KotlinVersion,
) {
class Builder {
var projectBaseDir: File? = null
val compileClasspath: MutableList<File> = mutableListOf()
val javaSourceRoots: MutableList<File> = mutableListOf()
var classOutputDir: File? = null
var javaOutputDir: File? = null
var kotlinOutputDir: File? = null
var resourceOutputDir: File? = null
val processingClasspath: MutableList<File> = mutableListOf()
val processors: MutableList<String> = mutableListOf()
val processingOptions: MutableMap<String, String> = mutableMapOf()
val knownModified: MutableList<File> = mutableListOf()
val knownRemoved: MutableList<File> = mutableListOf()
var cachesDir: File? = null
var kspOutputDir: File? = null
var incremental: Boolean = false
var incrementalLog: Boolean = false
var allWarningsAsErrors: Boolean = false
var withCompilation: Boolean = false
// Default is false. It can be turned on to workaround KT-30172.
var returnOkOnError: Boolean = false
var changedClasses: MutableList<String> = mutableListOf()
var languageVersion: KotlinVersion = LanguageVersion.LATEST_STABLE.toKotlinVersion()
var apiVersion: KotlinVersion = ApiVersion.LATEST_STABLE.toKotlinVersion()
var compilerVersion: KotlinVersion = KotlinVersion.CURRENT
fun build(): KspOptions {
return KspOptions(
projectBaseDir!!, compileClasspath, javaSourceRoots,
classOutputDir!!,
javaOutputDir!!,
kotlinOutputDir!!,
resourceOutputDir!!,
processingClasspath, processors, processingOptions,
knownModified, knownRemoved, cachesDir!!, kspOutputDir!!, incremental, incrementalLog,
allWarningsAsErrors, withCompilation, returnOkOnError, changedClasses,
languageVersion, apiVersion, compilerVersion
)
}
}
}
fun String?.toKotlinVersion(): KotlinVersion {
if (this == null)
return KotlinVersion.CURRENT
return split('-').first().split('.').map { it.toInt() }.let {
when (it.size) {
1 -> KotlinVersion(it[0], 0, 0)
2 -> KotlinVersion(it[0], it[1], 0)
3 -> KotlinVersion(it[0], it[1], it[2])
else -> KotlinVersion.CURRENT
}
}
}
fun ApiVersion.toKotlinVersion(): KotlinVersion = version.canonical.toKotlinVersion()
enum class KspCliOption(
override val optionName: String,
override val valueDescription: String,
override val description: String,
override val required: Boolean = false,
override val allowMultipleOccurrences: Boolean = false
) : AbstractCliOption {
CLASS_OUTPUT_DIR_OPTION(
"classOutputDir",
"<classOutputDir>",
"Dir of generated classes",
false
),
JAVA_OUTPUT_DIR_OPTION(
"javaOutputDir",
"<javaOutputDir>",
"Dir of generated Java sources",
false
),
KOTLIN_OUTPUT_DIR_OPTION(
"kotlinOutputDir",
"<kotlinOutputDir>",
"Dir of generated Kotlin sources",
false
),
RESOURCE_OUTPUT_DIR_OPTION(
"resourceOutputDir",
"<resourceOutputDir>",
"Dir of generated resources",
false
),
CACHES_DIR_OPTION(
"cachesDir",
"<cachesDir>",
"Dir of caches",
false
),
PROJECT_BASE_DIR_OPTION(
"projectBaseDir",
"<projectBaseDir>",
"path to gradle project",
false
),
KSP_OUTPUT_DIR_OPTION(
"kspOutputDir",
"<kspOutputDir>",
"root of ksp output dirs",
false
),
PROCESSING_OPTIONS_OPTION(
"apoption",
"<apOption>",
"processor defined option",
false,
true
),
PROCESSOR_CLASSPATH_OPTION(
"apclasspath",
"<classpath>",
"processor classpath",
false
),
PROCESSOR_NAME_OPTION(
"processorNames",
"<processorNames>",
"only executes following processor(s)",
false
),
KNOWN_MODIFIED_OPTION(
"knownModified",
"<knownModified>",
"known modified files",
false,
false
),
KNOWN_REMOVED_OPTION(
"knownRemoved",
"<knownRemoved>",
"known removed fiels",
false,
false
),
INCREMENTAL_OPTION(
"incremental",
"<incremental>",
"processing incrementally",
false,
false
),
INCREMENTAL_LOG_OPTION(
"incrementalLog",
"<incrementalLog>",
"log dirty files",
false,
false
),
ALL_WARNINGS_AS_ERRORS_OPTION(
"allWarningsAsErrors",
"<allWarningsAsErrors>",
"treat all warnings as errors",
false,
false
),
WITH_COMPILATION_OPTION(
"withCompilation",
"<withCompilation>",
"Run processors and compilation in a single compiler invocation",
false,
false
),
CHANGED_CLASSES_OPTION(
"changedClasses",
"<changedClasses>",
"canonical / dot-separated names of dirty classes in classpath",
false,
false
),
RETURN_OK_ON_ERROR_OPTION(
"returnOkOnError",
"<returnOkOnError>",
"Return OK even if there are errors",
false,
false
),
}
@Suppress("IMPLICIT_CAST_TO_ANY")
fun KspOptions.Builder.processOption(option: KspCliOption, value: String) = when (option) {
KspCliOption.PROCESSOR_CLASSPATH_OPTION -> processingClasspath += value.split(File.pathSeparator).map {
File(it)
}
KspCliOption.PROCESSOR_NAME_OPTION -> processors += value.split(File.pathSeparator)
KspCliOption.CLASS_OUTPUT_DIR_OPTION -> classOutputDir = File(value)
KspCliOption.JAVA_OUTPUT_DIR_OPTION -> javaOutputDir = File(value)
KspCliOption.KOTLIN_OUTPUT_DIR_OPTION -> kotlinOutputDir = File(value)
KspCliOption.RESOURCE_OUTPUT_DIR_OPTION -> resourceOutputDir = File(value)
KspCliOption.CACHES_DIR_OPTION -> cachesDir = File(value)
KspCliOption.KSP_OUTPUT_DIR_OPTION -> kspOutputDir = File(value)
KspCliOption.PROJECT_BASE_DIR_OPTION -> projectBaseDir = File(value)
KspCliOption.PROCESSING_OPTIONS_OPTION -> {
val (k, v) = value.split('=', ignoreCase = false, limit = 2)
processingOptions.put(k, v)
}
KspCliOption.KNOWN_MODIFIED_OPTION -> knownModified.addAll(value.split(File.pathSeparator).map { File(it) })
KspCliOption.KNOWN_REMOVED_OPTION -> knownRemoved.addAll(value.split(File.pathSeparator).map { File(it) })
KspCliOption.INCREMENTAL_OPTION -> incremental = value.toBoolean()
KspCliOption.INCREMENTAL_LOG_OPTION -> incrementalLog = value.toBoolean()
KspCliOption.ALL_WARNINGS_AS_ERRORS_OPTION -> allWarningsAsErrors = value.toBoolean()
KspCliOption.WITH_COMPILATION_OPTION -> withCompilation = value.toBoolean()
KspCliOption.CHANGED_CLASSES_OPTION -> changedClasses.addAll(value.split(':'))
KspCliOption.RETURN_OK_ON_ERROR_OPTION -> returnOkOnError = value.toBoolean()
}