com.exasol.projectkeeper.ProjectVersionDetector Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of project-keeper-core Show documentation
Show all versions of project-keeper-core Show documentation
Project keeper is a tool that verifies and fixes project setups.
package com.exasol.projectkeeper;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Collectors;
import com.exasol.errorreporting.ExaError;
import com.exasol.projectkeeper.shared.config.*;
import com.exasol.projectkeeper.sources.AnalyzedSource;
/**
* This class extracts the overall project version.
*/
public class ProjectVersionDetector {
private static final String FAILED_TO_DETECT_VERSION = "Failed to detect overall project version.";
/**
* Detect the version of the overall project.
*
* @param config project-keeper configuration
* @param analyzedSources analyzed sources
* @return detected version
* @throws IllegalArgumentException if it could not detect the version
*/
public String detectVersion(final ProjectKeeperConfig config, final List analyzedSources) {
if (config.getVersionConfig() != null) {
final VersionProviderVisitor visitor = new VersionProviderVisitor(analyzedSources);
config.getVersionConfig().accept(visitor);
return visitor.getVersion();
} else if ((analyzedSources.size() == 1) && (analyzedSources.get(0).getVersion() != null)) {
return analyzedSources.get(0).getVersion();
} else {
throw new IllegalArgumentException(ExaError.messageBuilder("E-PK-CORE-116").message(FAILED_TO_DETECT_VERSION
+ " Project-keeper can only auto-detect the project version for projects with exactly one source.")
.mitigation("Please configure how to detect the version in your project-keeper config.")
.toString());
}
}
private static class VersionProviderVisitor implements VersionConfig.Visitor {
private final List analyzedSources;
private String version;
private VersionProviderVisitor(final List analyzedSources) {
this.analyzedSources = analyzedSources;
}
@Override
public void visit(final FixedVersion fixedVersion) {
this.version = fixedVersion.getVersion();
}
@Override
public void visit(final VersionFromSource versionFromMavenSource) {
final Path requestedPath = versionFromMavenSource.getPathToPom();
this.version = this.analyzedSources.stream()//
.filter(source -> source.getPath().normalize().equals(requestedPath.normalize()))//
.findAny().orElseThrow(() -> getNoSourceFoundException(requestedPath, this.analyzedSources))//
.getVersion();
if (this.version == null) {
throw getSourceHasNoVersionException(requestedPath);
}
}
private IllegalArgumentException getSourceHasNoVersionException(final Path requestedPath) {
return new IllegalArgumentException(ExaError.messageBuilder("E-PK-CORE-115")
.message(FAILED_TO_DETECT_VERSION
+ " The specified source with path {{path}} did not provide a version.", requestedPath)
.mitigation(
"Please specify a different source to read from or set an explicit version in your project-keeper config.")
.toString());
}
private IllegalArgumentException getNoSourceFoundException(final Path requestedPath,
final List analyzedSources) {
final List knownSources = analyzedSources.stream().map(AnalyzedSource::getPath)
.collect(Collectors.toList());
return new IllegalArgumentException(ExaError.messageBuilder("E-PK-CORE-114")
.message(FAILED_TO_DETECT_VERSION + " Could not find a source with specified path {{path}}.",
requestedPath.toString())
.mitigation(
"Please make sure that you defined a source with exactly the same path. The following sources are defined in the config: {{sources}}.",
knownSources)
.toString());
}
/**
* @return version detected by {@link ProjectVersionDetector}
*/
public String getVersion() {
return this.version;
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy