diff --git a/BuildScripts/cos.gradle b/BuildScripts/cos.gradle index 18a270c..3abf1fa 100644 --- a/BuildScripts/cos.gradle +++ b/BuildScripts/cos.gradle @@ -20,6 +20,12 @@ import com.qcloud.cos.auth.COSCredentials import com.qcloud.cos.model.PutObjectRequest 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('_'), filename.indexOf('-')) +} + String getKey(String filename) { String name = filename.substring(0, filename.lastIndexOf('.')) if (filename.endsWith(".json")) @@ -38,20 +44,45 @@ String getKey(String filename) { return filename } -void uploadFileToCOS(String key, File file) { +String bucketName = System.getenv("COS_BUCKET") +String cosUrl = "https://csl.littleservice.cn/" +COSClient cosClient = null + +COSClient getCosClient() { + if (cosClient != null) return cosClient + COSCredentials cred = new BasicCOSCredentials(System.getenv("COS_SECRET_ID"), System.getenv("COS_SECRET_KEY")) ClientConfig clientConfig = new ClientConfig(new Region("ap-shanghai")) - COSClient cosClient = new COSClient(cred, clientConfig) - String bucketName = System.getenv("COS_BUCKET") - PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, file) - cosClient.putObject(putObjectRequest) - println("Uploaded to COS: " + key) + cosClient = new COSClient(cred, clientConfig) } -void uploadFileToCOS(File file) { - uploadFileToCOS(getKey(file.getName()), file) +void uploadFile(String key, File file) { + //COSCredentials cred = new BasicCOSCredentials(System.getenv("COS_SECRET_ID"), System.getenv("COS_SECRET_KEY")) + //ClientConfig clientConfig = new ClientConfig(new Region("ap-shanghai")) + //COSClient cosClient = new COSClient(cred, clientConfig) + + 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) +} + +void uploadArtifacts() { + File dir = rootProject.file("build/libs") + if (!dir.isDirectory()) return + File[] files = dir.listFiles() + for (file in files) { + String key = getKey(file.getName()) + uploadFile(key, file) + String url = cosUrl + key + String version = getMcVersion(file.getName()) + println(version + "\t" + url) + } } ext { - uploadFileToCOS = this.&uploadFileToCOS + uploadArtifacts = this.&uploadArtifacts } \ No newline at end of file diff --git a/BuildScripts/version.gradle b/BuildScripts/version.gradle index 9b4be51..638934a 100644 --- a/BuildScripts/version.gradle +++ b/BuildScripts/version.gradle @@ -18,15 +18,15 @@ String getBuildNum() { return "00" } -//Example: 14.4a-SNAPSHOT-20160820 +//Example: 14.10a-SNAPSHOT-33 String getCSLVersion() { return rootProject.ext.config.version + rootProject.ext.config.dev_version + (isRelease() ? '' : ('-SNAPSHOT-' + getBuildNum())) } -//Example: 14.4a-S-20160820 +//Example: 14.10a-s33 String getShortVersion() { - return getVersion().replace("SNAPSHOT", "S") + return getVersion().replace("SNAPSHOT-", "s") } ext { diff --git a/build.gradle b/build.gradle index f049ae0..d66c1cf 100644 --- a/build.gradle +++ b/build.gradle @@ -28,12 +28,8 @@ subprojects { apply from: rootProject.file('BuildScripts/cos.gradle') task upload { + onlyIf { System.getenv("COS_SECRET_KEY") != null } doLast { - File dir = rootProject.file("build/libs") - if (!dir.isDirectory()) return - File[] files = dir.listFiles() - for (file in files) { - uploadFileToCOS(file) - } + uploadArtifacts() } } \ No newline at end of file