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.checker;
019
020import java.io.BufferedWriter;
021import java.io.OutputStream;
022import java.io.OutputStreamWriter;
023import java.io.PrintStream;
024import java.io.PrintWriter;
025import java.util.ResourceBundle;
026
027import static java.nio.charset.StandardCharsets.UTF_8;
028
029/**
030 * @author Andres Almiray
031 * @since 1.9.0
032 */
033public class PomcheckerVersion {
034    private static final ResourceBundle BUNDLE = ResourceBundle.getBundle(PomcheckerVersion.class.getName());
035    private static final String JRELEASER_VERSION = BUNDLE.getString("pomchecker_version");
036    private static final String BUILD_TIMESTAMP = BUNDLE.getString("build_timestamp");
037    private static final String BUILD_REVISION = BUNDLE.getString("build_revision");
038    private static final String SEPARATOR = "------------------------------------------------------------%n";
039    private static final String POMCHECKER_VERSION_FORMATTED = "pomchecker %s%n";
040
041    public static String getPlainVersion() {
042        return JRELEASER_VERSION;
043    }
044
045    public static void banner(PrintStream out) {
046        banner(out, true);
047    }
048
049    public static void banner(PrintStream out, boolean full) {
050        banner(newPrintWriter(out), full);
051    }
052
053    public static void banner(PrintWriter out) {
054        banner(out, true);
055    }
056
057    public static void banner(PrintWriter out, boolean full) {
058        if (full) {
059            out.printf(SEPARATOR);
060            out.printf(POMCHECKER_VERSION_FORMATTED, JRELEASER_VERSION);
061
062            String jvm = System.getProperty("java.version") + " (" +
063                System.getProperty("java.vendor") + " " +
064                System.getProperty("java.vm.version") + ")";
065
066            out.printf(SEPARATOR);
067            out.printf("Build timestamp: %s%n", BUILD_TIMESTAMP);
068            out.printf("Revision:        %s%n", BUILD_REVISION);
069            out.printf("JVM:             %s%n", jvm);
070            out.printf(SEPARATOR);
071        } else {
072            out.printf(POMCHECKER_VERSION_FORMATTED, JRELEASER_VERSION);
073        }
074    }
075
076    private static PrintWriter newPrintWriter(OutputStream out) {
077        return newPrintWriter(out, true);
078    }
079
080    private static PrintWriter newPrintWriter(OutputStream out, boolean autoFlush) {
081        return new PrintWriter(new BufferedWriter(new OutputStreamWriter(out, UTF_8)), autoFlush);
082    }
083}