Skip to content

Commit

Permalink
! r refactored getProgramFiles
Browse files Browse the repository at this point in the history
Co-Authored-By: Llewellyn Falco <[email protected]>
  • Loading branch information
LarsEckart and isidore committed Oct 23, 2023
1 parent 4023584 commit 0b011ad
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,45 @@

import static org.junit.jupiter.api.Assertions.assertNotEquals;

class IntelliJResolverTest {
@Test
void testFindItOnMac() {
Queryable<String> validPaths = Queryable.as("/Applications/IntelliJ IDEA.app/Contents/MacOS/idea",
"Users/lars/Applications/IntelliJ IDEA Ultimate.app/Contents/MacOS/idea",
"Users/lars/Applications/IntelliJ IDEA Community.app/Contents/MacOS/idea",
"Users/lars/Applications/IntelliJ IDEA Community Edition.app/Contents/MacOS/idea");
verifyPaths(IntelliJToolboxResolver::getDiffInfoMac, "Users/lars", validPaths);
class IntelliJResolverTest
{
@Test
void testFindItOnMac()
{
Queryable<String> validPaths = Queryable.as("/Applications/IntelliJ IDEA.app/Contents/MacOS/idea",
"Users/lars/Applications/IntelliJ IDEA Ultimate.app/Contents/MacOS/idea",
"Users/lars/Applications/IntelliJ IDEA Community.app/Contents/MacOS/idea",
"Users/lars/Applications/IntelliJ IDEA Community Edition.app/Contents/MacOS/idea");
verifyPaths(IntelliJToolboxResolver::getDiffInfoMac, "Users/lars", validPaths);
}
private static void verifyPaths(Function2<String, Function1<String, Boolean>, DiffInfo> finder, String userHome,
Queryable<String> validPaths)
{
for (String path : validPaths)
{
DiffInfo diffInfo = finder.call(userHome, f -> f.equals(path));
assertNotEquals("", diffInfo.diffProgram, path);
}

private static void verifyPaths(Function2<String, Function1<String, Boolean>, DiffInfo> finder, String userHome, Queryable<String> validPaths) {
for (String path : validPaths) {
DiffInfo diffInfo = finder.call(userHome, f -> f.equals(path));
assertNotEquals("", diffInfo.diffProgram, path);
}
}

@Test
void testFindItOnLinux() {
Queryable<String> validPaths = Queryable.as(
"/home/lars/.local/share/JetBrains/Toolbox/apps/intellij-idea-ultimate/bin/idea.sh",
"/home/lars/.local/share/JetBrains/Toolbox/apps/intellij-idea-community-edition/bin/idea.sh");
verifyPaths(IntelliJToolboxResolver::getDiffInfoLinux, "/home/lars", validPaths);
}

// "C:\Users\larse\AppData\Local\Programs\IntelliJ IDEA Ultimate\bin\idea64.exe"
@Test
void testFindItOnWindows() {
Queryable<String> validPaths = Queryable.as(
"C:\\Users\\larse\\AppData\\Local\\Programs\\IntelliJ IDEA Ultimate\\bin\\idea64.exe");
String[] programFiles = {"C:\\Users\\larse\\AppData\\Local\\Programs"};
for (String path : validPaths) {
DiffInfo diffInfo = IntelliJToolboxResolver.getDiffInfoWindows(programFiles, f -> f.equals(path));
assertNotEquals("", diffInfo.diffProgram, path);
}
}
@Test
void testFindItOnLinux()
{
Queryable<String> validPaths = Queryable.as(
"/home/lars/.local/share/JetBrains/Toolbox/apps/intellij-idea-ultimate/bin/idea.sh",
"/home/lars/.local/share/JetBrains/Toolbox/apps/intellij-idea-community-edition/bin/idea.sh");
verifyPaths(IntelliJToolboxResolver::getDiffInfoLinux, "/home/lars", validPaths);
}
// "C:\Users\larse\AppData\Local\Programs\IntelliJ IDEA Ultimate\bin\idea64.exe"
@Test
void testFindItOnWindows()
{
Queryable<String> validPaths = Queryable
.as("C:\\Users\\larse\\AppData\\Local\\Programs\\IntelliJ IDEA Ultimate\\bin\\idea64.exe");
String[] programFiles = {"C:\\Users\\larse\\AppData\\Local\\Programs"};
for (String path : validPaths)
{
DiffInfo diffInfo = IntelliJToolboxResolver.getDiffInfoWindows(programFiles, f -> f.equals(path));
assertNotEquals("", diffInfo.diffProgram, path);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.spun.util.ArrayUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -60,12 +61,25 @@ public boolean isEmpty()
}
public static String[] getProgramFilesPaths()
{
HashSet<String> paths = new HashSet<>();
paths.add(System.getenv("ProgramFiles(x86)"));
paths.add(System.getenv("ProgramFiles"));
paths.add(System.getenv("ProgramW6432"));
paths.add(System.getenv("LOCALAPPDATA") + "\\Programs");
return paths.stream().filter(Objects::nonNull).toArray(String[]::new);
List<String> paths = new ArrayList<>();
addIfNotNull("ProgramFiles(x86)", paths);
addIfNotNull("ProgramFiles(x86)", paths);
addIfNotNull("ProgramFiles", paths);
addIfNotNull("ProgramW6432", paths);
addIfNotNull("LOCALAPPDATA", paths, "\\Programs");
return paths.toArray(new String[0]);
}
private static void addIfNotNull(String envVarName, List<String> paths)
{
addIfNotNull(envVarName, paths, "");
}
private static void addIfNotNull(String envVarName, List<String> paths, String postfix)
{
String path = System.getenv(envVarName);
if (path != null)
{
paths.add(path + postfix);
}
}
public static class One
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public class IntelliJToolboxResolver
{
private interface Resolver extends Function2<String, Function1<String, Boolean>, DiffInfo>
{

}
public static DiffInfo findIt()
{
Expand Down Expand Up @@ -43,15 +42,16 @@ public static DiffInfo getDiffInfoLinux(String userHome, Function1<String, Boole
String postfix = "/bin/idea.sh";
return getDiffInfo(fileExists, applications, locations, postfix, "/");
}
public static DiffInfo getDiffInfoWindows(String[] programFiles, Function1<String, Boolean> fileExists) {
public static DiffInfo getDiffInfoWindows(String[] programFiles, Function1<String, Boolean> fileExists)
{
Queryable<String> applications = as("IntelliJ IDEA Ultimate", "IntelliJ IDEA", "IntelliJ IDEA Community",
"IntelliJ IDEA Community Edition");
"IntelliJ IDEA Community Edition");
Queryable<String> locations = Queryable.as(programFiles);
String postfix = "\\bin\\idea64.exe";
return getDiffInfo(fileExists, locations, applications, postfix, "\\");
}
private static DiffInfo getDiffInfo(Function1<String, Boolean> fileExists, Queryable<String> locations,
Queryable<String> applications, String postfix, String pathSeparator)
Queryable<String> applications, String postfix, String pathSeparator)
{
Queryable<String> paths = locations.selectMany(l -> applications.select(a -> l + pathSeparator + a + postfix));
String matching = paths.first(fileExists);
Expand Down

0 comments on commit 0b011ad

Please sign in to comment.