2018-07-13 02:10:35 +02:00
|
|
|
/*
|
2019-01-02 01:57:15 +01:00
|
|
|
* Copyright © 2017-2019 WireGuard LLC. All Rights Reserved.
|
2018-07-13 02:10:35 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
task generateNonNullJavaFiles(dependsOn: "assembleDebug", type: Copy) {
|
|
|
|
group = "Copying"
|
|
|
|
description = "Generate package-info.java classes"
|
|
|
|
|
|
|
|
def basePackage = "com" + File.separatorChar + "wireguard"
|
|
|
|
def mainSrcPhrase = "src" + File.separatorChar + "main" + File.separatorChar +
|
|
|
|
"java" + File.separatorChar
|
|
|
|
def mainTestSrcPhrase = "src" + File.separatorChar + "test" + File.separatorChar +
|
|
|
|
"java" + File.separatorChar
|
|
|
|
def mainAndroidTestSrcPhrase = "src" + File.separatorChar + "androidTest" + File.separatorChar +
|
|
|
|
"java" + File.separatorChar
|
|
|
|
|
|
|
|
def sourceDir = file( "${projectDir}" + File.separatorChar + "src" + File.separatorChar +
|
|
|
|
"main" + File.separatorChar + "java" + File.separatorChar +
|
|
|
|
basePackage )
|
|
|
|
def testSourceDir = file( "${projectDir}" + File.separatorChar + "src" + File.separatorChar +
|
|
|
|
"test" + File.separatorChar + "java" + File.separatorChar +
|
|
|
|
basePackage)
|
|
|
|
def androidTestSourceDir = file( "${projectDir}" + File.separatorChar + "src" + File
|
|
|
|
.separatorChar +
|
|
|
|
"androidTest" + File.separatorChar + "java" + File.separatorChar +
|
|
|
|
basePackage )
|
|
|
|
|
2020-03-09 14:33:06 +01:00
|
|
|
generateInfoFiles(sourceDir, mainSrcPhrase)
|
2018-07-13 02:10:35 +02:00
|
|
|
sourceDir.eachDirRecurse { dir ->
|
|
|
|
generateInfoFiles(dir, mainSrcPhrase)
|
|
|
|
}
|
|
|
|
if (file(testSourceDir).exists()) {
|
2020-03-09 14:33:06 +01:00
|
|
|
generateInfoFiles(testSourceDir, mainTestSrcPhrase)
|
2018-07-13 02:10:35 +02:00
|
|
|
testSourceDir.eachDirRecurse { dir ->
|
|
|
|
generateInfoFiles(dir, mainTestSrcPhrase)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (file(androidTestSourceDir).exists()) {
|
2020-03-09 14:33:06 +01:00
|
|
|
generateInfoFiles(androidTestSourceDir, mainAndroidTestSrcPhrase)
|
2018-07-13 02:10:35 +02:00
|
|
|
androidTestSourceDir.eachDirRecurse { dir ->
|
|
|
|
generateInfoFiles(dir, mainAndroidTestSrcPhrase)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
println "[SUCCESS] NonNull generator: package-info.java files checked"
|
|
|
|
}
|
|
|
|
|
|
|
|
private void generateInfoFiles(File dir, String mainSrcPhrase) {
|
2020-03-09 14:33:06 +01:00
|
|
|
def infoFileContentHeader = getFileContentHeader()
|
|
|
|
def infoFileContentFooter = getFileContentFooter()
|
2018-07-13 02:10:35 +02:00
|
|
|
def infoFilePath = dir.getAbsolutePath() + File.separatorChar + "package-info.java"
|
|
|
|
|
|
|
|
//file(infoFilePath).delete(); //do not use in production code
|
|
|
|
if (!file(infoFilePath).exists()) {
|
2020-03-09 14:33:06 +01:00
|
|
|
def infoFileContentPackage = getFileContentPackage(dir.getAbsolutePath(), mainSrcPhrase)
|
2018-07-13 02:10:35 +02:00
|
|
|
new File(infoFilePath).write(infoFileContentHeader +
|
|
|
|
infoFileContentPackage + infoFileContentFooter)
|
2020-03-09 14:33:06 +01:00
|
|
|
println "[dir] " + infoFilePath + " created"
|
2018-07-13 02:10:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-09 14:33:06 +01:00
|
|
|
static def getFileContentPackage(String path, String mainSrcPhrase) {
|
2018-07-13 02:10:35 +02:00
|
|
|
def mainSrcPhraseIndex = path.indexOf(mainSrcPhrase)
|
|
|
|
def output = path.substring(mainSrcPhraseIndex)
|
|
|
|
|
|
|
|
// Win hotfix
|
|
|
|
if (System.properties['os.name'].toLowerCase().contains('windows')) {
|
|
|
|
output = output.replace("\\", "/")
|
|
|
|
mainSrcPhrase = mainSrcPhrase.replace("\\", "/")
|
|
|
|
}
|
|
|
|
|
|
|
|
return "package " + output.replaceAll(mainSrcPhrase, "").replaceAll(
|
|
|
|
"/", ".") + ";\n"
|
|
|
|
}
|
|
|
|
|
2020-03-09 14:33:06 +01:00
|
|
|
static def getFileContentHeader() {
|
2018-07-13 02:10:35 +02:00
|
|
|
return "/**\n" +
|
|
|
|
" * Make all method parameters @NonNull by default.\n" +
|
|
|
|
" */\n" +
|
|
|
|
"@NonNullForAll\n"
|
|
|
|
}
|
|
|
|
|
2020-03-09 14:33:06 +01:00
|
|
|
static def getFileContentFooter() {
|
2018-07-13 02:10:35 +02:00
|
|
|
return "\n" +
|
|
|
|
"import com.wireguard.util.NonNullForAll;\n"
|
|
|
|
}
|