/** * 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 map, int d) { StringBuilder sb = new StringBuilder("{\n") Iterator> it = map.entrySet().iterator() while (it.hasNext()) { Map.Entry 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 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 map = new LinkedHashMap() LinkedHashMap map2 = new LinkedHashMap() LinkedHashMap map3 = new LinkedHashMap() 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.endsWith(".jar")) { map2.put(mcversion, url) } else if (key.endsWith(".json")) { map3.put(mcversion, url) } } File latest = new File("build/libs/latest.json") writeToFile(latest, toJson(map)) uploadFile("latest.json", latest) } ext { uploadArtifacts = this.&uploadArtifacts }