-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.gradle
401 lines (366 loc) · 14.8 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
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
// Load plugin dependencies and initialize build variables.
buildscript {
ext {
// Version used for submodule artifacts.
// Snapshot publishing changes (or adds) the suffix after '-' with 'SNAPSHOT' prior to publishing.
globalVersion = '1.2.1'
clientsVersion = '1.2.1-alpha.1' // The clients subsystem is still expected to change drastically.
versions = [
// Kotlin multiplatform versions.
kotlin:'1.9.23',
serialization:'1.6.3',
coroutines:'1.8.0',
datetime:'0.5.0',
// JVM versions.
jvmTarget:'1.8',
dokkaPlugin:'1.9.20',
reflections:'0.10.2',
// JS versions.
nodePlugin:'7.0.2',
bigJs:'6.2.1',
// DevOps versions.
detektPlugin:'1.23.5',
detektVerifyImplementation:'1.2.5',
nexusPublishPlugin:'1.3.0',
apacheCommons:'2.15.1'
]
commonModule = subprojects.find { it.name == 'carp.common' }
coreModules = subprojects.findAll { it.name.endsWith( '.core' ) }
testModules = subprojects.findAll { it.name == 'carp.common.test' || it.name == 'carp.test' }
publishNpmModule = subprojects.find { it.name == 'publish-npm-packages' }
allModules = coreModules + testModules + commonModule + publishNpmModule
devOpsModules =
subprojects.findAll {it.name == 'carp.detekt' || it.name == 'rpc' } + publishNpmModule
}
dependencies {
// Kotlin plugins.
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${versions.kotlin}"
classpath "org.jetbrains.kotlin:kotlin-serialization:${versions.kotlin}"
// JS plugins.
classpath "com.github.node-gradle:gradle-node-plugin:${versions.nodePlugin}"
// JVM plugins.
classpath "org.jetbrains.dokka:dokka-gradle-plugin:${versions.dokkaPlugin}"
// DevOps plugins.
classpath "io.gitlab.arturbosch.detekt:detekt-gradle-plugin:${versions.detektPlugin}"
classpath "io.github.gradle-nexus:publish-plugin:${versions.nexusPublishPlugin}"
}
repositories {
mavenCentral()
gradlePluginPortal()
}
}
// Load dependent properties.
def publishProperties = new Properties()
def publishPropertiesFile = rootProject.file('publish.properties')
if (publishPropertiesFile.exists())
{
publishProperties.load(new FileInputStream(publishPropertiesFile))
}
// Configure all subprojects as testable, publishable, Kotlin multiplatform projects.
// A `kotlinx.serialization` dependency is added to serialize domain models.
// A `kotlinx-datetime` dependency is added to be able to store dates in domain models.
configure( subprojects - devOpsModules ) {
version = globalVersion
// Specify platforms and test frameworks to use.
apply plugin: 'kotlin-multiplatform'
apply plugin: 'kotlinx-serialization'
kotlin {
jvm {
compilations.main.kotlinOptions.jvmTarget = versions.jvmTarget
compilations.test.kotlinOptions.jvmTarget = versions.jvmTarget
testRuns["test"].executionTask.configure {
useJUnitPlatform()
}
}
js(IR) {
moduleName = project.name.replaceAll("\\.", "-") + "-generated"
binaries.executable() // Export JS/TypeScript files.
browser()
generateTypeScriptDefinitions()
}
targets.configureEach {
compilations.configureEach {
def isTestSourceSet = it.name == 'test'
compilerOptions.configure((Action) {
// Treat compilation warning as errors for all compilation targets.
it.allWarningsAsErrors = true
// We do not mind being early adopters of Jetbrains APIs likely to change in the future.
it.optIn.add('kotlin.RequiresOptIn')
it.optIn.add('kotlin.time.ExperimentalTime')
it.optIn.add('kotlin.js.ExperimentalJsExport')
if (isTestSourceSet)
{
it.optIn.add('kotlinx.coroutines.ExperimentalCoroutinesApi')
}
it.freeCompilerArgs.add('-Xexpect-actual-classes') // https://youtrack.jetbrains.com/issue/KT-61573
} )
}
}
sourceSets {
commonMain {
dependencies {
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:${versions.serialization}"
api "org.jetbrains.kotlinx:kotlinx-datetime:${versions.datetime}"
}
}
commonTest {
dependencies {
implementation kotlin('test')
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:${versions.coroutines}"
}
}
jvmTest {
dependencies {
implementation kotlin('reflect')
implementation "org.reflections:reflections:${versions.reflections}"
}
}
}
}
// Publish configuration.
// For signing and publishing to work, a 'publish.properties' file needs to be added to the root containing:
// The OpenPGP credentials to sign all artifacts:
// > signing.keyFile=<ABSOLUTE PATH TO THE ASCII-ARMORED KEY FILE>
// > signing.password=<SECRET>
// A username and password to upload artifacts to the Sonatype repository:
// > repository.username=<SONATYPE USERNAME>
// > repository.password=<SONATYPE PASSWORD>
apply plugin: 'maven-publish'
apply plugin: 'signing'
apply plugin: 'org.jetbrains.dokka'
task dokkaJvmJavadoc(type: org.jetbrains.dokka.gradle.DokkaTask) {
dokkaSourceSets {
register("jvm") {
platform.set(org.jetbrains.dokka.Platform.jvm)
sourceRoots.from(kotlin.sourceSets.getByName("jvmMain").kotlin.srcDirs)
}
}
}
task javadocJar(type: Jar) {
group JavaBasePlugin.DOCUMENTATION_GROUP
description 'Create javadoc jar using Dokka'
archiveClassifier = "javadoc"
from dokkaJvmJavadoc
}
publishing {
publications {
all {
pom {
url = 'https://github.com/cph-cachet/carp.core-kotlin'
licenses {
license {
name = 'MIT License'
url = 'https://github.com/cph-cachet/carp.core-kotlin/blob/master/LICENSE.md'
}
}
developers {
developer {
id = 'whathecode'
name = 'Steven Jeuris'
email = 'steven.jeuris@gmail.com'
organization = 'CACHET'
organizationUrl = 'http://www.cachet.dk/'
}
}
scm {
connection = 'scm:git:git://github.com/cph-cachet/carp.core-kotlin.git'
developerConnection = 'scm:git:ssh://github.com:cph-cachet/carp.core-kotlin.git'
url = 'https://github.com/cph-cachet/carp.core-kotlin'
}
}
}
jvm {
artifact javadocJar
}
}
repositories {
maven {
name "local"
url "$buildDir/repository"
}
}
}
signing {
def signingKeyFile = publishProperties['signing.keyFile']
if (signingKeyFile != null) {
def signingKey = new File(signingKeyFile).text
def signingPassword = publishProperties['signing.password']
useInMemoryPgpKeys(signingKey, signingPassword)
sign publishing.publications
}
}
}
// Sonatype Nexus publication.
apply plugin: 'io.github.gradle-nexus.publish-plugin'
group = "dk.cachet.carp"
version = globalVersion
nexusPublishing {
repositories {
sonatype {
username = publishProperties['repository.username']
password = publishProperties['repository.password']
}
}
}
task setSnapshotVersion {
doFirst {
def versionSplit = globalVersion.split('-')
def snapshotVersion = "${versionSplit[0]}-SNAPSHOT"
version = snapshotVersion
(rootProject.subprojects - devOpsModules).each { project ->
project.version = snapshotVersion
}
}
}
// TypeScript ambient declaration verification.
def typescriptFolder = 'typescript-declarations'
def npmScope = "@cachet"
apply plugin: 'com.github.node-gradle.node'
task setupTsProject(type: NpmTask) {
workingDir = file(typescriptFolder)
args = ['install']
}
task copyTestJsSources(type: Copy, dependsOn: setupTsProject) {
// Compile production sources for CARP, and the JS publication project (`publishNpmModule`).
allModules.each {
def project = it.name
dependsOn("$project:jsProductionExecutableCompileSync")
}
// Copy compiled JS and TypeScript sources to test project's node_modules.
from("$rootDir/build/js/packages/publish-npm-packages-generated") {
include "**/*.js"
}
from("$rootDir/build/js/packages") {
// Use individually generated TypeScript declarations to exclude publish-npm-packages exports.
include "**/*.d.ts"
includeEmptyDirs = false
}
eachFile { file ->
// Compiled sources have the name of the module they represent, followed by ".js" and ".d.ts".
// To be recognized by node, place them as "index.js" and "index.d.ts" in "node_modules/<scope>/<module-name>".
def fileMatch = file.name =~ /(.+)\.(js|d\.ts)/
def moduleName = fileMatch[0][1]
def extension = fileMatch[0][2]
file.relativePath = new RelativePath(true, moduleName, "index.$extension")
// Non-exported types show up as `any/* some.unknown.Type */` in generated TypeScript sources.
// Types for which a facade has been manually added can be replaced with the actual type (instead of `any`).
def knownFacadeTypes = []
def knownFacadeTypesFile = new File("$rootDir/publish-npm-packages/src/known-facade-types")
knownFacadeTypesFile.eachLine { type -> knownFacadeTypes << type }
// Modify sources to act like modules with exported named members.
file.filter { line ->
// Compiled sources refer to other modules as adjacent .js source files.
// Change these to the scoped modules created in the previous step.
def namedModules = line.replaceAll(~/'\.\/(.+?)\.js'/, "'$npmScope/\$1'")
// Replace `any` types with actual types for which facades are specified.
def replacedTypes = knownFacadeTypes.inject(namedModules) { curLine, type ->
def knownType = curLine.replaceAll(
~/any\/\* $type(<.+?>)? \*\//,
"$type\$1"
)
knownType.replaceAll(~/UnknownType \*/, "any")
}
// Add additional internal types to be exported, as configured in `forced-exports`.
def toExport = []
def forcedExportsFile = new File("$rootDir/publish-npm-packages/src/forced-exports/$moduleName")
if (forcedExportsFile.exists()) {
forcedExportsFile.eachLine { type -> toExport << type }
}
def toExportList = toExport.collect { "_.\\\$_\\\$.$it = $it\n " }
def additionalExports = replacedTypes.replaceAll(
~/return \_;/,
toExportList.join() + "return _;"
)
additionalExports
}
}
into "./$typescriptFolder/node_modules/$npmScope/"
}
task packageTestJsSources(type: Copy, dependsOn: copyTestJsSources) {
allModules.each {
def project = it.name
dependsOn("$project:jsPackageJson")
dependsOn("$project:jsTestPackageJson")
}
from("$rootDir/build/js/packages") {
include "**/package.json"
includeEmptyDirs = false
}
eachFile { file ->
def moduleName = file.getFile().getParentFile().name
file.filter { line ->
// Add scope to module name.
def changedName = line.replaceAll(~/("name": ).*/, "\$1 \"$npmScope/$moduleName\",")
// Point main source to 'index.js'.
changedName.replaceAll(~/("main": ).*/, "\$1 \"index.js\",")
}
}
into "./$typescriptFolder/node_modules/$npmScope/"
}
task compileTs(type: NpmTask, dependsOn: packageTestJsSources) {
workingDir = file(typescriptFolder)
args = ['run', 'tsc']
}
task verifyTsDeclarations(type: NodeTask, dependsOn: compileTs) {
script = file("${typescriptFolder}/node_modules/mocha/bin/mocha.js")
execOverrides {
it.workingDir = typescriptFolder
}
}
// Add `carp.test` helpers.
configure( coreModules + commonModule ) {
kotlin {
sourceSets {
commonTest {
dependencies {
implementation project(':carp.test')
}
}
}
}
}
// Add dependencies of all core modules on `carp.common`.
configure( coreModules ) {
kotlin {
sourceSets {
commonMain {
dependencies {
api project(':carp.common')
}
}
commonTest {
dependencies {
implementation project(':carp.common.test')
}
}
}
}
}
// Add code analysis.
configure( rootProject )
{
apply plugin: 'io.gitlab.arturbosch.detekt'
detekt {
dependencies {
detektPlugins "io.gitlab.arturbosch.detekt:detekt-formatting:${versions.detektPlugin}"
detektPlugins project(":carp.detekt") // Add custom project-specific rules.
detektPlugins "dk.cachet.detekt.extensions:detekt-verify-implementation:${versions.detektVerifyImplementation}"
}
}
task detektPasses(type: io.gitlab.arturbosch.detekt.Detekt) {
source = fileTree("$rootDir")
{
include('**/src/**')
exclude('**/node_modules/**', '**/resources/**')
}
config.from("$rootDir/detekt.yml")
buildUponDefaultConfig = true
ignoreFailures = false
def classPaths = project.configurations.getByName("detekt")
def multiplatformModules = coreModules + commonModule
multiplatformModules.each { classPaths += it.configurations.getByName("jvmCompileClasspath") }
classpath.setFrom(classPaths)
}
tasks.detekt.jvmTarget = "1.8"
tasks.detekt.dependsOn ":carp.detekt:assemble" // Ensure 'carp.detekt' is built prior to running code analysis.
}