Skip to content

Commit 7a4d41c

Browse files
committed
Fix for Nashorn deprecation warning (#287)
- Replaced minimatch-nashorn.js with Java-based implementation at https://github.com/angelozerr/minimatch.java
1 parent 574622e commit 7a4d41c

File tree

3 files changed

+5
-51
lines changed

3 files changed

+5
-51
lines changed

build.gradle

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ buildscript {
99

1010
plugins {
1111
id 'org.jetbrains.intellij' version '0.4.15'
12-
id "de.undercouch.download" version "3.4.3"
1312
id "com.github.ManifestClasspath" version "0.1.0-RELEASE"
1413
}
1514

@@ -33,6 +32,7 @@ dependencies {
3332
compile "commons-io:commons-io:2.6"
3433
compile "com.atlassian.commonmark:commonmark:0.12.1"
3534
compile group: 'org.yaml', name: 'snakeyaml', version: '1.21'
35+
compile 'fr.opensagres.js:minimatch.java:1.1.0'
3636

3737
testCompile group: 'junit', name: 'junit', version: '4.12'
3838
}
@@ -123,14 +123,7 @@ task generateGraphQLEndpointDocParser(type: GenerateParser) {
123123
pathToPsiRoot = 'com/intellij/lang/jsgraphql/endpoint/doc/psi'
124124
}
125125

126-
task downloadMinimatchNashorn(type: Download) {
127-
src 'https://raw.githubusercontent.com/jimkyndemeyer/minimatch-nashorn/1.0.1/dist/minimatch-nashorn.js'
128-
dest 'resources/META-INF'
129-
overwrite false
130-
}
131-
132126
compileJava {
133-
dependsOn downloadMinimatchNashorn
134127
dependsOn generateGraphQLEndpointLexer
135128
dependsOn generateGraphQLEndpointParser
136129
dependsOn generateGraphQLEndpointDocLexer

resources/META-INF/minimatch-nashorn.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/main/com/intellij/lang/jsgraphql/ide/project/graphqlconfig/GraphQLConfigGlobMatcherImpl.java

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,61 +8,23 @@
88
package com.intellij.lang.jsgraphql.ide.project.graphqlconfig;
99

1010
import com.google.common.collect.Maps;
11-
import com.intellij.ide.plugins.PluginManager;
12-
import com.intellij.openapi.application.ApplicationManager;
13-
import com.intellij.openapi.extensions.PluginDescriptor;
14-
import com.intellij.openapi.extensions.PluginId;
1511
import com.intellij.openapi.util.Pair;
16-
import jdk.nashorn.api.scripting.ScriptObjectMirror;
17-
import org.apache.commons.io.IOUtils;
12+
import minimatch.Minimatch;
13+
import minimatch.Options;
1814

19-
import javax.script.ScriptEngine;
20-
import javax.script.ScriptEngineManager;
21-
import javax.script.ScriptException;
22-
import java.io.IOException;
23-
import java.nio.charset.Charset;
2415
import java.util.Map;
2516

2617
/**
2718
* Matcher which uses nashorn-minimatch to achieve same glob semantics as graphql-config.
28-
*
29-
* See /META-INF/minimatch-nashorn.js and https://github.com/prismagraphql/graphql-config/blob/b6785a7f0c1b84010cd6e9b94797796254d527b9/src/utils.ts#L52
3019
*/
3120
public class GraphQLConfigGlobMatcherImpl implements GraphQLConfigGlobMatcher {
3221

33-
private static final String NASHORN_SCRIPT_OBJECT = "__nashorn__";
34-
35-
private final static Map<String, ScriptEngine> scriptEngines = Maps.newConcurrentMap();
3622
private final static Map<Pair<String, String>, Boolean> matches = Maps.newConcurrentMap();
37-
38-
private final static PluginDescriptor pluginDescriptor = PluginManager.getPlugin(PluginId.getId("com.intellij.lang.jsgraphql"));
23+
private final static Options OPTIONS = new Options().setMatchBase(true);
3924

4025
@Override
4126
public boolean matches(String filePath, String glob) {
42-
if (pluginDescriptor == null) {
43-
throw new IllegalStateException("Plugin description is null. Can not load minimatch-nashorn.js");
44-
}
45-
return matches.computeIfAbsent(Pair.create(filePath, glob), args -> {
46-
final ScriptEngine scriptEngine = scriptEngines.computeIfAbsent("default", s -> {
47-
final ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
48-
try {
49-
engine.put(NASHORN_SCRIPT_OBJECT, engine.eval("new Object()"));
50-
final String script;
51-
if(ApplicationManager.getApplication().isUnitTestMode()) {
52-
// plugin class loader appears to be unable to locate certain resources during unit-test?!
53-
script = IOUtils.toString(getClass().getResource("/META-INF/minimatch-nashorn.js"), Charset.forName("UTF-8"));
54-
} else {
55-
script = IOUtils.resourceToString("/META-INF/minimatch-nashorn.js", Charset.forName("UTF-8"), pluginDescriptor.getPluginClassLoader());
56-
}
57-
engine.eval(script);
58-
} catch (IOException | ScriptException e) {
59-
throw new RuntimeException("Unable to load minimatch-nashorn.js", e);
60-
}
61-
return engine;
62-
});
63-
final ScriptObjectMirror nashornScriptObject = (ScriptObjectMirror) scriptEngine.get(NASHORN_SCRIPT_OBJECT);
64-
return (Boolean) nashornScriptObject.callMember("minimatch", args.first, args.second);
65-
});
27+
return matches.computeIfAbsent(Pair.create(filePath, glob), args -> Minimatch.minimatch(args.first, args.second, OPTIONS));
6628
}
6729

6830
}

0 commit comments

Comments
 (0)