Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deffer accessing to apollo extension properties until task execution #979

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ android:
- google-gdk-license-.+

script:
- ./gradlew clean build connectedCheck -x checkstyleTest --stacktrace --max-workers=2 -x apollo-gradle-plugin:test -x apollo-integration:build -x apollo-integration:connectedCheck
- ./gradlew clean build connectedCheck -x checkstyleTest --stacktrace --max-workers=2 -x apollo-integration:build -x apollo-integration:connectedCheck
- .buildscript/integration_tests_composite.sh

before_script:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ class GraphQLCompiler {
companion object {
const val FILE_EXTENSION = "graphql"
@JvmField
val OUTPUT_DIRECTORY = listOf("generated", "source", "apollo")
val OUTPUT_DIRECTORY = listOf("generated", "source", "apollo", "classes")
@JvmField
val IR_OUTPUT_DIRECTORY = listOf("generated", "source", "apollo", "generatedIR")
const val APOLLOCODEGEN_VERSION = "0.19.1"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ enum class NullableValueType(val value: String) {
INPUT_TYPE("inputType");

companion object {
@JvmStatic
fun findByValue(value: String): NullableValueType? = NullableValueType.values().find { it.value == value }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.apollographql.apollo.gradle

import com.apollographql.apollo.compiler.GraphQLCompiler
import com.apollographql.apollo.compiler.NullableValueType
import com.google.common.base.Joiner
import org.gradle.api.Action
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.*
import org.gradle.api.tasks.incremental.IncrementalTaskInputs
import org.gradle.api.tasks.incremental.InputFileDetails
import org.jetbrains.annotations.NotNull

class ApolloClassGenerationTask extends SourceTask {
static final String NAME = "generate%sApolloClasses"

@Input Property<Map> customTypeMapping = project.objects.property(Map.class)
@Optional @Input Property<String> nullableValueType = project.objects.property(String.class)
@Input Property<Boolean> useSemanticNaming = project.objects.property(Boolean.class)
@Input Property<Boolean> generateModelBuilder = project.objects.property(Boolean.class)
@Input Property<Boolean> useJavaBeansSemanticNaming = project.objects.property(Boolean.class)
@Input Property<Boolean> suppressRawTypesWarning = project.objects.property(Boolean.class)
@Optional @Input Property<String> outputPackageName = project.objects.property(String.class)
@OutputDirectory DirectoryProperty outputDir

ApolloClassGenerationTask() {
outputDir = project.layout.directoryProperty()
outputDir.set(new File(project.buildDir, Joiner.on(File.separator).join(GraphQLCompiler.OUTPUT_DIRECTORY)))
}

@TaskAction
void generateClasses(IncrementalTaskInputs inputs) {
String nullableValueTypeStr = this.nullableValueType.get()
NullableValueType nullableValueType = null
if (nullableValueTypeStr != null) {
nullableValueType = NullableValueType.findByValue(nullableValueTypeStr)
}
inputs.outOfDate(new Action<InputFileDetails>() {
@Override
void execute(@NotNull InputFileDetails inputFileDetails) {
outputDir.asFile.get().delete()

String outputPackageName = outputPackageName.get()
if (outputPackageName != null && outputPackageName.trim().isEmpty()) {
outputPackageName = null
}
GraphQLCompiler.Arguments args = new GraphQLCompiler.Arguments(
inputFileDetails.getFile(), outputDir.get().asFile, customTypeMapping.get(),
nullableValueType != null ? nullableValueType : NullableValueType.ANNOTATED, useSemanticNaming.get(),
generateModelBuilder.get(), useJavaBeansSemanticNaming.get(), outputPackageName,
suppressRawTypesWarning.get()
)
new GraphQLCompiler().write(args)
}
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.apollographql.apollo.gradle

import com.apollographql.apollo.compiler.NullableValueType
import org.gradle.api.Project
import org.gradle.api.provider.Property

class ApolloExtension {
static final String NAME = "apollo"

final Property<String> nullableValueType
final Property<Boolean> useSemanticNaming
final Property<Boolean> generateModelBuilder
final Property<Boolean> useJavaBeansSemanticNaming
final Property<Boolean> suppressRawTypesWarning
final Property<String> schemaFilePath
final Property<String> outputPackageName
final Property<Map> customTypeMapping

ApolloExtension(Project project) {
nullableValueType = project.getObjects().property(String.class)
nullableValueType.set(NullableValueType.ANNOTATED.getValue())

useSemanticNaming = project.getObjects().property(Boolean.class)
useSemanticNaming.set(true)

generateModelBuilder = project.getObjects().property(Boolean.class)
generateModelBuilder.set(false)

useJavaBeansSemanticNaming = project.getObjects().property(Boolean.class)
useJavaBeansSemanticNaming.set(false)

suppressRawTypesWarning = project.getObjects().property(Boolean.class)
suppressRawTypesWarning.set(false)

schemaFilePath = project.getObjects().property(String.class)
schemaFilePath.set("")

outputPackageName = project.getObjects().property(String.class)
outputPackageName.set("")

customTypeMapping = project.getObjects().property(Map.class)
customTypeMapping.set(new LinkedHashMap())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.apollographql.apollo.gradle

import com.google.common.collect.ImmutableList
import com.moowork.gradle.node.task.NodeTask
import org.gradle.api.GradleException
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.OutputDirectory

class ApolloLocalCodegenGenerationTask extends NodeTask {
public static final String APOLLO_CODEGEN_EXEC_FILE = "lib/cli.js"
public static final String APOLLO_CODEGEN = "apollo-codegen/node_modules/apollo-codegen/" + APOLLO_CODEGEN_EXEC_FILE

String variant
ImmutableList<String> sourceSetNames
@Input @Optional Property<String> schemaFilePath = project.objects.property(String.class)
@Input @Optional Property<String> outputPackageName = project.objects.property(String.class)
@OutputDirectory final DirectoryProperty outputDir = project.layout.directoryProperty()

@Override
void exec() {
File apolloScript = new File(getProject().getBuildDir(), APOLLO_CODEGEN)
if (!apolloScript.isFile()) {
throw new GradleException(
"Apollo-codegen was not found in node_modules. Please run the installApolloCodegen task.")
}
setScript(apolloScript)

List<CodegenGenerationTaskCommandArgsBuilder.CommandArgs> args = new CodegenGenerationTaskCommandArgsBuilder(
this, schemaFilePath.get(), outputPackageName.get(), outputDir.get().asFile, variant, sourceSetNames
).build()

for (CodegenGenerationTaskCommandArgsBuilder.CommandArgs commandArgs : args) {
setArgs(commandArgs.taskArguments)
super.exec()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.android.build.gradle.AppPlugin
import com.android.build.gradle.LibraryPlugin
import com.android.build.gradle.api.BaseVariant
import com.apollographql.apollo.compiler.GraphQLCompiler
import com.google.common.base.Joiner
import com.google.common.collect.ImmutableList
import com.moowork.gradle.node.NodeExtension
import com.moowork.gradle.node.NodePlugin
Expand Down Expand Up @@ -31,11 +32,12 @@ class ApolloPlugin implements Plugin<Project> {
private boolean useGlobalApolloCodegen

@Inject
public ApolloPlugin(FileResolver fileResolver) {
ApolloPlugin(FileResolver fileResolver) {
this.fileResolver = fileResolver
}

@Override void apply(Project project) {
@Override
void apply(Project project) {
this.project = project
if (project.plugins.hasPlugin(AppPlugin)
|| project.plugins.hasPlugin(LibraryPlugin)
Expand All @@ -55,7 +57,7 @@ class ApolloPlugin implements Plugin<Project> {
setupNode()
}

project.extensions.create(ApolloExtension.NAME, ApolloExtension)
project.extensions.create(ApolloExtension.NAME, ApolloExtension, project)
createSourceSetExtensions()

if (!useGlobalApolloCodegen) {
Expand All @@ -67,10 +69,10 @@ class ApolloPlugin implements Plugin<Project> {

private void addApolloTasks() {
Task apolloIRGenTask = project.task("generateApolloIR")
apolloIRGenTask.group(TASK_GROUP)
apolloIRGenTask.group = TASK_GROUP

Task apolloClassGenTask = project.task("generateApolloClasses")
apolloClassGenTask.group(TASK_GROUP)
apolloClassGenTask.group = TASK_GROUP

if (isAndroidProject()) {
getVariants().all { v ->
Expand All @@ -89,7 +91,7 @@ class ApolloPlugin implements Plugin<Project> {
private void addVariantTasks(BaseVariant variant, Task apolloIRGenTask, Task apolloClassGenTask, Collection<?> sourceSets) {
AbstractTask variantIRTask = createApolloIRGenTask(variant.name, sourceSets)
ApolloClassGenerationTask variantClassTask = createApolloClassGenTask(variant.name)
variant.registerJavaGeneratingTask(variantClassTask, variantClassTask.outputDir)
variant.registerJavaGeneratingTask(variantClassTask, variantClassTask.outputDir.asFile.get())
apolloIRGenTask.dependsOn(variantIRTask)
apolloClassGenTask.dependsOn(variantClassTask)
}
Expand Down Expand Up @@ -127,37 +129,43 @@ class ApolloPlugin implements Plugin<Project> {
}

private AbstractTask createApolloIRGenTask(String sourceSetOrVariantName, Collection<Object> sourceSets) {
String taskName = String.format(APOLLO_CODEGEN_GENERATE_TASK_NAME, sourceSetOrVariantName.capitalize())
ImmutableList.Builder<String> sourceSetNamesList = ImmutableList.builder()
sourceSets.each { sourceSet ->
sourceSetNamesList.add(sourceSet.name)
}

AbstractTask task;
File outputFolder = new File(project.buildDir, Joiner.on(File.separator)
.join(GraphQLCompiler.IR_OUTPUT_DIRECTORY + sourceSetOrVariantName))

String taskName = String.format(APOLLO_CODEGEN_GENERATE_TASK_NAME, sourceSetOrVariantName.capitalize())
if (useGlobalApolloCodegen) {
task = project.tasks.create(taskName, ApolloSystemCodegenGenerationTask) {
group = TASK_GROUP
description = "Generate an IR file using apollo-codegen for ${sourceSetOrVariantName.capitalize()} GraphQL queries"
return project.tasks.create(taskName, ApolloSystemCodegenGenerationTask) {
sourceSets.each { sourceSet ->
inputs.files(sourceSet.graphql).skipWhenEmpty()
}
group = TASK_GROUP
description = "Generate an IR file using apollo-codegen for ${sourceSetOrVariantName.capitalize()} GraphQL queries"
schemaFilePath = project.apollo.schemaFilePath
outputPackageName = project.apollo.outputPackageName
variant = sourceSetOrVariantName
sourceSetNames = sourceSetNamesList.build()
outputDir.set(outputFolder)
}

} else {
task = project.tasks.create(taskName, ApolloLocalCodegenGenerationTask) {
group = TASK_GROUP
description = "Generate an IR file using apollo-codegen for ${sourceSetOrVariantName.capitalize()} GraphQL queries"
dependsOn(ApolloCodegenInstallTask.NAME)
return project.tasks.create(taskName, ApolloLocalCodegenGenerationTask) {
sourceSets.each { sourceSet ->
inputs.files(sourceSet.graphql).skipWhenEmpty()
}
group = TASK_GROUP
description = "Generate an IR file using apollo-codegen for ${sourceSetOrVariantName.capitalize()} GraphQL queries"
dependsOn(ApolloCodegenInstallTask.NAME)
schemaFilePath = project.apollo.schemaFilePath
outputPackageName = project.apollo.outputPackageName
variant = sourceSetOrVariantName
sourceSetNames = sourceSetNamesList.build()
outputDir.set(outputFolder)
}
}

ImmutableList.Builder<String> sourceSetNamesList = ImmutableList.builder()
sourceSets.each { sourceSet -> sourceSetNamesList.add(sourceSet.name) }
task.init(sourceSetOrVariantName, sourceSetNamesList.build())
return task
}

private ApolloClassGenerationTask createApolloClassGenTask(String name) {
Expand All @@ -167,7 +175,7 @@ class ApolloPlugin implements Plugin<Project> {
description = "Generate Android classes for ${name.capitalize()} GraphQL queries"
dependsOn(getProject().getTasks().findByName(String.format(APOLLO_CODEGEN_GENERATE_TASK_NAME, name.capitalize())))
source = project.tasks.findByName(
String.format(APOLLO_CODEGEN_GENERATE_TASK_NAME, name.capitalize())).outputFolder
String.format(APOLLO_CODEGEN_GENERATE_TASK_NAME, name.capitalize())).outputDir
include "**${File.separatorChar}*API.json"
customTypeMapping = project.apollo.customTypeMapping
nullableValueType = project.apollo.nullableValueType
Expand Down Expand Up @@ -227,4 +235,5 @@ class ApolloPlugin implements Plugin<Project> {
return false
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.apollographql.apollo.gradle

import com.google.common.collect.ImmutableList
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.AbstractExecTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.OutputDirectory

class ApolloSystemCodegenGenerationTask extends AbstractExecTask<ApolloSystemCodegenGenerationTask> {
String variant
ImmutableList<String> sourceSets
@Input @Optional Property<String> schemaFilePath = project.objects.property(String.class)
@Input @Optional Property<String> outputPackageName = project.objects.property(String.class)
@OutputDirectory final DirectoryProperty outputDir = project.layout.directoryProperty()

ApolloSystemCodegenGenerationTask() {
super(ApolloSystemCodegenGenerationTask.class)
}

@Override
void exec() {
setCommandLine("apollo-codegen")

List<CodegenGenerationTaskCommandArgsBuilder.CommandArgs> args = new CodegenGenerationTaskCommandArgsBuilder(
this, schemaFilePath.get(), outputPackageName.get(), outputDir.get().asFile, variant, sourceSets
).build()

for (CodegenGenerationTaskCommandArgsBuilder.CommandArgs commandArgs : args) {
setArgs(commandArgs.taskArguments)
super.exec()
}
}
}
Loading