Skip to content

Commit

Permalink
build: more automated, modular shared libs
Browse files Browse the repository at this point in the history
  • Loading branch information
Cdm2883 committed Jan 3, 2025
1 parent ec1d5c0 commit d8ea543
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 71 deletions.
63 changes: 42 additions & 21 deletions .github/workflows/preview-build.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Build a preview plugin
name: Build preview shared lib plugins

on: [ push, pull_request ]

Expand All @@ -10,31 +10,52 @@ jobs:
- name: Checkout master
uses: actions/checkout@v4

- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20'

- name: Clone actions/upload-artifact repo
run: git clone https://github.com/actions/upload-artifact.git

- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: '21'
distribution: 'zulu'

- name: Build with Gradle
- name: Build and upload plugins dynamically
env:
INPUT_OVERWRITE: false
INPUT_INCLUDE-HIDDEN-FILES: false
run: |
chmod +x ./gradlew
./gradlew :plugin:shadowJar
./gradlew :plugin:compose-lib:shadowJar
- name: Rename jar
run: |
mv "plugin/build/libs/plugin-all.jar" "plugin/build/libs/kotlinx-${{ github.sha }}.jar"
mv "plugin/compose-lib/build/libs/compose-lib-all.jar" "plugin/build/libs/kotlinx-compose-${{ github.sha }}.jar"
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: kotlinx-shared-lib-plugin-${{ github.sha }}
path: plugin/build/libs/kotlinx-${{ github.sha }}.jar

