Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Useful error message upon encountering missing runtime sources #1573

Merged
merged 2 commits into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions org.lflang/src/org/lflang/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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());
Expand All @@ -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<JarEntry> e = jar.entries(); e.hasMoreElements(); ) {
final JarEntry entry = e.nextElement();
Expand All @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}
}