• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import org.gradle.api.Plugin
2import org.gradle.api.Project
3import org.gradle.api.tasks.compile.JavaCompile
4
5import java.util.jar.JarFile
6
7@SuppressWarnings("GroovyUnusedDeclaration")
8class ShadowsPlugin implements Plugin<Project> {
9    @Override
10    void apply(Project project) {
11        project.apply plugin: 'idea'
12
13        project.extensions.create("shadows", ShadowsPluginExtension)
14
15        project.dependencies {
16            annotationProcessor project.project(":processor")
17        }
18
19        def compileJavaTask = project.tasks["compileJava"]
20
21        // write generated Java into its own dir... see https://github.com/gradle/gradle/issues/4956
22        def generatedSrcRelPath = 'build/generated/src/apt/main'
23        def generatedSrcDir = project.file(generatedSrcRelPath)
24
25        project.sourceSets.main.java { srcDir generatedSrcRelPath }
26        project.mkdir(generatedSrcDir)
27        compileJavaTask.options.annotationProcessorGeneratedSourcesDirectory = generatedSrcDir
28        compileJavaTask.outputs.dir(generatedSrcDir)
29
30        compileJavaTask.doFirst {
31            options.compilerArgs.add("-Aorg.robolectric.annotation.processing.jsonDocsEnabled=true")
32            options.compilerArgs.add("-Aorg.robolectric.annotation.processing.jsonDocsDir=${project.buildDir}/docs/json")
33            options.compilerArgs.add("-Aorg.robolectric.annotation.processing.shadowPackage=${project.shadows.packageName}")
34            options.compilerArgs.add("-Aorg.robolectric.annotation.processing.sdkCheckMode=${project.shadows.sdkCheckMode}")
35            options.compilerArgs.add("-Aorg.robolectric.annotation.processing.sdks=${project.rootProject.buildDir}/sdks.txt")
36        }
37
38        // include generated sources in javadoc jar
39        project.tasks['javadoc'].source(generatedSrcDir)
40
41        // verify that we have the apt-generated files in our javadoc and sources jars
42        project.tasks['javadocJar'].doLast { task ->
43            def shadowPackageNameDir = project.shadows.packageName.replaceAll(/\./, '/')
44            checkForFile(task.archivePath, "${shadowPackageNameDir}/Shadows.html")
45        }
46
47        project.tasks['sourcesJar'].doLast { task ->
48            def shadowPackageNameDir = project.shadows.packageName.replaceAll(/\./, '/')
49            checkForFile(task.archivePath, "${shadowPackageNameDir}/Shadows.java")
50        }
51
52        project.rootProject.configAnnotationProcessing += project
53
54        /* Prevents sporadic compilation error:
55         * 'Bad service configuration file, or exception thrown while constructing
56         *  Processor object: javax.annotation.processing.Processor: Error reading
57         *  configuration file'
58         *
59         * See https://discuss.gradle.org/t/gradle-not-compiles-with-solder-tooling-jar/7583/20
60         */
61        project.tasks.withType(JavaCompile) { options.fork = true }
62    }
63
64    static class ShadowsPluginExtension {
65        String packageName
66        String sdkCheckMode = "WARN"
67    }
68
69    private static void checkForFile(jar, String name) {
70        def files = new JarFile(jar).entries().collect { it.name }.toSet()
71
72        if (!files.contains(name)) {
73            throw new RuntimeException("Missing file ${name} in ${jar}")
74        }
75    }
76}
77