mirror of
https://github.com/xfl03/MCCustomSkinLoader.git
synced 2026-11-27 20:13:04 +00:00
Add: Custom Gradle Plugin in buildSrc
This commit is contained in:
1 parent
b2f8d2cc38
commit
187be47e88
15 files changed
+285
-211
No files matched your search
@@ -0,0 +1,14 @@
|
||||
package customskinloader.gradle;
|
||||
|
||||
import customskinloader.gradle.task.UploadTask;
|
||||
import org.gradle.api.Plugin;
|
||||
import org.gradle.api.Project;
|
||||
|
||||
public class BuildPlugin implements Plugin<Project> {
|
||||
@Override
|
||||
public void apply(Project project) {
|
||||
System.out.printf("Apply project '%s'", project.getName());
|
||||
project.getTasks().create("upload", UploadTask.class,
|
||||
task -> task.rootProject = project.getRootProject());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package customskinloader.gradle.task;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import customskinloader.gradle.util.CosUtil;
|
||||
import customskinloader.gradle.util.VersionUtil;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.gradle.api.DefaultTask;
|
||||
import org.gradle.api.Project;
|
||||
import org.gradle.api.tasks.TaskAction;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.LinkedHashMap;
|
||||
|
||||
public class UploadTask extends DefaultTask {
|
||||
public Project rootProject;
|
||||
|
||||
private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
|
||||
private void uploadArtifacts() throws IOException {
|
||||
String shortVersion = VersionUtil.getShortVersion(rootProject);
|
||||
File dir = rootProject.file("build/libs");
|
||||
if (!dir.isDirectory()) {
|
||||
return;
|
||||
}
|
||||
|
||||
String cslversion = shortVersion.replace(".", "");
|
||||
LinkedHashMap<String, Object> map = new LinkedHashMap<>();
|
||||
LinkedHashMap<String, String> map2 = new LinkedHashMap<>();
|
||||
LinkedHashMap<String, String> map3 = new LinkedHashMap<>();
|
||||
map.put("version", shortVersion);
|
||||
map.put("downloads", map2);
|
||||
map.put("launchermeta", map3);
|
||||
|
||||
File[] files = dir.listFiles();
|
||||
if (files == null) {
|
||||
return;
|
||||
}
|
||||
for (File file : files) {
|
||||
String key = CosUtil.getKey(file.getName());
|
||||
if (key == null) {
|
||||
continue;
|
||||
}
|
||||
CosUtil.uploadFile(key, file);
|
||||
String url = CosUtil.cosUrl + key;
|
||||
String mcversion = VersionUtil.getMcVersion(file.getName());
|
||||
if (mcversion.equals("ForgeLegacy")) {
|
||||
mcversion = "Forge";
|
||||
}
|
||||
System.out.printf("csl-%s-%s\t%s%n",
|
||||
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");
|
||||
FileUtils.write(latest, gson.toJson(map), StandardCharsets.UTF_8);
|
||||
CosUtil.uploadFile("latest.json", latest);
|
||||
}
|
||||
|
||||
@TaskAction
|
||||
public void upload() throws IOException {
|
||||
if (System.getenv("COS_SECRET_KEY") != null) {
|
||||
uploadArtifacts();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package customskinloader.gradle.util;
|
||||
|
||||
import groovy.util.ConfigObject;
|
||||
import org.gradle.api.Project;
|
||||
|
||||
public class ConfigUtil {
|
||||
public static ConfigObject getConfig(Project project) {
|
||||
if (!project.getExtensions().getExtraProperties().has("config")) {
|
||||
System.out.printf("Config not found in '%s'", project.getName());
|
||||
return new ConfigObject();
|
||||
}
|
||||
Object o = project.getExtensions().getExtraProperties().get("config");
|
||||
if (!(o instanceof ConfigObject)) {
|
||||
System.out.printf("Config in '%s' is not ConfigObject", project.getName());
|
||||
return new ConfigObject();
|
||||
}
|
||||
return (ConfigObject) o;
|
||||
}
|
||||
|
||||
public static String getConfigString(Project project, String key) {
|
||||
ConfigObject config = getConfig(project);
|
||||
Object o = config.get(key);
|
||||
if (o == null) {
|
||||
System.out.printf("Config '%s' not found in '%s'", key, project.getName());
|
||||
return null;
|
||||
}
|
||||
if (!(o instanceof String)) {
|
||||
System.out.printf("Config '%s' in '%s' is not String", key, project.getName());
|
||||
return null;
|
||||
}
|
||||
return (String) config.getProperty(key);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package customskinloader.gradle.util;
|
||||
|
||||
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.GetObjectRequest;
|
||||
import com.qcloud.cos.model.PutObjectRequest;
|
||||
import com.qcloud.cos.region.Region;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class CosUtil {
|
||||
public static 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;
|
||||
}
|
||||
private static final String bucketName = System.getenv("COS_BUCKET");
|
||||
public static final String cosUrl = "https://csl.littleservice.cn/";
|
||||
private static COSClient cosClient0 = null;
|
||||
|
||||
private static 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);
|
||||
|
||||
return cosClient0;
|
||||
}
|
||||
|
||||
public static void uploadFile(String key, File file) {
|
||||
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, file);
|
||||
getCosClient().putObject(putObjectRequest);
|
||||
}
|
||||
|
||||
public static void downloadFile(String key, File file) {
|
||||
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
|
||||
getCosClient().getObject(getObjectRequest, file);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package customskinloader.gradle.util;
|
||||
|
||||
import org.gradle.api.Project;
|
||||
import org.gradle.util.VersionNumber;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class VersionUtil {
|
||||
public static boolean isRelease(Project rootProject) {
|
||||
String s = ConfigUtil.getConfigString(rootProject, "snapshot");
|
||||
return s == null || s.equals("") || s.equals("0");
|
||||
}
|
||||
|
||||
public static boolean isSnapshot(Project rootProject) {
|
||||
return !isRelease(rootProject);
|
||||
}
|
||||
|
||||
public static String getBuildNum() {
|
||||
if (System.getenv("CIRCLE_BUILD_NUM") != null) {
|
||||
return System.getenv("CIRCLE_BUILD_NUM");
|
||||
}
|
||||
return "00";
|
||||
}
|
||||
|
||||
//Example: 14.10a-SNAPSHOT-33
|
||||
public static String getCSLVersion(Project rootProject) {
|
||||
|
||||
return ConfigUtil.getConfigString(rootProject, "version") +
|
||||
ConfigUtil.getConfigString(rootProject, "dev_version") +
|
||||
(isRelease(rootProject) ? "" : ("-SNAPSHOT-" + getBuildNum()));
|
||||
}
|
||||
|
||||
//Example: 14.10a-s33
|
||||
public static String getShortVersion(Project rootProject) {
|
||||
return getCSLVersion(rootProject).replace("SNAPSHOT-", "s");
|
||||
}
|
||||
|
||||
public static String getMcVersion(String filename) {
|
||||
if (filename.endsWith(".json")) {
|
||||
return filename.substring(0, filename.indexOf('-'));
|
||||
}
|
||||
return filename.substring(filename.indexOf('_') + 1, filename.indexOf('-'));
|
||||
}
|
||||
|
||||
public static Collection<String> getMcMajorVersions(String version) {
|
||||
return Arrays.stream(version.split(","))
|
||||
.map(VersionNumber::parse)
|
||||
.map(it -> String.format("%s.%s", it.getMajor(), it.getMinor()))
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user