Skip to content

Commit

Permalink
Add JDA, Lazy Library, and Magic Mongo dependencies/setups
Browse files Browse the repository at this point in the history
  • Loading branch information
srnyx committed Aug 23, 2024
1 parent 5f01982 commit 1390ee9
Show file tree
Hide file tree
Showing 3 changed files with 380 additions and 194 deletions.
66 changes: 64 additions & 2 deletions src/main/kotlin/xyz/srnyx/gradlegalaxy/utility/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ fun Project.paper(
* @param dependencies The Adventure dependencies to add
* @param configurationAll The configuration to use for the dependencies if they don't have one specified
*/
@Ignore
fun Project.adventure(vararg dependencies: AdventureDependency, configurationAll: String? = null) {
check(hasJavaPlugin()) { "Java plugin is not applied!" }
repository(Repository.MAVEN_CENTRAL)
Expand Down Expand Up @@ -135,6 +136,67 @@ fun Project.annoyingAPI(
}
}

/**
* 1. Adds the [Repository.MAVEN_CENTRAL] repository
* 2. Adds the dependency to the provided JDA version
*
* @param version The version of JDA to use
* @param configuration The configuration to add the dependency to
* @param configurationAction The action to apply to the dependency
*
* @return The [ExternalModuleDependency] of the added JDA dependency
*/
fun Project.jda(
version: String,
configuration: String = "implementation",
configurationAction: ExternalModuleDependency.() -> Unit = {}
): ExternalModuleDependency {
check(hasJavaPlugin()) { "Java plugin is not applied!" }
repository(Repository.MAVEN_CENTRAL)
return addDependencyTo(dependencies, configuration, "net.dv8tion:JDA:$version", configurationAction)
}

/**
* 1. Adds the [Repository.JITPACK] repository
* 2. Adds the dependency to the provided Lazy Library version
*
* @param version The version of Lazy Library to use
* @param configuration The configuration to add the dependency to
* @param configurationAction The action to apply to the dependency
*
* @return The [ExternalModuleDependency] of the added Lazy Library dependency
*/
fun Project.lazyLibrary(
version: String,
configuration: String = "implementation",
configurationAction: ExternalModuleDependency.() -> Unit = {}
): ExternalModuleDependency {
check(hasJavaPlugin()) { "Java plugin is not applied!" }
repository(Repository.JITPACK)
return addDependencyTo(dependencies, configuration, "xyz.srnyx:lazy-library:$version", configurationAction)
}

/**
* 1. Adds the [Repository.JITPACK] repository
* 2. Adds the dependency to the provided Magic Mongo version
*
* @param version The version of Magic Mongo to use
* @param configuration The configuration to add the dependency to
* @param configurationAction The action to apply to the dependency
*
* @return The [ExternalModuleDependency] of the added Magic Mongo dependency
*/
@Ignore
fun Project.magicMongo(
version: String,
configuration: String = "implementation",
configurationAction: ExternalModuleDependency.() -> Unit = {}
): ExternalModuleDependency {
check(hasJavaPlugin()) { "Java plugin is not applied!" }
repository(Repository.JITPACK)
return addDependencyTo(dependencies, configuration, "xyz.srnyx:magic-mongo:$version", configurationAction)
}

/**
* 1. Adds the provided dependency as an `implementation` dependency
* 2. Relocates the dependency to the provided package
Expand All @@ -155,7 +217,7 @@ fun <T: ModuleDependency> DependencyHandler.implementationRelocate(
relocateTo: String = "${project.getPackage()}.libs.${relocateFrom.split(".").last()}",
configuration: T.() -> Unit = {}
): T {
check(project.hasShadowPlugin()) { "Shadow plugin is not applied!" }
check(hasShadowPlugin()) { "Shadow plugin is not applied!" }
project.relocate(relocateFrom, relocateTo)
return add("implementation", dependency, configuration)
}
Expand All @@ -180,7 +242,7 @@ fun DependencyHandler.implementationRelocate(
relocateTo: String = "${project.getPackage()}.libs.${relocateFrom.split(".").last()}",
configuration: Action<ExternalModuleDependency> = Action {}
): ExternalModuleDependency {
check(project.hasShadowPlugin()) { "Shadow plugin is not applied!" }
check(hasShadowPlugin()) { "Shadow plugin is not applied!" }
project.relocate(relocateFrom, relocateTo)
return addDependencyTo(this, "implementation", dependency, configuration)
}
Expand Down
221 changes: 29 additions & 192 deletions src/main/kotlin/xyz/srnyx/gradlegalaxy/utility/Scripts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,14 @@ import org.gradle.api.DefaultTask
import org.gradle.api.JavaVersion
import org.gradle.api.Project
import org.gradle.api.UnknownDomainObjectException
import org.gradle.api.artifacts.ExternalModuleDependency
import org.gradle.api.component.SoftwareComponent
import org.gradle.api.plugins.JavaApplication
import org.gradle.api.plugins.JavaPluginExtension
import org.gradle.api.publish.PublishingExtension
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.api.tasks.Copy
import org.gradle.api.tasks.compile.JavaCompile
import org.gradle.api.tasks.bundling.Jar
import org.gradle.kotlin.dsl.*

import xyz.srnyx.gradlegalaxy.annotations.Ignore
import xyz.srnyx.gradlegalaxy.data.pom.DeveloperData
import xyz.srnyx.gradlegalaxy.data.pom.LicenseData
import xyz.srnyx.gradlegalaxy.data.pom.ScmData


/**
Expand Down Expand Up @@ -73,7 +67,7 @@ fun Project.hasJavaPlugin(): Boolean {
*
* @return If the Shadow plugin is applied
*/
fun Project.hasShadowPlugin(): Boolean {
fun hasShadowPlugin(): Boolean {
if (hasShadowPlugin == null) hasShadowPlugin = try {
Class.forName("com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar")
true
Expand All @@ -83,6 +77,20 @@ fun Project.hasShadowPlugin(): Boolean {
return hasShadowPlugin!!
}

/**
* Checks if the `application` plugin is applied
*
* @return If the `application` plugin is applied
*/
fun hasApplicationPlugin(): Boolean {
return try {
Class.forName("org.gradle.api.plugins.ApplicationPlugin")
true
} catch (e: ClassNotFoundException) {
false
}
}

/**
* Gets the Java plugin extension
*
Expand Down Expand Up @@ -114,6 +122,7 @@ fun Project.getDefaultReplacements(): Map<String, String> = mapOf(
* @param encoding The encoding to set
*/
fun Project.setTextEncoding(encoding: String = "UTF-8") {
check(hasJavaPlugin()) { "Java plugin is not applied!" }
tasks.withType<JavaCompile> { options.encoding = encoding }
}

Expand All @@ -123,6 +132,7 @@ fun Project.setTextEncoding(encoding: String = "UTF-8") {
* @param version The java version to set (example: [JavaVersion.VERSION_1_8])
*/
fun Project.setJavaVersion(version: JavaVersion = JavaVersion.VERSION_1_8) {
check(hasJavaPlugin()) { "Java plugin is not applied!" }
val java: JavaPluginExtension = getJavaExtension()
java.sourceCompatibility = version
java.targetCompatibility = version
Expand Down Expand Up @@ -182,9 +192,20 @@ fun Project.addReplacementsTask(files: Set<String> = setOf("plugin.yml"), replac
*/
@Ignore
fun Project.addCompilerArgs(vararg args: String) {
check(hasJavaPlugin()) { "Java plugin is not applied!" }
tasks.withType<JavaCompile> { options.compilerArgs.addAll(args) }
}

/**
* Sets the main class for the project
*
* @param mainClassName The main class name to set, defaults to "[getPackage].[Project.getName]"
*/
fun Project.setMainClass(mainClassName: String = "${getPackage()}.${project.name}") {
check(hasApplicationPlugin()) { "Application plugin is not applied!" }
extensions.configure<JavaApplication>("application") { mainClass.set(mainClassName) }
}

/**
* Relocates the specified package to the specified package
*
Expand All @@ -200,187 +221,3 @@ fun Project.relocate(
check(hasShadowPlugin()) { "Shadow plugin is not applied!" }
tasks.named<ShadowJar>("shadowJar") { relocate(from, to, action) }
}

/**
* Sets up the project with the specified [group] and [version] for a simple Java project
*
* Calls [setJavaVersion], [setTextEncoding], and [addReplacementsTask]
*
* If the [shadow plugin is applied][hasShadowPlugin], it will also call [setShadowArchiveClassifier] and [addJavadocSourcesJars]
*
* @param group The group of the project (example: `xyz.srnyx`)
* @param version The version of the project (example: `1.0.0`)
* @param description The description of the project
* @param javaVersion The java version of the project (example: [JavaVersion.VERSION_1_8])
* @param textEncoding The text encoding for the [text encoding task][setTextEncoding]
* @param archiveClassifier The archive classifier for the [shadow jar][setShadowArchiveClassifier]
*/
@Ignore
fun Project.setupJava(
group: String = project.group.toString(),
version: String = project.version.toString(),
description: String? = project.description,
javaVersion: JavaVersion? = null,
textEncoding: String? = "UTF-8",
archiveClassifier: String? = "",
) {
this.group = group
this.version = version
this.description = description
javaVersion?.let(::setJavaVersion)
textEncoding?.let(::setTextEncoding)
if (hasShadowPlugin()) {
archiveClassifier?.let(::setShadowArchiveClassifier)
addBuildShadowTask()
}
}

/**
* Sets up the project with the specified [group] and [version] for a simple Minecraft project
*
* Calls [setupJava] and [addReplacementsTask] with the specified parameters
*
* @param group The group of the project (example: `xyz.srnyx`)
* @param version The version of the project (example: `1.0.0`)
* @param description The description of the project
* @param javaVersion The java version of the project (example: [JavaVersion.VERSION_1_8])
* @param replacements The replacements for the [replacements task][addReplacementsTask]
* @param textEncoding The text encoding for the [text encoding task][setTextEncoding]
* @param archiveClassifier The archive classifier for the [shadow jar task][setShadowArchiveClassifier]
*/
@Ignore
fun Project.setupMC(
group: String = project.group.toString(),
version: String = project.version.toString(),
description: String? = project.description,
javaVersion: JavaVersion? = null,
replacementFiles: Set<String>? = setOf("plugin.yml"),
replacements: Map<String, String>? = mapOf("defaultReplacements" to "true"),
textEncoding: String? = "UTF-8",
archiveClassifier: String? = "",
) {
setupJava(group, version, description, javaVersion, textEncoding, archiveClassifier)
if (replacementFiles != null && replacements != null) addReplacementsTask(replacementFiles, replacements)
}

/**
* Sets up the project using Annoying API. **The [root project's name][Project.getName] must be the same as the one in plugin.yml**
*
* 1. Checks if the Shadow plugin is applied
* 2. Calls [setupMC] with the specified parameters
* 3. Calls and returns [annoyingAPI] with the specified parameters
*
* @param annoyingAPIVersion The version of Annoying API to use (example: `3.0.1`)
* @param group The group of the project (example: `xyz.srnyx`)
* @param version The version of the project (example: `1.0.0`)
* @param description The description of the project
* @param javaVersion The java version of the project (example: [JavaVersion.VERSION_1_8])
* @param replacements The replacements for the [replacements task][addReplacementsTask]
* @param textEncoding The text encoding for the [text encoding task][setTextEncoding]
* @param archiveClassifier The archive classifier for the [shadow jar task][setShadowArchiveClassifier]
* @param configurationAction The configuration for the Annoying API dependency
*/
@Ignore
fun Project.setupAnnoyingAPI(
annoyingAPIVersion: String,
group: String = project.group.toString(),
version: String = project.version.toString(),
description: String? = project.description,
javaVersion: JavaVersion? = null,
replacementFiles: Set<String>? = setOf("plugin.yml"),
replacements: Map<String, String>? = mapOf("defaultReplacements" to "true"),
textEncoding: String? = "UTF-8",
archiveClassifier: String? = "",
configuration: String = "implementation",
configurationAction: ExternalModuleDependency.() -> Unit = {},
): ExternalModuleDependency {
check(hasShadowPlugin()) { "Shadow plugin is required for Annoying API!" }
setupMC(group, version, description, javaVersion, replacementFiles, replacements, textEncoding, archiveClassifier)
return annoyingAPI(annoyingAPIVersion, configuration, configurationAction)
}

/**
* Sets up a simple publishing configuration
*
* 1. Applies the `maven-publish` plugin
* 2. Creates a [MavenPublication] with the specified parameters
* 3. Configures the [MavenPublication] with the specified [configuration]
*
* @param groupId The group ID
* @param artifactId The artifact ID
* @param version The version
* @param component The [SoftwareComponent] to publish
* @param artifacts The artifacts to publish
* @param name The name of the project
* @param description The description of the project
* @param url The URL of the project
* @param licenses The licenses of the project
* @param developers The developers of the project
* @param scm The SCM information of the project
* @param configuration The configuration of the [MavenPublication]
*
* @return The [MavenPublication] that was created
*/
@Ignore
inline fun Project.setupPublishing(
groupId: String? = null,
artifactId: String? = null,
version: String? = null,
withJavadocSourcesJars: Boolean = true,
component: SoftwareComponent? = components["java"],
artifacts: Collection<Any> = emptyList(),
name: String? = project.name,
description: String? = project.description,
url: String? = null,
licenses: List<LicenseData> = emptyList(),
developers: List<DeveloperData> = emptyList(),
scm: ScmData? = null,
crossinline configuration: MavenPublication.() -> Unit = {}
): MavenPublication {
apply(plugin = "maven-publish")
if (withJavadocSourcesJars) addJavadocSourcesJars()
return (extensions["publishing"] as PublishingExtension).publications.create<MavenPublication>("maven") {
groupId?.let { this.groupId = it }
artifactId?.let { this.artifactId = it }
version?.let { this.version = it }
component?.let { this.from(component) }
artifacts.forEach(this::artifact)
pom {
name?.let(this.name::set)
description?.let(this.description::set)
url?.let(this.url::set)
licenses {
licenses.forEach {
license {
this.name.set(it.name)
this.url.set(it.url)
it.distribution?.value?.let(this.distribution::set)
it.comments?.let(this.comments::set)
}
}
}
developers {
developers.filterNot(DeveloperData::isEmpty).forEach {
developer {
it.id?.let(this.id::set)
it.name?.let(this.name::set)
it.url?.let(this.url::set)
it.email?.let(this.email::set)
it.timezone?.let(this.timezone::set)
it.organization?.let(this.organization::set)
it.organizationUrl?.let(this.organizationUrl::set)
it.roles.takeIf(List<String>::isNotEmpty)?.let(this.roles::set)
it.properties.takeIf(Map<String, String>::isNotEmpty)?.let(this.properties::set)
}
}
}
if (scm != null) scm {
connection.set(scm.connection)
developerConnection.set(scm.developerConnection)
scm.url?.let(this.url::set)
scm.tag?.let(this.tag::set)
}
}
configuration()
}
}
Loading

0 comments on commit 1390ee9

Please sign in to comment.