Skip to content

Commit

Permalink
1.2.6
Browse files Browse the repository at this point in the history
- Fixed issues with updating outdated programs
- Excluded BSOD crash logs from being deleted as junk
- Updated CPU-Z to version 2.12.0
- Updated DDU to version 18.0.8.7
- Bug fixes and improvements
  • Loading branch information
Foulest committed Nov 24, 2024
1 parent ece1acc commit 3ba2883
Show file tree
Hide file tree
Showing 13 changed files with 392 additions and 358 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group = 'net.foulest'
version = '1.2.5'
version = '1.2.6'
description = 'RepairKit'

// Set the language level to Java 17
Expand Down
450 changes: 207 additions & 243 deletions src/main/java/net/foulest/repairkit/panels/AutomaticRepairs.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private void setupCPUZ() {

// Adds a description label for CPU-Z.
DebugUtil.debug("Creating the CPU-Z description label...");
JLabel description = SwingUtil.createLabel("Version: 2.11.0",
JLabel description = SwingUtil.createLabel("Version: 2.12.0",
new Rectangle(baseWidth + 43, baseHeight + 20, 200, 30),
new Font(ConstantUtil.ARIAL, Font.BOLD, 12)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ private void setupNVCleanstall() {
}

/**
* Sets up the DDU section.
* Sets up the Display Driver Uninstaller (DDU) section.
*/
private void setupDDU() {
int baseHeight = 150;
Expand All @@ -172,7 +172,7 @@ private void setupDDU() {

// Adds a description label for DDU.
DebugUtil.debug("Creating the DDU description label...");
JLabel description = SwingUtil.createLabel("Version: 18.0.8.4",
JLabel description = SwingUtil.createLabel("Version: 18.0.8.7",
new Rectangle(baseWidth + 43, baseHeight + 20, 200, 30),
new Font(ConstantUtil.ARIAL, Font.BOLD, 12)
);
Expand All @@ -190,7 +190,7 @@ private void setupDDU() {
new Color(200, 200, 200), () -> {
// Unzips and launches DDU.
String path = FileUtil.tempDirectory.getPath();
SwingUtil.launchApplication("DDU.7z", "\\DDU.exe", true, path);
SwingUtil.launchApplication("DDU.7z", "\\Display Driver Uninstaller.exe", true, path);
}
);
add(appButton);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/net/foulest/repairkit/util/DebugUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public static void debug(@NotNull String message) {
* Prints a warning message to a log file.
*
* @param message The message to print.
* @param ex The exception to print.
*/
public static void warn(@NotNull String message, @NotNull Exception ex) {
Throwable cause = ex.getCause();
Expand Down
114 changes: 55 additions & 59 deletions src/main/java/net/foulest/repairkit/util/JunkFileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,79 +63,75 @@ public class JunkFileUtil {
@SuppressWarnings({"unchecked", "NestedMethodCall"})
public static void removeJunkFiles() {
// Gets the file extensions to scan for from the config file.
try {
ConfigLoader configLoader = new ConfigLoader(FileUtil.getConfigFile("junkfiles.json"));
Map<String, Object> junkFilesConfig = configLoader.getConfig().get("junkFiles");
ConfigLoader configLoader = new ConfigLoader(FileUtil.getConfigFile("junkfiles.json"));
Map<String, Object> junkFilesConfig = configLoader.getConfig().get("junkFiles");

// Returns if the config file is missing.
if (junkFilesConfig == null) {
return;
}

// Returns if the feature is disabled.
if (junkFilesConfig.get("enabled") != null
&& !junkFilesConfig.get("enabled").equals(Boolean.TRUE)) {
return;
}
// Returns if the config file is missing.
if (junkFilesConfig == null) {
return;
}

// Gets the file extensions to scan for from the config file.
Object fileExtensions = junkFilesConfig.get("fileExtensions");
if (fileExtensions != null && !((Collection<String>) fileExtensions).isEmpty()) {
JUNK_FILE_EXTENSIONS = Set.copyOf((Collection<String>) fileExtensions);
}
// Returns if the feature is disabled.
if (junkFilesConfig.get("enabled") != null
&& !junkFilesConfig.get("enabled").equals(Boolean.TRUE)) {
return;
}

// Gets the paths to exclude from scanning from the config file.
if (junkFilesConfig.get("excludedPaths") != null
&& !((Collection<String>) junkFilesConfig.get("excludedPaths")).isEmpty()) {
Set<Path> excludedPaths = new HashSet<>();
// Gets the file extensions to scan for from the config file.
Object fileExtensions = junkFilesConfig.get("fileExtensions");
if (fileExtensions != null && !((Collection<String>) fileExtensions).isEmpty()) {
JUNK_FILE_EXTENSIONS = Set.copyOf((Collection<String>) fileExtensions);
}

for (String path : (Iterable<String>) junkFilesConfig.get("excludedPaths")) {
String fixedPath = path.replace("%temp%", System.getenv("TEMP"));
excludedPaths.add(Paths.get(fixedPath));
}
// Gets the paths to exclude from scanning from the config file.
if (junkFilesConfig.get("excludedPaths") != null
&& !((Collection<String>) junkFilesConfig.get("excludedPaths")).isEmpty()) {
Set<Path> excludedPaths = new HashSet<>();

EXCLUDED_PATHS = Set.copyOf(excludedPaths);
for (String path : (Iterable<String>) junkFilesConfig.get("excludedPaths")) {
String fixedPath = path.replace("%temp%", System.getenv("TEMP"));
excludedPaths.add(Paths.get(fixedPath));
}

// Collects data for analytics.
long now = System.currentTimeMillis();
totalCount = 0;
totalSize = 0;
EXCLUDED_PATHS = Set.copyOf(excludedPaths);
}

List<Runnable> tasks = new ArrayList<>(List.of());
// Collects data for analytics.
long now = System.currentTimeMillis();
totalCount = 0;
totalSize = 0;

// Empties the Recycle Bin.
if (junkFilesConfig.get("emptyRecycleBin") != null
&& junkFilesConfig.get("emptyRecycleBin").equals(Boolean.TRUE)) {
tasks.add(() -> CommandUtil.runPowerShellCommand("Clear-RecycleBin -Force", false));
}
List<Runnable> tasks = new ArrayList<>(List.of());

// Deletes files in the Temp directory older than one day.
if (junkFilesConfig.get("cleanUserTempFiles") != null
&& junkFilesConfig.get("cleanUserTempFiles").equals(Boolean.TRUE)) {
tasks.add(() -> CommandUtil.runPowerShellCommand("Get-ChildItem -Path $env:TEMP -Recurse | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-1) } | Remove-Item -Recurse -Force", false));
}
// Empties the Recycle Bin.
if (junkFilesConfig.get("emptyRecycleBin") != null
&& junkFilesConfig.get("emptyRecycleBin").equals(Boolean.TRUE)) {
tasks.add(() -> CommandUtil.runPowerShellCommand("Clear-RecycleBin -Force", false));
}

// Deletes files in the Windows temp directory.
if (junkFilesConfig.get("cleanSystemTempFiles") != null
&& junkFilesConfig.get("cleanSystemTempFiles").equals(Boolean.TRUE)) {
tasks.add(() -> CommandUtil.runPowerShellCommand("Get-ChildItem -Path $env:windir\\Temp -Recurse | Remove-Item -Recurse -Force", false));
}
// Deletes files in the Temp directory older than one day.
if (junkFilesConfig.get("cleanUserTempFiles") != null
&& junkFilesConfig.get("cleanUserTempFiles").equals(Boolean.TRUE)) {
tasks.add(() -> CommandUtil.runPowerShellCommand("Get-ChildItem -Path $env:TEMP -Recurse | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-1) } | Remove-Item -Recurse -Force", false));
}

// Scans each drive for junk files.
Iterable<Path> rootDirectories = FileSystems.getDefault().getRootDirectories();
for (Path root : rootDirectories) {
tasks.add(() -> pool.invoke(new DirectoryScanTask(root)));
}
// Deletes files in the Windows temp directory.
if (junkFilesConfig.get("cleanSystemTempFiles") != null
&& junkFilesConfig.get("cleanSystemTempFiles").equals(Boolean.TRUE)) {
tasks.add(() -> CommandUtil.runPowerShellCommand("Get-ChildItem -Path $env:windir\\Temp -Recurse | Remove-Item -Recurse -Force", false));
}

// Executes tasks using TaskUtil.
TaskUtil.executeTasks(tasks);
DebugUtil.debug("Junk files found: " + totalCount);
DebugUtil.debug("Total size: " + totalSize);
DebugUtil.debug("Time taken: " + (System.currentTimeMillis() - now) + "ms");
} catch (IOException ex) {
DebugUtil.warn("Failed to load junkfiles.json configuration", ex);
// Scans each drive for junk files.
Iterable<Path> rootDirectories = FileSystems.getDefault().getRootDirectories();
for (Path root : rootDirectories) {
tasks.add(() -> pool.invoke(new DirectoryScanTask(root)));
}

// Executes tasks using TaskUtil.
TaskUtil.executeTasks(tasks);
DebugUtil.debug("Junk files found: " + totalCount);
DebugUtil.debug("Total size: " + totalSize);
DebugUtil.debug("Time taken: " + (System.currentTimeMillis() - now) + "ms");
}

@AllArgsConstructor
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/net/foulest/repairkit/util/SwingUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@
@Data
public class SwingUtil {

public static TrayIcon trayIcon;

/**
* Creates an action button without a tooltip.
*
Expand Down
159 changes: 112 additions & 47 deletions src/main/java/net/foulest/repairkit/util/WinGetUtil.java
Original file line number Diff line number Diff line change
@@ -1,43 +1,105 @@
package net.foulest.repairkit.util;

import com.sshtools.twoslices.Slice;
import com.sshtools.twoslices.Toast;
import com.sshtools.twoslices.ToastType;
import lombok.Cleanup;
import lombok.Data;
import net.foulest.repairkit.util.config.ConfigLoader;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
* Utility class for WinGet operations.
*
* @author Foulest
*/
@Data
@SuppressWarnings("resource")
public class WinGetUtil {

@SuppressWarnings({"unused", "OverlyBroadCatchBlock"})
/**
* Updates all outdated programs.
*/
@SuppressWarnings("unchecked")
public static void updateAllPrograms() {
try {
List<String> outdatedPackages = getOutdatedPrograms();
List<String> outdatedPrograms = getOutdatedPrograms();

if (outdatedPackages.isEmpty()) {
@Cleanup Slice noneFound = Toast.toast(ToastType.INFO, "RepairKit", "No outdated programs found.");
DebugUtil.debug("No outdated programs found.");
return;
if (outdatedPrograms.isEmpty()) {
Toast.toast(ToastType.INFO, "RepairKit", "No outdated programs found.");
DebugUtil.debug("No outdated programs found.");
return;
}

ConfigLoader configLoader = new ConfigLoader(FileUtil.getConfigFile("programs.json"));
Map<String, Object> config = configLoader.getConfig().get("excludedPrograms");

List<String> excludedPrograms;
Object values = config.get("values");

if (values == null || ((Collection<String>) values).isEmpty()) {
Toast.toast(ToastType.INFO, "RepairKit", "No excluded programs found.");
DebugUtil.debug("No excluded programs found.");
return;
}

excludedPrograms = new ArrayList<>((Collection<String>) values);
List<String> updatedPrograms = new ArrayList<>();
int excludedCount = 0;

for (String id : outdatedPrograms) {
excludedPrograms.stream().filter(excluded -> excluded.contains(id)).forEach(excluded -> {
DebugUtil.debug("Skipping excluded program: " + id);
updatedPrograms.add(id);
});

// Skips programs that have already been marked as updated.
if (updatedPrograms.contains(id)) {
++excludedCount;
continue;
}

Toast.toast(ToastType.INFO, "RepairKit", "Updating program: " + id);
DebugUtil.debug("Updating program: " + id);

if (updatePackage(id)) {
Toast.toast(ToastType.INFO, "RepairKit", "Updated program: " + id);
DebugUtil.debug("Updated program: " + id);
updatedPrograms.add(id);
} else {
Toast.toast(ToastType.INFO, "RepairKit", "Failed to update program: " + id);
DebugUtil.debug("Failed to update program: " + id);
updatedPrograms.add(id);
}

for (String id : outdatedPackages) {
@Cleanup Slice updating = Toast.toast(ToastType.INFO, "RepairKit", "Updating package: " + id);
DebugUtil.debug("Updating program: " + id);

if (updatePackage(id)) {
@Cleanup Slice updated = Toast.toast(ToastType.INFO, "RepairKit", "Updated program: " + id);
DebugUtil.debug("Updated program: " + id);
} else {
@Cleanup Slice failed = Toast.toast(ToastType.INFO, "RepairKit", "Failed to update program: " + id);
DebugUtil.debug("Failed to update program: " + id);
}
// Pause for a moment to allow the notification to fade.
try {
Thread.sleep(3000);
} catch (InterruptedException ex) {
DebugUtil.warn("Failed to sleep thread", ex);
Thread.currentThread().interrupt();
}
} catch (Exception ex) {
ex.printStackTrace();
}

// Busy-wait until all programs have been updated.
while (updatedPrograms.size() < outdatedPrograms.size()) {
try {
Thread.sleep(500);
} catch (InterruptedException ex) {
DebugUtil.warn("Failed to sleep thread", ex);
Thread.currentThread().interrupt();
}
}

// Displays a final notification message.
if (excludedCount == outdatedPrograms.size()) {
Toast.toast(ToastType.INFO, "RepairKit", "No outdated programs found.");
DebugUtil.debug("No outdated programs found.");
} else {
Toast.toast(ToastType.INFO, "RepairKit", "All programs are up to date.");
DebugUtil.debug("All programs are up to date.");
}
}

Expand All @@ -47,43 +109,46 @@ private static void installDependencies() {
CommandUtil.runPowerShellCommand("Install-Module -Name Microsoft.WinGet.Client -Force -Repository PSGallery | Out-Null", false);
}

@SuppressWarnings({"OverlyBroadCatchBlock", "unused"})
private static @NotNull List<String> getOutdatedPrograms() {
try {
@Cleanup Slice checking = Toast.toast(ToastType.INFO, "RepairKit", "Checking for outdated programs...");
DebugUtil.debug("Checking for outdated programs...");

// Installs the required dependencies for WinGet.
installDependencies();
Toast.toast(ToastType.INFO, "RepairKit", "Checking for outdated programs...");
DebugUtil.debug("Checking for outdated programs...");

// Gets the list of outdated packages.
List<String> output = CommandUtil.getPowerShellCommandOutput("Get-WinGetPackage -Source winget"
+ " | Where-Object IsUpdateAvailable | Select-Object -ExpandProperty Id", false, false);
// Installs the required dependencies for WinGet.
installDependencies();

List<String> packages = new ArrayList<>();
// Gets the list of outdated programs.
List<String> output = CommandUtil.getPowerShellCommandOutput("Get-WinGetPackage -Source winget"
+ " | Where-Object IsUpdateAvailable | Select-Object -ExpandProperty Id", false, false);

// Ignores empty lines.
for (String line : output) {
String trim = line.trim();
List<String> programs = new ArrayList<>();

if (trim.isEmpty()) {
continue;
}
// Ignores empty lines.
for (String line : output) {
DebugUtil.debug("Found program: " + line);
String trim = line.trim();

packages.add(trim);
if (trim.isEmpty()) {
DebugUtil.debug("Skipping blank program...");
continue;
}
return packages;
} catch (Exception ex) {
ex.printStackTrace();

DebugUtil.debug("Adding program: " + trim);
programs.add(trim);
}
return new ArrayList<>();

DebugUtil.debug("Found " + programs.size() + " outdated programs.");
DebugUtil.debug("Programs: " + programs);
return programs;
}

private static boolean updatePackage(String id) {
List<String> output = CommandUtil.getPowerShellCommandOutput("winget upgrade --id " + id
String output = CommandUtil.getPowerShellCommandOutput("winget upgrade --id " + id
+ " --disable-interactivity --silent --accept-package-agreements --accept-source-agreements",
false, false);
false, false).toString();

return !output.contains("The package cannot be upgraded");
return !output.contains("The package cannot be upgraded")
&& !output.contains("This package's version number cannot be determined")
&& !output.contains("Installer hash does not match")
&& !output.contains("No available upgrade found");
}
}
Loading

0 comments on commit 3ba2883

Please sign in to comment.