diff --git a/plugins/base-gradle-plugin/src/main/groovy/org/kordamp/gradle/plugin/base/Banner.groovy b/plugins/base-gradle-plugin/src/main/groovy/org/kordamp/gradle/plugin/base/Banner.groovy index 5335c1fdd..67c3ea69b 100644 --- a/plugins/base-gradle-plugin/src/main/groovy/org/kordamp/gradle/plugin/base/Banner.groovy +++ b/plugins/base-gradle-plugin/src/main/groovy/org/kordamp/gradle/plugin/base/Banner.groovy @@ -18,9 +18,9 @@ package org.kordamp.gradle.plugin.base import groovy.transform.CompileStatic -import org.gradle.BuildAdapter -import org.gradle.BuildResult import org.gradle.api.Project +import org.gradle.api.services.BuildService +import org.gradle.api.services.BuildServiceParameters import java.text.MessageFormat @@ -30,52 +30,55 @@ import java.text.MessageFormat * @since 0.30.0 */ @CompileStatic -final class Banner { - private final ResourceBundle bundle = ResourceBundle.getBundle(Banner.name) - private final String productVersion = bundle.getString('product.version') - private final String productId = bundle.getString('product.id') - private final String productName = bundle.getString('product.name') - private final String banner = MessageFormat.format(bundle.getString('product.banner'), productName, productVersion) - private final List visited = [] +abstract class Banner implements BuildService { + private static final String ORG_KORDAMP_BANNER = 'org.kordamp.banner' - private static final Banner b = new Banner() + private String productVersion + private String productId + private final List projectNames = [] - private Banner() { - // nooop + interface Params extends BuildServiceParameters { } - static void display(Project project) { - if (b.visited.contains(project.rootProject.path)) { - return - } - b.visited.add(project.rootProject.path) - project.gradle.addBuildListener(new BuildAdapter() { - @Override - void buildFinished(BuildResult result) { - b.visited.clear() - } - }) + void display(Project project) { + if (checkIfVisited(project)) return + + ResourceBundle bundle = ResourceBundle.getBundle(Banner.name) + productVersion = bundle.getString('product.version') + productId = bundle.getString('product.id') + String productName = bundle.getString('product.name') + String banner = MessageFormat.format(bundle.getString('product.banner'), productName, productVersion) + + boolean printBanner = null == System.getProperty(ORG_KORDAMP_BANNER) || Boolean.getBoolean(ORG_KORDAMP_BANNER) File parent = new File(project.gradle.gradleUserHomeDir, 'caches') - File markerFile = b.getMarkerFile(parent) + File markerFile = getMarkerFile(parent) if (!markerFile.exists()) { markerFile.parentFile.mkdirs() markerFile.text = '1' - println(b.banner) + if (printBanner) System.err.println(banner) } else { try { int count = Integer.parseInt(markerFile.text) if (count < 3) { - println(b.banner) + if (printBanner) System.err.println(banner) } markerFile.text = (count + 1) + '' } catch (NumberFormatException e) { markerFile.text = '1' - println(b.banner) + if (printBanner) System.err.println(banner) } } } + private boolean checkIfVisited(Project project) { + if (projectNames.contains(project.rootProject.name)) { + return true + } + projectNames.add(project.rootProject.name) + return false + } + private File getMarkerFile(File parent) { new File(parent, 'kordamp' + diff --git a/plugins/base-gradle-plugin/src/main/groovy/org/kordamp/gradle/plugin/base/BasePlugin.groovy b/plugins/base-gradle-plugin/src/main/groovy/org/kordamp/gradle/plugin/base/BasePlugin.groovy index 5f55769f3..57d07b249 100644 --- a/plugins/base-gradle-plugin/src/main/groovy/org/kordamp/gradle/plugin/base/BasePlugin.groovy +++ b/plugins/base-gradle-plugin/src/main/groovy/org/kordamp/gradle/plugin/base/BasePlugin.groovy @@ -70,7 +70,10 @@ class BasePlugin extends AbstractKordampPlugin { } void apply(Project project) { - Banner.display(project) + project.gradle.sharedServices + .registerIfAbsent('kordamp-banner', Banner, { spec -> }) + .get().display(project) + this.project = project ProjectEvaluationListenerManager.register(project.gradle) diff --git a/plugins/insight-gradle-plugin/src/main/groovy/org/kordamp/gradle/plugin/insight/Banner.groovy b/plugins/insight-gradle-plugin/src/main/groovy/org/kordamp/gradle/plugin/insight/Banner.groovy deleted file mode 100644 index c74a53cd6..000000000 --- a/plugins/insight-gradle-plugin/src/main/groovy/org/kordamp/gradle/plugin/insight/Banner.groovy +++ /dev/null @@ -1,89 +0,0 @@ -/* - * SPDX-License-Identifier: Apache-2.0 - * - * Copyright 2018-2023 Andres Almiray. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.kordamp.gradle.plugin.insight - -import groovy.transform.CompileStatic -import org.gradle.BuildAdapter -import org.gradle.BuildResult -import org.gradle.api.Project - -import java.text.MessageFormat - -/** - * - * @author Andres Almiray - * @since 0.40.0 - */ -@CompileStatic -final class Banner { - private final ResourceBundle bundle = ResourceBundle.getBundle(Banner.name) - private final String productVersion = bundle.getString('product.version') - private final String productId = bundle.getString('product.id') - private final String productName = bundle.getString('product.name') - private final String banner = MessageFormat.format(bundle.getString('product.banner'), productName, productVersion) - private final List visited = [] - - private static final Banner b = new Banner() - - private Banner() { - // nooop - } - - static void display(Project project) { - if (b.visited.contains(project.rootProject.path)) { - return - } - b.visited.add(project.rootProject.path) - project.gradle.addBuildListener(new BuildAdapter() { - @Override - void buildFinished(BuildResult result) { - b.visited.clear() - } - }) - - File parent = new File(project.gradle.gradleUserHomeDir, 'caches') - File markerFile = b.getMarkerFile(parent) - if (!markerFile.exists()) { - markerFile.parentFile.mkdirs() - markerFile.text = '1' - println(b.banner) - } else { - try { - int count = Integer.parseInt(markerFile.text) - if (count < 3) { - println(b.banner) - } - markerFile.text = (count + 1) + '' - } catch (NumberFormatException e) { - markerFile.text = '1' - println(b.banner) - } - } - } - - private File getMarkerFile(File parent) { - new File(parent, - 'kordamp' + - File.separator + - productId + - File.separator + - productVersion + - File.separator + - 'marker.txt') - } -} diff --git a/plugins/insight-gradle-plugin/src/main/resources/org/kordamp/gradle/plugin/insight/Banner.properties b/plugins/insight-gradle-plugin/src/main/resources/org/kordamp/gradle/plugin/insight/Banner.properties deleted file mode 100644 index ee51af53a..000000000 --- a/plugins/insight-gradle-plugin/src/main/resources/org/kordamp/gradle/plugin/insight/Banner.properties +++ /dev/null @@ -1,22 +0,0 @@ -# -# SPDX-License-Identifier: Apache-2.0 -# -# Copyright 2018-2023 Andres Almiray. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -product.version=$version -product.id=$id -product.name=$name -product.banner={0} {1}. Consider becoming a patron at https://www.patreon.com/aalmiray \ No newline at end of file