com.exasol.projectkeeper.config.ProjectKeeperConfigReader 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.config;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.error.YAMLException;
import com.exasol.errorreporting.ExaError;
import com.exasol.projectkeeper.shared.config.*;
/**
* This class reads {@link ProjectKeeperConfig} from file.
*/
public class ProjectKeeperConfigReader {
/** Filename of project keeper's configuration file */
public static final String CONFIG_FILE_NAME = ".project-keeper.yml";
private static final String USER_GUIDE_URL = "https://github.com/exasol/project-keeper-maven-plugin";
private static final String CHECK_THE_USER_GUIDE = "Please check the user-guide " + USER_GUIDE_URL + ".";
private static final String INVALID_CONFIG_FILE = "Invalid " + CONFIG_FILE_NAME + ".";
/**
* Read a {@link ProjectKeeperConfig} from file.
*
* @param projectDirectory project directory
* @return read {@link ProjectKeeperConfig}
*/
public ProjectKeeperConfig readConfig(final Path projectDirectory) {
verifyWeReInProjectRoot(projectDirectory);
final Path configFile = projectDirectory.resolve(CONFIG_FILE_NAME);
validateConfigFileExists(configFile);
try (final FileReader fileReader = new FileReader(configFile.toFile())) {
final ProjectKeeperRawConfig rawConfig = readRawConfig(fileReader, configFile);
return parseRawConfig(rawConfig, projectDirectory);
} catch (final IOException exception) {
throw new IllegalStateException(ExaError.messageBuilder("E-PK-CORE-82")
.message("Failed to read '" + CONFIG_FILE_NAME + "'.").toString(), exception);
}
}
private void verifyWeReInProjectRoot(final Path projectDirectory) {
if (!Files.exists(projectDirectory.resolve(".git"))) {
throw new IllegalArgumentException(ExaError.messageBuilder("E-PK-CORE-90")
.message("Could not find .git directory in project-root {{root path}}.", projectDirectory)
.mitigation("Run 'git init'.")
.mitigation(
"Make sure that you run project-keeper only in the root directory of the git-repository. If you have multiple projects in that directory, define them in the '.project-keeper.yml'.")
.toString());
}
}
private ProjectKeeperRawConfig readRawConfig(final FileReader fileReader, final Path path) {
try {
return new Yaml().loadAs(fileReader, ProjectKeeperRawConfig.class);
} catch (final YAMLException exception) {
throw new IllegalArgumentException(ExaError.messageBuilder("E-PK-CORE-85")
.message(INVALID_CONFIG_FILE + " Path: {{path}}", path).mitigation(CHECK_THE_USER_GUIDE).toString(),
exception);
}
}
private void validateConfigFileExists(final Path configFile) {
if (!Files.exists(configFile)) {
throw new IllegalArgumentException(ExaError.messageBuilder("E-PK-CORE-89")
.message("Could not find '" + CONFIG_FILE_NAME + "'.")
.mitigation("Please create this configuration according to the user-guide " + USER_GUIDE_URL + ".")
.toString());//
}
}
private ProjectKeeperConfig parseRawConfig(final ProjectKeeperRawConfig rawConfig, final Path projectDir) {
final List rawSources = rawConfig.getSources();
final List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy