-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.gradle
364 lines (314 loc) · 16 KB
/
build.gradle
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// Quick guide to making a release:
// $ git tag vX.Y.Z
// $ gradle bintrayUpload
//
// Please choose a version number using the principles of semantic versioning (http://semver.org):
// - MAJOR version when you make incompatible API changes,
// - MINOR version when you add functionality in a backwards-compatible manner, and
// - PATCH version when you make backwards-compatible bug fixes.
buildscript {
repositories {
jcenter()
maven { url "http://dl.bintray.com/palantir/releases" }
}
dependencies {
classpath 'com.bmuschko:gradle-nexus-plugin:2.3.1'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
classpath 'com.palantir:gradle-gitsemver:0.7.1'
classpath 'org.jtwig:jtwig-core:5.65'
}
}
apply plugin: 'java'
// Lets us sign our artifacts, construct a POM and use the uploadArchives task to upload to Sonatype OSS
// The disadvantage of going this route is that anything we upload to Sonatype has to be
// manually "closed" in the web interface (Nexus: https://oss.sonatype.org/) before it
// shows up on Maven Central.
apply plugin: 'com.bmuschko.nexus'
// So we augment Nexus with this plugin, which lets us upload the signed, POMified artifacts
// to Bintray. Bintray operate a Maven repo called JCenter which can be used as an alternative
// to Maven Central, but which also supports fully automatic syncing to Maven Central via Sonatype OSS.
// So this plugin lets us upload to Bintray + JCenter + Maven Central with a single "bintrayUpload" task
apply plugin: 'com.jfrog.bintray'
apply plugin: 'gitsemver'
// The Palantir plugin infers the version number from the git tags: https://github.com/palantir/gradle-gitsemver
// When you update the version number, please follow the principles of Semantic Versioning: http://semver.org
version semverVersion()
group 'uk.co.omega-prime'
def projectName = 'btreemap'
def projectDescription = 'A simple and high-performance Java B-tree: drop-in replacement for java.util.TreeMap.'
sourceCompatibility = 1.8
import org.jtwig.environment.EnvironmentConfiguration
import org.jtwig.environment.DefaultEnvironmentConfiguration
import org.jtwig.JtwigTemplate
import org.jtwig.JtwigModel
class TypeProperties {
private final String name, primitive, boxed, erased, erasedBoxed, dfault;
private TypeProperties(String name, String primitive, String boxed, String erased, String erasedBoxed, String dfault) {
this.name = name
this.primitive = primitive;
this.boxed = boxed;
this.erased = erased;
this.erasedBoxed = erasedBoxed;
this.dfault = dfault;
}
String getName() { return name; }
String getPrimitive() { return primitive; }
String getBoxed() { return boxed; }
String getUnboxed() { return primitive == null ? boxed : primitive; }
String getErased() { return erased; }
String getErasedBoxed() { return erasedBoxed; }
String getDfault() { return dfault; }
boolean isPrimitive() { return primitive != null; }
boolean isObject() { return !isPrimitive(); }
String toString() { return primitive; }
static final TypeProperties INT = new TypeProperties("Int", "int", "Integer", "int", "Integer", "Integer.MIN_VALUE")
static final TypeProperties LONG = new TypeProperties("Long", "long", "Long", "long", "Long", "Long.MIN_VALUE")
static TypeProperties object(String tyParam) {
return new TypeProperties("Object", null, tyParam, "Object", "Object", "null");
}
static final List<TypeProperties> PRIMITIVE = [INT, LONG]
}
abstract class TypesProperties {
final JtwigModel model;
TypesProperties(JtwigModel model) {
this.model = model
}
// We have a few hacky extensions to the template language to let us write the templates without
// *totally* breaking intellij. We apply those in this method.
abstract String tweakTemplate(String line);
}
// There's no two ways about it: this is extremely ugly. But it does get the job done, and it doesn't impact library users.
task generateJava {
doLast{
delete fileTree(dir: "${buildDir}/generated-src")
EnvironmentConfiguration configuration = new DefaultEnvironmentConfiguration()
FileTree tree = fileTree(dir: 'src/main/templates', include: '**/*.java')
tree.each { File input ->
String relative = file('src/main/templates').toPath().relativize(input.getParentFile().toPath()).toString()
List<TypesProperties> types = []
if (input.getName().contains('{{KV_}}')) {
for (k in [TypeProperties.object("K")] + TypeProperties.PRIMITIVE) {
for (v in [TypeProperties.object("V")] + TypeProperties.PRIMITIVE) {
String kPrefix, kvPrefix, kObjectPrefix, kTyReplacement, kvTyReplacement, kvComparableTyReplacement,
kObjectTyReplacement, kvsupTyReplacement, ksupvTyReplacement, ksupTyReplacement, ksupvsupTyReplacement
if (k.isObject() && v.isObject()) {
kPrefix = ''
kvPrefix = ''
kObjectPrefix = ''
kTyReplacement = '<K>'
ksupTyReplacement = '<? super K>'
kvTyReplacement = '<K,V>'
kvComparableTyReplacement = '<K extends Comparable<? super K>, V>'
kObjectTyReplacement = '<K, AbstractNode>'
kvsupTyReplacement = '<?, ? super V>'
ksupvTyReplacement = '<? super K, ?>'
ksupvsupTyReplacement = '<? super K, ? super V>'
} else if (k.isObject()) {
kPrefix = ''
kvPrefix = 'Object' + v.name
kObjectPrefix = ''
kTyReplacement = '<K>'
ksupTyReplacement = '<? super K>'
kvTyReplacement = '<K>'
kvComparableTyReplacement = '<K extends Comparable<? super K>>'
kObjectTyReplacement = '<K, AbstractNode>'
kvsupTyReplacement = '<?>'
ksupvTyReplacement = '<? super K>'
ksupvsupTyReplacement = '<? super K>'
} else if (v.isObject()) {
kPrefix = k.name
kvPrefix = k.name + 'Object'
kObjectPrefix = k.name + 'Object'
kTyReplacement = ''
ksupTyReplacement = ''
kvTyReplacement = kvComparableTyReplacement = '<V>'
kObjectTyReplacement = '<Object>'
kvsupTyReplacement = '<? super V>'
ksupvTyReplacement = '<?>'
ksupvsupTyReplacement = '<? super V>'
} else {
kPrefix = k.name
kvPrefix = k.name + v.name
kObjectPrefix = k.name + 'Object'
kTyReplacement = ''
ksupTyReplacement = ''
kvTyReplacement = kvComparableTyReplacement = ''
kObjectTyReplacement = ''
kvsupTyReplacement = ''
ksupvTyReplacement = ''
ksupvsupTyReplacement = ''
}
types.add(new TypesProperties(JtwigModel.newModel()
.with("K", k).with("V", v)
.with("K_", k.isObject() ? "" : k.name)
.with("KV_", kvPrefix)
.with("KObject_", kObjectPrefix)) {
String tweakTemplate(String line) {
return line.replaceAll('([A-Za-z]+)<[$]K[$], ?[$]V[$]>[.]class', kvPrefix + '$1.class')
.replaceAll('([A-Za-z]+)<[$]K[$], ?[$]V[$]>', kvPrefix + '$1' + kvTyReplacement)
.replaceAll('([A-Za-z]+)<[$]K[$], ?AbstractNode>', kObjectPrefix + '$1' + kObjectTyReplacement)
.replaceAll('<[$]K[$] extends Comparable<[?] super [$]K[$]>, [$]V[$]>', kvComparableTyReplacement)
.replaceAll('([A-Za-z]+)<[?], ?[?] super [$]V[$]>', kvPrefix + '$1' + kvsupTyReplacement)
.replaceAll('([A-Za-z]+)<[?] super [$]K[$], ?[?]>', kvPrefix + '$1' + ksupvTyReplacement)
.replaceAll('([A-Za-z]+)<[?] super [$]K[$], ?[?] super [$]V[$]>', kvPrefix + '$1' + ksupvsupTyReplacement)
.replaceAll('([A-Za-z]+)<[?] super (@Boxed )?[$]K[$]>', kPrefix + '$1' + ksupTyReplacement)
.replaceAll('<[$]K[$], ?[$]V[$]>', kvTyReplacement)
.replaceAll('([A-Za-z]+)<[$]K[$]>', kPrefix + '$1' + kTyReplacement)
.replaceAll('<[$]K[$]>', kTyReplacement)
.replaceAll('([A-Za-z]+)<[$]K[$]>', '{{K_}}$1')
.replace('@Erased @Boxed $V$', '{{V.erasedBoxed}}')
.replace('@Erased $K$', '{{K.erased}}')
.replace('@Erased $V$', '{{V.erased}}')
.replace('@Boxed $K$', '{{K.boxed}}')
.replace('@Boxed $V$', '{{V.boxed}}')
.replace('$K$', '{{K.unboxed}}')
.replace('$V$', '{{V.unboxed}}')
}
})
}
}
} else if (input.getName().contains('{{K_}}')) {
for (k in [TypeProperties.object("K")] + TypeProperties.PRIMITIVE) {
if (k.isObject() && input.getName().equals("{{K_}}Comparator.java")) {
// Hack because the JDK already has a boxed comparator
continue
}
types.add(new TypesProperties(JtwigModel.newModel()
.with("K", k)
.with("K_", k.isObject() ? "" : k.name)) {
@Override
String tweakTemplate(String line) {
return line.replaceAll('([A-Za-z]+)<[$]K[$]>', '{{K_}}$1')
.replace('$K$', '{{K.unboxed}}')
}
})
}
} else {
throw new IllegalStateException("Found template ${input} that didn't have any obvious filename pattern")
}
for (TypesProperties type : types) {
JtwigTemplate filenameTemplate = JtwigTemplate.inlineTemplate(input.getName(), configuration)
String outputFilename = filenameTemplate.render(type.model)
List<String> inputLines = input.readLines("UTF-8").collect { type.tweakTemplate(it) }
JtwigTemplate fileTemplate = JtwigTemplate.inlineTemplate(inputLines.join("\n"), configuration)
String output = fileTemplate.render(type.model)
File outputFile = new File(file("${buildDir}/generated-src/${relative}"), outputFilename)
outputFile.getParentFile().mkdirs()
if (!java.nio.file.Files.notExists(outputFile.toPath())) {
throw new IOException("Can't write to ${outputFile} twice")
}
outputFile.write(output)
}
}
}
}
apply plugin: "idea"
sourceSets.main.java.srcDir new File("${buildDir}/generated-src")
idea {
module {
generatedSourceDirs += file("${buildDir}/generated-src")
}
}
compileJava {
dependsOn generateJava
source "${buildDir}/generated-src"
}
javadoc {
dependsOn generateJava
source "${buildDir}/generated-src"
}
jar {
baseName = projectName
manifest {
attributes 'Implementation-Title': projectName,
'Implementation-Version': version
}
}
repositories {
mavenCentral()
}
dependencies {
compile group: 'com.google.code.findbugs', name: 'jsr305', version: '3.0.1'
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile group: 'com.pholser', name: 'junit-quickcheck-core', version: '0.7'
testCompile group: 'com.pholser', name: 'junit-quickcheck-generators', version: '0.7'
testCompile group: 'org.openjdk.jmh', name: 'jmh-generator-annprocess', version: '1.17.3'
//testCompile group: 'it.unimi.dsi', name: 'fastutil', version: '7.0.13'
//testCompile group: 'org.mapdb', name: 'mapdb', version: '1.0.9'
}
task benchmark(type: JavaExec) {
classpath sourceSets.test.runtimeClasspath
main = "uk.co.omegaprime.btreemap.BTreeMapBenchmark"
args "-f1"
}
// I don't like writing JavaDoc for every param individually.
// http://blog.joda.org/2014/02/turning-off-doclint-in-jdk-8-javadoc.html
if (JavaVersion.current().isJava8Compatible()) {
allprojects {
tasks.withType(Javadoc) {
// The -quiet is because of some sort of weird JDK JavaCompiler bug: https://discuss.gradle.org/t/passing-arguments-to-compiler-and-javadoc/1661
options.addStringOption('Xdoclint:all,-missing', '-quiet')
}
}
}
// Used by the nexus plugin
modifyPom {
project {
name projectName
description projectDescription
url 'https://github.com/batterseapower/btreemap'
scm {
url 'https://github.com/batterseapower/btreemap'
connection 'scm:https://[email protected]/batterseapower/btreemap.git'
developerConnection 'scm:git://github.com/batterseapower/btreemap.git'
}
licenses {
license {
name 'The Apache Software License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
distribution 'repo'
}
}
developers {
developer {
id 'batterseapower'
name 'Max Bolingbroke'
email '[email protected]'
}
}
}
}
// I tried to have the Nexus plugin sign things, rather than using the Bintray
// automatic signing support, but when e.g. mdbi-1.0.2-sources.jar.asc got uploaded
// to Bintray, Bintray seemed to mangle the filename to mdbi-1.0.2-sources-jar.asc.
// This would obviously cause the Maven Central publish to fail because it couldn't
// figure out what file that was meant to be a signature for. So I gave up, gave
// Bintray my GPG private key, and turned on auto-signing in the BinTray repo settings.
nexus {
sign = false
}
// e.g. Travis won't have the Bintray config
if (hasProperty('bintrayUsername') || System.getenv().containsKey('BINTRAY_USER')) {
// Used by the bintray plugin
bintray {
user = System.getenv().getOrDefault('BINTRAY_USER', bintrayUsername)
key = System.getenv().getOrDefault('BINTRAY_KEY', bintrayApiKey)
publish = true
pkg {
repo = 'maven'
name = projectName
licenses = ['Apache-2.0']
vcsUrl = 'https://github.com/batterseapower/btreemap.git'
version {
name = project.version
desc = projectDescription
released = new Date()
mavenCentralSync {
user = System.getenv().getOrDefault('SONATYPE_USER', nexusUsername)
password = System.getenv().getOrDefault('SONATYPE_PASSWORD', nexusPassword)
}
}
}
configurations = ['archives']
}
}