Skip to content

Commit

Permalink
[core] Refactor artifact filter by extension
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmiray committed Dec 19, 2021
1 parent e73ecc7 commit 57fc46b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
Expand Up @@ -25,6 +25,7 @@
import org.jreleaser.model.Project;
import org.jreleaser.model.Tool;
import org.jreleaser.util.Errors;
import org.jreleaser.util.FileType;
import org.jreleaser.util.PlatformUtils;

import java.util.ArrayList;
Expand All @@ -46,7 +47,6 @@
import static org.jreleaser.model.validation.SdkmanValidator.validateSdkman;
import static org.jreleaser.model.validation.SnapValidator.validateSnap;
import static org.jreleaser.model.validation.SpecValidator.validateSpec;
import static org.jreleaser.util.StringUtils.getFilenameExtension;
import static org.jreleaser.util.StringUtils.isBlank;
import static org.jreleaser.util.StringUtils.isNotBlank;

Expand Down Expand Up @@ -152,10 +152,7 @@ private static void validateDistribution(JReleaserContext context, Distribution
byPlatform.forEach((p, artifacts) -> {
String platform = "<nil>".equals(p) ? "no" : p;
artifacts.stream()
.collect(groupingBy(artifact -> {
String ext = getFilenameExtension(artifact.getPath());
return isNotBlank(ext) ? ext : "";
}))
.collect(groupingBy(artifact -> FileType.getFileType(artifact.getPath())))
.forEach((ext, matches) -> {
if (matches.size() > 1) {
errors.configuration(RB.$("validation_distributions_multiple",
Expand Down
Expand Up @@ -28,7 +28,6 @@
import org.jreleaser.util.Algorithm;
import org.jreleaser.util.FileType;
import org.jreleaser.util.FileUtils;
import org.jreleaser.util.StringUtils;
import org.jreleaser.util.command.Command;
import org.jreleaser.util.command.CommandException;
import org.jreleaser.util.command.CommandExecutor;
Expand All @@ -49,7 +48,6 @@
import java.util.stream.Collectors;

import static org.jreleaser.util.Constants.KEY_ARTIFACT_ARCH;
import static org.jreleaser.util.Constants.KEY_ARTIFACT_ARCHIVE_FORMAT;
import static org.jreleaser.util.Constants.KEY_ARTIFACT_FILE;
import static org.jreleaser.util.Constants.KEY_ARTIFACT_FILE_EXTENSION;
import static org.jreleaser.util.Constants.KEY_ARTIFACT_FILE_FORMAT;
Expand All @@ -62,7 +60,6 @@
import static org.jreleaser.util.Constants.KEY_ARTIFACT_VERSION;
import static org.jreleaser.util.Constants.KEY_DISTRIBUTION_ARTIFACT;
import static org.jreleaser.util.Constants.KEY_DISTRIBUTION_ARTIFACT_ARCH;
import static org.jreleaser.util.Constants.KEY_DISTRIBUTION_ARTIFACT_ARCHIVE_FORMAT;
import static org.jreleaser.util.Constants.KEY_DISTRIBUTION_ARTIFACT_FILE;
import static org.jreleaser.util.Constants.KEY_DISTRIBUTION_ARTIFACT_FILE_EXTENSION;
import static org.jreleaser.util.Constants.KEY_DISTRIBUTION_ARTIFACT_FILE_FORMAT;
Expand Down Expand Up @@ -442,7 +439,7 @@ protected List<Artifact> collectArtifacts(Distribution distribution) {
.filter(artifact -> !isSkipped(artifact))
// sort by platform, then by extension
.sorted(Artifact.comparatorByPlatform().thenComparingInt(artifact -> {
String ext = "." + StringUtils.getFilenameExtension(artifact.getPath());
String ext = FileType.getFileNameExtension(artifact.getPath());
return fileExtensions.indexOf(ext);
}))
.collect(Collectors.toList());
Expand Down
Expand Up @@ -17,6 +17,7 @@
*/
package org.jreleaser.util;

import java.nio.file.Path;
import java.util.LinkedHashSet;
import java.util.Set;

Expand Down Expand Up @@ -84,4 +85,42 @@ public static Set<String> getSupportedExtensions() {
}
return set;
}

public static String getFileType(Path path) {
if (null != path) {
return getFileType(path.getFileName().toString());
}
return "";
}

public static String getFileType(String path) {
if (isBlank(path)) return "";

for (FileType value : values()) {
if (path.endsWith(value.extension())) {
return value.type();
}
}

return "";
}

public static String getFileNameExtension(Path path) {
if (null != path) {
return getFileNameExtension(path.getFileName().toString());
}
return "";
}

public static String getFileNameExtension(String path) {
if (isBlank(path)) return "";

for (FileType value : values()) {
if (path.endsWith(value.extension())) {
return value.extension();
}
}

return "";
}
}

0 comments on commit 57fc46b

Please sign in to comment.