Add automatic uploading to gradle script

This commit is contained in:
Martin Prokoph
2023-12-29 12:18:14 +01:00
parent ab317f3d31
commit 2b143c6bf1
2 changed files with 37 additions and 122 deletions

View File

@@ -3,25 +3,14 @@ plugins {
id 'java-library' id 'java-library'
id 'maven-publish' id 'maven-publish'
id 'com.github.johnrengelman.shadow' version '7.0.0' id 'com.github.johnrengelman.shadow' version '7.0.0'
id 'com.modrinth.minotaur' version '1.2.+' id 'com.modrinth.minotaur' version '2.+'
id 'net.darkhax.curseforgegradle' version '1.+'
} }
import com.google.gson.GsonBuilder import net.darkhax.curseforgegradle.TaskPublishCurseForge
import com.google.gson.JsonObject
import com.modrinth.minotaur.TaskModrinthUpload
import com.modrinth.minotaur.request.VersionType
import com.modrinth.minotaur.responses.ResponseError
import org.apache.http.client.config.CookieSpecs
import org.apache.http.client.config.RequestConfig
import org.apache.http.client.entity.EntityBuilder
import org.apache.http.client.methods.HttpPatch
import org.apache.http.entity.ContentType
import org.apache.http.impl.client.HttpClientBuilder
import org.apache.http.util.EntityUtils
group = project.maven_group group = project.maven_group
version = "${project.mod_version}+${getMCVersionString()}" version = "${project.mod_version}+${getMCVersionString()}"
archivesBaseName = project.archives_base_name
// This field defines the Java version your mod target. // This field defines the Java version your mod target.
def targetJavaVersion = 17 def targetJavaVersion = 17
@@ -159,115 +148,39 @@ processResources {
expand 'version': project.version expand 'version': project.version
} }
} }
modrinth {
task publishModrinth(type: TaskModrinthUpload) { token = System.getenv("MODRINTH_TOKEN") // Remember to have the MODRINTH_TOKEN environment variable set or else this will fail - just make sure it stays private!
dependsOn(build) projectId = project.archives_base_name // This can be the project ID or the slug. Either will work!
onlyIf { versionNumber = project.version // You don't need to set this manually. Will fail if Modrinth has this version already
System.getenv('MODRINTH_TOKEN') versionName = "MidnightControls " + project.mod_version + " - " + project.minecraft_version
} versionType = isMCVersionNonRelease() ? "beta" : "release" // Can also be `beta` or `alpha`
uploadFile = remapJar // With Loom, this MUST be set to `remapJar` instead of `jar`!
token = System.getenv('MODRINTH_TOKEN') gameVersions = [(String) project.minecraft_version] // Must be an array, even with only one version
projectId = project.modrinth_id loaders = ["fabric","quilt"] // Must also be an array - no need to specify this if you're using Loom or ForgeGradle
versionNumber = version dependencies { // A special DSL for creating dependencies
versionName = "midnightcontrols ${project.mod_version} (${getMCVersionString()})" // scope.type
addGameVersion((String) project.minecraft_version) // The scope can be `required`, `optional`, `incompatible`, or `embedded`
addLoader('fabric') // The type can either be `project` or `version`
versionType = isMCVersionNonRelease() ? VersionType.BETA : VersionType.RELEASE required.project "midnightlib" // Creates a new required dependency on MidnightLib
// Changelog fetching
def changelogText = file('CHANGELOG.md').text
def regexVersion = ((String) project.mod_version).replaceAll('\\.', /\\./).replaceAll('\\+', '\\+')
def changelogRegex = ~"###? ${regexVersion}\\n\\n(( *- .+\\n)+)"
def matcher = changelogText =~ changelogRegex
if (matcher.find()) {
changelog = matcher.group(1)
def changelogLines = changelogText.split('\n')
def linkRefRegex = ~'^\\[([A-z0-9 _\\-/+.]+)]: '
for (int i = changelogLines.length - 1; i > 0; i--) {
def line = changelogLines[i]
if ((line =~ linkRefRegex).find())
changelog += '\n' + line
else break
}
}
// Readme
doFirst {
final def client = HttpClientBuilder.create().setDefaultRequestConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.IGNORE_COOKIES).build()).build()
final def patch = new HttpPatch((String) (apiURL + '/v1/mod/' + projectId))
patch.addHeader("Authorization", token)
var json = new JsonObject()
json.addProperty("body", parseReadme())
patch.setEntity(EntityBuilder.create()
.setText(json.toString())
.setContentType(ContentType.APPLICATION_JSON)
.build())
final def response = client.execute(patch)
final int status = response.getStatusLine().getStatusCode()
final def gson = new GsonBuilder().create()
if (status == 200) {
project.getLogger().lifecycle("Successfully updated readme to ${projectId}.")
} else {
errorInfo = gson.fromJson(EntityUtils.toString(response.getEntity()), ResponseError.class)
project.getLogger().error("Upload failed! Status: ${status} Error: ${errorInfo.getError()} Reason: ${errorInfo.getDescription()}")
throw new GradleException("Upload failed! Status: ${status} Reason: ${errorInfo.getDescription()}")
}
} }
changelog = project.changelog
} }
tasks.register('publishCurseForge', TaskPublishCurseForge) {
// configure the maven publication // This token is used to authenticate with CurseForge. It should be handled
publishing { // with the same level of care and security as your actual password. You
publications { // should never share your token with an untrusted source or publish it
mavenJava(MavenPublication) { // publicly to GitHub or embed it within a project. The best practice is to
artifact(sourcesJar) { // store this token in an environment variable or a build secret.
builtBy remapSourcesJar apiToken = System.getenv("CURSEFORGE_TOKEN")
}
pom { // Tells CurseForgeGradle to publish the output of the jar task. This will
name = 'midnightcontrols' // return a UploadArtifact object that can be used to further configure the
description = 'Adds better controls, and controller support.' // file.
} def mainFile = upload(project.curseforge_id, remapJar)
mainFile.changelog = project.changelog
pom.withXml { mainFile.displayName = "MidnightControls " + project.mod_version + " - " + project.minecraft_version
def dependenciesNode = asNode().appendNode('dependencies') mainFile.addModLoader("Fabric", "Quilt")
mainFile.addRequirement("midnightlib")
configurations.shadow.allDependencies.each { mainFile.releaseType = "release"
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
dependencyNode.appendNode('scope', 'compile')
}
}
}
}
repositories {
mavenLocal()
maven {
name 'GithubPackages'
url uri('https://maven.pkg.github.com/LambdAurora/midnightcontrols')
credentials {
username = project.findProperty("gpr.user") ?: System.getenv("USERNAME")
password = project.findProperty("gpr.key") ?: System.getenv("TOKEN")
}
}
def midnightcontrolsMaven = System.getenv('midnightcontrols_MAVEN')
if (midnightcontrolsMaven) {
maven {
name 'midnightcontrolsMaven'
url uri(midnightcontrolsMaven)
credentials {
username = project.findProperty('gpr.user') ?: System.getenv('MAVEN_USERNAME')
password = project.findProperty('gpr.key') ?: System.getenv('MAVEN_PASSWORD')
}
}
}
}
} }

View File

@@ -11,7 +11,9 @@ loader_version=0.15.3
mod_version = 1.9.1 mod_version = 1.9.1
maven_group = eu.midnightdust maven_group = eu.midnightdust
archives_base_name = midnightcontrols archives_base_name = midnightcontrols
modrinth_id=bXX9h73M modrinth_id = bXX9h73M
curseforge_id = 621768
changelog = See changes at: https://github.com/TeamMidnightDust/MidnightControls/commits/
# Dependencies # Dependencies
# currently not on the main fabric site, check on the maven: https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api # currently not on the main fabric site, check on the maven: https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api