- name: Upload artifact (Compose)
uses: actions/upload-artifact@v4
with:
name: kotlinx-compose-shared-lib-plugin-${{ github.sha }}
path: plugin/build/libs/kotlinx-compose-${{ github.sha }}.jar
mkdir -p plugin/build/libs
for dir in plugin/*/; do
if [ -d "$dir" ]; then
dir_name=$(basename "$dir")
if [ "$dir_name" == "build" ]; then
continue
fi
echo "Building $dir_name"
./gradlew :plugin:"$dir_name":shadowJar
output_dir="plugin/$dir_name/build/libs"
jar_file=$(ls $output_dir/*.jar | grep -E ".*-all\.jar$")
if [ -z "$jar_file" ]; then
echo "No JAR file found in $output_dir for $dir_name"
exit 1
fi
base_name=$(basename "$jar_file" -all.jar)
new_name="plugin/build/libs/kotlinx-${base_name}-${GITHUB_SHA}.jar"
mv "$jar_file" "$new_name"
echo "Uploading artifact $new_name"
artifact_name="${base_name}-${GITHUB_SHA}"
INPUT_NAME="$artifact_name" INPUT_PATH="$new_name" node upload-artifact/dist/upload/index.js
fi
done
18 changes: 15 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,24 @@ plugins {
alias(libs.plugins.gradleup.shadow) apply false
}

version = libs.versions.allaymc.kotlinx.get()

subprojects {
afterEvaluate {

kotlin.compilerOptions.freeCompilerArgs.addAll(
"-Xcontext-receivers", // https://kotlinlang.org/docs/whatsnew2020.html#phased-replacement-of-context-receivers-with-context-parameters
)
runCatching {
kotlin {
sourceSets.all { // all is jvm :P
dependencies {
@Suppress("VulnerableLibrariesLocal", "RedundantSuppression")
compileOnly(libs.allaymc.api)
}
}
compilerOptions.freeCompilerArgs.addAll(
"-Xcontext-receivers", // https://kotlinlang.org/docs/whatsnew2020.html#phased-replacement-of-context-receivers-with-context-parameters
)
}
}

if (projectDir.resolve("src/main/resources/plugin.json").exists()) {
val version = rootProject.libs.versions.allaymc.kotlinx.get()
Expand Down
4 changes: 0 additions & 4 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
plugins {
alias(libs.plugins.kotlin.jvm)
}

dependencies {
compileOnly(libs.allaymc.api)
}
5 changes: 2 additions & 3 deletions example/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ plugins {
}

dependencies {
@Suppress("VulnerableLibrariesLocal", "RedundantSuppression")
compileOnly(libs.allaymc.api)
compileOnly(projects.plugin.composeLib)
compileOnly(projects.plugin.core)
compileOnly(projects.plugin.compose)
}

kotlin {
Expand Down
66 changes: 52 additions & 14 deletions plugin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,21 +1,59 @@
plugins {
alias(libs.plugins.kotlin.jvm)
alias(libs.plugins.gradleup.shadow)
}

dependencies {
@Suppress("VulnerableLibrariesLocal", "RedundantSuppression")
compileOnly(libs.allaymc.api)
api(projects.core)

api(kotlin("stdlib"))
api(kotlin("stdlib-jdk7"))
api(kotlin("stdlib-jdk8"))
api(kotlin("reflect"))
api(libs.kotlinx.coroutines.core)
api(libs.kotlinx.coroutines.swing)
class Names(val words: Array<String>) {
constructor(name: String) : this(name.split('-').toTypedArray())
val lodash get() = words.joinToString("_")
val kebab get() = words.joinToString("-")
val upperCamel get() = words.joinToString("") { word -> word.replaceFirstChar { it.uppercase() } }
}

kotlin {
jvmToolchain(21)
fun basePluginClassPackage(names: Names) = "vip.cdms.allaymc.kotlinx.shared.${names.lodash}"
fun basePluginClassName(names: Names) = "Kotlinx${names.upperCamel}SharedLib"
fun basePluginClass(names: Names) = """
|package ${basePluginClassPackage(names)}
|
|import org.allaymc.api.plugin.Plugin
|
|/** Automatically generated. Do NOT edit this file directly! */
|class ${basePluginClassName(names)} : Plugin()
|
""".trimMargin()

fun basePluginDescriptor(names: Names) = """
{
"entrance": "${basePluginClassPackage(names)}.${basePluginClassName(names)}",
"name": "kotlinx-${names.kebab}-lib",
"description": "${
if (names.lodash == "core") "Kotlin-style Extension Library and Shared Standard Library for Allay Server"
else "Kotlinx Shared Library for Allay Server -- ${names.upperCamel}"
}",
"authors": [
"MineBuilder"
],
"version": "${'$'}{version}",
"dependencies": [],
"website": "https://github.com/MineBuilders/allaymc-kotlinx"
}
""".trimIndent()

subprojects {
val names = Names(name)
if (names.lodash == "build" || names.words.getOrNull(1) == "generated") return@subprojects

val generatePath = arrayOf("build", "plugin-generated-$name")
val generateDir = File(projectDir, "../" + generatePath.joinToString("/")).apply { mkdirs() }
val sourceDir = File(generateDir, "src/main/kotlin/" + basePluginClassPackage(names).split('.').joinToString("/"))
val resourcesDir = File(generateDir, "src/main/resources")
File(sourceDir.apply { mkdirs() }, basePluginClassName(names) + ".kt").writeText(basePluginClass(names))
File(resourcesDir.apply { mkdirs() }, "plugin.json").writeText(basePluginDescriptor(names))

plugins.apply(rootProject.libs.plugins.kotlin.jvm.get().pluginId)
plugins.apply(rootProject.libs.plugins.gradleup.shadow.get().pluginId)
version = rootProject.rootProject.version
afterEvaluate {
dependencies { implementation(project(":plugin:" + generatePath.joinToString(":"))) }
kotlin { jvmToolchain(21) }
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import org.jetbrains.compose.ExperimentalComposeLibrary

plugins {
alias(libs.plugins.kotlin.jvm)
alias(libs.plugins.compose.multiplatform)
alias(libs.plugins.compose.compiler)
alias(libs.plugins.gradleup.shadow)
}

@OptIn(ExperimentalComposeLibrary::class)
dependencies {
api(projects.plugin)

api(compose.runtime)
api(compose.ui)
api(compose.foundation)
Expand All @@ -30,7 +26,3 @@ dependencies {
api(compose.desktop.components.splitPane)
api(compose.desktop.components.animatedImage)
}

kotlin {
jvmToolchain(21)
}
10 changes: 10 additions & 0 deletions plugin/core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
dependencies {
api(projects.core)

api(kotlin("stdlib"))
api(kotlin("stdlib-jdk7"))
api(kotlin("stdlib-jdk8"))
api(kotlin("reflect"))
api(libs.kotlinx.coroutines.core)
api(libs.kotlinx.coroutines.swing)
}

This file was deleted.

11 changes: 0 additions & 11 deletions plugin/src/main/resources/plugin.json

This file was deleted.

11 changes: 10 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ dependencyResolutionManagement {
}
}

val sharedDeps = arrayOf("core", "compose")
include(":core")
include(":plugin", ":plugin:compose-lib")
include(":plugin")
include(":example")

sharedDeps.forEach {
val generated = arrayOf("plugin", "build", "plugin-generated-$it")
File(File(rootProject.projectDir, generated.joinToString("/"))
.apply { mkdirs() }, "build.gradle.kts")
.writeText("plugins { alias(libs.plugins.kotlin.jvm) }")
include(":plugin:$it", ":" + generated.joinToString(":"))
}

0 comments on commit d8ea543

Please sign in to comment.