PluginPlugin.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  *     https://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.plugin
019 
020 import com.gradle.publish.PluginBundleExtension
021 import com.gradle.publish.PluginConfig
022 import com.gradle.publish.PublishPlugin
023 import com.jfrog.bintray.gradle.BintrayExtension
024 import groovy.transform.CompileDynamic
025 import groovy.transform.CompileStatic
026 import org.gradle.api.Action
027 import org.gradle.api.Project
028 import org.gradle.api.publish.maven.MavenPublication
029 import org.gradle.plugin.devel.GradlePluginDevelopmentExtension
030 import org.gradle.plugin.devel.plugins.JavaGradlePluginPlugin
031 import org.kordamp.gradle.plugin.AbstractKordampPlugin
032 import org.kordamp.gradle.plugin.base.BasePlugin
033 import org.kordamp.gradle.plugin.base.ProjectConfigurationExtension
034 import org.kordamp.gradle.plugin.base.plugins.util.PublishingUtils
035 
036 import static org.kordamp.gradle.PluginUtils.resolveConfig
037 import static org.kordamp.gradle.PluginUtils.resolveEffectiveConfig
038 import static org.kordamp.gradle.StringUtils.isBlank
039 import static org.kordamp.gradle.plugin.base.BasePlugin.isRootProject
040 
041 /**
042  @author Andres Almiray
043  @since 0.16.0
044  */
045 @CompileStatic
046 class PluginPlugin extends AbstractKordampPlugin {
047     Project project
048 
049     void apply(Project project) {
050         this.project = project
051 
052         if (isRootProject(project)) {
053             project.childProjects.values().each {
054                 configureProject(it)
055             }
056         }
057         configureProject(project)
058     }
059 
060     private void configureProject(Project project) {
061         if (hasBeenVisited(project)) {
062             return
063         }
064         setVisited(project, true)
065 
066         BasePlugin.applyIfMissing(project)
067         project.pluginManager.apply(JavaGradlePluginPlugin)
068         project.pluginManager.apply(PublishPlugin)
069 
070         String pluginName = resolveConfig(project).plugin.pluginName
071 
072         GradlePluginDevelopmentExtension gpde = project.extensions.findByType(GradlePluginDevelopmentExtension)
073         project.tasks.register('listPluginDescriptors', ListPluginDescriptors.class,
074             new Action<ListPluginDescriptors>() {
075                 void execute(ListPluginDescriptors t) {
076                     t.group = 'Plugin development'
077                     t.description = 'Lists plugin descriptors from plugin declarations.'
078                     t.declarations.set(gpde.plugins)
079                 }
080             })
081 
082         PluginBundleExtension pbe = project.extensions.findByType(PluginBundleExtension)
083         PluginConfig pc = pbe.plugins.maybeCreate(pluginName)
084 
085         project.afterEvaluate {
086             ProjectConfigurationExtension effectiveConfig = resolveEffectiveConfig(project)
087 
088             if (isBlank(pbe.website)) {
089                 pbe.website = effectiveConfig.info.url
090             }
091             if (isBlank(pbe.vcsUrl)) {
092                 pbe.vcsUrl = effectiveConfig.info.resolveScmLink()
093             }
094             if (isBlank(pbe.description)) {
095                 pbe.description = effectiveConfig.info.description
096             }
097             if (!pbe.tags) {
098                 pbe.tags = effectiveConfig.plugin.resolveTags(effectiveConfig)
099             }
100 
101             if (isBlank(pbe.mavenCoordinates.groupId)) {
102                 pbe.mavenCoordinates.groupId = project.group
103             }
104             if (isBlank(pbe.mavenCoordinates.artifactId)) {
105                 pbe.mavenCoordinates.artifactId = project.name
106             }
107 
108             if (isBlank(pbe.mavenCoordinates.version)) {
109                 pbe.mavenCoordinates.version = project.version
110             }
111 
112             if (isBlank(pc.id)) {
113                 pc.id = effectiveConfig.plugin.id
114             }
115             if (isBlank(pc.displayName)) {
116                 pc.displayName = effectiveConfig.info.name
117             }
118 
119             BintrayExtension bintray = project.extensions.findByType(BintrayExtension)
120             if (bintray) {
121                 bintray.pkg.version.attributes = ['gradle-plugin': "${effectiveConfig.plugin.id}:${project.group}:${project.name}".toString()]
122             }
123 
124             updatePublication(project)
125         }
126     }
127 
128     @CompileDynamic
129     private void updatePublication(Project project) {
130         ProjectConfigurationExtension effectiveConfig = resolveEffectiveConfig(project)
131         String pluginName = effectiveConfig.plugin.pluginName
132 
133         project.publishing {
134             publications {
135                 pluginMaven(MavenPublication) {
136                     PublishingUtils.configurePom(pom, effectiveConfig, effectiveConfig.publishing.pom)
137                 }
138                 "${pluginName}PluginMarkerMaven"(MavenPublication) {
139                     PublishingUtils.configurePom(pom, effectiveConfig, effectiveConfig.publishing.pom)
140                 }
141             }
142         }
143 
144         PublishingUtils.configureSigning(effectiveConfig, project, 'pluginMaven', pluginName + 'PluginMarkerMaven')
145     }
146 }