Skip to content

Commit

Permalink
core: Discoverer; InstallLocation
Browse files Browse the repository at this point in the history
- Discoverer has distinct functions to read Registry
and to search folders
- mbs uses Discoverer
- mbs, openocd & qemu use both InstallLocation and deprecated InstallFolder
  • Loading branch information
ilg-ul committed Mar 21, 2015
1 parent 48fc54b commit 9973522
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 167 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,6 @@ public class Discoverer {

// ------------------------------------------------------------------------

/**
* Find where the executable might have been installed. The returned path is
* known to be an existing folder.
*
* @param executableName
* @param searchPath
* a string with a sequence of folders.
* @param registrySubKey
* @param registryName
* @return a String with the absolute folder path, or null if not found.
*/
public static String discoverInstallFolder(String executableName,
String searchPath, String registrySubKey, String registryName) {

return discoverInstallFolder(executableName, searchPath, "bin",
registrySubKey, registryName);
}

/**
* Find where the executable might have been installed. The returned path is
* known to be an existing folder.
Expand All @@ -69,55 +51,12 @@ public static String discoverInstallFolder(String executableName,
* a string with a sequence of folders.
* @param binFolder
* a String, usually "bin", or null.
* @param registrySubKey
* @param registryName
* @return a String with the absolute folder path, or null if not found.
*/
public static String discoverInstallFolder(String executableName,
String searchPath, String binFolder, String registrySubKey,
String registryName) {
public static String searchInstallFolder(String executableName,
String searchPath, String binFolder) {

String value = null;
if (EclipseUtils.isWindows()) {

WindowsRegistry registry = WindowsRegistry.getRegistry();

if (registry != null) {
value = getRegistryValue(registry, REG_PREFIX, registrySubKey,
registryName);
if (value == null) {
// If on 64-bit, check the 32-bit registry too.
value = getRegistryValue(registry, REG32_PREFIX,
registrySubKey, registryName);
}

if (binFolder != null && value != null
&& !value.endsWith("\\" + binFolder)) {
value += "\\" + binFolder;
}

if (value != null) {
IPath path = new Path(value);
// Make portable
value = path.toString(); // includes /bin, if it exists
if (Activator.getInstance().isDebugging()) {
System.out
.println("Discoverer.discoverInstallFolder() WinReg "
+ registryName + " " + value);
}

File folder = path.append(executableName).toFile();
if (folder.isFile()) {
if (Activator.getInstance().isDebugging()) {
System.out
.println("Discoverer.discoverInstallFolder() WinReg="
+ value);
}
return value;
}
}
}
}

if (searchPath == null || searchPath.isEmpty()) {
return null;
Expand Down Expand Up @@ -159,7 +98,7 @@ public static String discoverInstallFolder(String executableName,

if (Activator.getInstance().isDebugging()) {
System.out
.println("Discoverer.discoverInstallFolder() resolved path "
.println("Discoverer.searchInstallFolder() resolved path "
+ resolvedPath);
}

Expand All @@ -174,6 +113,65 @@ public static String discoverInstallFolder(String executableName,
return null;
}

/**
* Get key value from registry and validate the executable. The returned
* path is known to be an existing folder.
*
* @param executableName
* @param binFolder
* a String, usually "bin", or null.
* @param registrySubKey
* @param registryName
* @return a String with the absolute folder path, or null if not found.
*/
public static String getRegistryInstallFolder(String executableName,
String binFolder, String registrySubKey, String registryName) {

String value = null;
if (EclipseUtils.isWindows()) {

WindowsRegistry registry = WindowsRegistry.getRegistry();

if (registry != null) {
value = getRegistryValue(registry, REG_PREFIX, registrySubKey,
registryName);
if (value == null) {
// If on 64-bit, check the 32-bit registry too.
value = getRegistryValue(registry, REG32_PREFIX,
registrySubKey, registryName);
}

if (binFolder != null && value != null
&& !value.endsWith("\\" + binFolder)) {
value += "\\" + binFolder;
}

if (value != null) {
IPath path = new Path(value);
// Make portable
value = path.toString(); // includes /bin, if it exists
if (Activator.getInstance().isDebugging()) {
System.out
.println("Discoverer.getRegistryInstallFolder() "
+ registryName + " " + value);
}

File folder = path.append(executableName).toFile();
if (folder.isFile()) {
if (Activator.getInstance().isDebugging()) {
System.out
.println("Discoverer.getRegistryInstallFolder()="
+ value);
}
return value;
}
}
}
}

return null;
}

/**
* Get the value of a registry key. It first tests the current user key,
* then the local machine key.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,20 @@ public void finalizeInitializationsDefaultPreferences() {
executableName += ".exe";
}

// Check if the search path is defined in the default
// preferences.
String searchPath = DefaultPreferences.getSearchPath();
if (searchPath.isEmpty()) {

// If not defined, get the OS Specific default
// from preferences.ini.
searchPath = DefaultPreferences.getSearchPathOs();
if (!searchPath.isEmpty()) {
// Store the search path in the preferences
DefaultPreferences.putSearchPath(searchPath);
}
}

// J-Link GDB Server install folder
// Check if the toolchain path is explictly defined in the
// default preferences.
Expand All @@ -239,34 +253,24 @@ public void finalizeInitializationsDefaultPreferences() {
folder = "";
}
}
if (folder.isEmpty()) {

// Check if the search path is defined in the default
// preferences.
String searchPath = DefaultPreferences.getSearchPath();
if (searchPath.isEmpty()) {

// If not defined, get the OS Specific default
// from preferences.ini.
searchPath = DefaultPreferences.getSearchPathOs();
if (!searchPath.isEmpty()) {
// Store the search path in the preferences
DefaultPreferences.putSearchPath(searchPath);
}
}
if (folder.isEmpty()) {

if (!searchPath.isEmpty()) {
// If the search path is known, discover toolchain.
folder = Discoverer.getRegistryInstallFolder(executableName,
"bin", REG_SUBKEY, REG_NAME);

// If the search path is known, discover toolchain.
folder = Discoverer.discoverInstallFolder(executableName,
searchPath, null, REG_SUBKEY, REG_NAME);
if (folder != null && !folder.isEmpty()) {
// If the install folder was finally discovered, store
// it in the preferences.
DefaultPreferences.putInstallFolder(folder);
}
if (folder == null) {
folder = Discoverer.searchInstallFolder(executableName,
searchPath, null);
}
}

if (folder != null && !folder.isEmpty()) {
// If the install folder was finally discovered, store
// it in the preferences.
DefaultPreferences.putInstallFolder(folder);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ public class DefaultPreferenceInitializer extends AbstractPreferenceInitializer

// ------------------------------------------------------------------------

// LOCAL_MACHINE
// HKCU & HKLM LOCAL_MACHINE
private static final String REG_SUBKEY = "\\GNU ARM Eclipse\\OpenOCD";
private static final String REG_NAME = "InstallFolder";
// Standard Microsoft recommendation.
private static final String REG_NAME = "InstallLocation";
// Custom name, used before reading the standard.
private static final String REG_NAME_DEPRECATED = "InstallFolder";

// ------------------------------------------------------------------------

Expand Down Expand Up @@ -136,6 +139,21 @@ public void finalizeInitializationsDefaultPreferences() {
executableName += ".exe";
}

// Check if the search path is defined in the default
// preferences.
String searchPath = DefaultPreferences.getSearchPath();
if (searchPath.isEmpty()) {

// If not defined, get the OS Specific default
// from preferences.ini.
searchPath = DefaultPreferences.getSearchPathOs();

if (!searchPath.isEmpty()) {
// Store the search path in the preferences
DefaultPreferences.putSearchPath(searchPath);
}
}

// OpenOCD install folder
// Check if the toolchain path is explictly defined in the
// default preferences.
Expand All @@ -148,34 +166,31 @@ public void finalizeInitializationsDefaultPreferences() {
folder = "";
}
}

if (folder.isEmpty()) {

// Check if the search path is defined in the default
// preferences.
String searchPath = DefaultPreferences.getSearchPath();
if (searchPath.isEmpty()) {

// If not defined, get the OS Specific default
// from preferences.ini.
searchPath = DefaultPreferences.getSearchPathOs();
if (!searchPath.isEmpty()) {
// Store the search path in the preferences
DefaultPreferences.putSearchPath(searchPath);
}
}
// If the search path is known, discover toolchain.
folder = Discoverer.getRegistryInstallFolder(executableName,
"bin", REG_SUBKEY, REG_NAME);

if (!searchPath.isEmpty()) {
// Search the non standard key too.
if (folder == null) {
folder = Discoverer.getRegistryInstallFolder(
executableName, "bin", REG_SUBKEY,
REG_NAME_DEPRECATED);
}

// If the search path is known, discover toolchain.
folder = Discoverer.discoverInstallFolder(executableName,
searchPath, REG_SUBKEY, REG_NAME);
if (folder != null && !folder.isEmpty()) {
// If the install folder was finally discovered, store
// it in the preferences.
DefaultPreferences.putInstallFolder(folder);
}
if (folder == null) {
folder = Discoverer.searchInstallFolder(executableName,
searchPath, "bin");
}
}

if (folder != null && !folder.isEmpty()) {
// If the install folder was finally discovered, store
// it in the preferences.
DefaultPreferences.putInstallFolder(folder);
}
}
}

Expand Down
Loading

0 comments on commit 9973522

Please sign in to comment.