Skip to content

Commit

Permalink
Prepare to merge to main
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarsotovalero committed Oct 20, 2022
1 parent 139859e commit cd2961b
Show file tree
Hide file tree
Showing 21 changed files with 944 additions and 41 deletions.
70 changes: 30 additions & 40 deletions src/main/java/se/kth/deptrim/mojo/DepTrimManager.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
package se.kth.deptrim.mojo;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.jar.JarOutputStream;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
Expand All @@ -28,6 +23,8 @@
import se.kth.depclean.core.util.JarUtils;
import se.kth.depclean.core.wrapper.DependencyManagerWrapper;
import se.kth.depclean.core.wrapper.LogWrapper;
import se.kth.depclean.util.MavenInvoker;
import se.kth.deptrim.mojo.util.TimeUtils;

/**
* Runs the DepTrim process, regardless of a specific dependency manager.
Expand Down Expand Up @@ -91,14 +88,15 @@ public ProjectDependencyAnalysis execute() throws AnalysisFailureException {
});

printString("STARTING TRIMMING DEPENDENCIES");
debloatLibClasses(analysis, trimDependencies);
trimLibClasses(analysis, trimDependencies);

// ************************ Code added ********************** //

// analysis.print();

final long stopTime = System.currentTimeMillis();
getLog().info("Analysis done in " + getTime(stopTime - startTime));
TimeUtils timeUtils = new TimeUtils();
getLog().info("Analysis done in " + timeUtils.toHumanReadableTime(stopTime - startTime));

return analysis;
}
Expand Down Expand Up @@ -137,7 +135,7 @@ private void extractLibClasses() {
* @param trimDependencies The dependencies to be trimmed, if empty then trims all the dependencies.
*/
@SneakyThrows
private void debloatLibClasses(ProjectDependencyAnalysis analysis, Set<String> trimDependencies) {
private void trimLibClasses(ProjectDependencyAnalysis analysis, Set<String> trimDependencies) {
analysis
.getDependencyClassesMap()
.forEach((key, value) -> {
Expand All @@ -152,54 +150,52 @@ private void debloatLibClasses(ProjectDependencyAnalysis analysis, Set<String> t
File srcDir = dependencyManager.getBuildDirectory().resolve(DIRECTORY_TO_EXTRACT_DEPENDENCIES + File.separator + dependencyDirName).toFile();
File destDir = dependencyManager.getBuildDirectory().resolve(DIRECTORY_TO_LOCATE_THE_DEBLOATED_DEPENDENCIES + File.separator + dependencyDirName).toFile();
getLog().info("copying from files from " + srcDir.getAbsolutePath() + " to " + destDir.getAbsolutePath());
// copy all files from srcDir to destDir

// Copy all files from srcDir to destDir
try {
FileUtils.copyDirectory(srcDir, destDir);
} catch (IOException e) {
getLog().error("Error copying files from " + srcDir + " to " + destDir);
}
// remove files in destDir
// Remove files in destDir.
for (ClassName className : unusedTypes) {
String fileName = className.toString().replace(".", File.separator) + ".class";
File file = new File(destDir.getAbsolutePath() + File.separator + fileName);
printString("Removing file " + file.getAbsolutePath());
file.delete();
}
// Delete all empty directories in destDir
deleteEmptyDirectories(destDir);
// Delete all empty directories in destDir.
se.kth.deptrim.mojo.util.FileUtils fileUtils = new se.kth.deptrim.mojo.util.FileUtils();
fileUtils.deleteEmptyDirectories(destDir);

// Create a new jar file with the debloated classes and move it to lib-deptrim
Path libDeptrimPath = Paths.get("lib-deptrim");
// Create a new jar file with the debloated classes and move it to libs-deptrim.
Path libDeptrimPath = Paths.get("libs-deptrim");
String jarName = destDir.getName() + ".jar";
File jarFile = libDeptrimPath.resolve(jarName).toFile();
try {
Files.createDirectories(libDeptrimPath); // create lib-deptrim directory if it does not exist
se.kth.deptrim.mojo.JarUtils.createJarFromDirectory(destDir, jarFile);
Files.createDirectories(libDeptrimPath); // create libs-deptrim directory if it does not exist
se.kth.deptrim.mojo.util.JarUtils.createJarFromDirectory(destDir, jarFile);
} catch (Exception e) {
getLog().error("Error creating trimmed jar for " + destDir.getName());
}

// Install the dependency in the local repository.
try {
MavenInvoker.runCommand(
"mvn deploy:deploy-file -Durl=" + libDeptrimPath +
" -Dpackaging=jar" +
" -Dfile=" + jarFile.getAbsolutePath() +
" -DgroupId=" + key.getGroupId() +
" -DartifactId=" + key.getDependencyId() +
" -Dversion=" + key.getVersion(),
null);
} catch (IOException | InterruptedException e) {
getLog().error("Error installing the trimmed dependency jar in local repo");
}
}
});
}

/**
* Delete all empty directories in the given directory.
*
* @param directory the directory to delete empty directories from
*/
private int deleteEmptyDirectories(File directory) {
List<File> toBeDeleted = Arrays.stream(directory.listFiles()).sorted()
.filter(File::isDirectory)
.filter(f -> f.listFiles().length == deleteEmptyDirectories(f))
.collect(Collectors.toList());
int size = toBeDeleted.size();
toBeDeleted.forEach(t -> {
final String path = t.getAbsolutePath();
final boolean delete = t.delete();
});
return size; // the number of deleted directories.
}

private void copyDependencies(Dependency dependency, File destFolder) {
copyDependencies(dependency.getFile(), destFolder);
}
Expand Down Expand Up @@ -307,12 +303,6 @@ private Dependency findDependency(Set<Dependency> allDependencies, String depend
.orElse(null);
}

private String getTime(long millis) {
long minutes = TimeUnit.MILLISECONDS.toMinutes(millis);
long seconds = (TimeUnit.MILLISECONDS.toSeconds(millis) % 60);
return String.format("%smin %ss", minutes, seconds);
}

private void printString(final String string) {
System.out.println(string); //NOSONAR avoid a warning of non-used logger
}
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/se/kth/deptrim/mojo/util/FileUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package se.kth.deptrim.mojo.util;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* Utility class for handling files.
*/
public class FileUtils {

/**
* Delete all empty directories in the given directory.
*
* @param directory the directory to delete empty directories from
*/
public int deleteEmptyDirectories(File directory) {
List<File> toBeDeleted = Arrays.stream(directory.listFiles()).sorted()
.filter(File::isDirectory)
.filter(f -> f.listFiles().length == deleteEmptyDirectories(f))
.collect(Collectors.toList());
int size = toBeDeleted.size();
toBeDeleted.forEach(t -> {
final String path = t.getAbsolutePath();
final boolean delete = t.delete();
});
return size; // the number of deleted directories.
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package se.kth.deptrim.mojo;
package se.kth.deptrim.mojo.util;

import java.io.BufferedInputStream;
import java.io.File;
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/se/kth/deptrim/mojo/util/PomUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package se.kth.deptrim.mojo.util;

public class PomUtils {

// TODO

}
22 changes: 22 additions & 0 deletions src/main/java/se/kth/deptrim/mojo/util/TimeUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package se.kth.deptrim.mojo.util;

import java.util.concurrent.TimeUnit;

/**
* Utility class for handling time.
*/
public class TimeUtils {

/**
* Convert milliseconds to a human-readable format.
*
* @param millis The milliseconds to be converted.
* @return A string representing the time.
*/
public String toHumanReadableTime(long millis) {
long minutes = TimeUnit.MILLISECONDS.toMinutes(millis);
long seconds = (TimeUnit.MILLISECONDS.toSeconds(millis) % 60);
return String.format("%smin %ss", minutes, seconds);
}

}
1 change: 1 addition & 0 deletions src/test/java/se/kth/deptrim/mojo/JarUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.rules.TemporaryFolder;
import se.kth.deptrim.mojo.util.JarUtils;

class JarUtilsTest {

Expand Down
20 changes: 20 additions & 0 deletions src/test/java/se/kth/deptrim/mojo/util/TimeUtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package se.kth.deptrim.mojo.util;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class TimeUtilsTest {

TimeUtils timeUtils;

@BeforeEach
void setUp() {
timeUtils = new TimeUtils();
}

@Test
void ifGettingTheTimeThenTimeIsCorrect() {
Assertions.assertEquals("1min 40s", timeUtils.toHumanReadableTime(100000));
}
}
8 changes: 8 additions & 0 deletions src/test/resources/kryo-serializers/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions src/test/resources/kryo-serializers/.idea/aws.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit cd2961b

Please sign in to comment.