Skip to content

Commit

Permalink
[TH2-4987] added com.gorylenko.gradle-git-properties plugin (#269)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita-Smirnov-Exactpro authored Jul 13, 2023
1 parent ac40e8f commit 727c9e3
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 33 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ plugins {
id 'org.jetbrains.kotlin.kapt' version "${kotlin_version}"
id "org.owasp.dependencycheck" version "8.2.1"
id "me.champeau.jmh" version "0.6.8"
id "com.gorylenko.gradle-git-properties" version "2.4.1"
}

group = 'com.exactpro.th2'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,24 +74,16 @@
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.nio.file.Path;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.Spliterators;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;
import java.util.jar.Attributes.Name;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.stream.StreamSupport;

import static com.exactpro.cradle.CradleStorage.DEFAULT_MAX_MESSAGE_BATCH_SIZE;
import static com.exactpro.cradle.CradleStorage.DEFAULT_MAX_TEST_EVENT_BATCH_SIZE;
Expand All @@ -108,8 +100,6 @@
*/
public abstract class AbstractCommonFactory implements AutoCloseable {

protected static final String EXACTPRO_IMPLEMENTATION_VENDOR = "Exactpro Systems LLC";

/**
* @deprecated please use {@link #LOG4J_PROPERTIES_DEFAULT_PATH}
*/
Expand Down Expand Up @@ -872,28 +862,6 @@ protected static void configureLogger(String... paths) {
listPath.addAll(Arrays.asList(requireNonNull(paths, "Paths can't be null")));
Log4jConfigUtils log4jConfigUtils = new Log4jConfigUtils();
log4jConfigUtils.configure(listPath, LOG4J2_PROPERTIES_NAME);
loggingManifests();
}

private static void loggingManifests() {
try {
Iterator<URL> urlIterator = Thread.currentThread().getContextClassLoader().getResources(JarFile.MANIFEST_NAME).asIterator();
StreamSupport.stream(Spliterators.spliteratorUnknownSize(urlIterator, 0), false)
.map(url -> {
try (InputStream inputStream = url.openStream()) {
return new Manifest(inputStream);
} catch (IOException e) {
LOGGER.warn("Manifest '{}' loading failere", url, e);
return null;
}
})
.filter(Objects::nonNull)
.map(Manifest::getMainAttributes)
.filter(attributes -> EXACTPRO_IMPLEMENTATION_VENDOR.equals(attributes.getValue(Name.IMPLEMENTATION_VENDOR)))
.forEach(attributes -> LOGGER.info("Manifest title {}, version {}"
, attributes.getValue(Name.IMPLEMENTATION_TITLE), attributes.getValue(Name.IMPLEMENTATION_VERSION)));
} catch (IOException e) {
LOGGER.warn("Manifest searching failure", e);
}
ExactproMetaInf.logging();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright 2023 Exactpro (Exactpro Systems Limited)
*
* 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
*
* http://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 com.exactpro.th2.common.schema.factory

import mu.KotlinLogging
import java.io.IOException
import java.net.URL
import java.nio.file.Path
import java.util.Properties
import java.util.jar.Attributes
import java.util.jar.JarFile
import java.util.jar.Manifest

internal class ExactproMetaInf(
url: URL,
private val title: String,
private val version: String
) {
private val jarPath = Path.of(url.path).parent.parent

private var gitEnriched = false
private var gitHash = ""
private var gitBranch = ""
private var gitRemoteUrl = ""
private var gitClosestTag = ""

fun enrich(gitProperty: URL) {
try {
gitProperty.openStream().use { inputStream ->
val properties = Properties()
properties.load(inputStream)
gitHash = properties.getProperty(GIT_HASH_PROPERTY)
gitBranch = properties.getProperty(GIT_BRANCH_PROPERTY)
gitRemoteUrl = properties.getProperty(GIT_REMOTE_URL_PROPERTY)
gitClosestTag = properties.getProperty(GIT_CLOSEST_TAG_PROPERTY)
gitEnriched = true
}
} catch (e: IOException) {
K_LOGGER.warn(e) { "Git properties '$gitProperty' loading failure" }
}
}

override fun toString(): String {
return "Manifest title: $title, version: $version , git { ${
if (gitEnriched) {
"hash: $gitHash, branch: $gitBranch, repository: $gitRemoteUrl, closest tag: $gitClosestTag"
} else {
"'${jarPath.fileName}' jar doesn't contain '$GIT_PROPERTIES_FILE' resource, please use '$GRADLE_GIT_PROPERTIES_PLUGIN' plugin"
}
} }"
}

companion object {
private val K_LOGGER = KotlinLogging.logger {}
private const val EXACTPRO_IMPLEMENTATION_VENDOR = "Exactpro Systems LLC"
private const val GRADLE_GIT_PROPERTIES_PLUGIN = "com.gorylenko.gradle-git-properties"
private const val GIT_PROPERTIES_FILE = "git.properties"
private const val GIT_HASH_PROPERTY = "git.commit.id"
private const val GIT_BRANCH_PROPERTY = "git.branch"
private const val GIT_REMOTE_URL_PROPERTY = "git.remote.origin.url"
private const val GIT_CLOSEST_TAG_PROPERTY = "git.closest.tag.name"

@JvmStatic
fun logging() {
if (K_LOGGER.isInfoEnabled) {
try {
val map = Thread.currentThread().contextClassLoader
.getResources(JarFile.MANIFEST_NAME).asSequence()
.mapNotNull(::create)
.map { metaInf -> metaInf.jarPath to metaInf }
.toMap()

Thread.currentThread().contextClassLoader
.getResources(GIT_PROPERTIES_FILE).asSequence()
.forEach { url -> map[Path.of(url.path).parent]?.enrich(url) }

map.values.forEach { metaInf -> K_LOGGER.info { "$metaInf" } }
} catch (e: IOException) {
K_LOGGER.warn(e) { "Manifest searching failure" }
}
}
}

private fun create(manifestUrl: URL): ExactproMetaInf? {
try {
manifestUrl.openStream().use { inputStream ->
val attributes = Manifest(inputStream).mainAttributes
return if (EXACTPRO_IMPLEMENTATION_VENDOR != attributes.getValue(Attributes.Name.IMPLEMENTATION_VENDOR)) {
null
} else ExactproMetaInf(
manifestUrl,
attributes.getValue(Attributes.Name.IMPLEMENTATION_TITLE),
attributes.getValue(Attributes.Name.IMPLEMENTATION_VERSION)
)
}
} catch (e: IOException) {
K_LOGGER.warn(e) { "Manifest '$manifestUrl' loading failure" }
return null
}
}
}
}

0 comments on commit 727c9e3

Please sign in to comment.