Skip to content

Commit 387f619

Browse files
committed
added magic annotation for 3rd party deps
1 parent f5b738d commit 387f619

File tree

4 files changed

+434
-3
lines changed

4 files changed

+434
-3
lines changed

nbcode/notebooks/src/org/netbeans/modules/nbcode/java/notebook/CodeEval.java

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.netbeans.modules.nbcode.java.notebook;
1717

18+
import java.io.File;
1819
import java.io.PrintWriter;
1920
import java.io.Writer;
2021
import java.util.ArrayList;
@@ -24,6 +25,7 @@
2425
import java.util.concurrent.CompletableFuture;
2526
import java.util.concurrent.ConcurrentHashMap;
2627
import java.util.function.BiConsumer;
28+
import java.util.function.Consumer;
2729
import java.util.logging.Level;
2830
import java.util.logging.Logger;
2931
import java.util.regex.Matcher;
@@ -43,6 +45,7 @@
4345
import org.netbeans.modules.java.lsp.server.notebook.NotebookCellExecutionProgressResultParams.Builder;
4446
import org.netbeans.modules.java.lsp.server.notebook.NotebookCellExecutionProgressResultParams.EXECUTION_STATUS;
4547
import org.netbeans.modules.java.lsp.server.protocol.NbCodeLanguageClient;
48+
import org.openide.util.Exceptions;
4649
import org.openide.util.NbBundle;
4750
import org.openide.util.RequestProcessor;
4851

@@ -79,7 +82,10 @@ public class CodeEval {
7982
private final Map<String, RequestProcessor> codeExecMap = new ConcurrentHashMap<>();
8083
private final Map<String, List<CompletableFuture<Boolean>>> pendingTasks = new ConcurrentHashMap<>();
8184
private final Map<String, String> activeCellExecutionMapping = new ConcurrentHashMap<>();
82-
85+
// Define the pattern for @MavenGrab("g", "a", "v")
86+
private static final Pattern MAVEN_GRAB_PATTERN =
87+
Pattern.compile("@MavenGrab\\s*\\(\\s*\"([^\"]+)\"\\s*,\\s*\"([^\"]+)\"\\s*,\\s*\"([^\"]+)\"\\s*\\)");
88+
8389
public static CodeEval getInstance() {
8490
return Singleton.instance;
8591
}
@@ -215,8 +221,9 @@ public void runCode(JShell jshell, String code) {
215221

216222
public void runCode(JShell jshell, String code, String notebookId) {
217223
try {
224+
String cleanCode = processMavenGrab(jshell, code, notebookId);
218225
SourceCodeAnalysis analysis = jshell.sourceCodeAnalysis();
219-
List<String> snippets = NotebookUtils.getCodeSnippets(analysis, code);
226+
List<String> snippets = NotebookUtils.getCodeSnippets(analysis, cleanCode);
220227

221228
for (String snippet : snippets) {
222229
for (SnippetEvent event : jshell.eval(snippet)) {
@@ -235,6 +242,41 @@ public void runCode(JShell jshell, String code, String notebookId) {
235242
throw new IllegalStateException(e);
236243
}
237244
}
245+
246+
private String processMavenGrab(JShell jshell, String code, String notebookId) {
247+
Matcher matcher = MAVEN_GRAB_PATTERN.matcher(code);
248+
StringBuffer sb = new StringBuffer();
249+
250+
while (matcher.find()) {
251+
try {
252+
String groupId = matcher.group(1);
253+
String artifactId = matcher.group(2);
254+
String version = matcher.group(3);
255+
String coords = String.format("%s:%s:%s", groupId, artifactId, version);
256+
257+
Consumer<String> progressCallback = (msg) -> {
258+
if (notebookId != null) {
259+
sendNotification(notebookId, msg.getBytes(), EXECUTION_STATUS.EXECUTING, false);
260+
}
261+
};
262+
263+
List<File> jars = MagicCommandHandler.handler(coords, progressCallback);
264+
265+
for (File jar : jars) {
266+
jshell.addToClasspath(jar.getAbsolutePath());
267+
String msg = "Jar added to classpath: " + jar.getName() + "\n";
268+
sendNotification(notebookId, msg.getBytes(), EXECUTION_STATUS.EXECUTING, false);
269+
}
270+
271+
matcher.appendReplacement(sb, "");
272+
} catch (Exception ex) {
273+
LOG.severe(() -> "Some error occurred while downloading artifacts" + ex.getMessage());
274+
sendNotification(notebookId, "Some error occurred while downloading artifacts".getBytes(), EXECUTION_STATUS.EXECUTING, true);
275+
}
276+
}
277+
matcher.appendTail(sb);
278+
return sb.toString();
279+
}
238280

239281
private RequestProcessor getCodeExec(String notebookId) {
240282
return codeExecMap.computeIfAbsent(notebookId, (id) -> {
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
/*
3+
* Copyright (c) 2025, Oracle and/or its affiliates.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.netbeans.modules.nbcode.java.notebook;
18+
19+
import java.io.File;
20+
import java.util.List;
21+
import java.util.function.Consumer;
22+
import java.util.logging.Logger;
23+
import org.netbeans.modules.java.lsp.server.notebook.NotebookDependencyHandler;
24+
25+
/**
26+
*
27+
* @author atalati
28+
*/
29+
public class MagicCommandHandler {
30+
31+
private static final Logger LOG = Logger.getLogger(MagicCommandHandler.class.getName());
32+
33+
public static List<File> handler(String coords, Consumer<String> progressCallback) throws Exception {
34+
NotebookDependencyHandler handler = NotebookDependencyHandler.getInstance();
35+
List<File> files;
36+
if (progressCallback == null) {
37+
files = handler.resolveTransitiveDependencies(coords);
38+
} else {
39+
files = handler.resolveTransitiveDependencies(coords, progressCallback);
40+
}
41+
42+
if (progressCallback == null) {
43+
for (File f : files) {
44+
LOG.info(() -> "Jar added to classpath: " + f.getName());
45+
}
46+
}
47+
48+
return files;
49+
}
50+
51+
public static List<File> handler(String coords) throws Exception {
52+
return handler(coords, null);
53+
}
54+
55+
}

nbcode/notebooks/src/org/netbeans/modules/nbcode/java/notebook/NotebookConfigs.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ private CompletableFuture<Void> initializeConfigs() {
154154
}
155155

156156
public String getJdkVersion() {
157-
return System.getProperty("java.version").split("\\.")[0];
157+
return System.getProperty("java.version").split("[\\.\\-]")[0];
158158
}
159159

160160
public void notebookConfigsChangeListener(JsonObject settings) {

0 commit comments

Comments
 (0)