Skip to content

Commit

Permalink
do not try to compile java files if we don't generate any (#1353)
Browse files Browse the repository at this point in the history
* do not try to compile java files if we don't generate any

Should fix #1352

* add a test

* cleanup the test when done

* make integration tests use the actual plugin code and not the snapshot

* I had to move the creation of the apollo tasks to `afterEvaluate`. This might or might not have side effects but it's the only way I found to know reliably the value of apollo.generateKotlinModels and configure the java task
* The other integration tests simply add the api code to the java sourceSet which defeats the value of this test (testing that the porject compiles without any java file). In order to still have the api dependency, I included the snapashot so the test will always lag a bit
martinbonnin authored and sav007 committed Jun 16, 2019

Verified

This commit was signed with the committer’s verified signature.
jlblancoc Jose Luis Blanco-Claraco
1 parent 3351482 commit a1cd963
Showing 7 changed files with 5,941 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -61,7 +61,10 @@ class ApolloPlugin implements Plugin<Project> {
project.tasks.create(ApolloCodegenInstallTask.NAME, ApolloCodegenInstallTask.class)
}

addApolloTasks()
// we use afterEvaluate here as we need to know the value of generateKotlinModels from addSourceSetTasks
project.afterEvaluate {
addApolloTasks()
}
}

private void addApolloTasks() {
@@ -101,9 +104,11 @@ class ApolloPlugin implements Plugin<Project> {
apolloIRGenTask.dependsOn(sourceSetIRTask)
apolloClassGenTask.dependsOn(sourceSetClassTask)

JavaCompile compileTask = (JavaCompile) project.tasks.findByName("compile${taskName.capitalize()}Java")
compileTask.source += project.fileTree(sourceSetClassTask.outputDir)
compileTask.dependsOn(apolloClassGenTask)
if (project.apollo.generateKotlinModels.get() != true) {
JavaCompile compileTask = (JavaCompile) project.tasks.findByName("compile${taskName.capitalize()}Java")
compileTask.source += project.fileTree(sourceSetClassTask.outputDir)
compileTask.dependsOn(apolloClassGenTask)
}

sourceSet.java.srcDir(sourceSetClassTask.outputDir)

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

import org.apache.commons.io.FileUtils
import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.TaskOutcome
import spock.lang.Shared
import spock.lang.Specification

class KotlinSpec extends Specification {
@Shared File testProjectDir

def setupSpec() {
def integrationTestsDir = new File(System.getProperty("user.dir"), "build/integrationTests/")
testProjectDir = new File(integrationTestsDir, "kotlin")
FileUtils.deleteDirectory(testProjectDir)
FileUtils.forceMkdir(testProjectDir)

File readOnlyDir = new File(System.getProperty("user.dir"), "src/test/testProject/kotlin/")
if (!readOnlyDir.isDirectory()) {
throw new IllegalArgumentException("Couldn't find test project")
}

FileUtils.copyDirectory(readOnlyDir, testProjectDir)
}

// ApolloExtension tests
def "compilation succeeeds"() {
when:
def result = GradleRunner.create().withProjectDir(testProjectDir)
.withPluginClasspath()
.withArguments("assemble", "-Dapollographql.skipRuntimeDep=true")
.forwardStdError(new OutputStreamWriter(System.err))
.forwardStdOutput(new OutputStreamWriter(System.out))
.build()

then:
result.task(":assemble").outcome == TaskOutcome.SUCCESS
}

def cleanupSpec() {
FileUtils.deleteDirectory(testProjectDir)
}
}
36 changes: 36 additions & 0 deletions apollo-gradle-plugin/src/test/testProject/kotlin/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
buildscript {
apply from: "../../../../gradle/testFixturesDeps.gradle"

repositories {
google() // the apollo android plugin depends on the android plugins who are hosted there
mavenCentral()
jcenter()
}
}

plugins {
id 'org.jetbrains.kotlin.jvm' version '1.3.31'
id 'com.apollographql.android'
}

repositories {
maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
mavenCentral()
jcenter()
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib"
implementation dep.apolloApi
}

compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}

apollo {
generateKotlinModels = true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
query Films {
allFilms {
films {
id
title
releaseDate
}
}
}

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions gradle/dependencies.gradle
Original file line number Diff line number Diff line change
@@ -27,6 +27,7 @@ ext.dep = [
apolloRx2Support : "com.apollographql.apollo:apollo-rx2-support:$versions.apolloVersion",
apolloCoroutinesSupport : "com.apollographql.apollo:apollo-coroutine-support:$versions.apolloVersion",
apolloHttpCache : "com.apollographql.apollo:apollo-http-cache:$versions.apolloVersion",
apolloApi : "com.apollographql.apollo:apollo-api:$versions.apolloVersion",
supportAnnotations : "com.android.support:support-annotations:$versions.supportLibVersion",
recyclerView : "com.android.support:recyclerview-v7:$versions.supportLibVersion",
compiletesting : 'com.google.testing.compile:compile-testing:0.15',
5 changes: 0 additions & 5 deletions gradle/testFixturesDeps.gradle
Original file line number Diff line number Diff line change
@@ -4,11 +4,6 @@ ext.supportDeps = [
dep.appcompat
]

ext.apolloRuntimeCompileDeps = [
dep.supportAnnotations,
dep.okHttp,
]

ext.apolloApiCompileDeps = [
dep.jetbrainsAnnotations,
dep.okHttp

0 comments on commit a1cd963

Please sign in to comment.