Skip to content

Commit 7540fa6

Browse files
committed
ScriptService: index scripts by path, not file
If two scripts exist at the same menu path, but exist as separate files on disk (due to being at two different script directory prefixes), then one script should override the other. Furthermore, we will soon be discovering URL-based scripts from classpath resources, which do not fit the files-on-disk assumption.
1 parent 179de87 commit 7540fa6

File tree

1 file changed

+5
-17
lines changed

1 file changed

+5
-17
lines changed

src/main/java/org/scijava/script/DefaultScriptService.java

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
package org.scijava.script;
3333

3434
import java.io.File;
35-
import java.io.IOException;
3635
import java.io.Reader;
3736
import java.io.StringReader;
3837
import java.math.BigDecimal;
@@ -106,8 +105,8 @@ public class DefaultScriptService extends
106105
/** Menu prefix to use for each script directory, if any. */
107106
private HashMap<File, MenuPath> menuPrefixes;
108107

109-
/** Index of available scripts, by script <em>file</em>. */
110-
private HashMap<File, ScriptInfo> scripts;
108+
/** Index of available scripts, by script path. */
109+
private HashMap<String, ScriptInfo> scripts;
111110

112111
/** Table of short type names to associated {@link Class}. */
113112
private HashMap<String, Class<?>> aliasMap;
@@ -330,7 +329,7 @@ private HashMap<File, MenuPath> menuPrefixes() {
330329
}
331330

332331
/** Gets {@link #scripts}, initializing if needed. */
333-
private HashMap<File, ScriptInfo> scripts() {
332+
private HashMap<String, ScriptInfo> scripts() {
334333
if (scripts == null) initScripts();
335334
return scripts;
336335
}
@@ -386,13 +385,13 @@ private synchronized void initMenuPrefixes() {
386385
private synchronized void initScripts() {
387386
if (scripts != null) return; // already initialized
388387

389-
final HashMap<File, ScriptInfo> map = new HashMap<>();
388+
final HashMap<String, ScriptInfo> map = new HashMap<>();
390389

391390
final ArrayList<ScriptInfo> scriptList = new ArrayList<>();
392391
new ScriptFinder(this).findScripts(scriptList);
393392

394393
for (final ScriptInfo info : scriptList) {
395-
map.put(asFile(info.getPath()), info);
394+
map.put(info.getPath(), info);
396395
}
397396

398397
scripts = map;
@@ -461,17 +460,6 @@ private ScriptInfo getOrCreate(final File file) {
461460
return new ScriptInfo(getContext(), file);
462461
}
463462

464-
private File asFile(final String path) {
465-
final File file = new File(path);
466-
try {
467-
return file.getCanonicalFile();
468-
}
469-
catch (final IOException exc) {
470-
log.warn(exc);
471-
return file.getAbsoluteFile();
472-
}
473-
}
474-
475463
@SuppressWarnings({ "rawtypes", "unchecked" })
476464
private Future<ScriptModule> cast(final Future<Module> future) {
477465
return (Future) future;

0 commit comments

Comments
 (0)