BintrayPlugin.groovy
001 /*
002  * SPDX-License-Identifier: Apache-2.0
003  *
004  * Copyright 2018-2019 Andres Almiray.
005  *
006  * Licensed under the Apache License, Version 2.0 (the "License");
007  * you may not use this file except in compliance with the License.
008  * You may obtain a copy of the License at
009  *
010  *     http://www.apache.org/licenses/LICENSE-2.0
011  *
012  * Unless required by applicable law or agreed to in writing, software
013  * distributed under the License is distributed on an "AS IS" BASIS,
014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015  * See the License for the specific language governing permissions and
016  * limitations under the License.
017  */
018 package org.kordamp.gradle.plugin.bintray
019 
020 import com.jfrog.bintray.gradle.BintrayExtension
021 import com.jfrog.bintray.gradle.tasks.BintrayUploadTask
022 import groovy.transform.CompileStatic
023 import org.gradle.api.Project
024 import org.gradle.api.plugins.JavaBasePlugin
025 import org.gradle.api.tasks.SourceSetContainer
026 import org.kordamp.gradle.plugin.AbstractKordampPlugin
027 import org.kordamp.gradle.plugin.base.ProjectConfigurationExtension
028 import org.kordamp.gradle.plugin.publishing.PublishingPlugin
029 
030 import static org.kordamp.gradle.PluginUtils.resolveEffectiveConfig
031 import static org.kordamp.gradle.PluginUtils.resolveSourceSets
032 import static org.kordamp.gradle.StringUtils.isBlank
033 import static org.kordamp.gradle.plugin.base.BasePlugin.isRootProject
034 
035 /**
036  * Configures artifact publication via Bintray.
037  *
038  @author Andres Almiray
039  @since 0.2.0
040  */
041 @CompileStatic
042 class BintrayPlugin extends AbstractKordampPlugin {
043     Project project
044 
045     void apply(Project project) {
046         this.project = project
047 
048         if (isRootProject(project)) {
049             if (project.childProjects.size()) {
050                 project.childProjects.values().each {
051                     configureProject(it)
052                 }
053             else {
054                 configureProject(project)
055             }
056         else {
057             configureProject(project)
058         }
059     }
060 
061     static void applyIfMissing(Project project) {
062         if (!project.plugins.findPlugin(BintrayPlugin)) {
063             project.plugins.apply(BintrayPlugin)
064         }
065     }
066 
067     private void configureProject(Project project) {
068         if (hasBeenVisited(project)) {
069             return
070         }
071         setVisited(project, true)
072 
073         PublishingPlugin.applyIfMissing(project)
074 
075         project.plugins.withType(JavaBasePlugin) {
076             if (!project.plugins.findPlugin(com.jfrog.bintray.gradle.BintrayPlugin)) {
077                 project.plugins.apply(com.jfrog.bintray.gradle.BintrayPlugin)
078             }
079         }
080 
081         project.afterEvaluate {
082             project.plugins.withType(JavaBasePlugin) {
083                 updatePublications(project)
084             }
085         }
086     }
087 
088     private void updatePublications(Project project) {
089         ProjectConfigurationExtension effectiveConfig = resolveEffectiveConfig(project)
090 
091         if (!effectiveConfig.bintray.enabled || !((SourceSetContainerresolveSourceSets(project)).findByName('main')) {
092             project.getTasks().findByName(BintrayUploadTask.TASK_NAME)?.enabled = false
093             setEnabled(false)
094             return
095         }
096 
097         BintrayExtension bintray = project.extensions.findByType(BintrayExtension)
098         if (isBlank(bintray.user)) bintray.user = effectiveConfig.bintray.credentials.username
099         if (isBlank(bintray.key)) bintray.key = effectiveConfig.bintray.credentials.password
100         if (!bintray.publicationsbintray.publications = effectiveConfig.bintray.resolvePublications().toArray(new String[0])
101         if (isBlank(bintray.pkg.repo)) bintray.pkg.repo = effectiveConfig.bintray.repo
102         if (isBlank(bintray.pkg.userOrg)) bintray.pkg.userOrg = effectiveConfig.bintray.userOrg
103         if (isBlank(bintray.pkg.name)) bintray.pkg.name = effectiveConfig.bintray.name
104         if (isBlank(bintray.pkg.desc)) bintray.pkg.desc = effectiveConfig.info.description
105         if (isBlank(bintray.pkg.websiteUrl)) bintray.pkg.websiteUrl = effectiveConfig.info.url
106         if (isBlank(bintray.pkg.issueTrackerUrl)) bintray.pkg.issueTrackerUrl = effectiveConfig.info.links.issueTracker
107         if (isBlank(bintray.pkg.vcsUrl)) bintray.pkg.vcsUrl = effectiveConfig.info.resolveScmLink()
108         if (!bintray.pkg.licensesbintray.pkg.licenses = effectiveConfig.licensing.resolveBintrayLicenseIds().toArray(new String[0])
109         if (!bintray.pkg.labelsbintray.pkg.labels = effectiveConfig.info.tags.toArray(new String[0])
110         bintray.pkg.publicDownloadNumbers = true // TODO: fin a way to conditionally set this value
111         if (isBlank(bintray.pkg.githubRepo)) bintray.pkg.githubRepo = effectiveConfig.bintray.githubRepo
112         if (isBlank(bintray.pkg.version.name)) bintray.pkg.version.name = project.version
113         if (isBlank(bintray.pkg.version.vcsTag)) bintray.pkg.version.vcsTag = "${effectiveConfig.bintray.name}-${project.version}".toString()
114         if (effectiveConfig.info.credentials.sonatype && !effectiveConfig.bintray.skipMavenSync) {
115             bintray.pkg.version.mavenCentralSync.with {
116                 sync = true
117                 user = effectiveConfig.info.credentials.sonatype.username
118                 password = effectiveConfig.info.credentials.sonatype.password
119             }
120         }
121     }
122 }