From 429f613713b06688eab2056349e37c74af705608 Mon Sep 17 00:00:00 2001 From: Peter Dell Date: Tue, 27 Dec 2022 02:29:23 +0100 Subject: [PATCH] Implement default paths handling, move CompilerPaths create to LanguagePlugin --- .../src/com/wudsn/ide/lng/LanguagePlugin.java | 17 ++++++ .../ide/lng/compiler/CompilerDefinition.java | 2 +- .../wudsn/ide/lng/compiler/CompilerPaths.java | 31 +++++++--- .../ide/lng/compiler/CompilerPathsTest.java | 59 +++++++++++++++++++ .../lng/preferences/CompilerPreferences.java | 5 +- .../preferences/LanguagePreferencesPage.java | 4 +- 6 files changed, 106 insertions(+), 12 deletions(-) create mode 100644 com.wudsn.ide.lng/src/com/wudsn/ide/lng/compiler/CompilerPathsTest.java diff --git a/com.wudsn.ide.lng/src/com/wudsn/ide/lng/LanguagePlugin.java b/com.wudsn.ide.lng/src/com/wudsn/ide/lng/LanguagePlugin.java index 69c8b20a..ad8f3117 100644 --- a/com.wudsn.ide.lng/src/com/wudsn/ide/lng/LanguagePlugin.java +++ b/com.wudsn.ide.lng/src/com/wudsn/ide/lng/LanguagePlugin.java @@ -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(ListenerList.IDENTITY); compilerRegistry = new CompilerRegistry(); + compilerPaths = new CompilerPaths(); compilerConsole = null; runnerRegistry = new RunnerRegistry(); properties = new HashMap(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. * diff --git a/com.wudsn.ide.lng/src/com/wudsn/ide/lng/compiler/CompilerDefinition.java b/com.wudsn.ide.lng/src/com/wudsn/ide/lng/compiler/CompilerDefinition.java index 8a86f66a..c2256e70 100644 --- a/com.wudsn.ide.lng/src/com/wudsn/ide/lng/compiler/CompilerDefinition.java +++ b/com.wudsn.ide.lng/src/com/wudsn/ide/lng/compiler/CompilerDefinition.java @@ -395,7 +395,7 @@ public final class CompilerDefinition implements Comparable } public List getDefaultPaths() { - CompilerPaths compilerPaths = new CompilerPaths(); + CompilerPaths compilerPaths = LanguagePlugin.getInstance().getCompilerPaths(); return compilerPaths.getCompilerPaths(language, id); } diff --git a/com.wudsn.ide.lng/src/com/wudsn/ide/lng/compiler/CompilerPaths.java b/com.wudsn.ide.lng/src/com/wudsn/ide/lng/compiler/CompilerPaths.java index 5d7011b7..d01761a9 100644 --- a/com.wudsn.ide.lng/src/com/wudsn/ide/lng/compiler/CompilerPaths.java +++ b/com.wudsn.ide.lng/src/com/wudsn/ide/lng/compiler/CompilerPaths.java @@ -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 compilerPaths; + /** + * Created by the {@linkplain LanguagePlugin}. + */ public CompilerPaths() { compilerPaths = new TreeMap(); - // 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 null. + * @param compilerId The compiler ID, not empty and not null. + * @return The file or null 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 getCompilerPaths() { + return Collections.unmodifiableList(new ArrayList(compilerPaths.values())); + } + public List getCompilerPaths(Language language, String id) { if (language == null) { throw new IllegalArgumentException("Parameter 'language' must not be null."); diff --git a/com.wudsn.ide.lng/src/com/wudsn/ide/lng/compiler/CompilerPathsTest.java b/com.wudsn.ide.lng/src/com/wudsn/ide/lng/compiler/CompilerPathsTest.java new file mode 100644 index 00000000..8602b017 --- /dev/null +++ b/com.wudsn.ide.lng/src/com/wudsn/ide/lng/compiler/CompilerPathsTest.java @@ -0,0 +1,59 @@ +/** + * Copyright (C) 2009 - 2021 Peter Dell + * + * 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 . + */ + +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 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); + } + } + +} \ No newline at end of file diff --git a/com.wudsn.ide.lng/src/com/wudsn/ide/lng/preferences/CompilerPreferences.java b/com.wudsn.ide.lng/src/com/wudsn/ide/lng/preferences/CompilerPreferences.java index c9bf0f2a..e58ef348 100644 --- a/com.wudsn.ide.lng/src/com/wudsn/ide/lng/preferences/CompilerPreferences.java +++ b/com.wudsn.ide.lng/src/com/wudsn/ide/lng/preferences/CompilerPreferences.java @@ -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(); diff --git a/com.wudsn.ide.lng/src/com/wudsn/ide/lng/preferences/LanguagePreferencesPage.java b/com.wudsn.ide.lng/src/com/wudsn/ide/lng/preferences/LanguagePreferencesPage.java index bc825a02..8ea03de4 100644 --- a/com.wudsn.ide.lng/src/com/wudsn/ide/lng/preferences/LanguagePreferencesPage.java +++ b/com.wudsn.ide.lng/src/com/wudsn/ide/lng/preferences/LanguagePreferencesPage.java @@ -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 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);