Last active
February 15, 2018 23:34
Script that generates package-info.java files for all packages in java source directories in a gradle project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Generates package-info.java for appropriate packages | |
* inside src/main/java folder. | |
* | |
* This is a workaround to define @ParametersAreNonnullByDefault for all Java classes | |
* in a package, including all subpackages. | |
* | |
* Adapted from https://gist.github.com/kosiara/233753e4de606eb5bd8b342209090e25 | |
*/ | |
task generatePackageInfoJavaFiles(dependsOn: "assembleDebug", type: Copy) { | |
group = "Copying" | |
description = "Generate package-info.java classes" | |
def srcDir = file(projectDir.canonicalPath + File.separatorChar + "src") | |
srcDir.eachDir { sourceSetDir -> | |
def javaSrcDir = file(sourceSetDir.canonicalPath + File.separatorChar + "java") | |
javaSrcDir.eachDirRecurse { packageDir -> | |
if (hasNonDirectoryChildren(packageDir)) { | |
def infoFilePath = packageDir.getAbsolutePath() + File.separatorChar + "package-info.java" | |
def infoFileContentPackage = getFileContentPackage(sourceSetDir, packageDir.getAbsolutePath()) | |
writeInfoFileToPath(infoFilePath, infoFileContentPackage) | |
println "[dir] " + infoFilePath + " created" | |
} | |
// TODO: Remove this later, just to clean up my local state | |
else { | |
def infoFilePath = packageDir.getAbsolutePath() + File.separatorChar + "package-info.java" | |
def infoFile = new File(infoFilePath) | |
if (infoFile.exists()) { | |
infoFile.delete() | |
} | |
} | |
} | |
} | |
println "[SUCCESS] NonNull generator: package-info.java files checked" | |
} | |
static def hasNonDirectoryChildren(File packageDir) { | |
for (File child : packageDir.listFiles()) { | |
if (!child.directory && getFileExtension(child) == "java" && child.name != "package-info.java") { | |
return true | |
} | |
} | |
return false | |
} | |
static def getFileExtension(File file) { | |
String filename = file.name | |
int i = filename.lastIndexOf('.') | |
if (i > 0) { | |
return filename.substring(i+1) | |
} else { | |
return null | |
} | |
} | |
static def writeInfoFileToPath(infoFilePath, infoFileContentPackage) { | |
def infoFile = new File(infoFilePath) | |
infoFile.write("") // Clear out file contents | |
infoFile.append(getFileContentAnnotations()) | |
infoFile.append(infoFileContentPackage) | |
} | |
static def getFileContentPackage(File sourceSet, String path) { | |
def mainSrcPhrase = | |
sourceSet.canonicalPath + | |
File.separatorChar + "java" + | |
File.separatorChar | |
def mainSrcPhraseIndex = path.indexOf(mainSrcPhrase) | |
def output = path.substring(mainSrcPhraseIndex) | |
// Windows fix | |
if (System.properties['os.name'].toLowerCase().contains('windows')) { | |
output = output.replace("\\", "/") | |
mainSrcPhrase = mainSrcPhrase.replace("\\", "/") | |
} | |
return "package " + output.replaceAll(mainSrcPhrase, "").replaceAll("/", ".") + ";\n" | |
} | |
static def getFileContentAnnotations() { | |
return """ | |
@com.quizlet.android.annotations.ParametersAreNonnullByDefault | |
@com.quizlet.android.annotations.ReturnValuesAreNonnullByDefault | |
@com.quizlet.android.annotations.FieldsAreNonnullByDefault | |
""" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment