forked from tuProlog/2p-kt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.gradle.kts
490 lines (421 loc) · 15.7 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
import com.github.breadmoirai.githubreleaseplugin.GithubReleaseExtension
import com.github.breadmoirai.githubreleaseplugin.GithubReleaseTask
import com.jfrog.bintray.gradle.tasks.BintrayUploadTask
import node.Bugs
import node.NpmPublishExtension
import node.NpmPublishPlugin
import node.People
import org.jetbrains.dokka.gradle.DokkaTask
import org.jetbrains.dokka.gradle.GradlePassConfigurationImpl
import org.jetbrains.kotlin.gradle.dsl.KotlinJsCompile
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmCompile
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsSetupTask
import org.jetbrains.kotlin.gradle.targets.js.npm.tasks.KotlinPackageJsonTask
import org.jetbrains.kotlin.gradle.tasks.Kotlin2JsCompile
plugins {
kotlin("multiplatform") version Versions.org_jetbrains_kotlin_multiplatform_gradle_plugin apply true
id("maven-publish")
signing
id("org.jetbrains.dokka") version Versions.org_jetbrains_dokka_gradle_plugin
id("com.jfrog.bintray") version Versions.com_jfrog_bintray_gradle_plugin
id("org.danilopianini.git-sensitive-semantic-versioning") version Versions.org_danilopianini_git_sensitive_semantic_versioning_gradle_plugin
id("de.fayard.buildSrcVersions") version Versions.de_fayard_buildsrcversions_gradle_plugin
id("com.github.breadmoirai.github-release") version Versions.com_github_breadmoirai_github_release_gradle_plugin
}
repositories {
maven(Repos.Mirrors.mavenCentralEurope)
mavenCentral()
jcenter()
maven(Repos.dokka)
}
group = "it.unibo.tuprolog"
gitSemVer {
minimumVersion.set("0.1.0")
developmentIdentifier.set("dev")
noTagIdentifier.set("archeo")
developmentCounterLength.set(2) // How many digits after `dev`
version = computeGitSemVer() // THIS IS MANDATORY, AND MUST BE LAST IN THIS BLOCK!
}
println("2p-Kt version: $version")
val javaVersion: String by project
val ktFreeCompilerArgsJvm: String by project
val gcName: String by project
val gcEmail: String by project
val gcUrl: String by project
val projectHomepage: String by project
val bintrayRepo: String by project
val bintrayUserOrg: String by project
val projectLicense: String by project
val projectIssues: String by project
val githubOwner: String by project
val githubRepo: String by project
val mochaTimeout: String by project
val signingKey = getPropertyOrWarnForAbsence("signingKey")
val signingPassword = getPropertyOrWarnForAbsence("signingPassword")
val bintrayUser = getPropertyOrWarnForAbsence("bintrayUser")
val bintrayKey = getPropertyOrWarnForAbsence("bintrayKey")
val ossrhUsername = getPropertyOrWarnForAbsence("ossrhUsername")
val ossrhPassword = getPropertyOrWarnForAbsence("ossrhPassword")
val githubToken = getPropertyOrWarnForAbsence("githubToken")
val npmToken = getPropertyOrWarnForAbsence("npmToken")
val allSubprojects = allprojects.map { it.name }.toSet()
val jvmSubprojects = setOf("parser-jvm", "examples")
val jsSubprojects = setOf("parser-js")
val docSubprojects = setOf("documentation")
val ktSubprojects = allSubprojects - jvmSubprojects - jsSubprojects - docSubprojects
val codeSubprojects = allSubprojects - docSubprojects
val publishAllToBintrayTask = tasks.create<DefaultTask>("publishAllToBintray") {
group = "publishing"
}
allSubprojects.forEachProject {
group = rootProject.group
version = rootProject.version
repositories.addAll(rootProject.repositories)
configureTestResultPrinting()
}
ktSubprojects.forEachProject {
apply(plugin = "org.jetbrains.kotlin.multiplatform")
apply(plugin = "maven-publish")
apply(plugin = "signing")
apply(plugin = "org.jetbrains.dokka")
apply(plugin = "com.jfrog.bintray")
kotlin {
sourceSets {
val commonMain by getting {
dependencies {
api(kotlin("stdlib-common"))
// api(kotlin("reflect"))
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
jvm {
compilations["main"].defaultSourceSet {
dependencies {
api(kotlin("stdlib-jdk8"))
}
}
compilations["test"].defaultSourceSet {
dependencies {
implementation(kotlin("test-junit"))
}
}
mavenPublication {
artifactId = project.name + "-jvm"
}
}
js {
nodejs {
testTask {
useMocha {
timeout = mochaTimeout
}
}
}
// browser()
tasks.withType<KotlinJsCompile> {
kotlinOptions {
moduleKind = "umd"
//noStdlib = true
metaInfo = true
sourceMap = true
sourceMapEmbedSources = "always"
}
}
compilations["main"].defaultSourceSet {
dependencies {
implementation(kotlin("stdlib-js"))
}
}
compilations["test"].defaultSourceSet {
dependencies {
implementation(kotlin("test-js"))
}
}
mavenPublication {
artifactId = project.name + "-js"
}
}
}
}
tasks.withType<KotlinJvmCompile> {
kotlinOptions {
kotlinOptions {
jvmTarget = "1.$javaVersion"
freeCompilerArgs = ktFreeCompilerArgsJvm.split(';').toList()
}
}
}
configureDokka("jvm", "js")
configureMavenPublications("packDokka")
configureUploadToMavenCentral()
configureUploadToBintray("kotlinMultiplatform", "js", "jvm", "metadata")
configureSigning()
configureJsPackage()
configureUploadToGithub({ "jvm" in it || "shadow" in it })
}
jvmSubprojects.forEachProject {
apply(plugin = "maven-publish")
apply(plugin = "java-library")
apply(plugin = "org.jetbrains.kotlin.jvm")
apply(plugin = "signing")
apply(plugin = "org.jetbrains.dokka")
apply(plugin = "com.jfrog.bintray")
configureDokka()
createMavenPublications("jvm", "java", docArtifact = "packDokka")
configureUploadToMavenCentral()
configureUploadToBintray()
configureSigning()
configureUploadToGithub()
}
jsSubprojects.forEachProject {
apply(plugin = "maven-publish")
apply(plugin = "org.jetbrains.kotlin.js")
apply(plugin = "signing")
apply(plugin = "org.jetbrains.dokka")
apply(plugin = "com.jfrog.bintray")
configureDokka()
createMavenPublications("js", "kotlin", docArtifact = "packDokka")
configureUploadToMavenCentral()
configureUploadToBintray()
configureSigning()
}
configure<GithubReleaseExtension> {
if (githubToken != null) {
token(githubToken)
owner(githubOwner)
repo(githubRepo)
tagName { version.toString() }
releaseName { version.toString() }
allowUploadToExisting { true }
prerelease { false }
draft { false }
// overwrite { false }
try {
body(
"""|## CHANGELOG
|${changelog().call()}
""".trimMargin()
)
} catch (e: Throwable) {
e.message?.let { warn(it) }
}
}
}
fun Project.configureUploadToGithub(
jarTaskPositiveFilter: (String) -> Boolean = { "jar" in it },
jarTaskNegativeFilter: (String) -> Boolean = { "dokka" in it || "source" in it }
) {
if (githubToken == null) return
val jarTasks = tasks.withType(Jar::class).asSequence()
.filter { jarTaskPositiveFilter(it.name.toLowerCase()) }
.filter { !jarTaskNegativeFilter(it.name.toLowerCase()) }
.map { it.archiveFile }
.toList()
rootProject.configure<GithubReleaseExtension> {
releaseAssets(*(releaseAssets.toList() + jarTasks).toTypedArray())
}
rootProject.tasks.withType(GithubReleaseTask::class) {
dependsOn(*jarTasks.toTypedArray())
}
}
fun Project.configureDokka(vararg platforms: String) {
tasks.withType<DokkaTask> {
outputDirectory = docDir
outputFormat = "html"
if (platforms.isNotEmpty()) {
multiplatform {
platforms.forEach { registerPlatform(it) }
}
}
}
val packAllDokka: DefaultTask by tasks.creating(DefaultTask::class.java) {
group = "documentation"
}
if (platforms.isNotEmpty()) {
val jarPlatform = tasks.withType<Jar>().map { it.name.replace("Jar", "") }
jarPlatform.forEach {
val packDokkaForPlatform = "packDokka${it.capitalize()}"
task<Jar>(packDokkaForPlatform) {
group = "documentation"
dependsOn("dokka")
from(docDir)
archiveBaseName.set(project.name)
archiveVersion.set(project.version.toString())
archiveAppendix.set(it)
archiveClassifier.set("javadoc")
}
packAllDokka.dependsOn(packDokkaForPlatform)
}
} else {
val packDokka by tasks.creating(Jar::class) {
group = "documentation"
dependsOn("dokka")
from(docDir)
archiveBaseName.set(project.name)
archiveVersion.set(project.version.toString())
archiveClassifier.set("javadoc")
}
packAllDokka.dependsOn(packDokka)
}
}
fun Project.configureSigning() {
signing {
useInMemoryPgpKeys(signingKey, signingPassword)
sign(publishing.publications)
}
publishing {
val pubs = publications.withType<MavenPublication>().map { "sign${it.name.capitalize()}Publication" }
task<Sign>("signAllPublications") {
dependsOn(*pubs.toTypedArray())
}
}
}
fun Project.configureUploadToBintray(vararg publicationNames: String) {
publishing {
bintray {
user = bintrayUser
key = bintrayKey
if (publicationNames.isEmpty()) {
setPublications(*project.publishing.publications.withType<MavenPublication>().map { it.name }
.toTypedArray())
} else {
setPublications(*publicationNames)
}
override = true
with(pkg) {
repo = bintrayRepo
name = project.name
userOrg = bintrayUserOrg
vcsUrl = projectHomepage
setLicenses(projectLicense)
with(version) {
name = project.version.toString()
}
}
}
}
this.tasks.withType<BintrayUploadTask> {
publishAllToBintrayTask.dependsOn(this)
}
}
fun Project.configureUploadToMavenCentral() {
val sonatypeUrl: String by this
if (ossrhUsername != null && ossrhPassword != null) {
publishing {
repositories {
maven(sonatypeUrl) {
credentials {
username = ossrhUsername
password = ossrhPassword
}
}
}
}
}
}
fun Project.configureMavenPublications(docArtifactBaseName: String) {
publishing {
publications.withType<MavenPublication> {
groupId = project.group.toString()
version = project.version.toString()
val docArtifact = "${docArtifactBaseName}${name.capitalize()}"
if (docArtifact in tasks.names) {
artifact(tasks.getByName(docArtifact)) {
classifier = "javadoc"
}
} else if (!docArtifact.endsWith("KotlinMultiplatform")) {
log(
"no javadoc artifact for publication $name in project ${project.name}: " +
"no such a task: $docArtifact"
)
}
configurePom(project.name)
}
}
}
fun Project.createMavenPublications(name: String, vararg componentsStrings: String, docArtifact: String? = null) {
val sourcesJar by tasks.creating(Jar::class) {
archiveBaseName.set(project.name)
archiveVersion.set(project.version.toString())
archiveClassifier.set("sources")
}
publishing {
publications.create<MavenPublication>(name) {
groupId = project.group.toString()
version = project.version.toString()
for (component in componentsStrings) {
if (component in components.names)
from(components[component])
}
if (docArtifact != null && docArtifact in tasks.names) {
artifact(tasks.getByName(docArtifact)) {
classifier = "javadoc"
}
} else if (docArtifact == null || !docArtifact.endsWith("KotlinMultiplatform")) {
log(
"no javadoc artifact for publication $name in project ${project.name}: " +
"no such a task: $docArtifact"
)
}
artifact(sourcesJar)
configurePom(project.name)
}
}
}
fun Set<String>.forEachProject(f: Project.() -> Unit) = allprojects.filter { it.name in this }.forEach(f)
fun NamedDomainObjectContainerScope<GradlePassConfigurationImpl>.registerPlatform(
platform: String, configuration: Action<in GradlePassConfigurationImpl>
) {
val low = platform.toLowerCase()
val up = platform.toUpperCase()
register(low) {
targets = listOf(up)
this@register.platform = low
includeNonPublic = false
reportUndocumented = false
collectInheritedExtensionsFromLibraries = true
skipEmptyPackages = true
noStdlibLink = true
noJdkLink = true
configuration(this@register)
}
}
fun NamedDomainObjectContainerScope<GradlePassConfigurationImpl>.registerPlatform(platform: String) =
registerPlatform(platform) { }
fun Project.configureJsPackage(packageJsonTask: String = "jsPackageJson", compileTask: String = "jsMainClasses") {
if (this == rootProject) return
apply<NpmPublishPlugin>()
configure<NpmPublishExtension> {
nodeRoot = rootProject.tasks.withType<NodeJsSetupTask>().asSequence().map { it.destination }.first()
token = npmToken ?: ""
packageJson = tasks.getByName<KotlinPackageJsonTask>(packageJsonTask).packageJson
nodeSetupTask = rootProject.tasks.getByName("kotlinNodeJsSetup").path
jsCompileTask = compileTask
jsSourcesDir = tasks.withType<Kotlin2JsCompile>().asSequence()
.filter { "Test" !in it.name }
.map { it.outputFile.parentFile }
.first()
liftPackageJson {
people = mutableListOf(People(gcName, gcEmail, gcUrl))
homepage = projectHomepage
bugs = Bugs(projectIssues, gcEmail)
license = projectLicense
name = "@tuprolog/$name"
dependencies = dependencies?.mapKeys {
if ("2p" in it.key) "@tuprolog/${it.key}" else it.key
}?.mapValues {
if ("2p" in it.key) it.value.substringBefore('+') else it.value
}?.toMutableMap()
version = version?.substringBefore('+')
}
liftJsSources { _, _, line ->
line.replace("'2p", "'@tuprolog/2p")
.replace("\"2p", "\"@tuprolog/2p")
}
}
}