import org.slf4j.LoggerFactory;
import jalview.bin.Cache;
-import jalview.bin.Console;
import jalview.gui.Preferences;
import jalview.util.FileUtils;
import jalview.util.Platform;
String chimeraExe = isChimeraX ? "ChimeraX" : "chimera";
String[] versionsBlacklist = isChimeraX ? CHIMERAX_VERSIONS_BLACKLIST
: CHIMERA_VERSIONS_BLACKLIST;
+ String[] versionsWhitelist = isChimeraX ? CHIMERAX_VERSIONS
+ : CHIMERA_VERSIONS;
/*
* Jalview addition: check if path set in user preferences
}
else if (Platform.isWin())
{
- Set<File> installedChimera = new HashSet<>();
- String[] templates = new String[] { "%s\\%s %s\\bin\\%s.exe" };
+ String[] templates = new String[] {
+ "%s\\" + chimera + "%s\\bin\\" + chimeraExe + ".exe" };
String[] roots = new String[] { "\\Program Files",
"C:\\Program Files", "\\Program Files (x86)",
"C:\\Program Files (x86)", String.format("%s\\AppData\\Local",
System.getProperty("user.home")) };
- Console.debug("Starting search for " + chimera);
- for (String root : roots)
+ String versionSeparator = " ";
+ for (File f : FileUtils.getMatchingVersionedFiles(templates, roots,
+ versionsWhitelist, versionsBlacklist, versionSeparator, true))
{
- Console.debug("Looking under '" + root + "'");
- for (String template : templates)
- {
- Console.debug("Using template '" + template + "'");
- String globMatch = String.format(template, root, chimera, "*",
- chimeraExe);
- Console.debug("Looking for installed versions of " + chimera
- + " using '" + globMatch + "'");
- List<File> foundInstalls = FileUtils.getFilesFromGlob(globMatch,
- false);
- for (File found : foundInstalls)
- {
- Console.debug("Checking " + found.getPath() + " is okay");
- boolean add = true;
- for (String notVersion : versionsBlacklist)
- {
- if (String.format(template, root, chimera, notVersion,
- chimeraExe).equals(found.getPath()))
- {
- add = false;
- break;
- }
- }
- if (add)
- {
- Console.debug("Adding " + found.getPath());
- installedChimera.add(found);
- }
- else
- {
- Console.debug(
- "Not adding " + found.getPath() + ", not valid");
- }
- }
- }
- // try without a version number too
- String nonVersioned = String.format("%s\\%s\\bin\\%s", root,
- chimera, chimeraExe);
- installedChimera
- .addAll(FileUtils.getFilesFromGlob(nonVersioned, false));
-
- for (File file : installedChimera)
- {
- pathList.add(file.getPath());
- }
-
- String[] candidates = isChimeraX ? CHIMERAX_VERSIONS
- : CHIMERA_VERSIONS;
- for (String version : candidates)
- {
- // TODO original code doesn't include version in path; which is right?
- String path = String.format("%s\\%s %s\\bin\\%s", root, chimera,
- version, chimeraExe);
- pathList.add(path);
- pathList.add(path + ".exe");
- }
- // try without a version number too
- String path = String.format("%s\\%s\\bin\\%s", root, chimera,
- chimeraExe);
- pathList.add(path);
- pathList.add(path + ".exe");
+ pathList.add(f.getPath());
}
}
else if (Platform.isMac())
{
Set<File> installedChimera = new HashSet<>();
String[] templates = new String[] {
- "%s/%s-%s.app/Contents/MacOS/%s" };
+ "%s/" + chimera + "%s.app/Contents/MacOS/" + chimeraExe };
String[] roots = new String[] { "/Applications", String
.format("%s/Applications", System.getProperty("user.home")) };
- for (String root : roots)
+ String versionSeparator = "-";
+ for (File f : FileUtils.getMatchingVersionedFiles(templates, roots,
+ versionsWhitelist, versionsBlacklist, versionSeparator, true))
{
- for (String template : templates)
- {
- String globMatch = String.format(template, root, chimera, "*",
- chimeraExe);
- List<File> foundInstalls = FileUtils.getFilesFromGlob(globMatch,
- false);
- for (File found : foundInstalls)
- {
- boolean add = true;
- for (String notVersion : versionsBlacklist)
- {
- if (String.format(template, root, chimera, notVersion,
- chimeraExe).equals(found.getPath()))
- {
- add = false;
- break;
- }
- }
- if (add)
- {
- installedChimera.add(found);
- }
- }
- }
- // try without a version number too
- String nonVersioned = String.format("%s/%s.app/Contents/MacOS/%s",
- root, chimera, chimeraExe);
- installedChimera
- .addAll(FileUtils.getFilesFromGlob(nonVersioned, false));
-
- for (File file : installedChimera)
- {
- pathList.add(file.getPath());
- }
-
- // check for installations with version numbers first
- String[] candidates = isChimeraX ? CHIMERAX_VERSIONS
- : CHIMERA_VERSIONS;
- for (String version : candidates)
- {
-
- for (String template : templates)
- {
- pathList.add(String.format(template, root, chimera, version,
- chimeraExe));
- }
- }
- pathList.add(String.format("%s/%s.app/Contents/MacOS/%s", root,
- chimera, chimeraExe));
+ pathList.add(f.getPath());
}
}
return pathList;
*/
public static List<File> getMatchingVersionedFiles(String[] templates,
String[] roots, String[] versionWhitelist,
- String[] versionBlacklist, String separator)
+ String[] versionBlacklist, String versionSeparator,
+ boolean exists)
{
Set<File> matchingFiles = new HashSet<>();
if (templates == null)
for (String root : roots)
{
Console.debug("Using root '" + root + "'");
+
+ // Blacklist. Use a file glob for version and see what's there
if (versionBlacklist != null)
{
String globMatch = String.format(template, root, "*");
for (String notVersion : versionBlacklist)
{
StringBuilder vSB = new StringBuilder();
- if (separator != null)
+ if (versionSeparator != null)
{
- vSB.append(separator);
+ vSB.append(versionSeparator);
}
vSB.append(notVersion);
String versionString = vSB.toString();
}
}
- if (separator != null)
+ if (versionSeparator != null)
{
// try without a version number too
String nonVersioned = String.format(template, root, "");
}
}
+ // Whitelist. Just add a path for every whitelisted version (or check it
+ // exists).
if (versionWhitelist != null)
{
Console.debug("Adding " + versionWhitelist.length
for (String addVersion : versionWhitelist)
{
StringBuilder vSB = new StringBuilder();
- if (separator != null)
+ if (versionSeparator != null)
{
- vSB.append(separator);
+ vSB.append(versionSeparator);
}
vSB.append(addVersion);
String versionString = vSB.toString();
String versionPath = String.format(template, root,
versionString);
Console.debug("Adding whitelist path '" + versionPath + "'");
- matchingFiles.add(new File(versionPath));
+ File file = new File(versionPath);
+ if (file.exists() || !exists)
+ {
+ matchingFiles.add(file);
+ }
}
- if (separator != null)
+ if (versionSeparator != null)
{
// try without a version number too
String nonVersioned = String.format(template, root, "");
- matchingFiles.add(new File(nonVersioned));
+ File file = new File(nonVersioned);
+ if (file.exists() || !exists)
+ {
+ matchingFiles.add(file);
+ }
}
}
}