-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1.3.1: Plugin and dev mode fixes, .env parsing
Fixes #5
- Loading branch information
Showing
23 changed files
with
308 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Gradle Plugins 1.3.1 | ||
|
||
This version makes some changes to the KordEx plugin. | ||
|
||
## KordEx Plugin | ||
|
||
This release improves support for running bots in development, and provides a more sensible way to resolve the latest Kord version (when `"latest"` is specified). | ||
|
||
- Add a new `dev` task, which runs your bot in development mode and can recursively parse `.env` files in your project: | ||
- First, the `.env` file in your project's root directory is parsed. | ||
- Then, if your bot is in a submodule, the KordEx plugin will traverse the directory tree down to it, parsing any `.env` files it finds on the way, overriding previously set variables if it finds any duplicates. | ||
- Example: `/root/.env` -> `/root/folder/.env` -> `/root/folder/current-submodule/.env` | ||
- The plugin adds these variables to your in-development bot's runtime environment variables. | ||
- You can disable this by setting `processDotEnv` in your `bot { }` builder to `false`. | ||
- When you supply `"latest"` for the `kordVersion` property, the plugin will now ignore Kord feature branch snapshots, which should make things more consistent and predictable. You can still supply feature branch snapshots manually if you need to use them. | ||
- The plugin now uses reflection to figure out which version of the Kotlin plugin you've applied, rather than inspecting the buildscript's classpath. This fixes a problem where the classpath is empty in some project configurations. | ||
- When writing KordEx plugins using the `plugin { }` builder, the plugin now disables all `Tar` tasks, as building a`.tar` distribution is nonsensical given Kord Extensions can't load them. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
kordex/src/main/kotlin/dev/kordex/gradle/plugins/kordex/helpers/ApplicationPluginHelper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
package dev.kordex.gradle.plugins.kordex.helpers | ||
|
||
import dev.kordex.gradle.plugins.kordex.base.KordExExtension | ||
import org.gradle.api.Project | ||
import org.gradle.api.plugins.ApplicationPlugin | ||
import org.gradle.api.plugins.JavaApplication | ||
import org.gradle.api.tasks.JavaExec | ||
import org.gradle.api.tasks.SourceSetContainer | ||
import org.gradle.api.tasks.bundling.Jar | ||
import org.gradle.kotlin.dsl.* | ||
import org.slf4j.LoggerFactory | ||
import java.io.File | ||
import kotlin.jvm.java | ||
|
||
object ApplicationPluginHelper { | ||
private val logger = LoggerFactory.getLogger(ApplicationPluginHelper::class.java) | ||
|
||
fun apply(target: Project, extension: KordExExtension) { | ||
target.plugins.apply(ApplicationPlugin::class.java) | ||
|
||
target.extensions.configure<JavaApplication> { | ||
mainClass = extension.bot.mainClass | ||
} | ||
|
||
target.tasks.withType<Jar> { | ||
manifest { | ||
attributes( | ||
"Main-Class" to extension.bot.mainClass.get() | ||
) | ||
} | ||
} | ||
|
||
val sourceSets = target.extensions.getByType<SourceSetContainer>() | ||
|
||
target.tasks.create<JavaExec>("dev") { | ||
group = "application" | ||
description = "Run the configured Kord Extensions bot in development mode" | ||
|
||
environment(processDotEnv(target, extension)) | ||
|
||
jvmArguments.add("-DdevMode=true") | ||
|
||
classpath = sourceSets.named("main").get().runtimeClasspath | ||
mainClass = extension.bot.mainClass | ||
} | ||
} | ||
|
||
fun processDotEnv(target: Project, extension: KordExExtension): MutableMap<String, String> { | ||
val variables = mutableMapOf<String, String>() | ||
|
||
if (!extension.bot.processDotEnv.get()) { | ||
return variables | ||
} | ||
|
||
val files = mutableListOf<File>() | ||
|
||
val top = target.rootProject.projectDir.toPath() | ||
var current = target.projectDir.toPath() | ||
|
||
while (current.startsWith(top)) { | ||
val file = current.resolve(".env").toFile() | ||
|
||
if (file.isFile) { | ||
files.add(file) | ||
} | ||
|
||
current = current.parent | ||
} | ||
|
||
files.reverse() | ||
|
||
files.forEach { file -> | ||
logger.info("Loading variables from: $file") | ||
|
||
val lines = file.readLines() | ||
|
||
for (line in lines) { | ||
var effectiveLine = line.trimStart() | ||
|
||
if (effectiveLine.isBlank() || effectiveLine.startsWith("#")) { | ||
continue | ||
} | ||
|
||
if (effectiveLine.contains("#")) { | ||
effectiveLine = effectiveLine.substring(0, effectiveLine.indexOf("#")) | ||
} | ||
|
||
if (!effectiveLine.contains('=')) { | ||
logger.warn( | ||
"Invalid line in dotenv file: \"=\" not found\n" + | ||
" -> $file\n" + | ||
" -> $effectiveLine" | ||
) | ||
|
||
continue | ||
} | ||
|
||
val split = effectiveLine | ||
.split("=", limit = 2) | ||
.map { it.trim() } | ||
|
||
if (split.size != 2) { | ||
logger.warn( | ||
"Invalid line in dotenv file: variables must be of the form \"name=value\"\n" + | ||
" -> $file\n" + | ||
" -> $effectiveLine" | ||
) | ||
|
||
continue | ||
} | ||
|
||
logger.trace("${split[0]} -> ${split[1]}") | ||
|
||
variables[split[0]] = split[1] | ||
} | ||
} | ||
|
||
return variables | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
kordex/src/main/kotlin/dev/kordex/gradle/plugins/kordex/helpers/KspPluginHelper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
package dev.kordex.gradle.plugins.kordex.helpers | ||
|
||
import dev.kordex.gradle.plugins.kordex.Version | ||
import dev.kordex.gradle.plugins.kordex.base.addDependency | ||
import org.gradle.api.Project | ||
import org.slf4j.LoggerFactory | ||
|
||
object KspPluginHelper { | ||
private val logger = LoggerFactory.getLogger(KspPluginHelper::class.java) | ||
|
||
fun apply(target: Project, basePackage: String, version: Version) { | ||
target.pluginManager.withPlugin("com.google.devtools.ksp") { | ||
logger.info("KSP plugin detected, adding Kord Extensions annotation processor") | ||
|
||
target.addDependency( | ||
arrayOf("ksp"), | ||
"$basePackage:annotation-processor:$version" | ||
) | ||
} | ||
} | ||
} |
Oops, something went wrong.