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 android.support.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 import java.util.Collection; 30 31 /** 32 * JDiff task to compare API changes. 33 */ 34 public class JDiffTask extends Javadoc { 35 36 private Collection<File> mDocletpath; 37 38 private File mOldApiXmlFile; 39 40 private File mNewApiXmlFile; 41 42 /** 43 * Relative path to the Javadoc corresponding to the old API, relative to 44 * "${destinationDir}/changes". Should end with the directory separator (usually '/'). 45 */ 46 private String mOldJavadocPrefix; 47 48 /** 49 * Relative path to the Javadoc corresponding to the new API, relative to 50 * "${destinationDir}/changes". Should end with the directory separator (usually '/'). 51 */ 52 private String mNewJavadocPrefix; 53 54 // HTML diff files will be placed in destinationDir, which is defined by the superclass. 55 56 private boolean mStats = true; 57 58 public JDiffTask() { 59 setFailOnError(true); 60 getOptions().setDoclet("jdiff.JDiff"); 61 getOptions().setEncoding("UTF-8"); 62 setMaxMemory("1280m"); 63 } 64 65 public void setOldApiXmlFile(File file) { 66 mOldApiXmlFile = file; 67 } 68 69 @InputFile 70 public File getOldApiXmlFile() { 71 return mOldApiXmlFile; 72 } 73 74 public void setNewApiXmlFile(File file) { 75 mNewApiXmlFile = file; 76 } 77 78 @InputFile 79 public File getNewApiXmlFile() { 80 return mNewApiXmlFile; 81 } 82 83 @Optional 84 public void setOldJavadocPrefix(String prefix) { 85 mOldJavadocPrefix = prefix; 86 } 87 88 @Optional 89 @Input 90 public String getOldJavadocPrefix() { 91 return mOldJavadocPrefix; 92 } 93 94 public void setNewJavadocPrefix(String prefix) { 95 mNewJavadocPrefix = prefix; 96 } 97 98 @Input 99 public String getNewJavadocPrefix() { 100 return mNewJavadocPrefix; 101 } 102 103 public void setStats(boolean enabled) { 104 mStats = enabled; 105 } 106 107 @Input 108 public boolean getStats() { 109 return mStats; 110 } 111 112 /** 113 * Sets the doclet path which has the {@code com.gogole.doclava.Doclava} class. 114 * <p> 115 * This option will override any doclet path set in this instance's 116 * {@link #getOptions() JavadocOptions}. 117 * 118 * @see MinimalJavadocOptions#setDocletpath(java.util.List) 119 */ 120 public void setDocletpath(Collection<File> docletpath) { 121 mDocletpath = docletpath; 122 123 // Go ahead and keep the mDocletpath in our JavadocOptions object in sync. 124 getOptions().setDocletpath(new ArrayList<>(docletpath)); 125 } 126 127 @InputFiles 128 public Collection<File> getDocletpath() { 129 return mDocletpath; 130 } 131 132 /** 133 * "Configures" this JDiffTask with parameters that might not be at their final values 134 * until this task is run. 135 */ 136 private void configureJDiffTask() { 137 CoreJavadocOptions options = (CoreJavadocOptions) getOptions(); 138 139 options.setDocletpath(new ArrayList<>(mDocletpath)); 140 141 if (getStats()) { 142 options.addStringOption("stats"); 143 } 144 145 File oldApiXmlFile = getOldApiXmlFile(); 146 File newApiXmlFile = getNewApiXmlFile(); 147 148 File oldApiXmlFileDir = oldApiXmlFile.getParentFile(); 149 File newApiXmlFileDir = newApiXmlFile.getParentFile(); 150 151 if (oldApiXmlFileDir.exists()) { 152 options.addStringOption("oldapidir", oldApiXmlFileDir.getAbsolutePath()); 153 } 154 // For whatever reason, jdiff appends .xml to the file name on its own. 155 // Strip the .xml off the end of the file name 156 options.addStringOption("oldapi", 157 oldApiXmlFile.getName().substring(0, oldApiXmlFile.getName().length() - 4)); 158 if (newApiXmlFileDir.exists()) { 159 options.addStringOption("newapidir", newApiXmlFileDir.getAbsolutePath()); 160 } 161 options.addStringOption("newapi", 162 newApiXmlFile.getName().substring(0, newApiXmlFile.getName().length() - 4)); 163 164 String oldJavadocPrefix = getOldJavadocPrefix(); 165 String newJavadocPrefix = getNewJavadocPrefix(); 166 167 if (oldJavadocPrefix != null) { 168 options.addStringOption("javadocold", oldJavadocPrefix); 169 } 170 if (newJavadocPrefix != null) { 171 options.addStringOption("javadocnew", newJavadocPrefix); 172 } 173 } 174 175 @Override 176 public void generate() { 177 configureJDiffTask(); 178 super.generate(); 179 } 180 } 181