mirror of
https://github.com/RatzzFatzz/MKVAudioSubtitleChanger.git
synced 2026-02-11 02:05:56 +01:00
Expand date filter by reading from last-execution.yml
This commit is contained in:
@@ -2,7 +2,7 @@ package at.pcgamingfreaks.mkvaudiosubtitlechanger.config;
|
||||
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.validator.*;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ConfigProperty;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.util.VersionUtil;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.util.ProjectUtil;
|
||||
import at.pcgamingfreaks.yaml.YAML;
|
||||
import at.pcgamingfreaks.yaml.YamlInvalidContentException;
|
||||
import org.apache.commons.cli.*;
|
||||
@@ -11,14 +11,17 @@ import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ConfigProperty.*;
|
||||
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.util.CommandLineOptionsUtil.optionOf;
|
||||
|
||||
public class ConfigLoader {
|
||||
private static final List<ConfigValidator<?>> VALIDATORS = List.of(
|
||||
private static final List<ConfigValidator<?>> VALIDATORS = Stream.of(
|
||||
new ConfigPathValidator(CONFIG_PATH, false),
|
||||
new PathValidator(LIBRARY, true, null),
|
||||
new ThreadValidator(THREADS, false, 2),
|
||||
@@ -34,7 +37,7 @@ public class ConfigLoader {
|
||||
new AttributeConfigValidator(),
|
||||
new CoherentConfigValidator(COHERENT, false),
|
||||
new BooleanValidator(FORCE_COHERENT, false)
|
||||
);
|
||||
).sorted(Comparator.comparing((ConfigValidator<?> validator) -> validator.getWeight()).reversed()).collect(Collectors.toList());
|
||||
|
||||
public static void initConfig(String[] args) {
|
||||
HelpFormatter formatter = new HelpFormatter();
|
||||
@@ -100,7 +103,7 @@ public class ConfigLoader {
|
||||
|
||||
private static void exitIfVersion(CommandLine cmd) {
|
||||
if (cmd.hasOption(VERSION.prop())) {
|
||||
System.out.printf("MKV Audio Subtitle Changer Version %s%n", VersionUtil.getVersion());
|
||||
System.out.printf("MKV Audio Subtitle Changer Version %s%n", ProjectUtil.getVersion());
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,4 +27,9 @@ public class ConfigPathValidator extends PathValidator {
|
||||
protected boolean isValid(File result) {
|
||||
return super.isValid(result) && (result.getAbsolutePath().endsWith(".yml") || result.getAbsolutePath().endsWith(".yaml"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getWeight() {
|
||||
return 100;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -154,11 +154,7 @@ public abstract class ConfigValidator<FieldType> {
|
||||
&& StringUtils.equalsIgnoreCase(method.getName().replace("get", ""), property.prop().replace("-", ""));
|
||||
}
|
||||
|
||||
protected static Set<Integer> getDependentValidators() {
|
||||
return Set.of();
|
||||
}
|
||||
|
||||
public static int getWeight() {
|
||||
return getDependentValidators().stream().sorted().findFirst().orElse(100) - 1;
|
||||
public int getWeight() {
|
||||
return 50;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,16 +3,21 @@ package at.pcgamingfreaks.mkvaudiosubtitlechanger.config.validator;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ConfigProperty;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.util.DateUtils;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.util.ProjectUtil;
|
||||
import at.pcgamingfreaks.yaml.YAML;
|
||||
import at.pcgamingfreaks.yaml.YamlInvalidContentException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.harawata.appdirs.AppDirsFactory;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Date;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
@Slf4j
|
||||
public class DateValidator extends ConfigValidator<Date> {
|
||||
private static final Date DEFAULT_DATE = new Date(0);
|
||||
private static final Date INVALID_DATE = new Date(0);
|
||||
private static final Date DEFAULT_DATE = new Date(1);
|
||||
|
||||
public DateValidator(ConfigProperty property, boolean required) {
|
||||
super(property, required, null);
|
||||
@@ -25,27 +30,32 @@ public class DateValidator extends ConfigValidator<Date> {
|
||||
|
||||
@Override
|
||||
protected Date overwriteValue() {
|
||||
Properties prop = new Properties();
|
||||
try {
|
||||
prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("/last-execution.properties"));
|
||||
return parse(prop.getProperty(Config.getInstance().getLibraryPath().getAbsolutePath()));
|
||||
} catch (IOException e) {
|
||||
String filePath = AppDirsFactory.getInstance().getUserConfigDir(ProjectUtil.getProjectName(), null, null);
|
||||
File lastExecutionFile = Path.of(filePath + "/last-execution.yml").toFile();
|
||||
if (!lastExecutionFile.exists()) {
|
||||
return DEFAULT_DATE;
|
||||
}
|
||||
YAML yaml = new YAML(lastExecutionFile);
|
||||
return parse(yaml.getString(Config.getInstance().getLibraryPath().getAbsolutePath(), DateUtils.convert(DEFAULT_DATE)));
|
||||
} catch (YamlInvalidContentException | IOException e) {
|
||||
log.error("Couldn't open last-execution.properties");
|
||||
return DEFAULT_DATE;
|
||||
return INVALID_DATE;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
Date parse(String value) {
|
||||
return DateUtils.convert(value, DEFAULT_DATE);
|
||||
return DateUtils.convert(value, INVALID_DATE);
|
||||
}
|
||||
|
||||
@Override
|
||||
boolean isValid(Date result) {
|
||||
return !result.equals(DEFAULT_DATE);
|
||||
return !result.equals(INVALID_DATE);
|
||||
}
|
||||
|
||||
protected static Set<Integer> getDependentValidators() {
|
||||
return Set.of(PathValidator.getWeight(), BooleanValidator.getWeight());
|
||||
@Override
|
||||
public int getWeight() {
|
||||
return 40;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,4 +22,8 @@ public class DateUtils {
|
||||
return defaultDate;
|
||||
}
|
||||
}
|
||||
|
||||
public static String convert(Date date) {
|
||||
return dateFormat.format(date);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package at.pcgamingfreaks.mkvaudiosubtitlechanger.util;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Properties;
|
||||
|
||||
public class ProjectUtil {
|
||||
private static final Properties PROJECT_PROPERTIES = new Properties();
|
||||
|
||||
static {
|
||||
try (InputStream propertiesStream = ProjectUtil.class.getClassLoader().getResourceAsStream("project.properties")) {
|
||||
PROJECT_PROPERTIES.load(propertiesStream);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static String getVersion() {
|
||||
return PROJECT_PROPERTIES.getProperty("version");
|
||||
}
|
||||
|
||||
public static String getProjectName() {
|
||||
return PROJECT_PROPERTIES.getProperty("project_name");
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package at.pcgamingfreaks.mkvaudiosubtitlechanger.util;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Properties;
|
||||
|
||||
public class VersionUtil {
|
||||
public static String getVersion() {
|
||||
try (InputStream propertiesStream = VersionUtil.class.getClassLoader().getResourceAsStream("version.properties")) {
|
||||
Properties properties = new Properties();
|
||||
properties.load(propertiesStream);
|
||||
|
||||
return properties.getProperty("version");
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user