Commit b5db4d3f authored by Dave Syer's avatar Dave Syer

Enhance FileOptions to support multiple classpath resources

parent cd7b1b19
...@@ -17,9 +17,12 @@ ...@@ -17,9 +17,12 @@
package org.springframework.boot.cli.command; package org.springframework.boot.cli.command;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Enumeration;
import java.util.List; import java.util.List;
import joptsimple.OptionSet; import joptsimple.OptionSet;
...@@ -69,11 +72,11 @@ public class FileOptions { ...@@ -69,11 +72,11 @@ public class FileOptions {
break; break;
} }
if (filename.endsWith(".groovy") || filename.endsWith(".java")) { if (filename.endsWith(".groovy") || filename.endsWith(".java")) {
File file = getFile(filename, classLoader); List<File> file = getFiles(filename, classLoader);
if (file == null) { if (file.isEmpty()) {
throw new IllegalArgumentException("Can't find " + filename); throw new IllegalArgumentException("Can't find " + filename);
} }
files.add(file); files.addAll(file);
} }
} }
} }
...@@ -84,27 +87,38 @@ public class FileOptions { ...@@ -84,27 +87,38 @@ public class FileOptions {
throw new RuntimeException("Please specify at least one file to run"); throw new RuntimeException("Please specify at least one file to run");
} }
for (String path : defaultPaths) { for (String path : defaultPaths) {
File file = getFile(path, classLoader); for (File file : getFiles(path, classLoader)) {
if (file != null && file.exists()) { if (file != null && file.exists()) {
files.add(file); files.add(file);
} }
} }
} }
}
this.files = Collections.unmodifiableList(files); this.files = Collections.unmodifiableList(files);
} }
private File getFile(String filename, ClassLoader classLoader) { private List<File> getFiles(String filename, ClassLoader classLoader) {
File file = new File(filename); File file = new File(filename);
if (file.isFile() && file.canRead()) { if (file.isFile() && file.canRead()) {
return file; return Arrays.asList(file);
} }
List<File> result = new ArrayList<File>();
if (classLoader != null) { if (classLoader != null) {
URL url = classLoader.getResource(filename); Enumeration<URL> urls;
try {
urls = classLoader.getResources(filename);
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
if (url != null && url.toString().startsWith("file:")) { if (url != null && url.toString().startsWith("file:")) {
return new File(url.toString().substring("file:".length())); result.add(new File(url.toString().substring("file:".length())));
}
}
}
catch (IOException e) {
// Ignore
} }
} }
return null; return result;
} }
public List<?> getArgs() { public List<?> getArgs() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment