001/*
002 * SPDX-License-Identifier: Apache-2.0
003 *
004 * Copyright 2020-2024 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 *     https://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 */
018package org.kordamp.maven.plugin.checker;
019
020import org.apache.maven.plugin.logging.Log;
021import org.apache.maven.project.MavenProject;
022
023import java.io.File;
024import java.io.FileInputStream;
025import java.io.FileOutputStream;
026import java.io.IOException;
027import java.io.PrintStream;
028import java.text.MessageFormat;
029import java.util.ArrayList;
030import java.util.List;
031import java.util.ResourceBundle;
032import java.util.Scanner;
033
034/**
035 * @author Andres Almiray
036 * @since 1.0.0
037 */
038public final class Banner {
039    private static final String ORG_KORDAMP_BANNER = "org.kordamp.banner";
040    private static final Banner INSTANCE = new Banner();
041    private final ResourceBundle bundle = ResourceBundle.getBundle(Banner.class.getName());
042    private final String productVersion = bundle.getString("product.version");
043    private final String productId = bundle.getString("product.id");
044    private final String productName = bundle.getString("product.name");
045    private final String message = MessageFormat.format(bundle.getString("product.banner"), productName, productVersion);
046    private final List<String> visited = new ArrayList<>();
047
048    private Banner() {
049        // noop
050    }
051
052    private File getMarkerFile(File parent) {
053        return new File(parent,
054            "kordamp" +
055                File.separator +
056                productId +
057                File.separator +
058                productVersion +
059                File.separator +
060                "marker.txt");
061    }
062
063    public static void display(MavenProject project, Log log) {
064        if (INSTANCE.visited.contains(project.getName())) {
065            return;
066        }
067
068        INSTANCE.visited.add(project.getName());
069
070        boolean quiet = log.isErrorEnabled() &&
071            !log.isWarnEnabled() &&
072            !log.isInfoEnabled() &&
073            !log.isDebugEnabled();
074
075        boolean printBanner = null == System.getProperty(ORG_KORDAMP_BANNER) || Boolean.getBoolean(ORG_KORDAMP_BANNER);
076
077        try {
078            File parent = new File(System.getProperty("user.home"), ".m2/caches");
079            File markerFile = INSTANCE.getMarkerFile(parent);
080            if (!markerFile.exists()) {
081                if (printBanner && !quiet) System.out.println(INSTANCE.message);
082                markerFile.getParentFile().mkdirs();
083                PrintStream out = new PrintStream(new FileOutputStream(markerFile));
084                out.println("1");
085                out.close();
086                writeQuietly(markerFile, "1");
087            } else {
088                try {
089                    int count = Integer.parseInt(readQuietly(markerFile));
090                    if (count < 3) {
091                        if (printBanner && !quiet) System.out.println(INSTANCE.message);
092                    }
093                    writeQuietly(markerFile, (count + 1) + "");
094                } catch (NumberFormatException e) {
095                    writeQuietly(markerFile, "1");
096                    if (printBanner && !quiet) System.out.println(INSTANCE.message);
097                }
098            }
099        } catch (IOException ignored) {
100            // noop
101        }
102    }
103
104    private static void writeQuietly(File file, String text) {
105        try {
106            PrintStream out = new PrintStream(new FileOutputStream(file));
107            out.println(text);
108            out.close();
109        } catch (IOException ignored) {
110            // ignored
111        }
112    }
113
114    private static String readQuietly(File file) {
115        try {
116            Scanner in = new Scanner(new FileInputStream(file));
117            return in.next();
118        } catch (Exception ignored) {
119            return "";
120        }
121    }
122}