BuildScanPlugin.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.buildscan
019 
020 import com.gradle.scan.plugin.BuildScanExtension
021 import groovy.transform.CompileStatic
022 import org.gradle.api.Action
023 import org.gradle.api.Project
024 import org.kordamp.gradle.plugin.AbstractKordampPlugin
025 import org.kordamp.gradle.plugin.base.BasePlugin
026 import org.kordamp.gradle.plugin.base.ProjectConfigurationExtension
027 
028 import static org.kordamp.gradle.PluginUtils.resolveEffectiveConfig
029 import static org.kordamp.gradle.plugin.base.BasePlugin.isRootProject
030 
031 /**
032  @author Andres Almiray
033  @since 0.16.0
034  */
035 @CompileStatic
036 class BuildScanPlugin extends AbstractKordampPlugin {
037     public static final String BUILD_SCAN_AGREE = 'build.scan.agree'
038     Project project
039 
040     void apply(Project project) {
041         this.project = project
042 
043         if (isRootProject(project)) {
044             configureProject(project)
045         }
046     }
047 
048     private void configureProject(Project project) {
049         if (hasBeenVisited(project)) {
050             return
051         }
052         setVisited(project, true)
053 
054         BasePlugin.applyIfMissing(project)
055         project.plugins.apply(com.gradle.scan.plugin.BuildScanPlugin)
056 
057         project.tasks.register('setBuildScanAgreement', BuildScanAgreementTask,
058             new Action<BuildScanAgreementTask>() {
059                 @Override
060                 void execute(BuildScanAgreementTask t) {
061                     t.group = 'BuildScan'
062                     t.description = 'Sets the value of the build scan agreement.'
063                     t.remove = false
064                 }
065             })
066 
067         project.tasks.register('removeBuildScanAgreement', BuildScanAgreementTask,
068             new Action<BuildScanAgreementTask>() {
069                 @Override
070                 void execute(BuildScanAgreementTask t) {
071                     t.group = 'BuildScan'
072                     t.description = 'Removes the value of the build scan agreement.'
073                     t.remove = true
074                 }
075             })
076 
077 
078         project.tasks.register('listBuildScanAgreements', ListBuildScanAgreementTask,
079             new Action<ListBuildScanAgreementTask>() {
080                 @Override
081                 void execute(ListBuildScanAgreementTask t) {
082                     t.group = 'BuildScan'
083                     t.description = 'List the value of each build scan agreement.'
084                 }
085             })
086 
087         project.afterEvaluate {
088             configureBuildScan(project)
089         }
090     }
091 
092     private void configureBuildScan(Project project) {
093         BuildScanExtension buildScan = project.extensions.findByType(BuildScanExtension)
094         ProjectConfigurationExtension effectiveConfig = resolveEffectiveConfig(project)
095         setEnabled(effectiveConfig.buildScan.enabled)
096 
097         if (!buildScan.termsOfServiceUrl) {
098             buildScan.termsOfServiceUrl = 'https://gradle.com/terms-of-service'
099         }
100 
101         if (!enabled) {
102             buildScan.termsOfServiceAgree = 'no'
103             return
104         }
105 
106         // 1. check if explicit set
107         if (buildScan.termsOfServiceAgree) {
108             // done
109             return
110         }
111 
112         // 2. check if set via System prop
113         if (System.getProperty(BUILD_SCAN_AGREE)) {
114             String answer = System.getProperty(BUILD_SCAN_AGREE)
115             if ('yes'.equalsIgnoreCase(answer|| 'no'.equalsIgnoreCase(answer)) {
116                 buildScan.termsOfServiceAgree = answer
117             else {
118                 project.logger.error("Invalid value for System property '${BUILD_SCAN_AGREE}'\nExpecting 'yes' or 'no', found '$answer'")
119                 buildScan.termsOfServiceAgree = 'no'
120             }
121             // done
122             return
123         }
124 
125         // 3. check if set at project level
126         File scanFile = resolveProjectScanFile(project)
127         if (scanFile.exists()) {
128             String answer = scanFile.text.trim()
129             if ('yes'.equalsIgnoreCase(answer|| 'no'.equalsIgnoreCase(answer)) {
130                 buildScan.termsOfServiceAgree = answer
131             else {
132                 project.logger.error("Invalid content in ${scanFile.absolutePath}\nExpecting 'yes' or 'no', found '$answer'")
133                 buildScan.termsOfServiceAgree = 'no'
134             }
135 
136             // done
137             return
138         }
139 
140         // 4. check if set at global level
141         scanFile = resolveGlobalScanFile(project)
142         if (scanFile.exists()) {
143             String answer = scanFile.text.trim()
144             if ('yes'.equalsIgnoreCase(answer|| 'no'.equalsIgnoreCase(answer)) {
145                 buildScan.termsOfServiceAgree = answer
146             else {
147                 project.logger.error("Invalid content in ${scanFile.absolutePath}\nExpecting 'yes' or 'no', found '$answer'")
148                 buildScan.termsOfServiceAgree = 'no'
149             }
150 
151             // done
152             return
153         }
154 
155         // no explicit agreement
156         buildScan.termsOfServiceAgree = null
157     }
158 
159     static void applyIfMissing(Project project) {
160         if (!project.plugins.findPlugin(BuildScanPlugin)) {
161             project.plugins.apply(BuildScanPlugin)
162         }
163     }
164 
165     static File resolveProjectScanFile(Project project) {
166         new File("${project.projectDir}/.gradle/build-scan-agree.txt")
167     }
168 
169     static File resolveGlobalScanFile(Project project) {
170         new File("${project.gradle.gradleUserHomeDir}/build-scans/${project.projectDir.name}/build-scan-agree.txt")
171     }
172 }