|
| 1 | +task japicc { |
| 2 | + group 'verification' |
| 3 | + description 'Checks for binary and source incompatibility.' |
| 4 | + |
| 5 | + dependsOn jar, shadowJar |
| 6 | + |
| 7 | + def japiccVersion = '2.4' |
| 8 | + def workingDir = new File(project.buildDir, 'japicc') |
| 9 | + def executable = new File(workingDir, 'japi-compliance-checker-' + japiccVersion + '/japi-compliance-checker.pl') |
| 10 | + |
| 11 | + def lastSemVer = lastSemVer() |
| 12 | + def shadedName = project.name + '-' + shadedAppendix |
| 13 | + def lastJar = new File(workingDir, project.name + '-' + lastSemVer + '.jar') |
| 14 | + def lastShadedJar = new File(workingDir, shadedName + '-' + lastSemVer + '.jar') |
| 15 | + |
| 16 | + def nonImplFile = new File(workingDir, 'non-impl') |
| 17 | + |
| 18 | + def reportDir = new File(workingDir, 'compat_reports') |
| 19 | + def versions = lastSemVer + '_to_' + project.version |
| 20 | + def report = new File(new File(new File(reportDir, project.name), versions), 'compat_report.html') |
| 21 | + def shadedReport = new File(new File(new File(reportDir, shadedName), versions), 'compat_report.html') |
| 22 | + |
| 23 | + inputs.files jar, shadowJar |
| 24 | + outputs.files files(report, shadedReport) |
| 25 | + |
| 26 | + doFirst { |
| 27 | + description 'Check if last semantic version is available' |
| 28 | + println(description) |
| 29 | + |
| 30 | + if (project.version == lastSemVer) { |
| 31 | + throw new StopExecutionException('No last semantic version available') |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + doLast { |
| 36 | + description 'List non impl interfaces' |
| 37 | + println(description) |
| 38 | + |
| 39 | + nonImplFile.delete() |
| 40 | + nonImplFile.createNewFile() |
| 41 | + |
| 42 | + sourceSets.main.java.visit { FileTreeElement f -> |
| 43 | + if (f.file.isFile()) { |
| 44 | + def packageName = f.relativePath.parent.pathString.replace('/', '.').replace('\\', '.') |
| 45 | + |
| 46 | + def content = f.file.getText("UTF-8") |
| 47 | + content = content.replaceAll('//.*\n', ' ') // remove line comments |
| 48 | + content = content.replaceAll('\n', ' ') // remove new lines |
| 49 | + content = content.replaceAll('/\\*.*?\\*/', ' ') // remove multi line comments |
| 50 | + content = content.replaceAll(' +', ' ') // remove unnecessary spaces |
| 51 | + |
| 52 | + def index = 0 |
| 53 | + def classNames = [] |
| 54 | + while (true) { |
| 55 | + def start = content.indexOf(' interface ', index) |
| 56 | + if (start == -1) break |
| 57 | + |
| 58 | + def sub = content.substring(0, start) |
| 59 | + def level = sub.count('{') - sub.count('}') |
| 60 | + while (level < classNames.size()) { |
| 61 | + classNames.remove(classNames.size() - 1) |
| 62 | + } |
| 63 | + |
| 64 | + start += ' interface '.length() |
| 65 | + def end = content.indexOf('{', start) |
| 66 | + if (end == -1) break |
| 67 | + |
| 68 | + def interfaceDef = content.substring(start, end) |
| 69 | + def className = interfaceDef.split('[ <{]', 2)[0] |
| 70 | + classNames.add(className) |
| 71 | + |
| 72 | + def annotationIndex = content.indexOf('@DoNotImplement', index) |
| 73 | + if (annotationIndex == -1) break |
| 74 | + |
| 75 | + if (annotationIndex < start) { |
| 76 | + def qualifiedName = packageName + "." + classNames.join('.') |
| 77 | + |
| 78 | + def rest = interfaceDef.substring(className.length()).trim() |
| 79 | + if (rest.startsWith('<')) { |
| 80 | + rest = rest.replaceAll('extends [^ <,]+', '') // remove all extends ... |
| 81 | + rest = rest.replaceAll('@.*? ', '') // remove all annotations |
| 82 | + def generics = '<' |
| 83 | + def nesting = 0 |
| 84 | + for (def c : rest.chars) { |
| 85 | + if (c == '<') { |
| 86 | + nesting++ |
| 87 | + } else if (c == '>') { |
| 88 | + nesting-- |
| 89 | + } else if (nesting == 1) { |
| 90 | + generics += c |
| 91 | + } else if (nesting == 0) { |
| 92 | + break |
| 93 | + } |
| 94 | + } |
| 95 | + generics += '>' |
| 96 | + generics = generics.replace(' ', '') |
| 97 | + qualifiedName += generics |
| 98 | + } |
| 99 | + |
| 100 | + nonImplFile.append(qualifiedName + '\n') |
| 101 | + } |
| 102 | + |
| 103 | + index = end + 1 |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + doLast { |
| 110 | + description 'Download Java API Compliance Checker' |
| 111 | + println(description) |
| 112 | + |
| 113 | + def archive = new File(workingDir, 'japi-compliance-checker-' + japiccVersion + '.zip') |
| 114 | + archive.parentFile.mkdirs() |
| 115 | + if (!archive.exists()) { |
| 116 | + new URL('https://github.com/lvc/japi-compliance-checker/archive/' + japiccVersion + '.zip') |
| 117 | + .withInputStream { i -> archive.withOutputStream { it << i } } |
| 118 | + |
| 119 | + copy { |
| 120 | + from zipTree(archive) |
| 121 | + into workingDir |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + doLast { |
| 127 | + description 'Download last version' |
| 128 | + println(description) |
| 129 | + |
| 130 | + lastJar.parentFile.mkdirs() |
| 131 | + if (!lastJar.exists()) { |
| 132 | + String path = project.group.replace('.', '/') |
| 133 | + path += '/' + project.name + '/' + lastSemVer + '/' |
| 134 | + path += project.name + '-' + lastSemVer + '.jar' |
| 135 | + new URL('http://central.maven.org/maven2/' + path) |
| 136 | + .withInputStream { i -> lastJar.withOutputStream { it << i } } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + doLast { |
| 141 | + description 'Download last shaded version' |
| 142 | + println(description) |
| 143 | + |
| 144 | + lastShadedJar.parentFile.mkdirs() |
| 145 | + if (!lastShadedJar.exists()) { |
| 146 | + String path = project.group.replace('.', '/') |
| 147 | + path += '/' + shadedName + '/' + lastSemVer + '/' |
| 148 | + path += shadedName + '-' + lastSemVer + '.jar' |
| 149 | + new URL('http://central.maven.org/maven2/' + path) |
| 150 | + .withInputStream { i -> lastShadedJar.withOutputStream { it << i } } |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + doLast { |
| 155 | + description 'Check binary and source compatibility for last version' |
| 156 | + println(description) |
| 157 | + |
| 158 | + def command = ['perl', executable.getPath(), '-lib', project.name, |
| 159 | + '-skip-internal-packages', 'com.hivemq.client.internal', |
| 160 | + '-non-impl', nonImplFile.getPath(), |
| 161 | + '-check-annotations', '-s', |
| 162 | + lastJar.getPath(), jar.archivePath.getPath()] |
| 163 | + |
| 164 | + def process = new ProcessBuilder(command).directory(workingDir).start() |
| 165 | + def returnCode = process.waitFor() |
| 166 | + if (returnCode != 0) { |
| 167 | + throw new GradleException('Binary or source incompatibilities, code ' + returnCode) |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + doLast { |
| 172 | + description 'Check binary and source compatibility for last shaded version' |
| 173 | + println(description) |
| 174 | + |
| 175 | + def command = ['perl', executable.getPath(), '-lib', shadedName, |
| 176 | + '-skip-internal-packages', 'com.hivemq.client.internal', |
| 177 | + '-skip-internal-packages', 'com.hivemq.shaded', |
| 178 | + '-non-impl', nonImplFile.getPath(), |
| 179 | + '-check-annotations', '-s', |
| 180 | + lastShadedJar.getPath(), shadowJar.archivePath.getPath()] |
| 181 | + |
| 182 | + def process = new ProcessBuilder(command).directory(workingDir).start() |
| 183 | + def returnCode = process.waitFor() |
| 184 | + if (returnCode != 0) { |
| 185 | + throw new GradleException('Binary or source incompatibilities in shaded, code ' + returnCode) |
| 186 | + } |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +tasks.check.dependsOn(japicc) |
| 191 | + |
| 192 | +String lastSemVer() { |
| 193 | + String version = project.version |
| 194 | + def split = version.split('-')[0].split('\\.') |
| 195 | + def major = Integer.valueOf(split[0]) |
| 196 | + def minor = Integer.valueOf(split[1]) |
| 197 | + def patch = Integer.valueOf(split[2]) |
| 198 | + if (patch > 0) { |
| 199 | + patch-- |
| 200 | + } else if (minor > 0) { |
| 201 | + minor-- |
| 202 | + } |
| 203 | + return major + '.' + minor + '.' + patch |
| 204 | +} |
0 commit comments