Skip to content

Commit 78c798e

Browse files
committed
build: more automated, modular shared libs
1 parent ec1d5c0 commit 78c798e

File tree

10 files changed

+131
-68
lines changed

10 files changed

+131
-68
lines changed

.github/workflows/preview-build.yml

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Build a preview plugin
1+
name: Build preview shared lib plugins
22

33
on: [ push, pull_request ]
44

@@ -10,31 +10,55 @@ jobs:
1010
- name: Checkout master
1111
uses: actions/checkout@v4
1212

13+
- name: Set up Node.js
14+
uses: actions/setup-node@v4
15+
with:
16+
node-version: '20'
17+
18+
- name: Clone actions/upload-artifact repo
19+
run: git clone https://github.com/actions/upload-artifact.git
20+
1321
- name: Set up JDK 21
1422
uses: actions/setup-java@v4
1523
with:
1624
java-version: '21'
1725
distribution: 'zulu'
1826

19-
- name: Build with Gradle
27+
- name: Build and upload plugins dynamically
28+
env:
29+
INPUT_IF-NO-FILES-FOUND: 'error'
30+
INPUT_RETENTION-DAYS: '0'
31+
INPUT_COMPRESSION-LEVEL: '6'
32+
INPUT_OVERWRITE: 'false'
33+
INPUT_INCLUDE-HIDDEN-FILES: 'false'
2034
run: |
2135
chmod +x ./gradlew
22-
./gradlew :plugin:shadowJar
23-
./gradlew :plugin:compose-lib:shadowJar
36+
37+
mkdir -p plugin/build/libs
38+
for dir in plugin/*/; do
39+
if [ -d "$dir" ]; then
40+
dir_name=$(basename "$dir")
41+
if [ "$dir_name" == "build" ]; then
42+
continue
43+
fi
44+
45+
echo "Building $dir_name"
46+
./gradlew :plugin:"$dir_name":shadowJar
2447
25-
- name: Rename jar
26-
run: |
27-
mv "plugin/build/libs/plugin-all.jar" "plugin/build/libs/kotlinx-${{ github.sha }}.jar"
28-
mv "plugin/compose-lib/build/libs/compose-lib-all.jar" "plugin/build/libs/kotlinx-compose-${{ github.sha }}.jar"
48+
output_dir="plugin/$dir_name/build/libs"
49+
jar_file=$(ls $output_dir/*.jar | grep -E ".*-all\.jar$")
50+
51+
if [ -z "$jar_file" ]; then
52+
echo "No JAR file found in $output_dir for $dir_name"
53+
exit 1
54+
fi
2955
30-
- name: Upload artifact
31-
uses: actions/upload-artifact@v4
32-
with:
33-
name: kotlinx-shared-lib-plugin-${{ github.sha }}
34-
path: plugin/build/libs/kotlinx-${{ github.sha }}.jar
56+
base_name=$(basename "$jar_file" -all.jar)
57+
new_name="plugin/build/libs/kotlinx-${base_name}-${GITHUB_SHA}.jar"
58+
mv "$jar_file" "$new_name"
3559
36-
- name: Upload artifact (Compose)
37-
uses: actions/upload-artifact@v4
38-
with:
39-
name: kotlinx-compose-shared-lib-plugin-${{ github.sha }}
40-
path: plugin/build/libs/kotlinx-compose-${{ github.sha }}.jar
60+
echo "Uploading artifact $new_name"
61+
artifact_name="${base_name}-${GITHUB_SHA}"
62+
INPUT_NAME="$artifact_name" INPUT_PATH="$new_name" node upload-artifact/dist/upload/index.js
63+
fi
64+
done

build.gradle.kts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,24 @@ plugins {
77
alias(libs.plugins.gradleup.shadow) apply false
88
}
99

10+
version = libs.versions.allaymc.kotlinx.get()
11+
1012
subprojects {
1113
afterEvaluate {
1214

13-
kotlin.compilerOptions.freeCompilerArgs.addAll(
14-
"-Xcontext-receivers", // https://kotlinlang.org/docs/whatsnew2020.html#phased-replacement-of-context-receivers-with-context-parameters
15-
)
15+
runCatching {
16+
kotlin {
17+
sourceSets.all { // all is jvm :P
18+
dependencies {
19+
@Suppress("VulnerableLibrariesLocal", "RedundantSuppression")
20+
compileOnly(libs.allaymc.api)
21+
}
22+
}
23+
compilerOptions.freeCompilerArgs.addAll(
24+
"-Xcontext-receivers", // https://kotlinlang.org/docs/whatsnew2020.html#phased-replacement-of-context-receivers-with-context-parameters
25+
)
26+
}
27+
}
1628

1729
if (projectDir.resolve("src/main/resources/plugin.json").exists()) {
1830
val version = rootProject.libs.versions.allaymc.kotlinx.get()

core/build.gradle.kts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
plugins {
22
alias(libs.plugins.kotlin.jvm)
33
}
4-
5-
dependencies {
6-
compileOnly(libs.allaymc.api)
7-
}

example/build.gradle.kts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ plugins {
66
}
77

88
dependencies {
9-
@Suppress("VulnerableLibrariesLocal", "RedundantSuppression")
10-
compileOnly(libs.allaymc.api)
11-
compileOnly(projects.plugin.composeLib)
9+
compileOnly(projects.plugin.core)
10+
compileOnly(projects.plugin.compose)
1211
}
1312

1413
kotlin {

plugin/build.gradle.kts

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,59 @@
11
plugins {
22
alias(libs.plugins.kotlin.jvm)
3-
alias(libs.plugins.gradleup.shadow)
43
}
54

6-
dependencies {
7-
@Suppress("VulnerableLibrariesLocal", "RedundantSuppression")
8-
compileOnly(libs.allaymc.api)
9-
api(projects.core)
10-
11-
api(kotlin("stdlib"))
12-
api(kotlin("stdlib-jdk7"))
13-
api(kotlin("stdlib-jdk8"))
14-
api(kotlin("reflect"))
15-
api(libs.kotlinx.coroutines.core)
16-
api(libs.kotlinx.coroutines.swing)
5+
class Names(val words: Array<String>) {
6+
constructor(name: String) : this(name.split('-').toTypedArray())
7+
val lodash get() = words.joinToString("_")
8+
val kebab get() = words.joinToString("-")
9+
val upperCamel get() = words.joinToString("") { word -> word.replaceFirstChar { it.uppercase() } }
1710
}
1811

19-
kotlin {
20-
jvmToolchain(21)
12+
fun basePluginClassPackage(names: Names) = "vip.cdms.allaymc.kotlinx.shared.${names.lodash}"
13+
fun basePluginClassName(names: Names) = "Kotlinx${names.upperCamel}SharedLib"
14+
fun basePluginClass(names: Names) = """
15+
|package ${basePluginClassPackage(names)}
16+
|
17+
|import org.allaymc.api.plugin.Plugin
18+
|
19+
|/** Automatically generated. Do NOT edit this file directly! */
20+
|class ${basePluginClassName(names)} : Plugin()
21+
|
22+
""".trimMargin()
23+
24+
fun basePluginDescriptor(names: Names) = """
25+
{
26+
"entrance": "${basePluginClassPackage(names)}.${basePluginClassName(names)}",
27+
"name": "kotlinx-${names.kebab}-lib",
28+
"description": "${
29+
if (names.lodash == "core") "Kotlin-style Extension Library and Shared Standard Library for Allay Server"
30+
else "Kotlinx Shared Library for Allay Server -- ${names.upperCamel}"
31+
}",
32+
"authors": [
33+
"MineBuilder"
34+
],
35+
"version": "${'$'}{version}",
36+
"dependencies": [],
37+
"website": "https://github.com/MineBuilders/allaymc-kotlinx"
38+
}
39+
""".trimIndent()
40+
41+
subprojects {
42+
val names = Names(name)
43+
if (names.lodash == "build" || names.words.getOrNull(1) == "generated") return@subprojects
44+
45+
val generatePath = arrayOf("build", "plugin-generated-$name")
46+
val generateDir = File(projectDir, "../" + generatePath.joinToString("/")).apply { mkdirs() }
47+
val sourceDir = File(generateDir, "src/main/kotlin/" + basePluginClassPackage(names).split('.').joinToString("/"))
48+
val resourcesDir = File(generateDir, "src/main/resources")
49+
File(sourceDir.apply { mkdirs() }, basePluginClassName(names) + ".kt").writeText(basePluginClass(names))
50+
File(resourcesDir.apply { mkdirs() }, "plugin.json").writeText(basePluginDescriptor(names))
51+
52+
plugins.apply(rootProject.libs.plugins.kotlin.jvm.get().pluginId)
53+
plugins.apply(rootProject.libs.plugins.gradleup.shadow.get().pluginId)
54+
version = rootProject.rootProject.version
55+
afterEvaluate {
56+
dependencies { implementation(project(":plugin:" + generatePath.joinToString(":"))) }
57+
kotlin { jvmToolchain(21) }
58+
}
2159
}

plugin/compose-lib/build.gradle.kts renamed to plugin/compose/build.gradle.kts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
import org.jetbrains.compose.ExperimentalComposeLibrary
22

33
plugins {
4-
alias(libs.plugins.kotlin.jvm)
54
alias(libs.plugins.compose.multiplatform)
65
alias(libs.plugins.compose.compiler)
7-
alias(libs.plugins.gradleup.shadow)
86
}
97

108
@OptIn(ExperimentalComposeLibrary::class)
119
dependencies {
12-
api(projects.plugin)
13-
1410
api(compose.runtime)
1511
api(compose.ui)
1612
api(compose.foundation)
@@ -30,7 +26,3 @@ dependencies {
3026
api(compose.desktop.components.splitPane)
3127
api(compose.desktop.components.animatedImage)
3228
}
33-
34-
kotlin {
35-
jvmToolchain(21)
36-
}

plugin/core/build.gradle.kts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
dependencies {
2+
api(projects.core)
3+
4+
api(kotlin("stdlib"))
5+
api(kotlin("stdlib-jdk7"))
6+
api(kotlin("stdlib-jdk8"))
7+
api(kotlin("reflect"))
8+
api(libs.kotlinx.coroutines.core)
9+
api(libs.kotlinx.coroutines.swing)
10+
}

plugin/src/main/kotlin/vip/cdms/allaymc/kotlinx/KotlinxPlugin.kt

Lines changed: 0 additions & 6 deletions
This file was deleted.

plugin/src/main/resources/plugin.json

Lines changed: 0 additions & 11 deletions
This file was deleted.

settings.gradle.kts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ dependencyResolutionManagement {
3535
}
3636
}
3737

38+
val sharedDeps = arrayOf("core", "compose")
3839
include(":core")
39-
include(":plugin", ":plugin:compose-lib")
40+
include(":plugin")
4041
include(":example")
42+
43+
sharedDeps.forEach {
44+
val generated = arrayOf("plugin", "build", "plugin-generated-$it")
45+
File(File(rootProject.projectDir, generated.joinToString("/"))
46+
.apply { mkdirs() }, "build.gradle.kts")
47+
.writeText("plugins { alias(libs.plugins.kotlin.jvm) }")
48+
include(":plugin:$it", ":" + generated.joinToString(":"))
49+
}

0 commit comments

Comments
 (0)