1/* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.databinding; 18 19import org.gradle.api.DefaultTask 20import org.gradle.api.artifacts.maven.MavenPom 21import org.gradle.api.file.FileCollection 22import org.gradle.api.publication.maven.internal.ant.DefaultGroovyMavenDeployer 23import org.gradle.api.tasks.Input; 24import org.gradle.api.tasks.TaskAction; 25import groovy.json.JsonSlurper; 26 27public class UploadToBintrayTask extends DefaultTask { 28 static final String API_URL = "https://api.bintray.com"; 29 static final String PACKAGE_HEADER = "X-Bintray-Package" 30 static final String VERSION_HEADER = "X-Bintray-Version" 31 static final String PUBLISH_HEADER = "X-Bintray-Publish" 32 static final String OVERRIDE_HEADER = "X-Bintray-Override" 33 static final String EXPLODE_HEADER = "X-Bintray-Explode" 34 static final String CONTENT_PREFIX = "content/android/android-tools" 35 String username; 36 String apiKey; 37 String pkg; 38 String version; 39 FileCollection localFiles; 40 String mavenRepoAbsPath; 41 String targetPath; 42 public void configureFrom(DefaultGroovyMavenDeployer deployer) { 43 String repoUrl = deployer.repository.url 44 if (repoUrl == null) { 45 throw new RuntimeException("Cannot find repo url for $deployer") 46 } 47 48 def pom = deployer.pom 49 pkg = pom.groupId + "." + pom.artifactId 50 version = pom.version 51 mavenRepoAbsPath = repoUrl 52 targetPath = "${pkg.replaceAll("\\.", "/")}/${version}" 53 localFiles = project.fileTree(mavenRepoAbsPath + "/" + targetPath) 54 } 55 @TaskAction 56 public void upload() { 57 if (username == null || apiKey == null) { 58 throw new IllegalArgumentException("You should pass your bintray user and " + 59 "api key as params e.g. ./gradlew ${BintrayPlugin.DEFAULT_TASK_NAME}" + 60 " -P${BintrayPlugin.USER_PROP}=<my username>" + 61 " -P${BintrayPlugin.API_KEY_PROP}=<my api key>") 62 } 63 println(log()) 64 for (File localFile : localFiles) { 65 def p = ['curl', '-u', "$username:$apiKey", "-H", 66 "$PACKAGE_HEADER: $pkg", "-H", "$VERSION_HEADER: $version", 67 "-X", "PUT", "--data-binary", "@${localFile.getAbsolutePath()}", 68 "$API_URL/$CONTENT_PREFIX/$targetPath/${localFile.name}"] 69 println("executing $p") 70 def execute = p.execute() 71 execute.waitFor() 72 if (execute.exitValue() != 0) { 73 throw new RuntimeException("failed to upload artifact. error: ${execute.err.text}") 74 } 75 def responseText = execute.text 76 def json = new JsonSlurper().parseText(responseText) 77 if (json.getAt("message") != "success") { 78 throw new RuntimeException("Cannot upload artifact. Error response: " + 79 "${json.getAt("message")}") 80 } 81 println("uploaded $localFile") 82 } 83 } 84 85 public String log() { 86 return "UploadToBintrayTask{" + 87 "username='" + username + '\'' + 88 ", apiKey='" + apiKey + '\'' + 89 ", pkg='" + pkg + '\'' + 90 ", version='" + version + '\'' + 91 ", localFile=" + localFiles + 92 ", mavenRepo=" + mavenRepoAbsPath + 93 '}'; 94 } 95} 96