-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathbuild.gradle.kts
More file actions
557 lines (491 loc) · 19.3 KB
/
build.gradle.kts
File metadata and controls
557 lines (491 loc) · 19.3 KB
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/*
* Copyright (c) 2026. JetBrains s.r.o.
* Use of this source code is governed by the MIT license that can be found in the LICENSE file.
*/
// okhttp3 added for publishing to the Sonatype Central Repository:
@file:OptIn(ExperimentalWasmDsl::class)
import okhttp3.MultipartBody
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.asRequestBody
import org.gradle.internal.os.OperatingSystem
import org.gradle.jvm.tasks.Jar
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.dsl.KotlinCommonCompilerOptions
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile
import java.io.FileNotFoundException
import java.util.*
buildscript {
dependencies {
classpath("com.squareup.okhttp3:okhttp:4.12.0")
}
}
plugins {
kotlin("multiplatform") apply false
kotlin("js") apply false
}
fun ExtraPropertiesExtension.getOrNull(name: String): Any? = if (has(name)) {
get(name)
} else {
null
}
val os: OperatingSystem = OperatingSystem.current()
val letsPlotTaskGroup by extra { "lets-plot" }
allprojects {
group = "org.jetbrains.lets-plot"
version = "4.9.1-SNAPSHOT" // see also: python-package/lets_plot/_version.py
// version = "0.0.0-SNAPSHOT" // for local publishing only
// Generate JVM 1.8 bytecode
tasks.withType<KotlinJvmCompile>().configureEach {
compilerOptions.jvmTarget.set(JvmTarget.JVM_1_8)
}
tasks.withType<JavaCompile>().configureEach {
sourceCompatibility = "1.8"
targetCompatibility = "1.8"
}
repositories {
mavenCentral()
}
}
// Read build settings from commandline parameters (for build_release.py script):
fun readPropertiesFromParameters() {
val properties = Properties()
if (project.hasProperty("enable_python_package")) {
properties["enable_python_package"] = project.property("enable_python_package")
}
if (properties.getProperty("enable_python_package").toBoolean()) {
properties["python.bin_path"] = project.property("python.bin_path")
properties["python.include_path"] = project.property("python.include_path")
}
if (!os.isWindows) {
properties["architecture"] = project.property("architecture")
}
for (property in properties) {
extra[property.key as String] = property.value
}
}
// Read build settings from local.properties:
fun readPropertiesFromFile() {
val properties = Properties()
val localPropsFileName = "local.properties"
if (project.file(localPropsFileName).exists()) {
properties.load(project.file(localPropsFileName).inputStream())
} else {
throw FileNotFoundException(
"$localPropsFileName file not found!\n" +
"Check ${localPropsFileName}_template file for the template."
)
}
if (!os.isWindows) {
// Only 64bit version can be built for Windows, so the arch parameter is not needed and may not be set.
assert(properties["architecture"] != null)
}
if (properties.getProperty("enable_python_package").toBoolean()) {
val pythonBinPath = properties["python.bin_path"]
val pythonIncludePath = properties["python.include_path"]
assert(pythonBinPath != null)
assert(pythonIncludePath != null)
if (!os.isWindows) {
val execResult = providers.exec {
commandLine(
"${pythonBinPath}/python",
"-c",
"import platform; print(platform.machine())"
)
}
val getArchOutput = execResult.standardOutput.asText.get()
val currentPythonArch = getArchOutput.toString().trim()
if (currentPythonArch != properties["architecture"]) {
throw IllegalArgumentException(
"Project and Python architectures don't match!\n" +
" - Value, from your '${localPropsFileName}' file: ${properties["architecture"]}\n" +
" - Your Python architecture: ${currentPythonArch}\n" +
"Check your '${localPropsFileName}' file."
)
}
}
}
for (property in properties) {
extra[property.key as String] = property.value
}
}
// For build_release.py settings will be read from commandline parameters.
// In other cases, settings will be read from local.properties.
if (project.hasProperty("build_release")) {
readPropertiesFromParameters()
} else {
readPropertiesFromFile()
}
// Maven publication settings:
// define local Maven Repository path:
val localMavenRepository by extra { "$rootDir/.maven-publish-dev-repo" }
// define the Maven Repository URL. Currently set to a local path for uploading
// artifacts to the Sonatype Central Repository.
val mavenReleasePublishUrl by extra { layout.buildDirectory.dir("maven/artifacts").get().toString() }
// define Maven Snapshot repository URL.
val mavenSnapshotPublishUrl by extra { "https://central.sonatype.com/repository/maven-snapshots/" }
// define Sonatype Central Repository settings:
val sonatypeUsername by extra { extra.getOrNull("sonatype.username") ?: "" }
val sonatypePassword by extra { extra.getOrNull("sonatype.password") ?: "" }
// Publish some sub-projects as Kotlin Multi-project libraries.
val publishLetsPlotCoreModulesToMavenLocalRepository by tasks.registering {
group = letsPlotTaskGroup
}
val publishLetsPlotCoreModulesToMavenRepository by tasks.registering {
group = letsPlotTaskGroup
}
// Configure a workaround tasks for publishing to the Sonatype Central Repository,
// as there is currently no official Gradle plugin support.
// Refer to documentation: https://central.sonatype.org/publish/publish-portal-gradle/
val packageMavenArtifacts by tasks.registering(Zip::class) {
group = letsPlotTaskGroup
from(mavenReleasePublishUrl)
archiveFileName.set("${project.name}-artifacts.zip")
destinationDirectory.set(layout.buildDirectory)
}
val uploadMavenArtifacts by tasks.registering {
group = letsPlotTaskGroup
dependsOn(packageMavenArtifacts)
doLast {
val uriBase = "https://central.sonatype.com/api/v1/publisher/upload"
val publishingType = "USER_MANAGED"
val deploymentName = "${project.name}-$version"
val uri = "$uriBase?name=$deploymentName&publishingType=$publishingType"
val userName = sonatypeUsername as String
val password = sonatypePassword as String
val base64Auth = Base64.getEncoder().encode("$userName:$password".toByteArray()).toString(Charsets.UTF_8)
val bundleFile = packageMavenArtifacts.get().archiveFile.get().asFile
println("Sending request to $uri...")
val client = OkHttpClient()
val request = Request.Builder()
.url(uri)
.header("Authorization", "Bearer $base64Auth")
.post(
MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("bundle", bundleFile.name, bundleFile.asRequestBody())
.build()
)
.build()
client.newCall(request).execute().use { response ->
val statusCode = response.code
println("Upload status code: $statusCode")
println("Upload result: ${response.body!!.string()}")
if (statusCode != 201) {
error("Upload error to Central repository. Status code $statusCode.")
}
}
}
}
val defaultImageMagickLibPath = rootDir.path + "/platf-imagick/deps"
val imageMagickLibPath = project.findProperty("imagemagick_lib_path") as? String
?: System.getenv("IMAGICK_LIB_PATH")
?: defaultImageMagickLibPath
extra.set("imagemagick_lib_path", imageMagickLibPath)
val initImageMagick by tasks.registering {
group = letsPlotTaskGroup
doLast {
exec {
this.workingDir = File(rootDir.path + "/platf-imagick")
commandLine(
"./init_imagemagick.sh",
imageMagickLibPath
)
}
}
}
// Generating JavaDoc task for each publication task.
// Fixes "Task ':canvas:publishJsPublicationToMavenRepository' uses this output of task ':canvas:signJvmPublication'
// without declaring an explicit or implicit dependency" error.
// Issues:
// - https://github.com/gradle-nexus/publish-plugin/issues/208
// - https://github.com/gradle/gradle/issues/26091
//
fun getJarJavaDocsTask(distributeName: String): TaskProvider<Jar> {
return tasks.register<Jar>("${distributeName}JarJavaDoc") {
archiveClassifier.set("javadoc")
from("$rootDir/README.md")
archiveBaseName.set(distributeName)
}
}
// Configure native targets for python-extension dependencies.
subprojects {
val pythonExtensionModules = listOf(
"commons",
"canvas",
"datamodel",
"plot-base",
"plot-builder",
"plot-stem",
"plot-raster",
"demo-and-test-shared",
"demo-common-svg",
"demo-svg-native",
)
val projectArchitecture = rootProject.extra.getOrNull("architecture")
if (name in pythonExtensionModules) {
apply(plugin = "org.jetbrains.kotlin.multiplatform")
configure<KotlinMultiplatformExtension> {
if (os.isMacOsX && projectArchitecture == "x86_64") {
macosX64()
} else if (os.isMacOsX && projectArchitecture == "arm64") {
if (project.hasProperty("build_release")) {
macosX64()
macosArm64()
} else {
macosArm64()
}
} else if (os.isLinux) {
if (project.hasProperty("build_release")) {
linuxX64()
linuxArm64()
} else if (projectArchitecture == "x86_64") {
linuxX64()
}
} else if (os.isWindows) {
mingwX64()
} else {
throw Exception("Unsupported platform! Check project settings.")
}
}
}
}
// Configure Lets-Plot Core multiplatform modules.
val multiPlatformCoreModulesForPublish = listOf(
"commons",
"datamodel",
"canvas",
"gis",
"livemap",
"plot-base",
"plot-builder",
"plot-raster",
"plot-stem",
"plot-livemap",
"visual-testing",
"platf-w3c",
"wasmjs-package"
)
subprojects {
if (name in multiPlatformCoreModulesForPublish) {
apply(plugin = "org.jetbrains.kotlin.multiplatform")
// For `jvmSourcesJar` task:
configure<KotlinMultiplatformExtension> {
jvm()
}
}
}
// Configure Lets-Plot Core JVM modules.
val jvmCoreModulesForPublish = listOf(
"platf-awt",
"platf-batik",
)
subprojects {
if (name in jvmCoreModulesForPublish) {
apply(plugin = "org.jetbrains.kotlin.jvm")
apply(plugin = "maven-publish")
configure<KotlinJvmProjectExtension> {
tasks.register<Jar>("${name}-sources") {
archiveClassifier.set("sources")
from(sourceSets.getByName("main").kotlin.srcDirs)
}
}
configure<PublishingExtension> {
publications {
register(name, MavenPublication::class) {
groupId = project.group as String
artifactId = name
version = project.version as String
artifact(tasks["jar"])
artifact(tasks["${name}-sources"])
}
}
}
}
}
// Configure Maven publication for Lets-Plot Core modules.
subprojects {
if (name in multiPlatformCoreModulesForPublish + jvmCoreModulesForPublish) {
apply(plugin = "maven-publish")
apply(plugin = "signing")
// Do not publish 'native' targets:
val targetsToPublish = listOf(
"platf-awt",
"platf-batik",
"jvm",
"js",
"wasmJs",
"kotlinMultiplatform",
"metadata"
)
configure<PublishingExtension> {
publications {
withType(MavenPublication::class) {
if (name in targetsToPublish) {
// Configure this publication.
artifact(getJarJavaDocsTask("${name}-${project.name}"))
pom {
name = "Lets-Plot core artifact"
description = "A part of the Lets-Plot library."
url = "https://github.com/JetBrains/lets-plot"
licenses {
license {
name = "MIT"
url = "https://raw.githubusercontent.com/JetBrains/lets-plot/master/LICENSE"
}
}
developers {
developer {
id = "jetbrains"
name = "JetBrains"
email = "lets-plot@jetbrains.com"
}
}
scm {
url = "https://github.com/JetBrains/lets-plot"
}
}
}
}
}
repositories {
mavenLocal {
url = uri(localMavenRepository)
}
maven {
if (version.toString().endsWith("-SNAPSHOT")) {
url = uri(mavenSnapshotPublishUrl)
credentials {
username = sonatypeUsername.toString()
password = sonatypePassword.toString()
}
} else {
url = uri(mavenReleasePublishUrl)
}
}
}
}
afterEvaluate {
// Add LICENSE file to the META-INF folder inside published JAR files.
tasks.filterIsInstance<Jar>()
.forEach {
if (it.name == "jvmJar" || it.name == "jar") { // "jar" for 'org.jetbrains.kotlin.jvm' plugin
it.metaInf {
from("$rootDir") {
include("LICENSE")
}
}
}
}
// Configure artifacts signing process for release versions.
val publicationsToSign = mutableListOf<Publication>()
for (task in tasks.withType(PublishToMavenRepository::class)) {
if (task.publication.name in targetsToPublish) {
val repoName = task.repository.name
if (repoName == "MavenLocal") {
publishLetsPlotCoreModulesToMavenLocalRepository.configure {
dependsOn += task
}
} else if (repoName == "maven") {
publishLetsPlotCoreModulesToMavenRepository.configure {
dependsOn += task
}
publicationsToSign.add(task.publication)
} else {
throw IllegalStateException("Repository expected: 'MavenLocal' or 'maven' but was: '$repoName'.")
}
}
}
// Sign artifacts.
publicationsToSign.forEach {
if (!project.version.toString().contains("SNAPSHOT")) {
configure<SigningExtension> {
sign(it)
}
}
}
}
}
}
// Step 1: force version and clean stale artifacts. Kept separate so publish tasks
// can use mustRunAfter on THIS task without creating a cycle with the aggregate below.
val cleanSnapshotFromMavenLocal by tasks.registering {
group = letsPlotTaskGroup
description = "Forces version 0.0.0-SNAPSHOT and removes its artifacts from ~/.m2."
doFirst {
// MavenPublication.version defaults to a lazy project.version provider,
// so changing it here (before publish tasks execute) is picked up in the POM.
allprojects { version = "0.0.0-SNAPSHOT" }
val snapshotDir = File("${System.getProperty("user.home")}/.m2/repository/org/jetbrains/lets-plot")
if (snapshotDir.exists()) {
snapshotDir.walkTopDown()
.filter { it.isDirectory && it.name == "0.0.0-SNAPSHOT" }
.forEach { dir ->
println("Deleting: ${dir.absolutePath}")
dir.deleteRecursively()
}
}
}
}
// Step 2: aggregate task — cleans first, then publishes all subprojects to ~/.m2.
val cleanAndPublishSnapshotToMavenLocal by tasks.registering {
group = letsPlotTaskGroup
description = "Deletes 0.0.0-SNAPSHOT lets-plot artifacts from ~/.m2, then builds and publishes fresh ones."
dependsOn(cleanSnapshotFromMavenLocal)
val cleanTask = cleanSnapshotFromMavenLocal
val rootTask = this
subprojects {
tasks.matching { it.name == "publishToMavenLocal" }.configureEach {
rootTask.dependsOn(this)
// mustRunAfter the CLEAN task (not rootTask) — avoids the circular dependency
mustRunAfter(cleanTask)
}
}
}
// Fix warnings in all projects.
subprojects {
fun KotlinCommonCompilerOptions.configCompilerWarnings() {
freeCompilerArgs.addAll(
// Suppress expect/actual classes are in Beta warning.
"-Xexpect-actual-classes",
// Non-public primary constructor is exposed via the generated 'copy()' method of the 'data' class.
// Kotlin 2.0 feature.
//"-Xconsistent-data-class-copy-visibility",
// Enable all warnings as errors.
// Disabled because even with the Suppress("unused") the warnings may still happen:
// (https://github.com/JetBrains/lets-plot/blob/f5af69befdd2fa963672d3b1d9992f3635f64840/plot-base/src/commonMain/kotlin/org/jetbrains/letsPlot/core/interact/mouse/MouseDragInteraction.kt#L70)
//"-Werror"
)
}
plugins.withId("org.jetbrains.kotlin.multiplatform") {
extensions.configure<KotlinMultiplatformExtension> {
targets.configureEach {
compilations.configureEach {
compileTaskProvider.get().compilerOptions {
configCompilerWarnings()
}
}
}
}
}
// Koltin 2.0
//plugins.withId("org.jetbrains.kotlin.jvm") {
// extensions.configure<KotlinJvmExtension> {
// compilerOptions {
// configCompilerWarnings()
// jvmTarget.set(JvmTarget.JVM_1_8)
// }
// }
//}
plugins.withId("org.jetbrains.kotlin.jvm") {
extensions.configure<KotlinJvmProjectExtension> {
compilerOptions {
configCompilerWarnings()
jvmTarget.set(JvmTarget.JVM_1_8)
}
}
}
}