From 181f7358a3768417b2fa98cf18e5b5a1ce60172c Mon Sep 17 00:00:00 2001 From: Christian Menard Date: Thu, 26 Jan 2023 11:25:41 +0100 Subject: [PATCH 1/2] Print a useful error message if the runtime sources are missing We used to have an error message in lfc, that notified the user if the runtime files are missing (i.e. the submodules are not checked out). It seems that with the introduction of `FileUtil` this feature got lost. It currently only complains if a resource cannot be found at all, but not if the resource is an empty directory. Aside from a bit of cleanup, this fix ensures that a useful error message is also produced if a submodule is missing. --- org.lflang/src/org/lflang/util/FileUtil.java | 26 ++++++++----------- .../util/TargetResourceNotFoundException.java | 14 ++++++++++ 2 files changed, 25 insertions(+), 15 deletions(-) create mode 100644 org.lflang/src/org/lflang/util/TargetResourceNotFoundException.java diff --git a/org.lflang/src/org/lflang/util/FileUtil.java b/org.lflang/src/org/lflang/util/FileUtil.java index e94bb44242..e58ab8f345 100644 --- a/org.lflang/src/org/lflang/util/FileUtil.java +++ b/org.lflang/src/org/lflang/util/FileUtil.java @@ -12,7 +12,6 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.nio.file.StandardCopyOption; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; @@ -279,12 +278,7 @@ public static void copyFileFromClassPath(final String source, final Path destina // Copy the file. if (sourceStream == null) { - throw new IOException( - "A required target resource could not be found: " + source + "\n" + - "Perhaps a git submodule is missing or not up to date.\n" + - "See https://github.com/icyphy/lingua-franca/wiki/downloading-and-building#clone-the-lingua-franca-repository.\n" - + - "Also try to refresh and clean the project explorer if working from eclipse."); + throw new TargetResourceNotFoundException(source); } else { try (sourceStream) { copyInputStream(sourceStream, destination, skipIfUnchanged); @@ -322,17 +316,15 @@ public static void copyFileFromClassPath(final String source, final Path destina public static void copyDirectoryFromClassPath(final String source, final Path destination, final boolean skipIfUnchanged) throws IOException { final URL resource = FileConfig.class.getResource(source); if (resource == null) { - throw new IOException( - "A required target resource could not be found: " + source + "\n" + - "Perhaps a git submodule is missing or not up to date.\n" + - "See https://github.com/icyphy/lingua-franca/wiki/downloading-and-building#clone-the-lingua-franca-repository.\n" - + - "Also try to refresh and clean the project explorer if working from eclipse."); + throw new TargetResourceNotFoundException(source); } final URLConnection connection = resource.openConnection(); if (connection instanceof JarURLConnection) { - copyDirectoryFromJar((JarURLConnection) connection, destination, skipIfUnchanged); + boolean copiedFiles = copyDirectoryFromJar((JarURLConnection) connection, destination, skipIfUnchanged); + if (!copiedFiles) { + throw new TargetResourceNotFoundException(source); + } } else { try { Path dir = Paths.get(FileLocator.toFileURL(resource).toURI()); @@ -355,12 +347,14 @@ public static void copyDirectoryFromClassPath(final String source, final Path de * @param connection a URLConnection to the source directory within the jar * @param destination The file system path that the source directory is copied to. * @param skipIfUnchanged If true, don't overwrite the file if its content would not be changed + * @return true if any files were copied * @throws IOException If the given source cannot be copied. */ - private static void copyDirectoryFromJar(JarURLConnection connection, final Path destination, final boolean skipIfUnchanged) throws IOException { + private static boolean copyDirectoryFromJar(JarURLConnection connection, final Path destination, final boolean skipIfUnchanged) throws IOException { final JarFile jar = connection.getJarFile(); final String connectionEntryName = connection.getEntryName(); + boolean copiedFiles = false; // Iterate all entries in the jar file. for (Enumeration e = jar.entries(); e.hasMoreElements(); ) { final JarEntry entry = e.nextElement(); @@ -377,10 +371,12 @@ private static void copyDirectoryFromJar(JarURLConnection connection, final Path InputStream is = jar.getInputStream(entry); try (is) { copyInputStream(is, currentFile, skipIfUnchanged); + copiedFiles = true; } } } } + return copiedFiles; } /** diff --git a/org.lflang/src/org/lflang/util/TargetResourceNotFoundException.java b/org.lflang/src/org/lflang/util/TargetResourceNotFoundException.java new file mode 100644 index 0000000000..5b8aae3938 --- /dev/null +++ b/org.lflang/src/org/lflang/util/TargetResourceNotFoundException.java @@ -0,0 +1,14 @@ +package org.lflang.util; + +import java.io.IOException; + +public class TargetResourceNotFoundException extends IOException { + public TargetResourceNotFoundException(String resourcePath) { + super(String.format(""" + A required resource could not be found on the classpath or is an empty directory: %s + Perhaps a git submodule is missing or not up to date. + Try running 'git submodule update --init --recursive' and rebuild. + Also see https://www.lf-lang.org/download#developer. + """, resourcePath)); + } +} From bc9980f21a49604352bf95d8167f8177869fc97a Mon Sep 17 00:00:00 2001 From: Christian Menard Date: Thu, 26 Jan 2023 15:47:20 +0100 Subject: [PATCH 2/2] Update TargetResourceNotFoundException.java --- .../src/org/lflang/util/TargetResourceNotFoundException.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/org.lflang/src/org/lflang/util/TargetResourceNotFoundException.java b/org.lflang/src/org/lflang/util/TargetResourceNotFoundException.java index 5b8aae3938..dfcc1d671a 100644 --- a/org.lflang/src/org/lflang/util/TargetResourceNotFoundException.java +++ b/org.lflang/src/org/lflang/util/TargetResourceNotFoundException.java @@ -8,7 +8,7 @@ public TargetResourceNotFoundException(String resourcePath) { A required resource could not be found on the classpath or is an empty directory: %s Perhaps a git submodule is missing or not up to date. Try running 'git submodule update --init --recursive' and rebuild. - Also see https://www.lf-lang.org/download#developer. + Also see https://www.lf-lang.org/docs/handbook/developer-setup. """, resourcePath)); } }