com.apollographql.apollo.gradle.ApolloPlugin.groovy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of apollo-gradle-plugin Show documentation
Show all versions of apollo-gradle-plugin Show documentation
Gradle plugin for generating java/kotlin classes for graphql files
package com.apollographql.apollo.gradle
import com.android.build.gradle.AppPlugin
import com.android.build.gradle.LibraryPlugin
import com.android.build.gradle.api.BaseVariant
import com.apollographql.android.VersionKt
import com.google.common.collect.ImmutableList
import com.moowork.gradle.node.NodeExtension
import com.moowork.gradle.node.NodePlugin
import org.gradle.api.DomainObjectCollection
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.internal.file.FileResolver
import org.gradle.api.plugins.JavaPlugin
import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.compile.JavaCompile
import javax.inject.Inject
class ApolloPlugin implements Plugin {
private static final String NODE_VERSION = "6.7.0"
public static final String TASK_GROUP = "apollo"
private Project project
private final FileResolver fileResolver
@Inject
public ApolloPlugin(FileResolver fileResolver) {
this.fileResolver = fileResolver
}
@Override void apply(Project project) {
this.project = project
if (project.plugins.hasPlugin(AppPlugin) || project.plugins.hasPlugin(LibraryPlugin) || project.plugins.hasPlugin(
JavaPlugin)) {
applyApolloPlugin()
} else {
throw new IllegalArgumentException(
"Apollo plugin couldn't be applied. The Android or Java plugin must be configured first")
}
}
private void applyApolloPlugin() {
setupNode()
project.extensions.create(ApolloExtension.NAME, ApolloExtension)
createSourceSetExtensions()
project.tasks.create(ApolloCodeGenInstallTask.NAME, ApolloCodeGenInstallTask.class)
addApolloTasks()
}
private void addApolloTasks() {
Task apolloIRGenTask = project.task("generateApolloIR")
apolloIRGenTask.group(TASK_GROUP)
Task apolloClassGenTask = project.task("generateApolloClasses")
apolloClassGenTask.group(TASK_GROUP)
if (isAndroidProject()) {
getVariants().all { v ->
addVariantTasks(v, apolloIRGenTask, apolloClassGenTask, v.sourceSets)
}
project.android.testVariants.each { tv ->
addVariantTasks(tv, apolloIRGenTask, apolloClassGenTask, tv.sourceSets)
}
} else {
getSourceSets().all { sourceSet ->
addSourceSetTasks(sourceSet, apolloIRGenTask, apolloClassGenTask)
}
}
}
private void addVariantTasks(Object variant, Task apolloIRGenTask, Task apolloClassGenTask, Collection> sourceSets) {
ApolloIRGenTask variantIRTask = createApolloIRGenTask(variant.name, sourceSets)
ApolloClassGenTask variantClassTask = createApolloClassGenTask(variant.name)
variant.registerJavaGeneratingTask(variantClassTask, variantClassTask.outputDir)
apolloIRGenTask.dependsOn(variantIRTask)
apolloClassGenTask.dependsOn(variantClassTask)
}
private void addSourceSetTasks(SourceSet sourceSet, Task apolloIRGenTask, Task apolloClassGenTask) {
String taskName = "main".equals(sourceSet.name) ? "" : sourceSet.name
ApolloIRGenTask sourceSetIRTask = createApolloIRGenTask(sourceSet.name, [sourceSet])
ApolloClassGenTask sourceSetClassTask = createApolloClassGenTask(sourceSet.name)
apolloIRGenTask.dependsOn(sourceSetIRTask)
apolloClassGenTask.dependsOn(sourceSetClassTask)
JavaCompile compileTask = (JavaCompile) project.tasks.getByName("compile${taskName.capitalize()}Java")
compileTask.source += project.fileTree(sourceSetClassTask.outputDir)
compileTask.dependsOn(apolloClassGenTask)
}
private void setupNode() {
project.plugins.apply NodePlugin
NodeExtension nodeConfig = project.extensions.findByName("node") as NodeExtension
nodeConfig.download = true
nodeConfig.version = NODE_VERSION
}
private ApolloIRGenTask createApolloIRGenTask(String sourceSetOrVariantName, Collection
© 2015 - 2025 Weber Informatics LLC | Privacy Policy