From 0bf74bda82658e722867f35a2d03d97ba4b185e9 Mon Sep 17 00:00:00 2001 From: Jiaxiang Chen Date: Tue, 12 Sep 2023 14:45:58 -0700 Subject: [PATCH] remove unused KspOptions and KSPCommandLineProcessor from analysis api --- .../ksp/impl/KSPCommandLineProcessor.kt | 55 --- .../ksp/impl/KotlinSymbolProcessing.kt | 14 + .../google/devtools/ksp/impl/KspOptions.kt | 344 ------------------ 3 files changed, 14 insertions(+), 399 deletions(-) delete mode 100644 kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/KSPCommandLineProcessor.kt delete mode 100644 kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/KspOptions.kt diff --git a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/KSPCommandLineProcessor.kt b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/KSPCommandLineProcessor.kt deleted file mode 100644 index ee8a538b86..0000000000 --- a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/KSPCommandLineProcessor.kt +++ /dev/null @@ -1,55 +0,0 @@ -/* - * 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.impl - -import com.google.devtools.ksp.processing.SymbolProcessorProvider -import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys -import org.jetbrains.kotlin.cli.common.config.addKotlinSourceRoots -import org.jetbrains.kotlin.cli.common.messages.MessageCollector -import org.jetbrains.kotlin.cli.jvm.config.addJavaSourceRoots -import org.jetbrains.kotlin.config.CompilerConfiguration -import org.jetbrains.kotlin.config.languageVersionSettings -import org.jetbrains.kotlin.util.ServiceLoaderLite -import java.io.File -import java.net.URLClassLoader - -class KSPCommandLineProcessor(val compilerConfiguration: CompilerConfiguration) { - private val kspOptionsBuilder = KspOptions.Builder().apply { - languageVersionSettings = compilerConfiguration.languageVersionSettings - } - - val kspOptions: KspOptions - get() = kspOptionsBuilder.build() - - lateinit var providers: List - - fun processArgs(args: Array) { - // TODO: support KSP options - val sources = args.toList() - compilerConfiguration.addKotlinSourceRoots(sources) - compilerConfiguration.addJavaSourceRoots(sources.map { File(it) }) - compilerConfiguration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, MessageCollector.NONE) - val processingClasspath = kspOptionsBuilder.processingClasspath - val classLoader = URLClassLoader( - processingClasspath.map { it.toURI().toURL() }.toTypedArray(), - javaClass.classLoader - ) - - providers = ServiceLoaderLite.loadImplementations(SymbolProcessorProvider::class.java, classLoader) - } -} diff --git a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/KotlinSymbolProcessing.kt b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/KotlinSymbolProcessing.kt index a1b9ef749d..19d4cd0388 100644 --- a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/KotlinSymbolProcessing.kt +++ b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/KotlinSymbolProcessing.kt @@ -622,3 +622,17 @@ private fun getDefaultJdkModuleRoots( result } } + +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 + } + } +} diff --git a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/KspOptions.kt b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/KspOptions.kt deleted file mode 100644 index 02b381a6a4..0000000000 --- a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/KspOptions.kt +++ /dev/null @@ -1,344 +0,0 @@ -/* - * 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.impl - -import org.jetbrains.kotlin.compiler.plugin.AbstractCliOption -import org.jetbrains.kotlin.config.ApiVersion -import org.jetbrains.kotlin.config.LanguageVersionSettings -import org.jetbrains.kotlin.config.toKotlinVersion -import java.io.File - -class KspOptions( - val projectBaseDir: File, - val compileClasspath: List, - val javaSourceRoots: List, - - val classOutputDir: File, - val javaOutputDir: File, - val kotlinOutputDir: File, - val resourceOutputDir: File, - - val processingClasspath: List, - val processors: List, - - val processingOptions: Map, - - val knownModified: List, - val knownRemoved: List, - - val cachesDir: File, - val kspOutputDir: File, - val incremental: Boolean, - val incrementalLog: Boolean, - val allWarningsAsErrors: Boolean, - val withCompilation: Boolean, - val returnOkOnError: Boolean, - val changedClasses: List, - - val languageVersionSettings: LanguageVersionSettings, - - val compilerVersion: KotlinVersion, - - val commonSources: List, - - val excludedProcessors: Set, - val mapAnnotationArgumentsInJava: Boolean, -) { - val languageVersion: KotlinVersion = languageVersionSettings.languageVersion.toKotlinVersion() - val apiVersion: KotlinVersion = languageVersionSettings.apiVersion.toKotlinVersion() - - class Builder { - var projectBaseDir: File? = null - val compileClasspath: MutableList = mutableListOf() - val javaSourceRoots: MutableList = mutableListOf() - - var classOutputDir: File? = null - var javaOutputDir: File? = null - var kotlinOutputDir: File? = null - var resourceOutputDir: File? = null - - val processingClasspath: MutableList = mutableListOf() - val processors: MutableList = mutableListOf() - - val processingOptions: MutableMap = mutableMapOf() - - val knownModified: MutableList = mutableListOf() - val knownRemoved: MutableList = 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 = mutableListOf() - - var languageVersionSettings: LanguageVersionSettings? = null - var compilerVersion: KotlinVersion = KotlinVersion.CURRENT - - var commonSources: MutableList = mutableListOf() - - var excludedProcessors: MutableSet = mutableSetOf() - var mapAnnotationArgumentsInJava: Boolean = false - - fun build(): KspOptions { - return KspOptions( - requireNotNull(projectBaseDir) { "A non-null projectBaseDir must be provided" }, - compileClasspath, - javaSourceRoots, - requireNotNull(classOutputDir) { "A non-null classOutputDir must be provided" }, - requireNotNull(javaOutputDir) { "A non-null javaOutputDir must be provided" }, - requireNotNull(kotlinOutputDir) { "A non-null kotlinOutputDir must be provided" }, - requireNotNull(resourceOutputDir) { "A non-null resourceOutputDir must be provided" }, - processingClasspath, - processors, - processingOptions, - knownModified, - knownRemoved, - requireNotNull(cachesDir) { "A non-null cachesDir must be provided" }, - requireNotNull(kspOutputDir) { "A non-null kspOutputDir must be provided" }, - incremental, - incrementalLog, - allWarningsAsErrors, - withCompilation, - returnOkOnError, - changedClasses, - requireNotNull(languageVersionSettings) { "A non-null languageVersionSettings must be provided" }, - compilerVersion, - commonSources, - excludedProcessors, - mapAnnotationArgumentsInJava, - ) - } - } -} - -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", - "", - "Dir of generated classes", - false - ), - - JAVA_OUTPUT_DIR_OPTION( - "javaOutputDir", - "", - "Dir of generated Java sources", - false - ), - - KOTLIN_OUTPUT_DIR_OPTION( - "kotlinOutputDir", - "", - "Dir of generated Kotlin sources", - false - ), - - RESOURCE_OUTPUT_DIR_OPTION( - "resourceOutputDir", - "", - "Dir of generated resources", - false - ), - - CACHES_DIR_OPTION( - "cachesDir", - "", - "Dir of caches", - false - ), - - PROJECT_BASE_DIR_OPTION( - "projectBaseDir", - "", - "path to gradle project", - false - ), - - KSP_OUTPUT_DIR_OPTION( - "kspOutputDir", - "", - "root of ksp output dirs", - false - ), - - PROCESSING_OPTIONS_OPTION( - "apoption", - "", - "processor defined option", - false, - true - ), - - PROCESSOR_CLASSPATH_OPTION( - "apclasspath", - "", - "processor classpath", - false - ), - - PROCESSOR_NAME_OPTION( - "processorNames", - "", - "only executes following processor(s)", - false - ), - - KNOWN_MODIFIED_OPTION( - "knownModified", - "", - "known modified files", - false, - false - ), - - KNOWN_REMOVED_OPTION( - "knownRemoved", - "", - "known removed fiels", - false, - false - ), - - INCREMENTAL_OPTION( - "incremental", - "", - "processing incrementally", - false, - false - ), - - INCREMENTAL_LOG_OPTION( - "incrementalLog", - "", - "log dirty files", - false, - false - ), - - ALL_WARNINGS_AS_ERRORS_OPTION( - "allWarningsAsErrors", - "", - "treat all warnings as errors", - false, - false - ), - - WITH_COMPILATION_OPTION( - "withCompilation", - "", - "Run processors and compilation in a single compiler invocation", - false, - false - ), - - CHANGED_CLASSES_OPTION( - "changedClasses", - "", - "canonical / dot-separated names of dirty classes in classpath", - false, - false - ), - - RETURN_OK_ON_ERROR_OPTION( - "returnOkOnError", - "", - "Return OK even if there are errors", - false, - false - ), - - COMMON_SOURCES_OPTION( - "commonSources", - "", - "common sources", - false, - true - ), - - EXCLUDED_PROCESSORS_OPTION( - "excludedProcessors", - "", - "exclude providers by fqn", - false, - true - ), - - MAP_ANNOTATION_ARGUMENTS_IN_JAVA_OPTION( - "mapAnnotationArgumentsInJava", - "", - "Map types in annotation arguments in Java sources", - 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() - KspCliOption.COMMON_SOURCES_OPTION -> commonSources.addAll(value.split(File.pathSeparator).map { File(it) }) - KspCliOption.EXCLUDED_PROCESSORS_OPTION -> excludedProcessors.addAll(value.split(":")) - KspCliOption.MAP_ANNOTATION_ARGUMENTS_IN_JAVA_OPTION -> mapAnnotationArgumentsInJava = value.toBoolean() -}