Refactor default path handling and enable starting folder for browsing

compilers
This commit is contained in:
Peter Dell 2022-12-27 03:53:39 +01:00
parent 7320de4fd5
commit 7664339b2f
6 changed files with 65 additions and 50 deletions

View File

@ -19,6 +19,10 @@
package com.wudsn.ide.lng;
import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
@ -28,6 +32,7 @@ import java.util.Set;
import java.util.TreeSet;
import org.eclipse.core.runtime.ListenerList;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.jface.preference.JFacePreferences;
import org.eclipse.jface.resource.JFaceResources;
@ -36,6 +41,7 @@ import org.eclipse.jface.util.PropertyChangeEvent;
import org.osgi.framework.BundleContext;
import com.wudsn.ide.base.common.AbstractIDEPlugin;
import com.wudsn.ide.base.common.FileUtility;
import com.wudsn.ide.lng.compiler.CompilerConsole;
import com.wudsn.ide.lng.compiler.CompilerPaths;
import com.wudsn.ide.lng.compiler.CompilerPathsTest;
@ -202,6 +208,30 @@ public final class LanguagePlugin extends AbstractIDEPlugin {
return languages;
}
public File getAbsoluteToolsFile(String relativePath) {
if (relativePath == null) {
return null;
}
URL eclipseFolderURL = Platform.getInstallLocation().getURL();
if (eclipseFolderURL == null) {
return null;
}
URI uri;
try {
uri = eclipseFolderURL.toURI();
} catch (URISyntaxException ignore) {
return null;
}
File eclipseFolder = FileUtility.getCanonicalFile(new File(uri));
File ideFolder = eclipseFolder.getParentFile();
File toolsFolder = ideFolder.getParentFile();
File compilerFile = new File(toolsFolder, relativePath);
return compilerFile;
}
/**
* Gets the compiler registry for this plugin.
*

View File

@ -20,9 +20,6 @@
package com.wudsn.ide.lng.compiler;
import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@ -31,7 +28,6 @@ import java.util.TreeMap;
import org.eclipse.core.runtime.Platform;
import com.wudsn.ide.base.common.FileUtility;
import com.wudsn.ide.lng.Language;
import com.wudsn.ide.lng.LanguagePlugin;
@ -73,6 +69,10 @@ public final class CompilerPaths {
return language.name() + "/" + compilerId.toUpperCase() + "/" + executablePath;
}
public File getAbsoluteFile() {
return LanguagePlugin.getInstance().getAbsoluteToolsFile(getRelativePath());
}
}
private Map<String, CompilerPath> compilerPaths;
@ -120,49 +120,20 @@ public final class CompilerPaths {
compilerPaths.put(compilerPath.getKey(), compilerPath);
}
private String getRelativePath(Language language, String compilerId) {
public CompilerPath getDefaultCompilerPath(Language language, String compilerId) {
String os = Platform.getOS();
String osArch = Platform.getOSArch();
String key = CompilerPath.getKey(language, compilerId, os, osArch);
CompilerPath compilerPath = compilerPaths.get(key);
if (compilerPath != null) {
return compilerPath.getRelativePath();
// Default to 32-bit version if 64-bit version not defined?
if (compilerPath == null) {
if (osArch.equals(Platform.ARCH_X86_64)) {
osArch = Platform.ARCH_X86;
key = CompilerPath.getKey(language, compilerId, os, osArch);
compilerPath = compilerPaths.get(key);
}
}
return null;
}
/**
* Gets the absolute file path the default executable on the current OS and OS
* architecture.
*
* @param language The language, not empty and not <code>null</code>.
* @param compilerId The compiler ID, not empty and not <code>null</code>.
* @return The file or <code>null</code> is no file could be determined.
*/
public File getAbsoluteFileForOSAndArch(Language language, String compilerId) {
return getAbsoluteFile(getRelativePath(language, compilerId));
}
public File getAbsoluteFile(String relativePath) {
if (relativePath == null) {
return null;
}
URL eclipseFolderURL = Platform.getInstallLocation().getURL();
if (eclipseFolderURL == null) {
return null;
}
URI uri;
try {
uri = eclipseFolderURL.toURI();
} catch (URISyntaxException ignore) {
return null;
}
File eclipseFolder = FileUtility.getCanonicalFile(new File(uri));
File ideFolder = eclipseFolder.getParentFile();
File toolsFolder = ideFolder.getParentFile();
File compilerFile = new File(toolsFolder, relativePath);
return compilerFile;
return compilerPath;
}
public List<CompilerPath> getCompilerPaths() {

View File

@ -42,8 +42,7 @@ public final class CompilerPathsTest {
CompilerPaths compilerPaths = LanguagePlugin.getInstance().getCompilerPaths();
List<CompilerPath> compilerPathList = compilerPaths.getCompilerPaths();
for (CompilerPath compilerPath : compilerPathList) {
String relativePath = compilerPath.getRelativePath();
File file = compilerPaths.getAbsoluteFile(relativePath);
File file = compilerPath.getAbsoluteFile();
String filePath = "";
String result = "NOT defined";
if (file != null) {

View File

@ -398,7 +398,9 @@ public final class LanguageHelpContentProducer implements IHelpContentProducer {
innerWriter.beginTableRow();
innerWriter.writeTableCell(compilerPath.os);
innerWriter.writeTableCell(compilerPath.osArch);
innerWriter.writeTableCell(compilerPath.getRelativePath());
File file = compilerPath.getAbsoluteFile();
// Display absolute path if available.
innerWriter.writeTableCell(file != null ? file.getAbsolutePath() : compilerPath.getRelativePath());
innerWriter.end();
}

View File

@ -27,6 +27,7 @@ import com.wudsn.ide.lng.LanguagePlugin;
import com.wudsn.ide.lng.Target;
import com.wudsn.ide.lng.compiler.CompilerOutputFolderMode;
import com.wudsn.ide.lng.compiler.CompilerPaths;
import com.wudsn.ide.lng.compiler.CompilerPaths.CompilerPath;
import com.wudsn.ide.lng.runner.RunnerId;
/**
@ -89,10 +90,14 @@ public final class CompilerPreferences {
CompilerPaths compilerPaths = LanguagePlugin.getInstance().getCompilerPaths();
if (StringUtility.isEmpty(compilerExecutablePath)) {
File compilerFile = compilerPaths.getAbsoluteFileForOSAndArch(languagePreferences.getLanguage(), compilerId);
if (compilerFile != null) {
if (compilerFile.exists() && compilerFile.isFile() && compilerFile.canExecute()) {
compilerExecutablePath = compilerFile.getAbsolutePath();
CompilerPath compilerPath = compilerPaths.getDefaultCompilerPath(languagePreferences.getLanguage(),
compilerId);
if (compilerPath != null) {
File compilerFile = compilerPath.getAbsoluteFile();
if (compilerFile != null) {
if (compilerFile.exists() && compilerFile.isFile() && compilerFile.canExecute()) {
compilerExecutablePath = compilerFile.getAbsolutePath();
}
}
}

View File

@ -19,6 +19,7 @@
package com.wudsn.ide.lng.preferences;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
@ -63,6 +64,7 @@ import com.wudsn.ide.lng.LanguagePlugin;
import com.wudsn.ide.lng.Texts;
import com.wudsn.ide.lng.compiler.CompilerDefinition;
import com.wudsn.ide.lng.compiler.CompilerPaths;
import com.wudsn.ide.lng.compiler.CompilerPaths.CompilerPath;
import com.wudsn.ide.lng.compiler.CompilerRegistry;
import com.wudsn.ide.lng.editor.LanguageContentAssistProcessorDefaultCase;
import com.wudsn.ide.lng.editor.LanguageEditor;
@ -444,7 +446,13 @@ public abstract class LanguagePreferencesPage extends FieldEditorPreferencePage
composite = SWTFactory.createComposite(tabContent, 4, 2, GridData.FILL_HORIZONTAL);
FileFieldDownloadEditor fileFieldEditor = new FileFieldDownloadEditor(name,
Texts.PREFERENCES_COMPILER_EXECUTABLE_PATH_LABEL, composite);
fileFieldEditor.setFilterPath(compilerPaths.getAbsoluteFileForOSAndArch(language, compilerId));
CompilerPath compilerPath = compilerPaths.getDefaultCompilerPath(language, compilerId);
if (compilerPath != null) {
File file = compilerPath.getAbsoluteFile();
if (file != null) {
fileFieldEditor.setFilterPath(file.getParentFile());
}
}
fileFieldEditor.setFileExtensions(ProcessWithLogs.getExecutableExtensions());
addField(fileFieldEditor);