-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle.kts
274 lines (231 loc) · 8.33 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
import org.jetbrains.dokka.gradle.DokkaTask
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJvmCompilation
import java.io.ByteArrayOutputStream
import java.nio.file.Files
val kmm: String by properties
val keccak: String by properties
val random: String by properties
plugins {
kotlin("multiplatform") //Kotlin Multiplatform
id("org.jetbrains.dokka") //KDocs
id("maven-publish")
id("signing") //GPG
}
group = "asia.hombre"
version = "1.0.0"
description = "ML-KEM (NIST FIPS 203) optimized implementation on 100% Kotlin."
val projectName = "kyber"
val baseProjectName = projectName.plus("-").plus(project.version)
val isAutomated = false
val mavenDir = projectDir.resolve("maven")
val mavenBundlingDir = mavenDir.resolve("bundling")
val mavenDeep = "$mavenBundlingDir/" + (project.group.toString().replace(".", "/")) + "/" + version
val npmDir = "./npm"
val npmKotlinDir = "$npmDir/kotlin"
val jarFileName = baseProjectName.plus(".jar")
val jarFullFileName = baseProjectName.plus("-full.jar")
val javadocsFileName = baseProjectName.plus("-javadoc.jar")
val sourcesFileName = baseProjectName.plus("-sources.jar")
val mavenBundleFileName = baseProjectName.plus("-bundle.zip")
repositories {
mavenCentral()
mavenLocal()
}
kotlin {
jvm {
val main by compilations.getting {
compileTaskProvider.configure {
//Set up the Kotlin compiler options for the 'main' compilation:
compilerOptions.jvmTarget.set(JvmTarget.JVM_1_8)
}
compileTaskProvider //Get the Kotlin task 'compileKotlinJvm'
output //Get the main compilation output
}
val jvmJar by tasks.getting(org.gradle.jvm.tasks.Jar::class) {
archiveFileName.set(jarFileName)
val jvmMainCompilation = kotlin.targets.getByName("jvm").compilations.getByName("main") as KotlinJvmCompilation
from(jvmMainCompilation.output.allOutputs)
}
compilations["test"].runtimeDependencyFiles // get the test runtime classpath
}
js(IR) {
nodejs()
browser {
}
binaries.executable()
}
sourceSets {
val commonMain by getting {
dependencies {
implementation("org.kotlincrypto:secure-random:$random")
implementation("asia.hombre:keccak:$keccak")
}
}
val commonTest by getting {
dependencies {
implementation("org.jetbrains.kotlin:kotlin-test")
}
}
val jvmMain by getting {
dependencies {
}
}
val jsMain by getting {
dependencies {
}
}
}
}
signing {
if (project.hasProperty("signing.gnupg.keyName")) {
useGpgCmd()
sign(publishing.publications)
}
}
publishing {
repositories {
maven {
url = mavenDir.toURI()
}
}
publications {
//Dynamically rename all artifacts
this.forEach {
val mavenPublication = it as MavenPublication
mavenPublication.artifactId = projectName +
if(mavenPublication.artifactId.contains("-"))
"-" + mavenPublication.artifactId.split("-").last()
else
""
}
}
publications.withType<MavenPublication> {
// Stub javadoc.jar artifact
artifact(tasks.register("${name}JavadocJar", Jar::class) {
archiveClassifier.set("javadoc")
archiveAppendix.set(this@withType.name)
})
// Provide artifacts information required by Maven Central
pom {
name.set("Kyber Kotlin Multiplatform Library")
description.set(project.description)
url.set("https://github.com/ronhombre/KyberKotlin")
licenses {
license {
name.set("The Apache Software License, Version 2.0")
url.set("https://www.apache.org/licenses/LICENSE-2.0.txt")
}
}
developers {
developer {
name.set("Ron Lauren Hombre")
email.set("ronlauren@hombre.asia")
}
}
scm {
url.set("https://github.com/ronhombre/KyberKotlin")
}
}
}
}
fun parseArtifactId(artifactId: String): String {
val list = artifactId.splitToSequence("-").map { it.replaceFirstChar(Char::uppercase) }
return list.joinToString("")
}
fun parseArtifactArchiveName(artifact: MavenPublication): String {
return artifact.artifactId + "-" + artifact.version + "-bundle.zip"
}
for (publication in publishing.publications.asMap) {
val artifact = publication.value as MavenPublication
val parsedArtifactId = parseArtifactId(artifact.artifactId)
val bundleFileName = parseArtifactArchiveName(artifact)
tasks.register<Zip>("bundle$parsedArtifactId") {
group = "Bundle"
from(mavenDir)
val mavenDeepDir = artifact.groupId.replace(".", "/") + "/" + artifact.artifactId
include("$mavenDeepDir/*/*")
destinationDirectory = mavenDir
archiveFileName = parseArtifactArchiveName(artifact)
}
tasks.register<Exec>("publish" + parsedArtifactId + "ToMavenCentral") {
mustRunAfter("bundle$parsedArtifactId")
group = "Publish"
/*if(!mavenDir.resolve(bundleFileName).exists())
throw RuntimeException("Bundle does not exist! Please run `bundle$parsedArtifactId`")*/
commandLine(
"curl", "-X", "POST",
"https://central.sonatype.com/api/v1/publisher/upload?name=${artifact.artifactId}&publishingType=" + if(isAutomated) "AUTOMATED" else "USER_MANAGED",
"-H", "accept: text/plain",
"-H", "Content-Type: multipart/form-data",
"-H", "Authorization: Bearer " + System.getenv("SONATYPE_TOKEN"),
"-F", "bundle=@$bundleFileName;type=application/x-zip-compressed"
)
workingDir(mavenDir.toString())
standardOutput = ByteArrayOutputStream()
errorOutput = ByteArrayOutputStream()
// Execute some action with the output
doLast {
println("$standardOutput")
println("$errorOutput")
}
}
}
tasks.register("bundleAll") {
group = "Bundle"
dependsOn("publish")
for (publication in publishing.publications.asMap) {
val artifact = publication.value as MavenPublication
dependsOn("bundle" + parseArtifactId(artifact.artifactId))
}
}
tasks.register("publishAllToMavenCentral") {
group = "Publish"
dependsOn("bundleAll")
for (publication in publishing.publications.asMap) {
val artifact = publication.value as MavenPublication
dependsOn("publish" + parseArtifactId(artifact.artifactId) + "ToMavenCentral")
}
}
tasks.register("packageNPM") {
val packageSourcePath = projectDir.toPath().resolve("npm.json")
val packagePath = projectDir.toPath().resolve("npm").resolve("package.json")
var packageFile = String(Files.readAllBytes(packageSourcePath))
packageFile = packageFile.replace("<VERSION>", version.toString()).replace("<DESCRIPTION>", project.description.toString())
Files.write(packagePath, packageFile.toByteArray())
}
tasks.register<Copy>("bundleNPM") {
dependsOn("jsBrowserProductionWebpack", "packageNPM")
from(buildDir.resolve("js").resolve("packages").resolve(project.name).resolve("kotlin"))
into(npmKotlinDir)
doFirst {
delete(npmKotlinDir)
mkdir(npmKotlinDir)
}
}
tasks.dokkaHtml.configure {
dokkaSourceSets {
named("commonMain") {
// Adjust visibility to include internal and private members
perPackageOption {
matchingRegex.set(".*") // Match all packages
includeNonPublic.set(false)
}
// Optionally, report undocumented members
reportUndocumented.set(true)
}
}
}
tasks.withType<DokkaTask>().configureEach {
val dokkaBaseConfiguration = """
{
"footerMessage": "(C) 2024 Ron Lauren Hombre"
}
"""
pluginsMapConfiguration.set(
mapOf(
// fully qualified plugin name to json configuration
"org.jetbrains.dokka.base.DokkaBase" to dokkaBaseConfiguration
)
)
}