/*
 * Copyright (c) 2017 Nam Nguyen, nam@ene.im
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

apply plugin: 'maven-publish'
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: "com.jfrog.artifactory"
apply plugin: 'com.jfrog.bintray'

group = releaseGroupId
version = releaseVersion

artifactory {
  contextUrl = "https://oss.jfrog.org/artifactory"
  //The base Artifactory URL if not overridden by the publisher/resolver
  publish {
    repository {
      repoKey = 'oss-snapshot-local'
      username = "${bintray_user}"
      password = "${bintray_apikey}"
      maven = true
    }
  }
  resolve {
    repository {
      repoKey = 'libs-snapshot'
      username = "${bintray_user}"
      password = "${bintray_apikey}"
      maven = true
    }
  }
}

artifactoryPublish.dependsOn('build', 'install')

task androidSourcesJar(type: Jar) {
  archiveClassifier.set("sources")
  from android.sourceSets.main.java.srcDirs
}

publishing {
  publications {
    aar(MavenPublication) {
      groupId = releaseGroupId
      artifactId = releaseArtifact
      version = releaseVersion
      // Tell maven to prepare the generated "*.aar" file for publishing
      artifact(androidSourcesJar)
      artifact("$buildDir/outputs/aar/${project.archivesBaseName}-release.aar")
    }
  }
}

artifactoryPublish {
  publications(publishing.publications.aar)
}

install {
  repositories.mavenInstaller {
    pom.project {
      packaging 'aar'
      groupId releaseGroupId
      artifactId releaseArtifact

      name releaseArtifact
      description releaseDescription
      url releaseWebsite

      licenses {
        license {
          name licenseName
          url licenseUrl
        }
      }

      developers {
        developer {
          id developerId
          name developerName
          email developerEmail
        }
      }

      scm {
        connection gitUrl
        developerConnection gitUrl
        url siteUrl
      }
    }
  }
}

task sourcesJar(type: Jar) {
  archiveClassifier.set("sources")
  from android.sourceSets.main.java.srcDirs
}

task javadoc(type: Javadoc) {
  source = android.sourceSets.main.java.srcDirs
  classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}

javadoc {
  // source configurations.doc.collect { zipTree(it) }
  include '**/*.java'
  options.addStringOption('Xdoclint:none', '-quiet')
}

task javadocJar(type: Jar, dependsOn: javadoc) {
  archiveClassifier.set("javadoc")
  from javadoc.destinationDir
}

artifacts {
  archives javadocJar
  archives sourcesJar
}

bintray {
  user = "${bintray_user}"
  key = "${bintray_apikey}"

  configurations = ['archives']
  pkg {
    repo = releaseRepoName
    userOrg = releaseUserOrg
    name = releaseArtifact
    desc = releaseDescription
    websiteUrl = siteUrl
    vcsUrl = gitUrl
    issueTrackerUrl = issueTracker
    githubReleaseNotesFile = 'CHANGELOG.md'
    licenses = allLicenses
    dryRun = false
    publish = true
    override = false
    publicDownloadNumbers = false
    version {
      desc = releaseDescription
    }
  }
}

bintrayUpload.dependsOn('install')