Android Extensions: Add global cache flag in compiler plugin
This commit is contained in:
+29
-6
@@ -19,14 +19,12 @@ package org.jetbrains.kotlin.android.synthetic.idea
|
||||
import com.intellij.openapi.externalSystem.model.DataNode
|
||||
import com.intellij.openapi.externalSystem.model.ProjectKeys
|
||||
import com.intellij.openapi.externalSystem.model.project.ModuleData
|
||||
import com.intellij.openapi.externalSystem.model.project.ProjectData
|
||||
import com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil
|
||||
import com.intellij.openapi.util.Key
|
||||
import org.gradle.api.Project
|
||||
import org.gradle.tooling.model.idea.IdeaModule
|
||||
import org.gradle.tooling.model.idea.IdeaProject
|
||||
import org.jetbrains.kotlin.android.synthetic.AndroidCommandLineProcessor
|
||||
import org.jetbrains.kotlin.android.synthetic.AndroidCommandLineProcessor.Companion.ANDROID_COMPILER_PLUGIN_ID
|
||||
import org.jetbrains.kotlin.android.synthetic.AndroidCommandLineProcessor.Companion.DEFAULT_CACHE_IMPL_OPTION
|
||||
import org.jetbrains.kotlin.android.synthetic.AndroidCommandLineProcessor.Companion.EXPERIMENTAL_OPTION
|
||||
import org.jetbrains.kotlin.android.synthetic.AndroidCommandLineProcessor.Companion.ENABLED_OPTION
|
||||
import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments
|
||||
@@ -46,14 +44,22 @@ var DataNode<ModuleData>.hasAndroidExtensionsPlugin: Boolean
|
||||
var DataNode<ModuleData>.isExperimental: Boolean
|
||||
by NotNullableUserDataProperty(Key.create<Boolean>("ANDROID_EXTENSIONS_IS_EXPERIMENTAL"), false)
|
||||
|
||||
private const val DEFAULT_CACHE_IMPLEMENTATION_DEFAULT_VALUE = "hashMap"
|
||||
|
||||
var DataNode<ModuleData>.defaultCacheImplementation: String
|
||||
by NotNullableUserDataProperty(Key.create<String>("ANDROID_EXTENSIONS_DEFAULT_CACHE_IMPL"),
|
||||
DEFAULT_CACHE_IMPLEMENTATION_DEFAULT_VALUE)
|
||||
|
||||
interface AndroidExtensionsGradleModel : Serializable {
|
||||
val hasAndroidExtensionsPlugin: Boolean
|
||||
val isExperimental: Boolean
|
||||
val defaultCacheImplementation: String
|
||||
}
|
||||
|
||||
class AndroidExtensionsGradleModelImpl(
|
||||
override val hasAndroidExtensionsPlugin: Boolean,
|
||||
override val isExperimental: Boolean
|
||||
override val isExperimental: Boolean,
|
||||
override val defaultCacheImplementation: String
|
||||
) : AndroidExtensionsGradleModel
|
||||
|
||||
@Suppress("unused")
|
||||
@@ -66,6 +72,7 @@ class AndroidExtensionsProjectResolverExtension : AbstractProjectResolverExtensi
|
||||
|
||||
ideModule.hasAndroidExtensionsPlugin = androidExtensionsModel.hasAndroidExtensionsPlugin
|
||||
ideModule.isExperimental = androidExtensionsModel.isExperimental
|
||||
ideModule.defaultCacheImplementation = androidExtensionsModel.defaultCacheImplementation
|
||||
|
||||
super.populateModuleExtraModels(gradleModule, ideModule)
|
||||
}
|
||||
@@ -82,7 +89,9 @@ class AndroidExtensionsModelBuilderService : ModelBuilderService {
|
||||
override fun buildAll(modelName: String?, project: Project): Any {
|
||||
val androidExtensionsPlugin = project.plugins.findPlugin("kotlin-android-extensions")
|
||||
|
||||
val isExperimental = project.extensions.findByName("androidExtensions")?.let { ext ->
|
||||
val androidExtensionsExtension = project.extensions.findByName("androidExtensions")
|
||||
|
||||
val isExperimental = androidExtensionsExtension?.let { ext ->
|
||||
val isExperimentalMethod = ext::class.java.methods
|
||||
.firstOrNull { it.name == "isExperimental" && it.parameterCount == 0 }
|
||||
?: return@let false
|
||||
@@ -90,7 +99,20 @@ class AndroidExtensionsModelBuilderService : ModelBuilderService {
|
||||
isExperimentalMethod.invoke(ext) as? Boolean
|
||||
} ?: false
|
||||
|
||||
return AndroidExtensionsGradleModelImpl(androidExtensionsPlugin != null, isExperimental)
|
||||
val defaultCacheImplementation = androidExtensionsExtension?.let { ext ->
|
||||
val defaultCacheImplementationMethod = ext::class.java.methods.firstOrNull {
|
||||
it.name == "getDefaultCacheImplementation" && it.parameterCount == 0
|
||||
} ?: return@let DEFAULT_CACHE_IMPLEMENTATION_DEFAULT_VALUE
|
||||
|
||||
val enumValue = defaultCacheImplementationMethod.invoke(ext) ?: return@let DEFAULT_CACHE_IMPLEMENTATION_DEFAULT_VALUE
|
||||
|
||||
val optionNameMethod = enumValue::class.java.methods.firstOrNull { it.name == "getOptionName" && it.parameterCount == 0 }
|
||||
?: return@let DEFAULT_CACHE_IMPLEMENTATION_DEFAULT_VALUE
|
||||
|
||||
optionNameMethod.invoke(enumValue) as? String
|
||||
} ?: DEFAULT_CACHE_IMPLEMENTATION_DEFAULT_VALUE
|
||||
|
||||
return AndroidExtensionsGradleModelImpl(androidExtensionsPlugin != null, isExperimental, defaultCacheImplementation)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,6 +135,7 @@ class AndroidExtensionsGradleImportHandler : GradleProjectImportHandler {
|
||||
if (moduleNode.hasAndroidExtensionsPlugin) {
|
||||
newPluginOptions += makePluginOption(EXPERIMENTAL_OPTION.name, moduleNode.isExperimental.toString())
|
||||
newPluginOptions += makePluginOption(ENABLED_OPTION.name, moduleNode.hasAndroidExtensionsPlugin.toString())
|
||||
newPluginOptions += makePluginOption(DEFAULT_CACHE_IMPL_OPTION.name, moduleNode.defaultCacheImplementation)
|
||||
}
|
||||
|
||||
commonArguments.pluginOptions = newPluginOptions.toTypedArray()
|
||||
|
||||
+22
-6
@@ -16,20 +16,25 @@
|
||||
|
||||
package org.jetbrains.kotlin.android.synthetic.idea
|
||||
|
||||
import com.intellij.openapi.application.ApplicationManager
|
||||
import com.intellij.openapi.module.Module
|
||||
import kotlinx.android.extensions.CacheImplementation
|
||||
import org.jetbrains.android.facet.AndroidFacet
|
||||
import org.jetbrains.kotlin.analyzer.ModuleInfo
|
||||
import org.jetbrains.kotlin.android.synthetic.AndroidCommandLineProcessor.Companion.ANDROID_COMPILER_PLUGIN_ID
|
||||
import org.jetbrains.kotlin.android.synthetic.AndroidCommandLineProcessor.Companion.EXPERIMENTAL_OPTION
|
||||
import org.jetbrains.kotlin.android.synthetic.AndroidCommandLineProcessor.Companion.ENABLED_OPTION
|
||||
import org.jetbrains.kotlin.android.synthetic.AndroidCommandLineProcessor.Companion.DEFAULT_CACHE_IMPL_OPTION
|
||||
import org.jetbrains.kotlin.android.synthetic.AndroidComponentRegistrar.Companion.parseCacheImplementationType
|
||||
import org.jetbrains.kotlin.compiler.plugin.CliOption
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.ModuleSourceInfo
|
||||
import org.jetbrains.kotlin.idea.facet.KotlinFacet
|
||||
|
||||
private val ANNOTATION_OPTION_PREFIX = "plugin:$ANDROID_COMPILER_PLUGIN_ID:"
|
||||
|
||||
private fun Module.isOptionEnabledInFacet(option: CliOption): Boolean {
|
||||
val kotlinFacet = KotlinFacet.get(this) ?: return false
|
||||
val commonArgs = kotlinFacet.configuration.settings.compilerArguments ?: return false
|
||||
private fun Module.getOptionValueInFacet(option: CliOption): String? {
|
||||
val kotlinFacet = KotlinFacet.get(this) ?: return null
|
||||
val commonArgs = kotlinFacet.configuration.settings.compilerArguments ?: return null
|
||||
|
||||
val prefix = ANNOTATION_OPTION_PREFIX + option.name + "="
|
||||
|
||||
@@ -37,14 +42,25 @@ private fun Module.isOptionEnabledInFacet(option: CliOption): Boolean {
|
||||
?.firstOrNull { it.startsWith(prefix) }
|
||||
?.substring(prefix.length)
|
||||
|
||||
return optionValue == "true"
|
||||
return optionValue
|
||||
}
|
||||
|
||||
private fun isTestMode(module: Module): Boolean {
|
||||
return ApplicationManager.getApplication().isUnitTestMode && AndroidFacet.getInstance(module) != null
|
||||
}
|
||||
|
||||
internal val Module.androidExtensionsIsEnabled: Boolean
|
||||
get() = isOptionEnabledInFacet(ENABLED_OPTION)
|
||||
get() = isTestMode(this) || getOptionValueInFacet(ENABLED_OPTION) == "true"
|
||||
|
||||
internal val ModuleInfo.androidExtensionsIsExperimental: Boolean
|
||||
get() {
|
||||
val module = (this as? ModuleSourceInfo)?.module ?: return false
|
||||
return module.isOptionEnabledInFacet(EXPERIMENTAL_OPTION)
|
||||
if (isTestMode(module)) return true
|
||||
return module.getOptionValueInFacet(EXPERIMENTAL_OPTION) == "true"
|
||||
}
|
||||
|
||||
val ModuleInfo.androidExtensionsGlobalCacheImpl: CacheImplementation
|
||||
get() {
|
||||
val module = (this as? ModuleSourceInfo)?.module ?: return CacheImplementation.NO_CACHE
|
||||
return parseCacheImplementationType(module.getOptionValueInFacet(DEFAULT_CACHE_IMPL_OPTION))
|
||||
}
|
||||
+7
-5
@@ -16,13 +16,15 @@
|
||||
|
||||
package org.jetbrains.kotlin.android.synthetic.idea
|
||||
|
||||
import kotlinx.android.extensions.CacheImplementation
|
||||
import org.jetbrains.kotlin.android.synthetic.codegen.AbstractAndroidExtensionsExpressionCodegenExtension
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.getModuleInfo
|
||||
import org.jetbrains.kotlin.psi.KtClassOrObject
|
||||
import org.jetbrains.kotlin.psi.KtElement
|
||||
|
||||
class IDEAndroidExtensionsExpressionCodegenExtension : AbstractAndroidExtensionsExpressionCodegenExtension() {
|
||||
override fun isExperimental(clazz: KtClassOrObject): Boolean {
|
||||
val moduleInfo = clazz.getModuleInfo() ?: return false
|
||||
return moduleInfo.androidExtensionsIsExperimental
|
||||
}
|
||||
override fun isExperimental(element: KtElement?) =
|
||||
element?.getModuleInfo()?.androidExtensionsIsExperimental ?: false
|
||||
|
||||
override fun getGlobalCacheImpl(element: KtElement?) =
|
||||
element?.getModuleInfo()?.androidExtensionsGlobalCacheImpl ?: CacheImplementation.DEFAULT
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright 2010-2017 JetBrains s.r.o.
|
||||
*
|
||||
* 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 org.jetbrains.kotlin.android.synthetic.idea
|
||||
|
||||
import kotlinx.android.extensions.CacheImplementation
|
||||
import org.jetbrains.kotlin.android.synthetic.codegen.AbstractAndroidOnDestroyClassBuilderInterceptorExtension
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.getModuleInfo
|
||||
import org.jetbrains.kotlin.psi.KtElement
|
||||
|
||||
class IDEAndroidOnDestroyClassBuilderInterceptorExtension : AbstractAndroidOnDestroyClassBuilderInterceptorExtension() {
|
||||
override fun getGlobalCacheImpl(element: KtElement) = element.getModuleInfo().androidExtensionsGlobalCacheImpl
|
||||
}
|
||||
Reference in New Issue
Block a user