Skip to content

Commit

Permalink
Change how Dependencies works
Browse files Browse the repository at this point in the history
They now will actually add the dependency instead of just returning the annotation
  • Loading branch information
srnyx committed Jun 22, 2023
1 parent 43d324f commit 0ac9bac
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 80 deletions.
18 changes: 0 additions & 18 deletions src/main/kotlin/xyz/srnyx/gradlegalaxy/data/AnnoyingAPIReturn.kt

This file was deleted.

109 changes: 76 additions & 33 deletions src/main/kotlin/xyz/srnyx/gradlegalaxy/utility/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.gradle.api.artifacts.ModuleDependency
import org.gradle.api.artifacts.dsl.DependencyHandler
import org.gradle.kotlin.dsl.accessors.runtime.addDependencyTo
import org.gradle.kotlin.dsl.add
import org.gradle.kotlin.dsl.exclude

import xyz.srnyx.gradlegalaxy.annotations.Ignore
import xyz.srnyx.gradlegalaxy.enums.PaperVersion
Expand All @@ -19,84 +20,110 @@ import xyz.srnyx.gradlegalaxy.enums.repository
* 1. Sets the Java version for the project depending on the version
* 2. Adds the [Repository.SONATYPE_SNAPSHOTS_OLD] repository if the version is 1.15 or below
* 3. Adds the [Repository.MAVEN_CENTRAL] and [Repository.SPIGOT] repositories
* 4. Returns the dependency annotation (org.spigotmc:spigot-api:[getVersionString])
* 4. Adds the Spigot-API dependency (org.spigotmc:spigot-api:[getVersionString]) using the [configuration] and [configurationAction]
*
* @param versionString The version of Spigot-API to use
* @param version The version of Spigot-API to use
* @param configuration The configuration to add the dependency to
* @param configurationAction The action to apply to the dependency
*
* @return The dependency annotation for the provided Spigot-API version
* @return The [ExternalModuleDependency] of the added Spigot-API dependency
*/
@Ignore
fun Project.spigotAPI(versionString: String): String {
fun Project.spigotAPI(
version: String,
configuration: String = "compileOnly",
configurationAction: Action<ExternalModuleDependency> = Action {}
): ExternalModuleDependency {
check(hasJavaPlugin()) { "Java plugin is not applied!" }
// Java version
getJavaVersionForMC(versionString)?.let(::setJavaVersion)
// Repositories
val version = SemanticVersion(versionString)
if (version.major <= 1 && version.minor <= 15) repository(Repository.SONATYPE_SNAPSHOTS_OLD)
getJavaVersionForMC(version)?.let(::setJavaVersion)
val semanticVersion = SemanticVersion(version)
if (semanticVersion.major <= 1 && semanticVersion.minor <= 15) repository(Repository.SONATYPE_SNAPSHOTS_OLD)
repository(Repository.MAVEN_CENTRAL, Repository.SPIGOT)
// Dependency
return "org.spigotmc:spigot-api:${getVersionString(versionString)}"
return addDependencyTo(dependencies, configuration, "org.spigotmc:spigot-api:${getVersionString(version)}", configurationAction)
}

/**
* 1. Adds the [Repository.MAVEN_CENTRAL], maven local, and [Repository.SPIGOT] repositories
* 2. Returns the dependency annotation (org.spigotmc:spigot:[getVersionString])
* 2. Adds the Spigot dependency (org.spigotmc:spigot:[getVersionString]) using the [configuration] and [configurationAction]
*
* @param versionString The version of Spigot to use
* @param version The version of Spigot to use
* @param configuration The configuration to add the dependency to
* @param configurationAction The action to apply to the dependency
*
* @return The dependency annotation for the provided Spigot version
* @return The [ExternalModuleDependency] of the added Spigot dependency
*/
@Ignore
fun Project.spigotNMS(versionString: String): String {
fun Project.spigotNMS(
version: String,
configuration: String = "compileOnly",
configurationAction: Action<ExternalModuleDependency> = Action {}
): ExternalModuleDependency {
check(hasJavaPlugin()) { "Java plugin is not applied!" }
// Java version
getJavaVersionForMC(versionString)?.let(::setJavaVersion)
// Repositories
getJavaVersionForMC(version)?.let(::setJavaVersion)
repository(Repository.MAVEN_CENTRAL, Repository.SPIGOT)
repositories.mavenLocal()
// Dependency
return "org.spigotmc:spigot:${getVersionString(versionString)}"
return addDependencyTo(dependencies, configuration, "org.spigotmc:spigot:${getVersionString(version)}", configurationAction)
}

/**
* 1. Adds the [Repository.MAVEN_CENTRAL], [Repository.SONATYPE_SNAPSHOTS_OLD], and [Repository.PAPER] repositories
* 2. Returns the dependency annotation ([PaperVersion.groupId]:[PaperVersion.artifactId]:[version]-R0.1-SNAPSHOT)
* 2. Adds the Paper dependency ([PaperVersion.groupId]:[PaperVersion.artifactId]:[version]-R0.1-SNAPSHOT) using the [configuration] and [configurationAction]
*
* @param version The version of Paper to use
* @param configuration The configuration to add the dependency to
* @param configurationAction The action to apply to the dependency
*
* @return The dependency annotation for the provided Paper version
* @return The [ExternalModuleDependency] of the added Paper dependency
*/
@Ignore
fun Project.paper(version: String): String {
fun Project.paper(
version: String,
configuration: String = "compileOnly",
configurationAction: Action<ExternalModuleDependency> = Action {}
): ExternalModuleDependency {
check(hasJavaPlugin()) { "Java plugin is not applied!" }
// Java version
getJavaVersionForMC(version)?.let(::setJavaVersion)
// Repositories
repository(Repository.MAVEN_CENTRAL, Repository.SONATYPE_SNAPSHOTS_OLD, Repository.PAPER)
// Dependency
val paperVersion: PaperVersion = PaperVersion.parse(version)
return "${paperVersion.groupId}:${paperVersion.artifactId}:${getVersionString(version)}"
return addDependencyTo(dependencies, configuration, "${paperVersion.groupId}:${paperVersion.artifactId}:${getVersionString(version)}", configurationAction)
}

/**
* 1. Adds the [Repository.JITPACK] repository
* 2. Returns the dependency to the provided Annoying API version
* 2. Adds the dependency to the provided Annoying API version
*
* @param version The version of Annoying API to use
* @param configuration The configuration to add the dependency to
* @param configurationAction The action to apply to the dependency
*
* @return The dependency annotation for the provided Annoying API version
* @return The [ExternalModuleDependency] of the added Annoying API dependency
*/
fun Project.annoyingAPI(version: String): String {
fun Project.annoyingAPI(
version: String,
configuration: String = "implementation",
configurationAction: Action<ExternalModuleDependency> = Action {}
): ExternalModuleDependency {
check(hasJavaPlugin()) { "Java plugin is not applied!" }
// Repositories
repository(Repository.JITPACK)
// Dependency
return "xyz.srnyx:annoying-api:$version"
if (hasShadowPlugin()) relocate("xyz.srnyx.annoyingapi")
return addDependencyTo(dependencies, configuration, "xyz.srnyx:annoying-api:$version") {
exclude("org.bstats", "bstats-bukkit")
exclude("de.tr7zw", "item-nbt-api")
configurationAction.execute(this)
}
}

/**
* 1. Adds the provided dependency as an `implementation` dependency
* 2. Relocates the dependency to the provided package
*
* @param project The project to relocate the dependency for
* @param dependency The dependency to add
* @param relocateFrom The package to relocate from
* @param relocateTo The package to relocate to
* @param configuration The configuration to add the dependency to
*
* @return The [T] of the added dependency
*/
@Ignore
fun <T: ModuleDependency> DependencyHandler.implementationRelocate(
Expand All @@ -114,6 +141,14 @@ fun <T: ModuleDependency> DependencyHandler.implementationRelocate(
/**
* 1. Adds the provided dependency as an `implementation` dependency
* 2. Relocates the dependency to the provided package
*
* @param project The project to relocate the dependency for
* @param dependency The dependency to add
* @param relocateFrom The package to relocate from
* @param relocateTo The package to relocate to
* @param configuration The configuration to add the dependency to
*
* @return The [ExternalModuleDependency] of the added dependency
*/
@Ignore
fun DependencyHandler.implementationRelocate(
Expand All @@ -130,6 +165,10 @@ fun DependencyHandler.implementationRelocate(

/**
* Returns the correct Java version that is required for the Minecraft version
*
* @param minecraftVersion The Minecraft version to get the Java version for
*
* @return The [JavaVersion] that is required for the Minecraft version
*/
fun getJavaVersionForMC(minecraftVersion: String): JavaVersion? {
val version = SemanticVersion(minecraftVersion)
Expand All @@ -139,5 +178,9 @@ fun getJavaVersionForMC(minecraftVersion: String): JavaVersion? {

/**
* Returns the version string with `-R0.1-SNAPSHOT` appended to it
*
* @param version The version to append to
*
* @return The version string with `-R0.1-SNAPSHOT` appended to it
*/
fun getVersionString(version: String): String = "${version}-R0.1-SNAPSHOT"
42 changes: 13 additions & 29 deletions src/main/kotlin/xyz/srnyx/gradlegalaxy/utility/Scripts.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package xyz.srnyx.gradlegalaxy.utility

import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar

import org.gradle.api.Action

import org.gradle.api.DefaultTask
import org.gradle.api.JavaVersion
import org.gradle.api.Project
Expand All @@ -15,14 +15,11 @@ 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 org.gradle.kotlin.dsl.accessors.runtime.addDependencyTo

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


/**
Expand Down Expand Up @@ -125,7 +122,7 @@ fun Project.addJavadocSourcesJars(javadocClassifier: String? = null, sourcesClas
}

/**
* Configures the ProcessResources [Task] to add replacements
* Configures the ProcessResources task to add replacements
*
* @param replacements A [Map] of all the replacements
*/
Expand Down Expand Up @@ -200,12 +197,11 @@ fun Project.setupJava(
/**
* Sets up the project with the specified [group] and [version] for a simple Minecraft project
*
* Calls [setupJava] with the specified parameters, as well as adding the [dependency] (`compileOnly`) and calling [addReplacementsTask]
* 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 dependency The dependency to add to the project (using `compileOnly`)
* @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]
Expand All @@ -216,59 +212,47 @@ fun Project.setupMC(
group: String = project.group.toString(),
version: String = project.version.toString(),
description: String? = project.description,
dependency: String? = null,
javaVersion: JavaVersion? = null,
replacements: Map<String, String>? = getSentinelReplacements(),
textEncoding: String? = "UTF-8",
archiveClassifier: String? = "",
configuration: Action<ExternalModuleDependency> = Action {}
): ExternalModuleDependency? {
) {
setupJava(group, version, description, javaVersion, textEncoding, archiveClassifier)
replacements?.let(::addReplacementsTask)
if (dependency != null) return addDependencyTo(project.dependencies, "compileOnly", dependency, configuration)
return null
}

/**
* Sets up the project using Annoying API
*
* 1. Checks if the Shadow plugin is applied
* 2. Calls [setupMC] with the specified parameters
* 3. Adds the [Repository.JITPACK] repository
* 4. Adds the Annoying API dependency (`implementation`)
* 5. Relocates Annoying API using [relocate]
* 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 dependency The dependency to add to the project (using `compileOnly`)
* @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 artifactClassifier The artifact classifier for the [shadow jar task][setShadowArchiveClassifier]
* @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,
dependency: String? = null,
javaVersion: JavaVersion? = null,
replacements: Map<String, String>? = getSentinelReplacements(),
textEncoding: String? = "UTF-8",
artifactClassifier: String? = "",
configuration: AnnoyingAPIReturn.() -> Unit = {},
): AnnoyingAPIReturn {
archiveClassifier: String? = "",
configuration: String = "implementation",
configurationAction: Action<ExternalModuleDependency> = Action {}
): ExternalModuleDependency {
check(hasShadowPlugin()) { "Shadow plugin is required for Annoying API!" }
val annoyingAPIReturn = AnnoyingAPIReturn(
setupMC(group, version, description, dependency, javaVersion, replacements, textEncoding, artifactClassifier),
dependencies.implementationRelocate(project, annoyingAPI(annoyingAPIVersion), "xyz.srnyx.annoyingapi") {
exclude("org.bstats", "bstats-bukkit")
exclude("de.tr7zw", "item-nbt-api")
})
annoyingAPIReturn.configuration()
return annoyingAPIReturn
setupMC(group, version, description, javaVersion, replacements, textEncoding, archiveClassifier)
return annoyingAPI(annoyingAPIVersion, configuration, configurationAction)
}

/**
Expand Down

0 comments on commit 0ac9bac

Please sign in to comment.