• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.build.jdiff
18 
19 import org.gradle.api.tasks.Input
20 import org.gradle.api.tasks.InputFile
21 import org.gradle.api.tasks.InputFiles
22 import org.gradle.api.tasks.Optional
23 import org.gradle.api.tasks.javadoc.Javadoc
24 import org.gradle.external.javadoc.CoreJavadocOptions
25 import org.gradle.external.javadoc.MinimalJavadocOptions
26 
27 import java.io.File
28 import java.util.ArrayList
29 
30 /**
31  * JDiff task to compare API changes.
32  */
33 open class JDiffTask : Javadoc() {
34 
35     /**
36      * Sets the doclet path which has the `com.google.doclava.Doclava` class.
37      *
38      *
39      * This option will override any doclet path set in this instance's
40      * [JavadocOptions][.getOptions].
41      *
42      * @see MinimalJavadocOptions.setDocletpath
43      */
44     @get:InputFiles
45     var docletpath: Collection<File>? = null
46         set(docletpath) {
47             field = docletpath
48             // Go ahead and keep the mDocletpath in our JavadocOptions object in sync.
49             options.docletpath = ArrayList(docletpath)
50         }
51 
52     @get:InputFile
53     lateinit var oldApiXmlFile: File
54 
55     @get:InputFile
56     lateinit var newApiXmlFile: File
57 
58     /**
59      * Relative path to the Javadoc corresponding to the old API, relative to
60      * "${destinationDir}/changes". Should end with the directory separator (usually '/').
61      */
62     @get:[Input Optional]
63     var oldJavadocPrefix: String? = null
64 
65     /**
66      * Relative path to the Javadoc corresponding to the new API, relative to
67      * "${destinationDir}/changes". Should end with the directory separator (usually '/').
68      */
69     @get:[Input Optional]
70     var newJavadocPrefix: String? = null
71 
72     // HTML diff files will be placed in destinationDir, which is defined by the superclass.
73 
74     @get:Input
75     var stats = true
76 
<lambda>null77     init {
78         isFailOnError = true
79         options.doclet = "jdiff.JDiff"
80         options.encoding = "UTF-8"
81         maxMemory = "1280m"
82     }
83 
84     /**
85      * "Configures" this JDiffTask with parameters that might not be at their final values
86      * until this task is run.
87      */
configureJDiffTasknull88     private fun configureJDiffTask() {
89         val options = options as CoreJavadocOptions
90 
91         options.docletpath = ArrayList(docletpath!!)
92 
93         if (stats) {
94             options.addStringOption("stats")
95         }
96 
97         val oldApiXmlFileDir = oldApiXmlFile.parentFile
98         val newApiXmlFileDir = newApiXmlFile.parentFile
99 
100         if (oldApiXmlFileDir.exists()) {
101             options.addStringOption("oldapidir", oldApiXmlFileDir.absolutePath)
102         }
103         // For whatever reason, jdiff appends .xml to the file name on its own.
104         // Strip the .xml off the end of the file name
105         options.addStringOption("oldapi",
106                 oldApiXmlFile.name.substring(0, oldApiXmlFile.name.length - 4))
107         if (newApiXmlFileDir.exists()) {
108             options.addStringOption("newapidir", newApiXmlFileDir.absolutePath)
109         }
110         options.addStringOption("newapi",
111                 newApiXmlFile.name.substring(0, newApiXmlFile.name.length - 4))
112 
113         if (oldJavadocPrefix != null) {
114             options.addStringOption("javadocold", oldJavadocPrefix)
115         }
116         if (newJavadocPrefix != null) {
117             options.addStringOption("javadocnew", newJavadocPrefix)
118         }
119     }
120 
generatenull121     public override fun generate() {
122         configureJDiffTask()
123         super.generate()
124     }
125 }
126