Skip to content

Commit

Permalink
fix script cache not working
Browse files Browse the repository at this point in the history
  • Loading branch information
brachy84 committed Oct 6, 2024
1 parent 9e6f64c commit a0b95a0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ public void init(SourceUnit context, CallbackInfo ci) {
// can happen with traits
return;
}
int i = rel.lastIndexOf(File.separatorChar);
int i = rel.lastIndexOf('/');
if (i >= 0) {
// inject correct package declaration into script
String packageName = rel.substring(0, i).replace(File.separatorChar, '.') + '.';
String packageName = rel.substring(0, i).replace('/', '.') + '.';
this.packageNode = new PackageNode(packageName);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,10 @@ public Class<?> onRecompileClass(URL source, String className) {

@Override
protected Class<?> loadScriptClass(GroovyScriptEngine engine, File file) {
File relativeFile = this.scriptRoot.toPath().relativize(file.toPath()).toFile();
String relativeFileName = FileUtil.relativize(this.scriptRoot.getPath(), file.getPath());
File relativeFile = new File(relativeFileName);
long lastModified = file.lastModified();
CompiledScript comp = this.index.get(relativeFile.toString());
CompiledScript comp = this.index.get(relativeFileName);

if (ENABLE_CACHE && comp != null && lastModified <= comp.lastEdited && comp.clazz == null && comp.readData(this.cacheRoot.getPath())) {
// class is not loaded, but the cached class bytes are still valid
Expand All @@ -285,8 +286,8 @@ protected Class<?> loadScriptClass(GroovyScriptEngine engine, File file) {
} else if (!ENABLE_CACHE || (comp == null || comp.clazz == null || lastModified > comp.lastEdited)) {
// class is not loaded and class bytes don't exist yet or script has been edited
if (comp == null) {
comp = new CompiledScript(relativeFile.toString(), 0);
this.index.put(relativeFile.toString(), comp);
comp = new CompiledScript(relativeFileName, 0);
this.index.put(relativeFileName, comp);
}
if (lastModified > comp.lastEdited || comp.preprocessors == null) {
// recompile preprocessors if there is no data or script was edited
Expand All @@ -303,7 +304,7 @@ protected Class<?> loadScriptClass(GroovyScriptEngine engine, File file) {
Class<?> clazz = super.loadScriptClass(engine, relativeFile);
if (comp.clazz == null) {
// should not happen
GroovyLog.get().errorMC("Class for {} was loaded, but didn't receive class created callback!", relativeFile);
GroovyLog.get().errorMC("Class for {} was loaded, but didn't receive class created callback!", relativeFileName);
if (ENABLE_CACHE) comp.clazz = clazz;
}
} else {
Expand Down

0 comments on commit a0b95a0

Please sign in to comment.