Add: Custom Gradle Plugin in buildSrc

This commit is contained in:
33 committed 2022-07-19 20:40:53 +08:00
1 parent b2f8d2cc38
commit 187be47e88
15 files changed
+285 -211

No files matched your search

-154
View File
@@ -1,154 +0,0 @@
/**
* Gradle Script for COS
* It can be used to upload or download file from Tencent Cloud COS.
* COS: https://cloud.tencent.com/document/product/436
*/
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.qcloud:cos_api:5.4.6'
}
}
import com.qcloud.cos.COSClient
import com.qcloud.cos.ClientConfig
import com.qcloud.cos.auth.BasicCOSCredentials
import com.qcloud.cos.auth.COSCredentials
import com.qcloud.cos.model.PutObjectRequest
import com.qcloud.cos.model.GetObjectRequest
import com.qcloud.cos.region.Region
String getMcVersion(String filename) {
if (filename.endsWith(".json"))
return filename.substring(0, filename.indexOf('-'))
return filename.substring(filename.indexOf('_') + 1, filename.indexOf('-'))
}
String getKey(String filename) {
String name = filename.substring(0, filename.lastIndexOf('.'))
if (name.indexOf('-') == -1) return null
if (filename.endsWith(".json"))
return String.format(
"versions/%s/%s",
name,
filename
)
if (filename.endsWith(".jar")) {
if (filename.contains("Fabric") || filename.contains("Forge")) {
return String.format(
"mods/%s",
filename
)
} else {
return String.format(
"libraries/customskinloader/%s/%s/%s",
name.substring(0, name.indexOf('-')),
name.substring(name.indexOf('-') + 1),
filename
)
}
}
return filename
}
ext.bucketName = System.getenv("COS_BUCKET")
ext.cosUrl = "https://csl.littleservice.cn/"
ext.cosClient0 = null
COSClient getCosClient() {
if (cosClient0 != null) return cosClient0
COSCredentials cred = new BasicCOSCredentials(System.getenv("COS_SECRET_ID"), System.getenv("COS_SECRET_KEY"))
ClientConfig clientConfig = new ClientConfig(new Region("ap-shanghai"))
cosClient0 = new COSClient(cred, clientConfig)
}
void uploadFile(String key, File file) {
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, file)
getCosClient().putObject(putObjectRequest)
}
void downloadFile(String key, File file) {
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key)
getCosClient().getObject(getObjectRequest, file)
}
String toJson(Map<String, Object> map, int d) {
StringBuilder sb = new StringBuilder("{\n")
Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator()
while (it.hasNext()) {
Map.Entry<String, Object> e = it.next()
String k = e.key
String v = null
if (e.value instanceof String) {
v = "\"" + e.value + "\""
} else if (e.value instanceof Map) {
v = toJson((Map) e.value, d + 1)
}
for (int i = 0; i < d * 2; i++) {
sb.append(' ')
}
if (it.hasNext()) {
sb.append(String.format("\"%s\" : %s,\n", k, v))
} else {
sb.append(String.format("\"%s\" : %s\n", k, v))
}
}
for (int i = 0; i < (d - 1) * 2; i++) {
sb.append(' ')
}
sb.append("}")
return sb.toString()
}
String toJson(Map<String, Object> map) {
return toJson(map, 1)
}
void writeToFile(File f, String s) {
FileWriter fw = new FileWriter(f)
fw.write(s)
fw.flush()
fw.close()
}
void uploadArtifacts(String shortVersion) {
File dir = rootProject.file("build/libs")
if (!dir.isDirectory()) return
String cslversion = shortVersion.replace(".", "")
LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>()
LinkedHashMap<String, String> map2 = new LinkedHashMap<String, String>()
LinkedHashMap<String, String> map3 = new LinkedHashMap<String, String>()
map.put("version", shortVersion)
map.put("downloads", map2)
map.put("launchermeta", map3)
File[] files = dir.listFiles()
for (file in files) {
String key = getKey(file.getName())
if (key == null) continue
uploadFile(key, file)
String url = cosUrl + key
String mcversion = getMcVersion(file.getName())
println(String.format("csl-%s-%s\t%s", mcversion.replace(".", "").toLowerCase(), cslversion, url))
if (key.startsWith("mods/") && key.endsWith(".jar") && !key.endsWith("-sources.jar")) {
map2.put(mcversion, url)
} else if (key.endsWith(".json")) {
map3.put(mcversion, url)
}
}
File latest = new File("build/libs/latest.json")
map.get("downloads").put("Forge", map.get("downloads").remove("ForgeLegacy"))
writeToFile(latest, toJson(map))
uploadFile("latest.json", latest)
}
ext {
uploadArtifacts = this.&uploadArtifacts
}
+3 -2
View File
@@ -13,6 +13,7 @@ buildscript {
import com.matthewprenger.cursegradle.CurseArtifact
import com.matthewprenger.cursegradle.CurseGradlePlugin
import com.matthewprenger.cursegradle.CurseUploadTask
import customskinloader.gradle.util.VersionUtil
// https://www.curseforge.com/minecraft/mc-mods/customskinloader
static void provideCurseForge0(Project proj) {
@@ -35,8 +36,8 @@ static void provideCurseForge0(Project proj) {
gameVersionStrings += proj.name.split("/")[0] // Loader Version
changelogType = "text"
changelog = System.getenv("GIT_COMMIT_DESC") ?: ""
displayName = "${proj.archivesBaseName}-${proj.ext.getShortVersion()}"
releaseType = proj.ext.isSnapshot() ? "beta" : "release"
displayName = "${proj.archivesBaseName}-${VersionUtil.getShortVersion(proj.rootProject)}"
releaseType = VersionUtil.isSnapshot(proj.rootProject) ? "beta" : "release"
it
}
additionalArtifacts = []
+4 -3
View File
@@ -12,6 +12,7 @@ buildscript {
import com.modrinth.minotaur.TaskModrinthUpload
import com.modrinth.minotaur.request.VersionType
import customskinloader.gradle.util.VersionUtil
// https://modrinth.com/mod/customskinloader
static void provideModrinth0(Project proj) {
@@ -22,11 +23,11 @@ static void provideModrinth0(Project proj) {
token = System.getenv("MODRINTH_TOKEN")
projectId = "idMHQ4n2"
versionNumber = "${proj.ext.getCSLVersion()}-${proj.name.replace("/", ".")}"
versionName = "${proj.archivesBaseName}-${proj.ext.getShortVersion()}"
versionNumber = "${VersionUtil.getCSLVersion(proj.rootProject)}-${proj.name.replace("/", ".")}"
versionName = "${proj.archivesBaseName}-${VersionUtil.getShortVersion(proj.rootProject)}"
changelog = System.getenv("GIT_COMMIT_DESC") ?: ""
uploadFile = proj.tasks.jar
versionType = proj.ext.isSnapshot() ? VersionType.BETA : VersionType.RELEASE
versionType = VersionUtil.isSnapshot(proj.rootProject) ? VersionType.BETA : VersionType.RELEASE
failSilently = true
detectLoaders = false
-37
View File
@@ -1,37 +0,0 @@
/**
* Gradle Script for Version
* Format Version String
*/
boolean isRelease() {
String s = rootProject.ext.config.snapshot
return s == null || s == "" || s == "0"
}
boolean isSnapshot() {
return !isRelease()
}
String getBuildNum() {
if (System.getenv("CIRCLE_BUILD_NUM") != null)
return System.getenv("CIRCLE_BUILD_NUM")
return "00"
}
//Example: 14.10a-SNAPSHOT-33
String getCSLVersion() {
return rootProject.ext.config.version + rootProject.ext.config.dev_version +
(isRelease() ? '' : ('-SNAPSHOT-' + getBuildNum()))
}
//Example: 14.10a-s33
String getShortVersion() {
return getCSLVersion().replace("SNAPSHOT-", "s")
}
ext {
isSnapshot = this.&isSnapshot
getBuildNum = this.&getBuildNum
getCSLVersion = this.&getCSLVersion
getShortVersion = this.&getShortVersion
}