Check supportv4 library presence on the spot
This commit is contained in:
+2
-6
@@ -38,7 +38,6 @@ import org.jetbrains.kotlin.psi.JetFile
|
||||
public object AndroidConfigurationKeys {
|
||||
public val ANDROID_RES_PATH: CompilerConfigurationKey<List<String>> = CompilerConfigurationKey.create<List<String>>("android resources search path")
|
||||
public val ANDROID_MANIFEST: CompilerConfigurationKey<String> = CompilerConfigurationKey.create<String>("android manifest file")
|
||||
public val SUPPORT_V4: CompilerConfigurationKey<String> = CompilerConfigurationKey.create<String>("'true' if compiled with support-v4 library")
|
||||
}
|
||||
|
||||
public class AndroidCommandLineProcessor : CommandLineProcessor {
|
||||
@@ -47,12 +46,11 @@ public class AndroidCommandLineProcessor : CommandLineProcessor {
|
||||
|
||||
public val RESOURCE_PATH_OPTION: CliOption = CliOption("androidRes", "<path>", "Android resources path", allowMultipleOccurrences = true)
|
||||
public val MANIFEST_FILE_OPTION: CliOption = CliOption("androidManifest", "<path>", "Android manifest file")
|
||||
public val SUPPORT_V4_OPTION: CliOption = CliOption("supportV4", "<path>", "Support android-v4 library", required = false)
|
||||
}
|
||||
|
||||
override val pluginId: String = ANDROID_COMPILER_PLUGIN_ID
|
||||
|
||||
override val pluginOptions: Collection<CliOption> = listOf(RESOURCE_PATH_OPTION, MANIFEST_FILE_OPTION, SUPPORT_V4_OPTION)
|
||||
override val pluginOptions: Collection<CliOption> = listOf(RESOURCE_PATH_OPTION, MANIFEST_FILE_OPTION)
|
||||
|
||||
override fun processOption(option: CliOption, value: String, configuration: CompilerConfiguration) {
|
||||
when (option) {
|
||||
@@ -62,7 +60,6 @@ public class AndroidCommandLineProcessor : CommandLineProcessor {
|
||||
configuration.put(AndroidConfigurationKeys.ANDROID_RES_PATH, paths)
|
||||
}
|
||||
MANIFEST_FILE_OPTION -> configuration.put(AndroidConfigurationKeys.ANDROID_MANIFEST, value)
|
||||
SUPPORT_V4_OPTION -> configuration.put(AndroidConfigurationKeys.SUPPORT_V4, value)
|
||||
else -> throw CliOptionProcessingException("Unknown option: ${option.name}")
|
||||
}
|
||||
}
|
||||
@@ -80,10 +77,9 @@ public class AndroidComponentRegistrar : ComponentRegistrar {
|
||||
public override fun registerProjectComponents(project: MockProject, configuration: CompilerConfiguration) {
|
||||
val androidResPath = configuration.get(AndroidConfigurationKeys.ANDROID_RES_PATH)
|
||||
val androidManifest = configuration.get(AndroidConfigurationKeys.ANDROID_MANIFEST)
|
||||
val supportV4 = configuration.get(AndroidConfigurationKeys.SUPPORT_V4) ?: "false"
|
||||
|
||||
if (androidResPath != null && androidManifest != null) {
|
||||
val xmlProcessor = CliSyntheticFileGenerator(project, androidManifest, androidResPath, supportV4 == "true")
|
||||
val xmlProcessor = CliSyntheticFileGenerator(project, androidManifest, androidResPath)
|
||||
|
||||
project.registerService(javaClass<SyntheticFileGenerator>(), xmlProcessor)
|
||||
project.registerService(javaClass<AndroidLayoutXmlFileManager>(), CliAndroidLayoutXmlFileManager(project, androidManifest, androidResPath))
|
||||
|
||||
+3
-7
@@ -30,19 +30,15 @@ import kotlin.properties.Delegates
|
||||
public open class CliSyntheticFileGenerator(
|
||||
project: Project,
|
||||
private val manifestPath: String,
|
||||
private val resDirectories: List<String>,
|
||||
private val supportV4: Boolean
|
||||
private val resDirectories: List<String>
|
||||
) : SyntheticFileGenerator(project) {
|
||||
|
||||
private val javaPsiFacade: JavaPsiFacade by lazy { JavaPsiFacade.getInstance(project) }
|
||||
private val projectScope: GlobalSearchScope by lazy { GlobalSearchScope.allScope(project) }
|
||||
|
||||
private val cachedJetFiles by lazy {
|
||||
generateSyntheticJetFiles(generateSyntheticFiles(true, projectScope))
|
||||
}
|
||||
|
||||
override fun supportV4(): Boolean {
|
||||
return supportV4
|
||||
val supportV4 = supportV4Available(javaPsiFacade, projectScope)
|
||||
generateSyntheticJetFiles(generateSyntheticFiles(true, projectScope, supportV4))
|
||||
}
|
||||
|
||||
override val layoutXmlFileManager: CliAndroidLayoutXmlFileManager by Delegates.lazy {
|
||||
|
||||
+18
-11
@@ -39,12 +39,14 @@ public abstract class SyntheticFileGenerator(protected val project: Project) {
|
||||
|
||||
public abstract val layoutXmlFileManager: AndroidLayoutXmlFileManager
|
||||
|
||||
protected abstract fun supportV4(): Boolean
|
||||
|
||||
public abstract fun getSyntheticFiles(): List<JetFile>
|
||||
|
||||
protected fun generateSyntheticFiles(generateCommonFiles: Boolean = true, scope: GlobalSearchScope): List<AndroidSyntheticFile> {
|
||||
val commonFiles = if (generateCommonFiles) generateCommonFiles() else listOf()
|
||||
protected fun generateSyntheticFiles(
|
||||
generateCommonFiles: Boolean,
|
||||
scope: GlobalSearchScope,
|
||||
supportV4: Boolean
|
||||
): List<AndroidSyntheticFile> {
|
||||
val commonFiles = if (generateCommonFiles) generateCommonFiles(supportV4) else listOf()
|
||||
|
||||
return layoutXmlFileManager.getLayoutXmlFiles().flatMap { entry ->
|
||||
val files = entry.getValue()
|
||||
@@ -52,20 +54,22 @@ public abstract class SyntheticFileGenerator(protected val project: Project) {
|
||||
|
||||
val layoutName = entry.getKey()
|
||||
|
||||
val mainLayoutFile = renderMainLayoutFile(layoutName, resources)
|
||||
val mainLayoutFile = renderMainLayoutFile(layoutName, resources, supportV4)
|
||||
val viewLayoutFile = renderViewLayoutFile(layoutName, resources)
|
||||
|
||||
listOf(mainLayoutFile, viewLayoutFile)
|
||||
}.filterNotNull() + commonFiles
|
||||
}
|
||||
|
||||
private fun generateCommonFiles(): List<AndroidSyntheticFile> {
|
||||
private fun generateCommonFiles(supportV4: Boolean): List<AndroidSyntheticFile> {
|
||||
val renderSyntheticFile = renderSyntheticFile("clearCache") {
|
||||
writePackage(AndroidConst.SYNTHETIC_PACKAGE)
|
||||
writeAndroidImports()
|
||||
writeClearCacheFunction(AndroidConst.ACTIVITY_FQNAME)
|
||||
writeClearCacheFunction(AndroidConst.FRAGMENT_FQNAME)
|
||||
if (supportV4()) writeClearCacheFunction(AndroidConst.SUPPORT_FRAGMENT_FQNAME)
|
||||
if (supportV4) {
|
||||
writeClearCacheFunction(AndroidConst.SUPPORT_FRAGMENT_FQNAME)
|
||||
}
|
||||
}
|
||||
val clearCacheFile = renderSyntheticFile
|
||||
|
||||
@@ -74,11 +78,10 @@ public abstract class SyntheticFileGenerator(protected val project: Project) {
|
||||
|
||||
protected abstract fun extractLayoutResources(files: List<PsiFile>, scope: GlobalSearchScope): List<AndroidResource>
|
||||
|
||||
private fun renderMainLayoutFile(layoutName: String, resources: List<AndroidResource>): AndroidSyntheticFile {
|
||||
return renderLayoutFile(layoutName + AndroidConst.LAYOUT_POSTFIX,
|
||||
escapeAndroidIdentifier(layoutName), resources) {
|
||||
private fun renderMainLayoutFile(layoutName: String, resources: List<AndroidResource>, supportV4: Boolean): AndroidSyntheticFile {
|
||||
return renderLayoutFile(layoutName + AndroidConst.LAYOUT_POSTFIX, escapeAndroidIdentifier(layoutName), resources) {
|
||||
val properties = it.mainProperties.toArrayList()
|
||||
if (supportV4()) properties.addAll(it.mainPropertiesForSupportV4)
|
||||
if (supportV4) properties.addAll(it.mainPropertiesForSupportV4)
|
||||
properties
|
||||
}
|
||||
}
|
||||
@@ -155,6 +158,10 @@ public abstract class SyntheticFileGenerator(protected val project: Project) {
|
||||
return null
|
||||
}
|
||||
|
||||
protected fun supportV4Available(javaPsiFacade: JavaPsiFacade, scope: GlobalSearchScope): Boolean {
|
||||
return javaPsiFacade.findClasses(AndroidConst.SUPPORT_FRAGMENT_FQNAME, scope).isNotEmpty()
|
||||
}
|
||||
|
||||
protected fun filterDuplicates(resources: List<AndroidResource>): List<AndroidResource> {
|
||||
val resourceMap = linkedMapOf<String, AndroidResource>()
|
||||
val resourcesToExclude = hashSetOf<String>()
|
||||
|
||||
+5
-5
@@ -47,9 +47,9 @@ class CliSyntheticFileGeneratorForConversionTest(
|
||||
project: Project,
|
||||
manifestPath: String,
|
||||
resDirectories: List<String>,
|
||||
supportV4: Boolean
|
||||
) : CliSyntheticFileGenerator(project, manifestPath, resDirectories, supportV4) {
|
||||
fun gen(scope: GlobalSearchScope) = generateSyntheticFiles(false, scope)
|
||||
private val supportV4: Boolean
|
||||
) : CliSyntheticFileGenerator(project, manifestPath, resDirectories) {
|
||||
fun gen(scope: GlobalSearchScope) = generateSyntheticFiles(false, scope, supportV4)
|
||||
}
|
||||
|
||||
fun UsefulTestCase.createAndroidTestEnvironment(
|
||||
@@ -61,7 +61,7 @@ fun UsefulTestCase.createAndroidTestEnvironment(
|
||||
configuration.put(AndroidConfigurationKeys.ANDROID_RES_PATH, resPaths)
|
||||
configuration.put(AndroidConfigurationKeys.ANDROID_MANIFEST, manifestPath)
|
||||
|
||||
val myEnvironment = KotlinCoreEnvironment.createForTests(getTestRootDisposable()!!, configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES)
|
||||
val myEnvironment = KotlinCoreEnvironment.createForTests(testRootDisposable, configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES)
|
||||
val project = myEnvironment.project
|
||||
|
||||
val declarationsProvider = AndroidTestExternalDeclarationsProvider(project, resPaths, manifestPath, supportV4)
|
||||
@@ -72,5 +72,5 @@ fun UsefulTestCase.createAndroidTestEnvironment(
|
||||
}
|
||||
|
||||
fun getResPaths(path: String): List<String> {
|
||||
return File(path).listFiles { it.name.startsWith("res") && it.isDirectory() }!!.map { "$path${it.name}/" }
|
||||
return File(path).listFiles { it.name.startsWith("res") && it.isDirectory }!!.map { "$path${it.name}/" }
|
||||
}
|
||||
Reference in New Issue
Block a user