Make mkvtoolnix path default value of dependant

This commit is contained in:
RatzzFatzz
2024-11-27 00:32:23 +01:00
parent 7ea0ab17b0
commit 1863432dc6
5 changed files with 58 additions and 23 deletions

View File

@@ -2,23 +2,16 @@ package at.pcgamingfreaks.mkvaudiosubtitlechanger.config;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.AttributeConfig; import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.AttributeConfig;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.MkvToolNix; import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.MkvToolNix;
import com.sun.jna.platform.win32.Netapi32Util;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.Validation;
import jakarta.validation.Validator;
import jakarta.validation.constraints.Min; import jakarta.validation.constraints.Min;
import lombok.AccessLevel; import lombok.AccessLevel;
import lombok.Getter; import lombok.Getter;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.Setter; import lombok.Setter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.cli.CommandLineParser; import org.apache.commons.lang3.SystemUtils;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import picocli.CommandLine; import picocli.CommandLine;
import java.io.File; import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.*; import java.util.*;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@@ -46,7 +39,6 @@ public class Config {
@CommandLine.Option(names = {"-s", "--safemode"}, description = "test run (no files will be changes)") @CommandLine.Option(names = {"-s", "--safemode"}, description = "test run (no files will be changes)")
private boolean safeMode; private boolean safeMode;
@Getter(AccessLevel.NONE)
@Setter(AccessLevel.NONE) @Setter(AccessLevel.NONE)
private File mkvToolNix; private File mkvToolNix;
@@ -79,23 +71,26 @@ public class Config {
description = "Additional keywords to prefer specific subtitle tracks (Defaults are will be overwritten; Default: ${DEFAULT-VALUE}") description = "Additional keywords to prefer specific subtitle tracks (Defaults are will be overwritten; Default: ${DEFAULT-VALUE}")
private Set<String> preferredSubtitles = new HashSet<>(Arrays.asList("unstyled")); private Set<String> preferredSubtitles = new HashSet<>(Arrays.asList("unstyled"));
@CommandLine.Option(names = {"-l", "--library"}, required = true, description = "path to library") @CommandLine.Option(names = {"-l", "--library"}, required = true, description = "path to library")
public void setLibraryPath(File libraryPath) { public void setLibraryPath(File libraryPath) {
if (!libraryPath.exists()) throw new CommandLine.ParameterException(spec.commandLine(), "Path does not exist: " + libraryPath.getAbsolutePath()); if (!libraryPath.exists()) throw new CommandLine.ParameterException(spec.commandLine(), "Path does not exist: " + libraryPath.getAbsolutePath());
this.libraryPath = libraryPath; this.libraryPath = libraryPath;
} }
@CommandLine.Option(names = {"-m", "--mkvtoolnix"}, defaultValue = "C:\\Program Files\\MKVToolNix", description = "path to mkvtoolnix installation") static {
// Set default value into system properties to picocli can read the conditional value
System.setProperty("DEFAULT_MKV_TOOL_NIX", SystemUtils.IS_OS_WINDOWS ? "C:\\Program Files\\MKVToolNix" : "/usr/bin/");
}
@CommandLine.Option(names = {"-m", "--mkvtoolnix"}, defaultValue = "${DEFAULT_MKV_TOOL_NIX}", description = "path to mkvtoolnix installation")
public void setMkvToolNix(File mkvToolNix) { public void setMkvToolNix(File mkvToolNix) {
this.mkvToolNix = mkvToolNix; this.mkvToolNix = mkvToolNix;
if (!mkvToolNix.exists() if (!mkvToolNix.exists()
|| !Path.of(getPathFor(MkvToolNix.MKV_MERGER)).toFile().exists() || !Path.of(getPathFor(MkvToolNix.MKV_MERGE, SystemUtils.IS_OS_WINDOWS)).toFile().exists()
|| !Path.of(getPathFor(MkvToolNix.MKV_PROP_EDIT)).toFile().exists()) { || !Path.of(getPathFor(MkvToolNix.MKV_PROP_EDIT, SystemUtils.IS_OS_WINDOWS)).toFile().exists()) {
throw new CommandLine.ParameterException(spec.commandLine(), throw new CommandLine.ParameterException(spec.commandLine(),
"Invalid path to mkvtoolnix installation: " + mkvToolNix.getAbsolutePath()); "Invalid path to mkvtoolnix installation: " + mkvToolNix.getAbsolutePath());
} }
} }
public static Config getInstance() { public static Config getInstance() {
@@ -119,8 +114,13 @@ public class Config {
* @return absolute path to desired application. * @return absolute path to desired application.
*/ */
public String getPathFor(MkvToolNix application) { public String getPathFor(MkvToolNix application) {
return mkvToolNix.getAbsolutePath().endsWith("/") ? mkvToolNix.getAbsolutePath() + application : return mkvToolNix.getAbsolutePath().endsWith("/")
mkvToolNix.getAbsolutePath() + "/" + application; ? mkvToolNix.getAbsolutePath() + application
: mkvToolNix.getAbsolutePath() + "/" + application;
}
public String getPathFor(MkvToolNix application, boolean isWindows) {
return getPathFor(application) + (isWindows ? ".exe" : "");
} }
public String getNormalizedLibraryPath() { public String getNormalizedLibraryPath() {

View File

@@ -5,7 +5,7 @@ import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ConfigProperty;
import java.io.File; import java.io.File;
import java.nio.file.Path; import java.nio.file.Path;
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.model.MkvToolNix.MKV_MERGER; import static at.pcgamingfreaks.mkvaudiosubtitlechanger.model.MkvToolNix.MKV_MERGE;
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.model.MkvToolNix.MKV_PROP_EDIT; import static at.pcgamingfreaks.mkvaudiosubtitlechanger.model.MkvToolNix.MKV_PROP_EDIT;
@Deprecated @Deprecated
@@ -19,9 +19,9 @@ public class MkvToolNixPathValidator extends PathValidator {
@Override @Override
protected boolean isValid(File result) { protected boolean isValid(File result) {
return result.isDirectory() return result.isDirectory()
&& (Path.of(result.getAbsolutePath() + "/" + MKV_MERGER + EXE).toFile().isFile() && (Path.of(result.getAbsolutePath() + "/" + MKV_MERGE + EXE).toFile().isFile()
&& Path.of(result.getAbsolutePath() + "/" + MKV_PROP_EDIT + EXE).toFile().isFile()) && Path.of(result.getAbsolutePath() + "/" + MKV_PROP_EDIT + EXE).toFile().isFile())
|| (Path.of(result.getAbsolutePath() + "/" + MKV_MERGER).toFile().isFile() || (Path.of(result.getAbsolutePath() + "/" + MKV_MERGE).toFile().isFile()
&& Path.of(result.getAbsolutePath() + "/" + MKV_PROP_EDIT).toFile().isFile()); && Path.of(result.getAbsolutePath() + "/" + MKV_PROP_EDIT).toFile().isFile());
} }
} }

View File

@@ -39,7 +39,7 @@ public class MkvFileProcessor implements FileProcessor {
Map<String, Object> jsonMap; Map<String, Object> jsonMap;
List<FileAttribute> fileAttributes = new ArrayList<>(); List<FileAttribute> fileAttributes = new ArrayList<>();
try { try {
String command = format("\"%s\"", Config.getInstance().getPathFor(MkvToolNix.MKV_MERGER)); String command = format("\"%s\"", Config.getInstance().getPathFor(MkvToolNix.MKV_MERGE));
String[] arguments = new String[]{ String[] arguments = new String[]{
command, command,
"--identify", "--identify",

View File

@@ -4,7 +4,7 @@ import lombok.AllArgsConstructor;
@AllArgsConstructor @AllArgsConstructor
public enum MkvToolNix { public enum MkvToolNix {
MKV_MERGER("mkvmerge"), MKV_MERGE("mkvmerge"),
MKV_PROP_EDIT("mkvpropedit"); MKV_PROP_EDIT("mkvpropedit");
private final String file; private final String file;

View File

@@ -1,19 +1,54 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.config; package at.pcgamingfreaks.mkvaudiosubtitlechanger.config;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.Main; import at.pcgamingfreaks.mkvaudiosubtitlechanger.Main;
import org.apache.commons.lang3.SystemUtils;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import picocli.CommandLine; import picocli.CommandLine;
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.util.CommandLineOptionsUtil.optionOf; import java.io.File;
import java.nio.file.Path;
import java.util.function.Function;
import java.util.stream.Stream;
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.util.TestUtil.args; import static at.pcgamingfreaks.mkvaudiosubtitlechanger.util.TestUtil.args;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
class MkvToolNixPathConfigParameterTest { class MkvToolNixPathConfigParameterTest {
private static final String TEST_INVALID_DIR = "src/test/resources/test-dir";
private static final String TEST_MKVTOOLNIX_DIR = "src/test/resources/mkvtoolnix";
private static final String TEST_MKVTOOLNIX_EXE_DIR = "src/test/resources/mkvtoolnix_exe";
private static Stream<Arguments> provideTestCases() {
if (SystemUtils.IS_OS_WINDOWS) {
return Stream.of(
Arguments.of(args("-m", TEST_MKVTOOLNIX_EXE_DIR), TEST_MKVTOOLNIX_EXE_DIR, (Function<Config, File>) Config::getMkvToolNix),
Arguments.of(args("--mkvtoolnix", TEST_MKVTOOLNIX_EXE_DIR), TEST_MKVTOOLNIX_EXE_DIR, (Function<Config, File>) Config::getMkvToolNix)
);
}
return Stream.of(
Arguments.of(args("-m", TEST_MKVTOOLNIX_DIR), TEST_MKVTOOLNIX_DIR, (Function<Config, File>) Config::getMkvToolNix),
Arguments.of(args("--mkvtoolnix", TEST_MKVTOOLNIX_DIR), TEST_MKVTOOLNIX_DIR, (Function<Config, File>) Config::getMkvToolNix)
);
}
@ParameterizedTest
@MethodSource("provideTestCases")
void validate(String[] cmdArgs, String expected, Function<Config, File> fieldUnderTest) {
Main sut = new Main();
CommandLine.populateCommand(sut, cmdArgs);
assertEquals(Path.of(expected).toFile().getAbsolutePath(), fieldUnderTest.apply(sut.getConfig()).getAbsolutePath());
}
@Test @Test
void validate() { void validate() {
Main sut = new Main(); Main sut = new Main();
assertThrows(CommandLine.ParameterException.class, () -> CommandLine.populateCommand(sut, args("-m", "./"))); assertThrows(CommandLine.ParameterException.class, () -> CommandLine.populateCommand(sut, args("-m", TEST_INVALID_DIR)));
assertThrows(CommandLine.ParameterException.class, () -> CommandLine.populateCommand(sut, args("-m"))); assertThrows(CommandLine.ParameterException.class, () -> CommandLine.populateCommand(sut, args("-m")));
assertThrows(CommandLine.ParameterException.class, () -> CommandLine.populateCommand(sut, args("")));
} }
} }