Banner.groovy
01 /*
02  * SPDX-License-Identifier: Apache-2.0
03  *
04  * Copyright 2019-2022 Andres Almiray.
05  *
06  * Licensed under the Apache License, Version 2.0 (the "License");
07  * you may not use this file except in compliance with the License.
08  * You may obtain a copy of the License at
09  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 package org.kordamp.gradle.plugin.oci
19 
20 import groovy.transform.CompileStatic
21 import org.gradle.BuildAdapter
22 import org.gradle.BuildResult
23 import org.gradle.api.Project
24 
25 import java.text.MessageFormat
26 
27 /**
28  *
29  @author Andres Almiray
30  @since 0.30.0
31  */
32 @CompileStatic
33 final class Banner {
34     private final ResourceBundle bundle = ResourceBundle.getBundle(Banner.name)
35     private final String productVersion = bundle.getString('product.version')
36     private final String productId = bundle.getString('product.id')
37     private final String productName = bundle.getString('product.name')
38     private final String banner = MessageFormat.format(bundle.getString('product.banner'), productName, productVersion)
39     private final List<String> visited = []
40     private static final String ORG_KORDAMP_BANNER = 'org.kordamp.banner'
41 
42     private static final Banner b = new Banner()
43 
44     private Banner() {
45         // noop
46     }
47 
48     static void display(Project project) {
49         if (b.visited.contains(project.rootProject.name)) {
50             return
51         }
52         b.visited.add(project.rootProject.name)
53         project.gradle.addBuildListener(new BuildAdapter() {
54             @Override
55             void buildFinished(BuildResult result) {
56                 b.visited.clear()
57             }
58         })
59 
60         boolean printBanner = null == System.getProperty(ORG_KORDAMP_BANNER|| Boolean.getBoolean(ORG_KORDAMP_BANNER)
61 
62         File parent = new File(project.gradle.gradleUserHomeDir, 'caches')
63         File markerFile = b.getMarkerFile(parent)
64         if (!markerFile.exists()) {
65             markerFile.parentFile.mkdirs()
66             markerFile.text = '1'
67             if (printBannerprintln(b.banner)
68         else {
69             try {
70                 int count = Integer.parseInt(markerFile.text)
71                 if (count < 3) {
72                     if (printBannerprintln(b.banner)
73                 }
74                 markerFile.text = (count + 1''
75             catch (NumberFormatException e) {
76                 markerFile.text = '1'
77                 if (printBannerprintln(b.banner)
78             }
79         }
80     }
81 
82     private File getMarkerFile(File parent) {
83         new File(parent,
84             'kordamp' +
85                 File.separator +
86                 productId +
87                 File.separator +
88                 productVersion +
89                 File.separator +
90                 'marker.txt')
91     }
92 }