From e6d05e0609427b04e7c02389daa307edb95b2276 Mon Sep 17 00:00:00 2001 From: Niklas Rentz Date: Mon, 19 Aug 2024 13:27:35 +0200 Subject: [PATCH] fix copying icons in Eclipse generator --- .../spviz/generator/SPVizGenerator.xtend | 45 ++++++++++++++++++- .../spvizmodel/generator/FileGenerator.xtend | 26 ----------- 2 files changed, 43 insertions(+), 28 deletions(-) diff --git a/plugins/de.cau.cs.kieler.spviz.spviz/src/de/cau/cs/kieler/spviz/spviz/generator/SPVizGenerator.xtend b/plugins/de.cau.cs.kieler.spviz.spviz/src/de/cau/cs/kieler/spviz/spviz/generator/SPVizGenerator.xtend index ff99e5a..bf7029c 100644 --- a/plugins/de.cau.cs.kieler.spviz.spviz/src/de/cau/cs/kieler/spviz/spviz/generator/SPVizGenerator.xtend +++ b/plugins/de.cau.cs.kieler.spviz.spviz/src/de/cau/cs/kieler/spviz/spviz/generator/SPVizGenerator.xtend @@ -3,7 +3,7 @@ * * http://rtsys.informatik.uni-kiel.de/kieler * - * Copyright 2020-2023 by + * Copyright 2020-2024 by * + Kiel University * + Department of Computer Science * + Real-Time and Embedded Systems Group @@ -17,10 +17,14 @@ import de.cau.cs.kieler.spviz.spvizmodel.generator.JavaMavenProjectGenerator import de.cau.cs.kieler.spviz.spvizmodel.generator.ProjectGenerator import de.cau.cs.kieler.spviz.spvizmodel.generator.maven.Dependency import java.io.File +import java.io.InputStream +import java.nio.file.Files import java.nio.file.Path import java.nio.file.Paths import java.util.List import org.eclipse.core.resources.ResourcesPlugin +import org.eclipse.core.runtime.FileLocator +import org.eclipse.core.runtime.Platform import org.eclipse.emf.ecore.resource.Resource import org.eclipse.xtext.generator.AbstractGenerator import org.eclipse.xtext.generator.IFileSystemAccess2 @@ -112,7 +116,7 @@ class SPVizGenerator extends AbstractGenerator { GenerateActions.generate(sourceVizFolder, data) // Copy icons over into the project - FileGenerator.copyIcons(FileGenerator.createDirectory(vizProjectDirectory, "icons")) + copyIcons(FileGenerator.createDirectory(vizProjectDirectory, "icons")) // Generate the .language.server Maven project @@ -142,6 +146,43 @@ class SPVizGenerator extends AbstractGenerator { GenerateMavenBuild.generate(root, data.bundleNamePrefix, data.visualizationName.toFirstUpper, data.modelBundleNamePrefix, "0.1.0") } + /** + * Copies the files from the icons/ folder to the target folder. + * + * @param targetFolder The folder to copy the files to. + */ + static def void copyIcons(File targetFolder) { + targetFolder.mkdirs + if (Platform.isRunning) { + // If the platform is running, the folder can be found in the bundle under the resource path. + val url = Platform.getBundle("de.cau.cs.kieler.spviz.spviz") + ?.getEntry("icons") + val folder = new File(FileLocator.toFileURL(url).toURI.path) + for (file : folder.listFiles) { + val fileName = file.name + val newFile = new File(targetFolder, fileName) + if (!newFile.exists) { + Files.copy(file.toPath, newFile.toPath) + } + } + } else { + // The file path is searched on the classpath directly + val fileNames = #["connect.svg", "connect128.png", + "expand.svg", "expand128.png", + "loupe.svg", "loupe128.png", + "loupe-crossed.svg", "loupe-crossed128.png", + "minimize.svg", "minimize128.png", + "restore.svg", "restore128.png"] + for (fileName : fileNames) { + val InputStream source = SPVizGenerator.classLoader.getResourceAsStream("icons/" + fileName) + val newFile = new File(targetFolder, fileName) + if (!newFile.exists) { + Files.copy(source, newFile.toPath) + } + } + } + } + static def lsDependencyManagement() { return ''' diff --git a/plugins/de.cau.cs.kieler.spviz.spvizmodel/src/de/cau/cs/kieler/spviz/spvizmodel/generator/FileGenerator.xtend b/plugins/de.cau.cs.kieler.spviz.spvizmodel/src/de/cau/cs/kieler/spviz/spvizmodel/generator/FileGenerator.xtend index 41a78be..1bb881a 100644 --- a/plugins/de.cau.cs.kieler.spviz.spvizmodel/src/de/cau/cs/kieler/spviz/spvizmodel/generator/FileGenerator.xtend +++ b/plugins/de.cau.cs.kieler.spviz.spvizmodel/src/de/cau/cs/kieler/spviz/spvizmodel/generator/FileGenerator.xtend @@ -13,7 +13,6 @@ package de.cau.cs.kieler.spviz.spvizmodel.generator import java.io.File -import java.io.InputStream import java.nio.file.Files import java.nio.file.Paths @@ -92,31 +91,6 @@ class FileGenerator { } Files.write(Paths.get(file.path), fileContent.bytes) } - - /** - * Copies the files at the source location to the target folder. - * - * @param sourceFolder The folder to copy the files from. - * @param targetFolder The folder to copy the files to. - */ - def static void copyIcons(File targetFolder) { - targetFolder.mkdirs - // The file path is searched on the classpath directly - // TODO: this has to be tested. - val fileNames = #["connect.svg", "connect128.png", - "expand.svg", "expand128.png", - "loupe.svg", "loupe128.png", - "loupe-crossed.svg", "loupe-crossed128.png", - "minimize.svg", "minimize128.png", - "restore.svg", "restore128.png"] - for (fileName : fileNames) { - val InputStream source = FileGenerator.classLoader.getResourceAsStream("icons/" + fileName) - val newFile = new File(targetFolder, fileName) - if (!newFile.exists) { - Files.copy(source, newFile.toPath) - } - } - } /** * Creates a directory under the given filePath.