cstamas commented on code in PR #2117:
URL: https://github.com/apache/maven/pull/2117#discussion_r1967411276


##########
impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnsh/builtin/BuiltinShellCommandRegistryFactory.java:
##########
@@ -130,53 +129,133 @@ public String name() {
             return "Builtin Maven Shell commands";
         }
 
-        private List<Completers.OptDesc> commandOptions(String command) {
+        private void shell(CommandInput input) {
+            if (input.args().length > 0) {
+                try {
+                    ProcessBuilder builder = new ProcessBuilder();
+                    List<String> processArgs = new ArrayList<>();
+                    if (Os.IS_WINDOWS) {
+                        processArgs.add("cmd.exe");
+                        processArgs.add("/c");
+                    } else {
+                        processArgs.add("sh");
+                        processArgs.add("-c");
+                    }
+                    processArgs.add(String.join(" ", 
Arrays.asList(input.args())));
+                    builder.command(processArgs);
+                    builder.directory(shellContext.cwd.get().toFile());
+                    Process process = builder.start();
+                    Thread out = new Thread(new 
StreamGobbler(process.getInputStream(), shellContext.logger::info));
+                    Thread err = new Thread(new 
StreamGobbler(process.getErrorStream(), shellContext.logger::error));
+                    out.start();
+                    err.start();
+                    int exitCode = process.waitFor();
+                    out.join();
+                    err.join();
+                    if (exitCode != 0) {
+                        shellContext.logger.error("Shell command exited with 
code " + exitCode);
+                    }
+                } catch (Exception e) {
+                    saveException(e);
+                }
+            }
+        }
+
+        private void cd(CommandInput input) {
             try {
-                invoke(new CommandSession(), command, "--help");
-            } catch (Options.HelpException e) {
-                return compileCommandOptions(e.getMessage());
+                if (input.args().length == 1) {
+                    shellContext.cwd.change(input.args()[0]);
+                } else {
+                    shellContext.writer.accept("Error: 'cd' accepts only one 
argument");
+                }
             } catch (Exception e) {
-                // ignore
+                saveException(e);
+            }
+        }
+
+        private List<Completer> cdCompleter(String name) {
+            return List.of(new ArgumentCompleter(new 
Completers.DirectoriesCompleter(shellContext.cwd)));
+        }
+
+        private void ls(CommandInput input) {
+            try {
+                try (Stream<Path> list = Files.list(shellContext.cwd.get())) {
+                    list.forEach(file -> {
+                        if (Files.isDirectory(file)) {
+                            
shellContext.writer.accept(file.getFileName().toString() + "/");
+                        } else {
+                            
shellContext.writer.accept(file.getFileName().toString());
+                        }
+                    });
+                }
+            } catch (Exception e) {
+                saveException(e);
+            }
+        }
+
+        private void pwd(CommandInput input) {
+            try {
+                shellContext.writer.accept(shellContext.cwd.get().toString());
+            } catch (Exception e) {
+                saveException(e);
             }
-            return null;
         }
 
         private void mvn(CommandInput input) {
             try {
                 shellMavenInvoker.invoke(mavenParser.parseInvocation(

Review Comment:
   Re ctrl+c case, am really unsure what to do, esp if user uses `-T` 
command... Adding this single line to mvnsh invoker does NOT work (and even 
deadlocks in some circumstances):
   ```
               context.terminal.handle(Terminal.Signal.INT, signal -> 
Thread.currentThread().interrupt());
   
   ```



##########
impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnsh/builtin/BuiltinShellCommandRegistryFactory.java:
##########
@@ -130,53 +129,133 @@ public String name() {
             return "Builtin Maven Shell commands";
         }
 
-        private List<Completers.OptDesc> commandOptions(String command) {
+        private void shell(CommandInput input) {
+            if (input.args().length > 0) {
+                try {
+                    ProcessBuilder builder = new ProcessBuilder();
+                    List<String> processArgs = new ArrayList<>();
+                    if (Os.IS_WINDOWS) {
+                        processArgs.add("cmd.exe");
+                        processArgs.add("/c");
+                    } else {
+                        processArgs.add("sh");
+                        processArgs.add("-c");
+                    }
+                    processArgs.add(String.join(" ", 
Arrays.asList(input.args())));
+                    builder.command(processArgs);
+                    builder.directory(shellContext.cwd.get().toFile());
+                    Process process = builder.start();
+                    Thread out = new Thread(new 
StreamGobbler(process.getInputStream(), shellContext.logger::info));
+                    Thread err = new Thread(new 
StreamGobbler(process.getErrorStream(), shellContext.logger::error));
+                    out.start();
+                    err.start();
+                    int exitCode = process.waitFor();
+                    out.join();
+                    err.join();
+                    if (exitCode != 0) {
+                        shellContext.logger.error("Shell command exited with 
code " + exitCode);
+                    }
+                } catch (Exception e) {
+                    saveException(e);
+                }
+            }
+        }
+
+        private void cd(CommandInput input) {
             try {
-                invoke(new CommandSession(), command, "--help");
-            } catch (Options.HelpException e) {
-                return compileCommandOptions(e.getMessage());
+                if (input.args().length == 1) {
+                    shellContext.cwd.change(input.args()[0]);
+                } else {
+                    shellContext.writer.accept("Error: 'cd' accepts only one 
argument");
+                }
             } catch (Exception e) {
-                // ignore
+                saveException(e);
+            }
+        }
+
+        private List<Completer> cdCompleter(String name) {
+            return List.of(new ArgumentCompleter(new 
Completers.DirectoriesCompleter(shellContext.cwd)));
+        }
+
+        private void ls(CommandInput input) {
+            try {
+                try (Stream<Path> list = Files.list(shellContext.cwd.get())) {
+                    list.forEach(file -> {
+                        if (Files.isDirectory(file)) {
+                            
shellContext.writer.accept(file.getFileName().toString() + "/");
+                        } else {
+                            
shellContext.writer.accept(file.getFileName().toString());
+                        }
+                    });
+                }
+            } catch (Exception e) {
+                saveException(e);
+            }
+        }
+
+        private void pwd(CommandInput input) {
+            try {
+                shellContext.writer.accept(shellContext.cwd.get().toString());
+            } catch (Exception e) {
+                saveException(e);
             }
-            return null;
         }
 
         private void mvn(CommandInput input) {
             try {
                 shellMavenInvoker.invoke(mavenParser.parseInvocation(

Review Comment:
   Re ctrl+c case, am really unsure what to do, esp if user uses `-T` 
command... Adding this single line to mvnsh invoker does NOT work (and even 
deadlocks in some circumstances):
   ```
   context.terminal.handle(Terminal.Signal.INT, signal -> 
Thread.currentThread().interrupt());
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@maven.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to