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