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..dfcc1d671a --- /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/docs/handbook/developer-setup. + """, resourcePath)); + } +}