Implement default paths handling, move CompilerPaths create to

LanguagePlugin
This commit is contained in:
Peter Dell 2022-12-27 02:29:23 +01:00
parent 21baba5bf7
commit 429f613713
6 changed files with 106 additions and 12 deletions

View File

@ -37,6 +37,8 @@ import org.osgi.framework.BundleContext;
import com.wudsn.ide.base.common.AbstractIDEPlugin;
import com.wudsn.ide.lng.compiler.CompilerConsole;
import com.wudsn.ide.lng.compiler.CompilerPaths;
import com.wudsn.ide.lng.compiler.CompilerPathsTest;
import com.wudsn.ide.lng.compiler.CompilerRegistry;
import com.wudsn.ide.lng.preferences.LanguagePreferences;
import com.wudsn.ide.lng.preferences.LanguagePreferencesChangeListener;
@ -75,6 +77,11 @@ public final class LanguagePlugin extends AbstractIDEPlugin {
*/
private CompilerRegistry compilerRegistry;
/**
* The compiler paths.
*/
private CompilerPaths compilerPaths;
/**
* The compiler console.
*/
@ -97,6 +104,7 @@ public final class LanguagePlugin extends AbstractIDEPlugin {
preferences = null;
preferencesChangeListeners = new ListenerList<LanguagePreferencesChangeListener>(ListenerList.IDENTITY);
compilerRegistry = new CompilerRegistry();
compilerPaths = new CompilerPaths();
compilerConsole = null;
runnerRegistry = new RunnerRegistry();
properties = new HashMap<QualifiedName, String>(10);
@ -157,6 +165,9 @@ public final class LanguagePlugin extends AbstractIDEPlugin {
}
});
// TODO: Call unit tests
CompilerPathsTest.main(new String[0]);
}
@ -203,6 +214,12 @@ public final class LanguagePlugin extends AbstractIDEPlugin {
return compilerRegistry;
}
public CompilerPaths getCompilerPaths() {
if (compilerPaths == null) {
throw new IllegalStateException("Field 'compilerPaths' must not be null.");
}
return compilerPaths;
}
/**
* Gets the compiler console for this plugin.
*

View File

@ -395,7 +395,7 @@ public final class CompilerDefinition implements Comparable<CompilerDefinition>
}
public List<CompilerPath> getDefaultPaths() {
CompilerPaths compilerPaths = new CompilerPaths();
CompilerPaths compilerPaths = LanguagePlugin.getInstance().getCompilerPaths();
return compilerPaths.getCompilerPaths(language, id);
}

View File

@ -33,6 +33,7 @@ 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;
/**
* Computation of default paths for compilers.
@ -76,10 +77,12 @@ public final class CompilerPaths {
private Map<String, CompilerPath> compilerPaths;
/**
* Created by the {@linkplain LanguagePlugin}.
*/
public CompilerPaths() {
compilerPaths = new TreeMap<String, CompilerPath>();
// TODO: Complete default compiler paths for all assemblers and compilers and
// write unit test.
// TODO: Complete default compiler paths for all assemblers and compilers DSAM/KickAss/TASS.
// See https://github.com/peterdell/wudsn-ide-tools
add(Language.ASM, "acme", Platform.OS_WIN32, Platform.ARCH_X86, "acme.exe");
add(Language.ASM, "asm6", Platform.OS_WIN32, Platform.ARCH_X86, "asm6.exe");
@ -98,7 +101,6 @@ public final class CompilerPaths {
add(Language.ASM, "xasm", Platform.OS_WIN32, Platform.ARCH_X86, "xasm.exe");
add(Language.PAS, "mp", Platform.OS_MACOSX, Platform.ARCH_X86_64, "mp.macos-x86-64");
add(Language.PAS, "mp", Platform.OS_WIN32, Platform.ARCH_X86_64, "mp.exe");
}
private void add(Language language, String compilerId, String os, String osArch, String executablePath) {
@ -109,7 +111,7 @@ public final class CompilerPaths {
compilerPaths.put(compilerPath.getKey(), compilerPath);
}
public String getRelativePath(Language language, String compilerId) {
private String getRelativePath(Language language, String compilerId) {
String os = Platform.getOS();
String osArch = Platform.getOSArch();
String key = CompilerPath.getKey(language, compilerId, os, osArch);
@ -120,10 +122,19 @@ public final class CompilerPaths {
return null;
}
public File getAbsoluteFile(Language language, String compilerId) {
String path = getRelativePath(language, compilerId);
/**
* 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));
}
if (path == null) {
public File getAbsoluteFile(String relativePath) {
if (relativePath == null) {
return null;
}
URL eclipseFolderURL = Platform.getInstallLocation().getURL();
@ -140,10 +151,14 @@ public final class CompilerPaths {
File eclipseFolder = FileUtility.getCanonicalFile(new File(uri));
File ideFolder = eclipseFolder.getParentFile();
File toolsFolder = ideFolder.getParentFile();
File compilerFile = new File(toolsFolder, path);
File compilerFile = new File(toolsFolder, relativePath);
return compilerFile;
}
public List<CompilerPath> getCompilerPaths() {
return Collections.unmodifiableList(new ArrayList<CompilerPath>(compilerPaths.values()));
}
public List<CompilerPath> getCompilerPaths(Language language, String id) {
if (language == null) {
throw new IllegalArgumentException("Parameter 'language' must not be null.");

View File

@ -0,0 +1,59 @@
/**
* Copyright (C) 2009 - 2021 <a href="https://www.wudsn.com" target="_top">Peter Dell</a>
*
* This file is part of WUDSN IDE.
*
* WUDSN IDE is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* WUDSN IDE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with WUDSN IDE. If not, see <http://www.gnu.org/licenses/>.
*/
package com.wudsn.ide.lng.compiler;
import java.io.File;
import java.util.List;
import com.wudsn.ide.base.common.Test;
import com.wudsn.ide.base.common.TestMethod;
import com.wudsn.ide.lng.LanguagePlugin;
import com.wudsn.ide.lng.compiler.CompilerPaths.CompilerPath;
/**
* Computation of default paths for compilers.
*
* @author Peter Dell
*
* @since 1.7.2
*
*/
public final class CompilerPathsTest {
@TestMethod
public static void main(String[] args) {
CompilerPaths compilerPaths = LanguagePlugin.getInstance().getCompilerPaths();
List<CompilerPath> compilerPathList = compilerPaths.getCompilerPaths();
for (CompilerPath compilerPath : compilerPathList) {
String relativePath = compilerPath.getRelativePath();
File file = compilerPaths.getAbsoluteFile(relativePath);
String filePath = "";
String result = "NOT defined";
if (file != null) {
filePath = file.getAbsolutePath();
result = file.exists() ? "found" : "NOT found";
}
Test.log("Language " + compilerPath.language + ", compiler " + compilerPath.compilerId + ", OS "
+ compilerPath.os + ", OS architecture " + compilerPath.osArch + ": File " + filePath + " "
+ result);
}
}
}

View File

@ -23,6 +23,7 @@ import java.io.File;
import com.wudsn.ide.base.common.StringUtility;
import com.wudsn.ide.base.hardware.Hardware;
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;
@ -86,9 +87,9 @@ public final class CompilerPreferences {
public String getCompilerExecutablePathOrDefault() {
String compilerExecutablePath = getCompilerExecutablePath();
CompilerPaths compilerPaths = new CompilerPaths();
CompilerPaths compilerPaths = LanguagePlugin.getInstance().getCompilerPaths();
if (StringUtility.isEmpty(compilerExecutablePath)) {
File compilerFile = compilerPaths.getAbsoluteFile(languagePreferences.getLanguage(), compilerId);
File compilerFile = compilerPaths.getAbsoluteFileForOSAndArch(languagePreferences.getLanguage(), compilerId);
if (compilerFile != null) {
if (compilerFile.exists() && compilerFile.isFile() && compilerFile.canExecute()) {
compilerExecutablePath = compilerFile.getAbsolutePath();

View File

@ -62,6 +62,7 @@ import com.wudsn.ide.lng.Language;
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.CompilerRegistry;
import com.wudsn.ide.lng.editor.LanguageContentAssistProcessorDefaultCase;
import com.wudsn.ide.lng.editor.LanguageEditor;
@ -423,7 +424,7 @@ public abstract class LanguagePreferencesPage extends FieldEditorPreferencePage
// Create the editors for all compilers of the hardware.
CompilerRegistry compilerRegistry = plugin.getCompilerRegistry();
List<CompilerDefinition> compilerDefinitions = compilerRegistry.getCompilerDefinitions(language);
CompilerPaths compilerPaths = plugin.getCompilerPaths();
for (CompilerDefinition compilerDefinition : compilerDefinitions) {
String compilerId = compilerDefinition.getId();
TabItem tabItem = new TabItem(tabFolder, SWT.NONE);
@ -443,6 +444,7 @@ 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));
fileFieldEditor.setFileExtensions(ProcessWithLogs.getExecutableExtensions());
addField(fileFieldEditor);