Introduce tooling model for Android extensions plugin
Introduce Gradle models for plugins 'kotlin-android-extensions'.
This commit is contained in:
committed by
Sergey Igushkin
parent
5a423e8dcd
commit
3c88f761a6
+3
-1
@@ -284,7 +284,9 @@ abstract class BaseGradleIT {
|
|||||||
|
|
||||||
val connection = GradleConnector.newConnector().forProjectDirectory(projectDir).connect()
|
val connection = GradleConnector.newConnector().forProjectDirectory(projectDir).connect()
|
||||||
val options = defaultBuildOptions()
|
val options = defaultBuildOptions()
|
||||||
val model = connection.action(ModelFetcherBuildAction(modelType)) .withArguments("-Pkotlin_version=" + options.kotlinVersion).run()
|
val arguments = mutableListOf("-Pkotlin_version=${options.kotlinVersion}")
|
||||||
|
options.androidGradlePluginVersion?.let { arguments.add("-Pandroid_tools_version=$it") }
|
||||||
|
val model = connection.action(ModelFetcherBuildAction(modelType)).withArguments(arguments).run()
|
||||||
connection.close()
|
connection.close()
|
||||||
return model
|
return model
|
||||||
}
|
}
|
||||||
|
|||||||
+54
@@ -0,0 +1,54 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||||
|
* that can be found in the license/LICENSE.txt file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.gradle.model
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.gradle.BaseGradleIT
|
||||||
|
import org.jetbrains.kotlin.test.KotlinTestUtils
|
||||||
|
import org.junit.Test
|
||||||
|
import kotlin.test.assertEquals
|
||||||
|
import kotlin.test.assertFalse
|
||||||
|
import kotlin.test.assertNull
|
||||||
|
import kotlin.test.assertTrue
|
||||||
|
|
||||||
|
class KotlinAndroidExtensionIT : BaseGradleIT() {
|
||||||
|
|
||||||
|
override fun defaultBuildOptions(): BuildOptions {
|
||||||
|
return super.defaultBuildOptions().copy(
|
||||||
|
androidGradlePluginVersion = "3.0.0",
|
||||||
|
androidHome = KotlinTestUtils.findAndroidSdk()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testAndroidExtensionsProject() {
|
||||||
|
val project = Project("AndroidExtensionsProject")
|
||||||
|
val androidExtensionModel = project.getModels(KotlinAndroidExtension::class.java).getModel(":app")!!
|
||||||
|
|
||||||
|
assertEquals(1L, androidExtensionModel.modelVersion)
|
||||||
|
assertEquals("app", androidExtensionModel.name)
|
||||||
|
assertFalse(androidExtensionModel.isExperimental)
|
||||||
|
assertEquals("hashMap", androidExtensionModel.defaultCacheImplementation)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testAndroidExtensionsManyVariants() {
|
||||||
|
val project = Project("AndroidExtensionsManyVariants")
|
||||||
|
val androidExtensionModel = project.getModels(KotlinAndroidExtension::class.java).getModel(":app")!!
|
||||||
|
|
||||||
|
assertEquals(1L, androidExtensionModel.modelVersion)
|
||||||
|
assertEquals("app", androidExtensionModel.name)
|
||||||
|
assertTrue(androidExtensionModel.isExperimental)
|
||||||
|
assertEquals("hashMap", androidExtensionModel.defaultCacheImplementation)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testNonAndroidExtensionsProjects() {
|
||||||
|
val project = Project("kotlinProject")
|
||||||
|
val model = project.getModels(KotlinAndroidExtension::class.java).getModel(":")
|
||||||
|
|
||||||
|
assertNull(model)
|
||||||
|
}
|
||||||
|
}
|
||||||
+47
@@ -0,0 +1,47 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||||
|
* that can be found in the license/LICENSE.txt file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.gradle.model;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Entry point for Kotlin Android Extensions models.
|
||||||
|
* Represents the description of Android only features. Provided by 'kotlin-android-extensions' plugin.
|
||||||
|
*/
|
||||||
|
public interface KotlinAndroidExtension {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a number representing the version of this API.
|
||||||
|
* Always increasing if changed.
|
||||||
|
*
|
||||||
|
* @return the version of this model.
|
||||||
|
*/
|
||||||
|
long getModelVersion();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the module (Gradle project) name.
|
||||||
|
*
|
||||||
|
* @return the module name.
|
||||||
|
*/
|
||||||
|
@NotNull
|
||||||
|
String getName();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Indicate the use of experimental features.
|
||||||
|
*
|
||||||
|
* @return if experimental features are used.
|
||||||
|
*/
|
||||||
|
boolean isExperimental();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the default cache implementation.
|
||||||
|
*
|
||||||
|
* @return the default cache implementation.
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
String getDefaultCacheImplementation();
|
||||||
|
}
|
||||||
+6
-1
@@ -27,15 +27,18 @@ import org.gradle.api.Project
|
|||||||
import org.gradle.api.UnknownDomainObjectException
|
import org.gradle.api.UnknownDomainObjectException
|
||||||
import org.gradle.api.tasks.SourceSet
|
import org.gradle.api.tasks.SourceSet
|
||||||
import org.gradle.api.tasks.compile.AbstractCompile
|
import org.gradle.api.tasks.compile.AbstractCompile
|
||||||
|
import org.gradle.tooling.provider.model.ToolingModelBuilderRegistry
|
||||||
|
import org.jetbrains.kotlin.gradle.model.builder.KotlinAndroidExtensionModelBuilder
|
||||||
import org.jetbrains.kotlin.gradle.plugin.*
|
import org.jetbrains.kotlin.gradle.plugin.*
|
||||||
import org.jetbrains.kotlin.gradle.plugin.android.AndroidGradleWrapper
|
import org.jetbrains.kotlin.gradle.plugin.android.AndroidGradleWrapper
|
||||||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
||||||
import org.w3c.dom.Document
|
import org.w3c.dom.Document
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
import javax.inject.Inject
|
||||||
import javax.xml.parsers.DocumentBuilderFactory
|
import javax.xml.parsers.DocumentBuilderFactory
|
||||||
|
|
||||||
// Use apply plugin: 'kotlin-android-extensions' to enable Android Extensions in an Android project.
|
// Use apply plugin: 'kotlin-android-extensions' to enable Android Extensions in an Android project.
|
||||||
class AndroidExtensionsSubpluginIndicator : Plugin<Project> {
|
class AndroidExtensionsSubpluginIndicator @Inject internal constructor(private val registry: ToolingModelBuilderRegistry) : Plugin<Project> {
|
||||||
override fun apply(project: Project) {
|
override fun apply(project: Project) {
|
||||||
val extension = project.extensions.create("androidExtensions", AndroidExtensionsExtension::class.java)
|
val extension = project.extensions.create("androidExtensions", AndroidExtensionsExtension::class.java)
|
||||||
|
|
||||||
@@ -44,6 +47,8 @@ class AndroidExtensionsSubpluginIndicator : Plugin<Project> {
|
|||||||
addAndroidExtensionsRuntimeIfNeeded(project)
|
addAndroidExtensionsRuntimeIfNeeded(project)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
registry.register(KotlinAndroidExtensionModelBuilder())
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun addAndroidExtensionsRuntimeIfNeeded(project: Project) {
|
private fun addAndroidExtensionsRuntimeIfNeeded(project: Project) {
|
||||||
|
|||||||
+31
@@ -0,0 +1,31 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||||
|
* that can be found in the license/LICENSE.txt file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.gradle.model.builder
|
||||||
|
|
||||||
|
import org.gradle.api.Project
|
||||||
|
import org.gradle.tooling.provider.model.ToolingModelBuilder
|
||||||
|
import org.jetbrains.kotlin.gradle.internal.AndroidExtensionsExtension
|
||||||
|
import org.jetbrains.kotlin.gradle.model.KotlinAndroidExtension
|
||||||
|
import org.jetbrains.kotlin.gradle.model.impl.KotlinAndroidExtensionImpl
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [ToolingModelBuilder] for [KotlinAndroidExtension] models.
|
||||||
|
* This model builder is registered for Kapt Gradle sub-plugin.
|
||||||
|
*/
|
||||||
|
class KotlinAndroidExtensionModelBuilder : ToolingModelBuilder {
|
||||||
|
|
||||||
|
override fun canBuild(modelName: String): Boolean {
|
||||||
|
return modelName == KotlinAndroidExtension::class.java.name
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun buildAll(modelName: String, project: Project): Any? {
|
||||||
|
if (modelName == KotlinAndroidExtension::class.java.name) {
|
||||||
|
val extension = project.extensions.getByType(AndroidExtensionsExtension::class.java)
|
||||||
|
return KotlinAndroidExtensionImpl(project.name, extension.isExperimental, extension.defaultCacheImplementation.optionName)
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
+39
@@ -0,0 +1,39 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||||
|
* that can be found in the license/LICENSE.txt file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.gradle.model.impl
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.gradle.model.KotlinAndroidExtension
|
||||||
|
import java.io.Serializable
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implementation of the [KotlinAndroidExtension] interface.
|
||||||
|
*/
|
||||||
|
data class KotlinAndroidExtensionImpl(
|
||||||
|
private val myName: String,
|
||||||
|
private val myIsExperimental: Boolean,
|
||||||
|
private val myDefaultCacheImplementation: String?
|
||||||
|
) : KotlinAndroidExtension, Serializable {
|
||||||
|
|
||||||
|
override fun getModelVersion(): Long {
|
||||||
|
return serialVersionUID
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getName(): String {
|
||||||
|
return myName
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun isExperimental(): Boolean {
|
||||||
|
return myIsExperimental
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getDefaultCacheImplementation(): String? {
|
||||||
|
return myDefaultCacheImplementation
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private const val serialVersionUID = 1L
|
||||||
|
}
|
||||||
|
}
|
||||||
+20
@@ -0,0 +1,20 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||||
|
* that can be found in the license/LICENSE.txt file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.gradle.model.builder
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.gradle.model.KotlinAndroidExtension
|
||||||
|
import org.junit.Test
|
||||||
|
import kotlin.test.assertFalse
|
||||||
|
import kotlin.test.assertTrue
|
||||||
|
|
||||||
|
class KotlinAndroidExtensionModelBuilderTest {
|
||||||
|
@Test
|
||||||
|
fun testCanBuild() {
|
||||||
|
val modelBuilder = KotlinAndroidExtensionModelBuilder()
|
||||||
|
assertTrue(modelBuilder.canBuild(KotlinAndroidExtension::class.java.name))
|
||||||
|
assertFalse(modelBuilder.canBuild("wrongModel"))
|
||||||
|
}
|
||||||
|
}
|
||||||
+18
@@ -0,0 +1,18 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||||
|
* that can be found in the license/LICENSE.txt file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.gradle.model.impl
|
||||||
|
|
||||||
|
import nl.jqno.equalsverifier.EqualsVerifier
|
||||||
|
import nl.jqno.equalsverifier.Warning
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
class KotlinAndroidExtensionImplTest {
|
||||||
|
@Test
|
||||||
|
@Throws(Exception::class)
|
||||||
|
fun equals() {
|
||||||
|
EqualsVerifier.forClass(KotlinAndroidExtensionImpl::class.java).suppress(Warning.NULL_FIELDS).verify()
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user