BasePlugin.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.base
019 
020 import groovy.transform.CompileStatic
021 import org.gradle.BuildAdapter
022 import org.gradle.api.Action
023 import org.gradle.api.GradleException
024 import org.gradle.api.Project
025 import org.gradle.api.invocation.Gradle
026 import org.kordamp.gradle.PluginUtils
027 import org.kordamp.gradle.plugin.AbstractKordampPlugin
028 import org.kordamp.gradle.plugin.base.tasks.EffectiveSettingsTask
029 import org.kordamp.gradle.plugin.base.tasks.ExtensionsTask
030 import org.kordamp.gradle.plugin.base.tasks.PluginsTask
031 import org.kordamp.gradle.plugin.base.tasks.ProjectPropertiesTask
032 import org.kordamp.gradle.plugin.base.tasks.RepositoriesTask
033 
034 /**
035  *
036  @author Andres Almiray
037  @since 0.1.0
038  */
039 @CompileStatic
040 class BasePlugin extends AbstractKordampPlugin {
041     static final String ORG_KORDAMP_GRADLE_BASE_VALIDATE = 'org.kordamp.gradle.base.validate'
042 
043     Project project
044 
045     void apply(Project project) {
046         this.project = project
047 
048         if (hasBeenVisited(project)) {
049             return
050         }
051         setVisited(project, true)
052 
053         if (!project.plugins.findPlugin(org.gradle.api.plugins.BasePlugin)) {
054             project.plugins.apply(org.gradle.api.plugins.BasePlugin)
055         }
056 
057         if (!project.extensions.findByType(ProjectConfigurationExtension)) {
058             project.extensions.create(ProjectConfigurationExtension.CONFIG_NAME, ProjectConfigurationExtension, project)
059         }
060 
061         project.tasks.register('effectiveSettings', EffectiveSettingsTask,
062             new Action<EffectiveSettingsTask>() {
063                 @Override
064                 void execute(EffectiveSettingsTask t) {
065                     t.group = 'Insight'
066                     t.description = "Displays resolved settings for project '$project.name'"
067                 }
068             })
069 
070         project.tasks.register('repositories', RepositoriesTask,
071             new Action<RepositoriesTask>() {
072                 @Override
073                 void execute(RepositoriesTask t) {
074                     t.group = 'Insight'
075                     t.description = "Displays all repositories for project '$project.name'"
076                 }
077             })
078 
079         project.tasks.register('plugins', PluginsTask,
080             new Action<PluginsTask>() {
081                 @Override
082                 void execute(PluginsTask t) {
083                     t.group = 'Insight'
084                     t.description = "Displays all plugins applied to project '$project.name'"
085                 }
086             })
087 
088         project.tasks.register('extensions', ExtensionsTask,
089             new Action<ExtensionsTask>() {
090                 @Override
091                 void execute(ExtensionsTask t) {
092                     t.group = 'Insight'
093                     t.description = "Displays all extensions applied to project '$project.name'"
094                 }
095             })
096 
097         project.tasks.register('projectProperties', ProjectPropertiesTask,
098             new Action<ProjectPropertiesTask>() {
099                 @Override
100                 void execute(ProjectPropertiesTask t) {
101                     t.group = 'Insight'
102                     t.description = "Displays all properties found in project '$project.name'"
103                 }
104             })
105 
106         if (isRootProject(project)) {
107             project.gradle.addBuildListener(new BuildAdapter() {
108                 @Override
109                 void projectsEvaluated(Gradle gradle) {
110                     project.subprojects.each Project subproject ->
111                         PluginUtils.resolveEffectiveConfig(subproject).rootReady()
112                     }
113                     PluginUtils.resolveEffectiveConfig(project).rootReady()
114                 }
115             })
116         }
117 
118         project.afterEvaluate {
119             ProjectConfigurationExtension rootExtension = project.rootProject.extensions.findByType(ProjectConfigurationExtension)
120             ProjectConfigurationExtension extension = project.extensions.findByType(ProjectConfigurationExtension)
121             extension.normalize()
122 
123             boolean validate = PluginUtils.checkFlag(ORG_KORDAMP_GRADLE_BASE_VALIDATE, true)
124 
125             List<String> errors = []
126             if (isRootProject(project)) {
127                 ProjectConfigurationExtension merged = extension.merge(rootExtension)
128                 if (validateerrors.addAll(merged.validate())
129                 project.extensions.create(ProjectConfigurationExtension.EFFECTIVE_CONFIG_NAME, ProjectConfigurationExtension, merged).ready()
130             else {
131                 if (rootExtension) {
132                     ProjectConfigurationExtension merged = extension.merge(rootExtension)
133                     if (validateerrors.addAll(merged.validate())
134                     project.extensions.create(ProjectConfigurationExtension.EFFECTIVE_CONFIG_NAME, ProjectConfigurationExtension, merged).ready()
135                 else {
136                     if (validateerrors.addAll(extension.validate())
137                     extension.postMerge()
138                     project.extensions.create(ProjectConfigurationExtension.EFFECTIVE_CONFIG_NAME, ProjectConfigurationExtension, extension).ready()
139                 }
140             }
141 
142             if (validate && errors) {
143                 errors.each project.logger.error(it) }
144                 throw new GradleException("Project ${project.name} has not been properly configured")
145             }
146         }
147     }
148 
149     static void applyIfMissing(Project project) {
150         if (!project.plugins.findPlugin(BasePlugin)) {
151             project.plugins.apply(BasePlugin)
152         }
153     }
154 
155     static boolean isRootProject(Project project) {
156         project == project.rootProject
157     }
158 }