summaryrefslogtreecommitdiffhomepage
path: root/app/nonnull.gradle
blob: 014e999a88f0583f38af009d6336823211b935c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
 * Copyright © 2018 Eric Kuck <eric@bluelinelabs.com>.
 * 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 )

    generateInfoFiles(sourceDir, mainSrcPhrase);
    sourceDir.eachDirRecurse { dir ->
        generateInfoFiles(dir, mainSrcPhrase)
    }
    if (file(testSourceDir).exists()) {
        generateInfoFiles(testSourceDir, mainTestSrcPhrase);
        testSourceDir.eachDirRecurse { dir ->
            generateInfoFiles(dir, mainTestSrcPhrase)
        }
    }
    if (file(androidTestSourceDir).exists()) {
        generateInfoFiles(androidTestSourceDir, mainAndroidTestSrcPhrase);
        androidTestSourceDir.eachDirRecurse { dir ->
            generateInfoFiles(dir, mainAndroidTestSrcPhrase)
        }
    }
    println "[SUCCESS] NonNull generator: package-info.java files checked"
}

private void generateInfoFiles(File dir, String mainSrcPhrase) {
    def infoFileContentHeader = getFileContentHeader();
    def infoFileContentFooter = getFileContentFooter();
    def infoFilePath = dir.getAbsolutePath() + File.separatorChar + "package-info.java"

    //file(infoFilePath).delete(); //do not use in production code
    if (!file(infoFilePath).exists()) {
        def infoFileContentPackage = getFileContentPackage(dir.getAbsolutePath(), mainSrcPhrase);
        new File(infoFilePath).write(infoFileContentHeader +
                infoFileContentPackage + infoFileContentFooter)
        println "[dir] " + infoFilePath + "  created";
    }
}

def getFileContentPackage(String path, String mainSrcPhrase) {
    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"
}

def getFileContentHeader() {
    return  "/**\n" +
            " * Make all method parameters @NonNull by default.\n" +
            " */\n" +
            "@NonNullForAll\n"
}

def getFileContentFooter() {
    return  "\n" +
            "import com.wireguard.util.NonNullForAll;\n"
}