-
Notifications
You must be signed in to change notification settings - Fork 13
/
build.gradle.kts
507 lines (463 loc) · 17.5 KB
/
build.gradle.kts
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
import de.undercouch.gradle.tasks.download.Download
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import java.security.MessageDigest
import java.security.NoSuchAlgorithmException
group = "org.edrdg.jmdict.simplified"
version = "3.6.1"
val jmdictLanguages = listOf( // ISO 639-2, 3-letter codes
"all", "eng", "eng-common",
// Other languages, in order of # of entries:
"ger", "rus", "hun", "dut", "spa", "fre", "swe", "slv",
)
val jmdictReportFile = "jmdict-release-info.md"
val jmdictExamplesReportFile = "jmdict-examples-release-info.md"
val jmnedictLanguages = listOf("all") // There is only English
val jmnedictReportFile = "jmnedict-release-info.md"
val kanjidicLanguages = listOf("all", "en") // ISO 639-1, 2-letter codes
val kanjidicReportFile = "kanjidic-release-info.md"
plugins {
id("de.undercouch.download") version "5.3.0"
kotlin("jvm") version "1.8.0"
kotlin("plugin.serialization") version "1.8.0"
application
}
application {
mainClass.set("org.edrdg.jmdict.simplified.MainKt")
applicationDefaultJvmArgs = listOf("-Xmx6g", "-Djdk.xml.entityExpansionLimit=0")
}
tasks {
// See <https://www.baeldung.com/kotlin/gradle-executable-jar>
val uberJar = register<Jar>("uberJar") {
// We need this for Gradle optimization to work
dependsOn.addAll(listOf("compileJava", "compileKotlin", "processResources"))
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
manifest { attributes(mapOf("Main-Class" to application.mainClass)) }
val sourcesMain = sourceSets.main.get()
val runtime = configurations.runtimeClasspath.get()
.map { if (it.isDirectory) it else zipTree(it) }
from(runtime + sourcesMain.output)
}
build {
dependsOn(uberJar)
}
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "13"
}
repositories {
mavenCentral()
}
dependencies {
implementation(kotlin("stdlib"))
implementation("org.slf4j:slf4j-simple:2.0.6")
implementation("io.github.microutils:kotlin-logging:3.0.4")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.4.1")
implementation("com.github.ajalt.clikt:clikt:3.5.0")
implementation("net.swiftzer.semver:semver:1.2.0")
}
val createDictXmlDir: Task by tasks.creating {
val dictXmlDir = "$buildDir/dict-xml"
extra["dictXmlDir"] = dictXmlDir
doLast {
mkdir(dictXmlDir)
}
}
val jmdictDownload by tasks.creating(Download::class) {
group = "Download"
description = "Download JMdict source XML archive"
val dictXmlDir: String by createDictXmlDir.extra
val filePath = "$dictXmlDir/JMdict.gz"
src("http://ftp.edrdg.org/pub/Nihongo/JMdict.gz")
dest(filePath)
extra["archivePath"] = filePath
overwrite(true)
onlyIfModified(true)
}
val jmdictExtract: Task by tasks.creating {
group = "Extract"
description = "Extract JMdict source XML from an archive"
dependsOn(jmdictDownload)
val dictXmlDir: String by createDictXmlDir.extra
val archivePath: String by jmdictDownload.extra
val filePath = "$dictXmlDir/JMdict.xml"
extra["jmdictPath"] = filePath
doLast {
resources.gzip(archivePath).read().copyTo(file(filePath).outputStream())
}
}
val jmdictExamplesDownload by tasks.creating(Download::class) {
group = "Download"
description = "Download JMdict with examples source XML archive"
val dictXmlDir: String by createDictXmlDir.extra
val filePath = "$dictXmlDir/JMdict_e_examp.gz"
src("http://ftp.edrdg.org/pub/Nihongo/JMdict_e_examp.gz")
dest(filePath)
extra["archivePath"] = filePath
overwrite(true)
onlyIfModified(true)
}
val jmdictExamplesExtract by tasks.creating {
group = "Extract"
description = "Extract JMdict with examples source XML from an archive"
dependsOn(jmdictExamplesDownload)
val dictXmlDir: String by createDictXmlDir.extra
val archivePath: String by jmdictExamplesDownload.extra
val filePath = "$dictXmlDir/JMdict_e_examp.xml"
extra["jmdictExamplesPath"] = filePath
doLast {
resources.gzip(archivePath).read().copyTo(file(filePath).outputStream())
}
}
val jmnedictDownload by tasks.creating(Download::class) {
group = "Download"
description = "Download JMnedict source XML archive"
val dictXmlDir: String by createDictXmlDir.extra
val filePath = "$dictXmlDir/JMnedict.xml.gz"
src("http://ftp.edrdg.org/pub/Nihongo/JMnedict.xml.gz")
dest(filePath)
extra["archivePath"] = filePath
overwrite(true)
onlyIfModified(true)
}
val jmnedictExtract: Task by tasks.creating {
group = "Extract"
description = "Extract JMnedict source XML from an archive"
dependsOn(jmnedictDownload)
val dictXmlDir: String by createDictXmlDir.extra
val archivePath: String by jmnedictDownload.extra
val filePath = "$dictXmlDir/JMnedict.xml"
extra["jmnedictPath"] = filePath
doLast {
resources.gzip(archivePath).read().copyTo(file(filePath).outputStream())
}
}
val kanjidicDownload by tasks.creating(Download::class) {
group = "Download"
description = "Download Kanjidic source XML archive"
val dictXmlDir: String by createDictXmlDir.extra
val filePath = "$dictXmlDir/kanjidic2.xml.gz"
src("http://www.edrdg.org/kanjidic/kanjidic2.xml.gz")
dest(filePath)
extra["archivePath"] = filePath
overwrite(true)
onlyIfModified(true)
}
val kanjidicExtract: Task by tasks.creating {
group = "Extract"
description = "Extract Kanjidic source XML from an archive"
dependsOn(kanjidicDownload)
val dictXmlDir: String by createDictXmlDir.extra
val archivePath: String by kanjidicDownload.extra
val filePath = "$dictXmlDir/kanjidic2.xml"
extra["kanjidicPath"] = filePath
doLast {
resources.gzip(archivePath).read().copyTo(file(filePath).outputStream())
}
}
val kradfileDownload by tasks.creating(Download::class) {
group = "Download"
description = "Download KRADFILE2/RADKFILE2 archive"
val dictXmlDir: String by createDictXmlDir.extra
val filePath = "$dictXmlDir/kradzip.zip"
src("http://ftp.edrdg.org/pub/Nihongo/kradzip.zip")
dest(filePath)
extra["archivePath"] = filePath
overwrite(true)
onlyIfModified(true)
}
val kradfileExtract by tasks.creating(Copy::class) {
group = "Extract"
description = "Extract KRADFILE2/RADKFILE2 sources from an archive"
dependsOn(kradfileDownload)
val dictXmlDir: String by createDictXmlDir.extra
val archivePath: String by kradfileDownload.extra
val kradfilePath = "$dictXmlDir/kradfile"
val kradfile2Path = "$dictXmlDir/kradfile2"
val radkfilexPath = "$dictXmlDir/radkfilex"
extra["kradfilePath"] = kradfilePath
extra["kradfile2Path"] = kradfile2Path
extra["radkfilexPath"] = radkfilexPath
from(
zipTree(archivePath)
)
into(dictXmlDir)
}
/**
* Download and extract all dictionaries
*/
val download: Task by tasks.creating {
group = "Download"
description = "Download and unpack all dictionaries"
dependsOn(jmdictExtract, jmdictExamplesExtract, jmnedictExtract, kanjidicExtract, kradfileExtract)
}
fun getFileHash(inputFilePath: String): String {
try {
val md = MessageDigest.getInstance("SHA-256")
return md.digest(file(inputFilePath).readBytes())
.joinToString("") { b -> (0xFF and b.toInt()).toString(16) }
} catch (e: NoSuchAlgorithmException) {
throw Exception("SHA-256 is not supported in this Java instance", e)
}
}
val jmdictUpdateChecksumFile: Task by tasks.creating {
group = "Checksum"
description = "Generate a checksum of JMdict XML file and write it into a checksum file"
val jmdictPath: String by jmdictExtract.extra
val jmdictChecksumPath = "$projectDir/checksums/JMdict.xml.sha256"
extra["jmdictChecksumPath"] = jmdictChecksumPath
doLast {
file(jmdictChecksumPath).writeText(getFileHash(jmdictPath))
}
}
val jmdictExamplesUpdateChecksumFile: Task by tasks.creating {
group = "Checksum"
description = "Generate a checksum of JMdict with examples XML file and write it into a checksum file"
val jmdictExamplesPath: String by jmdictExamplesExtract.extra
val jmdictExamplesChecksumPath = "$projectDir/checksums/JMdict_e_examp.xml.sha256"
extra["jmdictExamplesChecksumPath"] = jmdictExamplesChecksumPath
doLast {
file(jmdictExamplesChecksumPath).writeText(getFileHash(jmdictExamplesPath))
}
}
val jmnedictUpdateChecksumFile: Task by tasks.creating {
group = "Checksum"
description = "Generate a checksum of JMnedict XML file and write it into a checksum file"
val jmnedictPath: String by jmnedictExtract.extra
val jmnedictChecksumPath = "$projectDir/checksums/JMnedict.xml.sha256"
extra["jmnedictChecksumPath"] = jmnedictChecksumPath
doLast {
file(jmnedictChecksumPath).writeText(getFileHash(jmnedictPath))
}
}
val kanjidicUpdateChecksumFile: Task by tasks.creating {
group = "Checksum"
description = "Generate a checksum of Kanjidic XML file and write it into a checksum file"
val kanjidicPath: String by kanjidicExtract.extra
val kanjidicChecksumPath = "$projectDir/checksums/kanjidic2.xml.sha256"
extra["kanjidicChecksumPath"] = kanjidicChecksumPath
doLast {
file(kanjidicChecksumPath).writeText(getFileHash(kanjidicPath))
}
}
/**
* We don't do change checks for kradfile and radkfile, as they change rarely.
* Instead, we expect other dictionaries to change, which triggers conversion
* of kradfile and radkfile.
*/
val updateChecksums: Task by tasks.creating {
group = "Checksum"
description = "Generate checksums of all dictionaries and write into checksum files"
dependsOn(
jmdictUpdateChecksumFile,
jmdictExamplesUpdateChecksumFile,
jmnedictUpdateChecksumFile,
kanjidicUpdateChecksumFile,
)
}
val jmdictHasChanged: Task by tasks.creating {
group = "Checksum"
description = "Check if a checksum of JMdict XML file has changed"
val jmdictPath: String by jmdictExtract.extra
val jmdictChecksumPath: String by jmdictUpdateChecksumFile.extra
doLast {
val previousChecksum = file(jmdictChecksumPath).readText().trim()
val newChecksum = getFileHash(jmdictPath).trim()
println(if (previousChecksum == newChecksum) "NO" else "YES")
}
}
val jmdictExamplesHasChanged: Task by tasks.creating {
group = "Checksum"
description = "Check if a checksum of JMdict with examples XML file has changed"
val jmdictExamplesPath: String by jmdictExamplesExtract.extra
val jmdictExamplesChecksumPath: String by jmdictExamplesUpdateChecksumFile.extra
doLast {
val previousChecksum = file(jmdictExamplesChecksumPath).readText().trim()
val newChecksum = getFileHash(jmdictExamplesPath).trim()
println(if (previousChecksum == newChecksum) "NO" else "YES")
}
}
val jmnedictHasChanged: Task by tasks.creating {
group = "Checksum"
description = "Check if a checksum of JMnedict XML file has changed"
val jmnedictPath: String by jmnedictExtract.extra
val jmnedictChecksumPath: String by jmnedictUpdateChecksumFile.extra
doLast {
val previousChecksum = file(jmnedictChecksumPath).readText().trim()
val newChecksum = getFileHash(jmnedictPath).trim()
println(if (previousChecksum == newChecksum) "NO" else "YES")
}
}
val kanjidicHasChanged: Task by tasks.creating {
group = "Checksum"
description = "Check if a checksum of Kanjidic XML file has changed"
val kanjidicPath: String by kanjidicExtract.extra
val kanjidicChecksumPath: String by kanjidicUpdateChecksumFile.extra
doLast {
val previousChecksum = file(kanjidicChecksumPath).readText().trim()
val newChecksum = getFileHash(kanjidicPath).trim()
println(if (previousChecksum == newChecksum) "NO" else "YES")
}
}
val createDictJsonDir: Task by tasks.creating {
val dictJsonDir = "$buildDir/dict-json"
extra["dictJsonDir"] = dictJsonDir
doLast {
mkdir(dictJsonDir)
}
}
val jmdictConvert: Task by tasks.creating(Exec::class) {
group = "Convert"
description = "Convert JMdict"
dependsOn(createDictJsonDir, tasks.getByName("uberJar"))
val dictJsonDir: String by createDictJsonDir.extra
val jmdictPath: String by jmdictExtract.extra
commandLine = listOf(
"java",
"-Djdk.xml.entityExpansionLimit=0", // To avoid errors about # of entities in XML files
"-jar",
(tasks.getByName("uberJar") as Jar).archiveFile.get().asFile.path,
"convert-jmdict",
"--version=$version",
"--languages=${jmdictLanguages.joinToString(",")}",
"--report=$dictJsonDir${File.separator}$jmdictReportFile",
jmdictPath,
dictJsonDir,
)
}
val jmdictExamplesConvert: Task by tasks.creating(Exec::class) {
group = "Convert"
description = "Convert JMdict with examples"
dependsOn(createDictJsonDir, tasks.getByName("uberJar"))
val dictJsonDir: String by createDictJsonDir.extra
val jmdictExamplesPath: String by jmdictExamplesExtract.extra
commandLine = listOf(
"java",
"-Djdk.xml.entityExpansionLimit=0", // To avoid errors about # of entities in XML files
"-jar",
(tasks.getByName("uberJar") as Jar).archiveFile.get().asFile.path,
"convert-jmdict-examples",
"--version=$version",
"--languages=eng", // in this task, the source file is English-only
"--report=$dictJsonDir${File.separator}$jmdictExamplesReportFile",
jmdictExamplesPath,
dictJsonDir,
)
}
val jmnedictConvert: Task by tasks.creating(Exec::class) {
group = "Convert"
description = "Convert JMnedict"
dependsOn(createDictJsonDir, tasks.getByName("uberJar"))
val dictJsonDir: String by createDictJsonDir.extra
val jmnedictPath: String by jmnedictExtract.extra
commandLine = listOf(
"java",
"-Djdk.xml.entityExpansionLimit=0", // To avoid errors about # of entities in XML files
"-jar",
(tasks.getByName("uberJar") as Jar).archiveFile.get().asFile.path,
"convert-jmnedict",
"--version=$version",
"--languages=${jmnedictLanguages.joinToString(",")}",
"--report=$dictJsonDir${File.separator}$jmnedictReportFile",
jmnedictPath,
dictJsonDir,
)
}
val kanjidicConvert: Task by tasks.creating(Exec::class) {
group = "Convert"
description = "Convert Kanjidic"
dependsOn(createDictJsonDir, tasks.getByName("uberJar"))
val dictJsonDir: String by createDictJsonDir.extra
val kanjidicPath: String by kanjidicExtract.extra
commandLine = listOf(
"java",
"-Djdk.xml.entityExpansionLimit=0", // To avoid errors about # of entities in XML files
"-jar",
(tasks.getByName("uberJar") as Jar).archiveFile.get().asFile.path,
"convert-kanjidic",
"--version=$version",
"--languages=${kanjidicLanguages.joinToString(",")}",
"--report=$dictJsonDir${File.separator}$kanjidicReportFile",
kanjidicPath,
dictJsonDir,
)
}
val kradfileConvert by tasks.creating(Exec::class) {
group = "Convert"
description = "Convert KRADFILE and KRADFILE2"
dependsOn(createDictJsonDir, tasks.getByName("uberJar"))
val dictJsonDir: String by createDictJsonDir.extra
val kradfilePath: String by kradfileExtract.extra
val kradfile2Path: String by kradfileExtract.extra
commandLine = listOf(
"java",
"-Djdk.xml.entityExpansionLimit=0", // To avoid errors about # of entities in XML files
"-jar",
(tasks.getByName("uberJar") as Jar).archiveFile.get().asFile.path,
"convert-kradfile",
"--version=$version",
kradfilePath,
kradfile2Path,
dictJsonDir,
)
}
val radkfileConvert by tasks.creating(Exec::class) {
group = "Convert"
description = "Convert RADKFILE (combined radkfilex)"
dependsOn(createDictJsonDir, tasks.getByName("uberJar"))
val dictJsonDir: String by createDictJsonDir.extra
val radkfilexPath: String by kradfileExtract.extra
commandLine = listOf(
"java",
"-Djdk.xml.entityExpansionLimit=0", // To avoid errors about # of entities in XML files
"-jar",
(tasks.getByName("uberJar") as Jar).archiveFile.get().asFile.path,
"convert-radkfile",
"--version=$version",
radkfilexPath,
dictJsonDir,
)
}
val convert: Task by tasks.creating {
group = "Convert"
description = "Convert all dictionaries"
dependsOn(
jmdictConvert,
jmdictExamplesConvert,
jmnedictConvert,
kanjidicConvert,
kradfileConvert,
radkfileConvert,
)
}
val zipAll: Task by tasks.creating {
group = "Distribution"
description = "Zip all JSON files"
val dictJsonDir: String by createDictJsonDir.extra
fileTree(dictJsonDir)
.filter { it.isFile && it.extension == "json" }
.forEachIndexed { idx, file ->
dependsOn.add(tasks.create("zip$idx", Zip::class) {
from(dictJsonDir) { include(file.name) }
archiveFileName.set("${file.name}.zip")
})
}
}
val tarAll: Task by tasks.creating {
group = "Distribution"
description = "Tar+gzip all JSON files"
val dictJsonDir: String by createDictJsonDir.extra
fileTree(dictJsonDir)
.filter { it.isFile && it.extension == "json" }
.forEachIndexed { idx, file ->
dependsOn.add(tasks.create("tar$idx", Tar::class) {
compression = Compression.GZIP
from(dictJsonDir) { include(file.name) }
archiveFileName.set("${file.name}.tgz")
})
}
}
val archive: Task by tasks.creating {
group = "Distribution"
description = "Create archives of all JSON files"
dependsOn(zipAll, tarAll)
}