|
8 | 8 | package com.intellij.lang.jsgraphql.ide.project.graphqlconfig; |
9 | 9 |
|
10 | 10 | 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; |
15 | 11 | 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; |
18 | 14 |
|
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; |
24 | 15 | import java.util.Map; |
25 | 16 |
|
26 | 17 | /** |
27 | 18 | * 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 |
30 | 19 | */ |
31 | 20 | public class GraphQLConfigGlobMatcherImpl implements GraphQLConfigGlobMatcher { |
32 | 21 |
|
33 | | - private static final String NASHORN_SCRIPT_OBJECT = "__nashorn__"; |
34 | | - |
35 | | - private final static Map<String, ScriptEngine> scriptEngines = Maps.newConcurrentMap(); |
36 | 22 | 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); |
39 | 24 |
|
40 | 25 | @Override |
41 | 26 | 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)); |
66 | 28 | } |
67 | 29 |
|
68 | 30 | } |
0 commit comments