Skip to content

Commit

Permalink
[BUG] Detect newest file with the shortest name
Browse files Browse the repository at this point in the history
  • Loading branch information
hypherionmc committed Apr 25, 2024
1 parent 8710fd7 commit ab3351a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,84 +80,39 @@ void mergeJars() throws IOException {
if (forgeSettings.getJarLocation() != null) {
forgeJar = new File(forgeProject.getProjectDir(), forgeSettings.getJarLocation());
} else {
int i = 0;
for (File file : new File(forgeProject.getBuildDir(), "libs").listFiles()) {
if (file.isDirectory()) continue;
if (io.github.pacifistmc.forgix.utils.FileUtils.isZipFile(file)) {
if (file.getName().length() < i || i == 0) {
i = file.getName().length();
forgeJar = file;
}
}
}
forgeJar = io.github.pacifistmc.forgix.utils.FileUtils.findLatestFile(new File(forgeProject.getBuildDir(), "libs"));
}
}

if (neoforgeProject != null) {
if (neoforgeSettings.getJarLocation() != null) {
neoforgeJar = new File(neoforgeProject.getProjectDir(), neoforgeSettings.getJarLocation());
} else {
int i = 0;
for (File file : new File(neoforgeProject.getBuildDir(), "libs").listFiles()) {
if (file.isDirectory()) continue;
if (io.github.pacifistmc.forgix.utils.FileUtils.isZipFile(file)) {
if (file.getName().length() < i || i == 0) {
i = file.getName().length();
neoforgeJar = file;
}
}
}
neoforgeJar = io.github.pacifistmc.forgix.utils.FileUtils.findLatestFile(new File(neoforgeProject.getBuildDir(), "libs"));
}
}

if (fabricProject != null) {
if (fabricSettings.getJarLocation() != null) {
fabricJar = new File(fabricProject.getProjectDir(), fabricSettings.getJarLocation());
} else {
int i = 0;
for (File file : new File(fabricProject.getBuildDir(), "libs").listFiles()) {
if (file.isDirectory()) continue;
if (io.github.pacifistmc.forgix.utils.FileUtils.isZipFile(file)) {
if (file.getName().length() < i || i == 0) {
i = file.getName().length();
fabricJar = file;
}
}
}
fabricJar = io.github.pacifistmc.forgix.utils.FileUtils.findLatestFile(new File(fabricProject.getBuildDir(), "libs"));
}
}

if (quiltProject != null) {
if (quiltSettings.getJarLocation() != null) {
quiltJar = new File(quiltProject.getProjectDir(), quiltSettings.getJarLocation());
} else {
int i = 0;
for (File file : new File(quiltProject.getBuildDir(), "libs").listFiles()) {
if (file.isDirectory()) continue;
if (io.github.pacifistmc.forgix.utils.FileUtils.isZipFile(file)) {
if (file.getName().length() < i || i == 0) {
i = file.getName().length();
quiltJar = file;
}
}
}
quiltJar = io.github.pacifistmc.forgix.utils.FileUtils.findLatestFile(new File(quiltProject.getBuildDir(), "libs"));
}
}

for (Map.Entry<Project, ForgixMergeExtension.CustomContainer> entry : customProjects.entrySet()) {
if (entry.getValue().getJarLocation() != null) {
customJars.put(entry.getValue(), new File(entry.getKey().getProjectDir(), entry.getValue().getJarLocation()));
} else {
int i = 0;
for (File file : new File(entry.getKey().getBuildDir(), "libs").listFiles()) {
if (file.isDirectory()) continue;
if (io.github.pacifistmc.forgix.utils.FileUtils.isZipFile(file)) {
if (file.getName().length() < i || i == 0) {
i = file.getName().length();
customJars.put(entry.getValue(), file);
}
}
}
customJars.put(entry.getValue(), io.github.pacifistmc.forgix.utils.FileUtils.findLatestFile(new File(entry.getKey().getBuildDir(), "libs")));
}
}

Expand Down
40 changes: 40 additions & 0 deletions src/main/java/io/github/pacifistmc/forgix/utils/FileUtils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.pacifistmc.forgix.utils;

import org.apache.commons.io.FilenameUtils;
import org.jetbrains.annotations.Nullable;

import java.io.*;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -229,6 +230,45 @@ private static boolean isBinary(File file) {
}
}

/**
* Try to find the latest file with the shortest name in a directory
* @param directory The directory to find the file in
* @return The newest file or null if no files were found
*/
@Nullable
public static File findLatestFile(File directory) {
File latestFile = null;
long lastModifiedTime = Long.MIN_VALUE;
int i = 0;
List<File> processedFiles = new ArrayList<>();

File[] files = directory.listFiles();
if (files == null)
return null;

for (File f : files) {
if (f.isDirectory() || !isZipFile(f))
continue;

if (f.getName().contains("-shadow") || f.getName().contains("-sources") || f.getName().contains("-dev"))
continue;

if (f.lastModified() > lastModifiedTime) {
lastModifiedTime = f.lastModified();
processedFiles.add(f);
}
}

for (File f : processedFiles) {
if (f.getName().length() < i || i == 0) {
latestFile = f;
i = f.getName().length();
}
}

return latestFile;
}

/**
* This method returns true if the character is a magic character that's used in binary files
* @return If it's a magic character
Expand Down

0 comments on commit ab3351a

Please sign in to comment.