From 499e73dd4f4db214689ce7a31d67158d8e1f0643 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Sun, 15 Dec 2024 20:42:44 +0100 Subject: [PATCH] Collapse all --- .../builtin/BuiltinShellCommandRegistry.java | 148 ------------- .../BuiltinShellCommandRegistryFactory.java | 196 ++++++++++++++++++ .../mvnsh/builtin/ShellEncryptInvoker.java | 62 ------ .../mvnsh/builtin/ShellMavenInvoker.java | 62 ------ 4 files changed, 196 insertions(+), 272 deletions(-) delete mode 100644 impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnsh/builtin/BuiltinShellCommandRegistry.java delete mode 100644 impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnsh/builtin/ShellEncryptInvoker.java delete mode 100644 impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnsh/builtin/ShellMavenInvoker.java diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnsh/builtin/BuiltinShellCommandRegistry.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnsh/builtin/BuiltinShellCommandRegistry.java deleted file mode 100644 index f472798c883..00000000000 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnsh/builtin/BuiltinShellCommandRegistry.java +++ /dev/null @@ -1,148 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.maven.cling.invoker.mvnsh.builtin; - -import java.util.ArrayList; -import java.util.EnumSet; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import org.apache.maven.api.cli.ParserRequest; -import org.apache.maven.cling.invoker.LookupContext; -import org.apache.maven.cling.invoker.mvn.MavenParser; -import org.apache.maven.cling.invoker.mvnenc.EncryptParser; -import org.jline.builtins.Completers; -import org.jline.builtins.Options; -import org.jline.console.CmdDesc; -import org.jline.console.CommandInput; -import org.jline.console.CommandMethods; -import org.jline.console.impl.AbstractCommandRegistry; -import org.jline.reader.Completer; -import org.jline.reader.impl.completer.ArgumentCompleter; -import org.jline.reader.impl.completer.NullCompleter; - -import static java.util.Objects.requireNonNull; -import static org.jline.console.impl.JlineCommandRegistry.compileCommandOptions; - -public class BuiltinShellCommandRegistry extends AbstractCommandRegistry implements AutoCloseable { - public enum Command { - MVN, - MVNENC - } - - private final LookupContext shellContext; - private final ShellMavenInvoker shellMavenInvoker; - private final MavenParser mavenParser; - private final ShellEncryptInvoker shellEncryptInvoker; - private final EncryptParser encryptParser; - - public BuiltinShellCommandRegistry(LookupContext shellContext) { - this.shellContext = requireNonNull(shellContext, "shellContext"); - this.shellMavenInvoker = new ShellMavenInvoker(shellContext); - this.mavenParser = new MavenParser(); - this.shellEncryptInvoker = new ShellEncryptInvoker(shellContext); - this.encryptParser = new EncryptParser(); - Set commands = new HashSet<>(EnumSet.allOf(Command.class)); - Map commandName = new HashMap<>(); - Map commandExecute = new HashMap<>(); - for (Command c : commands) { - commandName.put(c, c.name().toLowerCase()); - } - commandExecute.put(Command.MVN, new CommandMethods(this::mvn, this::mvnCompleter)); - commandExecute.put(Command.MVNENC, new CommandMethods(this::mvnenc, this::mvnencCompleter)); - registerCommands(commandName, commandExecute); - } - - @Override - public void close() throws Exception { - shellMavenInvoker.close(); - shellEncryptInvoker.close(); - } - - @Override - public List commandInfo(String command) { - return List.of(); - } - - @Override - public CmdDesc commandDescription(List args) { - return null; - } - - @Override - public String name() { - return "Builtin Maven Shell commands"; - } - - private List commandOptions(String command) { - try { - invoke(new CommandSession(), command, "--help"); - } catch (Options.HelpException e) { - return compileCommandOptions(e.getMessage()); - } catch (Exception e) { - // ignore - } - return null; - } - - private void mvn(CommandInput input) { - try { - shellMavenInvoker.invoke(mavenParser.parseInvocation(ParserRequest.mvn( - input.args(), - shellContext.invokerRequest.logger(), - shellContext.invokerRequest.messageBuilderFactory()) - .build())); - } catch (Exception e) { - saveException(e); - } - } - - private List mvnCompleter(String name) { - List completers = new ArrayList<>(); - completers.add(new ArgumentCompleter( - NullCompleter.INSTANCE, - new Completers.OptionCompleter( - new Completers.FilesCompleter(shellContext.invokerRequest::cwd), this::commandOptions, 1))); - return completers; - } - - private void mvnenc(CommandInput input) { - try { - shellEncryptInvoker.invoke(encryptParser.parseInvocation(ParserRequest.mvnenc( - input.args(), - shellContext.invokerRequest.logger(), - shellContext.invokerRequest.messageBuilderFactory()) - .build())); - } catch (Exception e) { - saveException(e); - } - } - - private List mvnencCompleter(String name) { - List completers = new ArrayList<>(); - completers.add(new ArgumentCompleter( - NullCompleter.INSTANCE, - new Completers.OptionCompleter( - new Completers.FilesCompleter(shellContext.invokerRequest::cwd), this::commandOptions, 1))); - return completers; - } -} diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnsh/builtin/BuiltinShellCommandRegistryFactory.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnsh/builtin/BuiltinShellCommandRegistryFactory.java index 6a19082344b..70af795ca7a 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnsh/builtin/BuiltinShellCommandRegistryFactory.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnsh/builtin/BuiltinShellCommandRegistryFactory.java @@ -18,11 +18,42 @@ */ package org.apache.maven.cling.invoker.mvnsh.builtin; +import java.util.ArrayList; +import java.util.EnumSet; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.function.Consumer; + +import org.apache.maven.api.cli.InvokerException; +import org.apache.maven.api.cli.InvokerRequest; +import org.apache.maven.api.cli.ParserRequest; import org.apache.maven.api.di.Named; import org.apache.maven.api.di.Singleton; +import org.apache.maven.api.services.Lookup; import org.apache.maven.cling.invoker.LookupContext; +import org.apache.maven.cling.invoker.mvn.MavenContext; +import org.apache.maven.cling.invoker.mvn.MavenInvoker; +import org.apache.maven.cling.invoker.mvn.MavenParser; +import org.apache.maven.cling.invoker.mvnenc.EncryptContext; +import org.apache.maven.cling.invoker.mvnenc.EncryptInvoker; +import org.apache.maven.cling.invoker.mvnenc.EncryptParser; import org.apache.maven.cling.invoker.mvnsh.ShellCommandRegistryFactory; +import org.jline.builtins.Completers; +import org.jline.builtins.Options; +import org.jline.console.CmdDesc; +import org.jline.console.CommandInput; +import org.jline.console.CommandMethods; import org.jline.console.CommandRegistry; +import org.jline.console.impl.AbstractCommandRegistry; +import org.jline.reader.Completer; +import org.jline.reader.impl.completer.ArgumentCompleter; +import org.jline.reader.impl.completer.NullCompleter; + +import static java.util.Objects.requireNonNull; +import static org.jline.console.impl.JlineCommandRegistry.compileCommandOptions; @Named("builtin") @Singleton @@ -30,4 +61,169 @@ public class BuiltinShellCommandRegistryFactory implements ShellCommandRegistryF public CommandRegistry createShellCommandRegistry(LookupContext context) { return new BuiltinShellCommandRegistry(context); } + + private static class BuiltinShellCommandRegistry extends AbstractCommandRegistry implements AutoCloseable { + public enum Command { + MVN, + MVNENC + } + + private final LookupContext shellContext; + private final ShellMavenInvoker shellMavenInvoker; + private final MavenParser mavenParser; + private final ShellEncryptInvoker shellEncryptInvoker; + private final EncryptParser encryptParser; + + private BuiltinShellCommandRegistry(LookupContext shellContext) { + this.shellContext = requireNonNull(shellContext, "shellContext"); + this.shellMavenInvoker = new ShellMavenInvoker(shellContext.invokerRequest.lookup(), contextCopier()); + this.mavenParser = new MavenParser(); + this.shellEncryptInvoker = new ShellEncryptInvoker(shellContext.invokerRequest.lookup(), contextCopier()); + this.encryptParser = new EncryptParser(); + Set commands = new HashSet<>(EnumSet.allOf(Command.class)); + Map commandName = new HashMap<>(); + Map commandExecute = new HashMap<>(); + for (Command c : commands) { + commandName.put(c, c.name().toLowerCase()); + } + commandExecute.put(Command.MVN, new CommandMethods(this::mvn, this::mvnCompleter)); + commandExecute.put(Command.MVNENC, new CommandMethods(this::mvnenc, this::mvnencCompleter)); + registerCommands(commandName, commandExecute); + } + + private Consumer contextCopier() { + return result -> { + result.logger = shellContext.logger; + result.loggerFactory = shellContext.loggerFactory; + result.slf4jConfiguration = shellContext.slf4jConfiguration; + result.loggerLevel = shellContext.loggerLevel; + result.coloredOutput = shellContext.coloredOutput; + result.terminal = shellContext.terminal; + result.writer = shellContext.writer; + + result.installationSettingsPath = shellContext.installationSettingsPath; + result.projectSettingsPath = shellContext.projectSettingsPath; + result.userSettingsPath = shellContext.userSettingsPath; + result.interactive = shellContext.interactive; + result.localRepositoryPath = shellContext.localRepositoryPath; + result.effectiveSettings = shellContext.effectiveSettings; + + result.containerCapsule = shellContext.containerCapsule; + result.lookup = shellContext.lookup; + result.eventSpyDispatcher = shellContext.eventSpyDispatcher; + }; + } + + @Override + public void close() throws Exception { + shellMavenInvoker.close(); + shellEncryptInvoker.close(); + } + + @Override + public List commandInfo(String command) { + return List.of(); + } + + @Override + public CmdDesc commandDescription(List args) { + return null; + } + + @Override + public String name() { + return "Builtin Maven Shell commands"; + } + + private List commandOptions(String command) { + try { + invoke(new CommandSession(), command, "--help"); + } catch (Options.HelpException e) { + return compileCommandOptions(e.getMessage()); + } catch (Exception e) { + // ignore + } + return null; + } + + private void mvn(CommandInput input) { + try { + shellMavenInvoker.invoke(mavenParser.parseInvocation(ParserRequest.mvn( + input.args(), + shellContext.invokerRequest.logger(), + shellContext.invokerRequest.messageBuilderFactory()) + .build())); + } catch (Exception e) { + saveException(e); + } + } + + private List mvnCompleter(String name) { + List completers = new ArrayList<>(); + completers.add(new ArgumentCompleter( + NullCompleter.INSTANCE, + new Completers.OptionCompleter( + new Completers.FilesCompleter(shellContext.invokerRequest::cwd), this::commandOptions, 1))); + return completers; + } + + private void mvnenc(CommandInput input) { + try { + shellEncryptInvoker.invoke(encryptParser.parseInvocation(ParserRequest.mvnenc( + input.args(), + shellContext.invokerRequest.logger(), + shellContext.invokerRequest.messageBuilderFactory()) + .build())); + } catch (Exception e) { + saveException(e); + } + } + + private List mvnencCompleter(String name) { + List completers = new ArrayList<>(); + completers.add(new ArgumentCompleter( + NullCompleter.INSTANCE, + new Completers.OptionCompleter( + new Completers.FilesCompleter(shellContext.invokerRequest::cwd), this::commandOptions, 1))); + return completers; + } + } + + /** + * Shell Encrypt invoker: passes over relevant context bits. + */ + private static class ShellEncryptInvoker extends EncryptInvoker { + private final Consumer contextCopier; + + private ShellEncryptInvoker(Lookup lookup, Consumer contextCopier) { + super(lookup); + this.contextCopier = contextCopier; + } + + @Override + protected EncryptContext createContext(InvokerRequest invokerRequest) throws InvokerException { + EncryptContext result = new EncryptContext(invokerRequest, false); + contextCopier.accept(result); + return result; + } + } + + /** + * Shell Maven invoker: passes over relevant context bits. + */ + private static class ShellMavenInvoker extends MavenInvoker { + private final Consumer contextCopier; + + private ShellMavenInvoker(Lookup lookup, Consumer contextCopier) { + super(lookup); + this.contextCopier = contextCopier; + } + + @Override + protected MavenContext createContext(InvokerRequest invokerRequest) throws InvokerException { + MavenContext result = new MavenContext(invokerRequest, false); + contextCopier.accept(result); + return result; + } + } } diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnsh/builtin/ShellEncryptInvoker.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnsh/builtin/ShellEncryptInvoker.java deleted file mode 100644 index 9be01297878..00000000000 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnsh/builtin/ShellEncryptInvoker.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.maven.cling.invoker.mvnsh.builtin; - -import org.apache.maven.api.cli.InvokerException; -import org.apache.maven.api.cli.InvokerRequest; -import org.apache.maven.cling.invoker.LookupContext; -import org.apache.maven.cling.invoker.mvnenc.EncryptContext; -import org.apache.maven.cling.invoker.mvnenc.EncryptInvoker; - -/** - * Shell Encrypt invoker: passes over relevant context bits. - */ -public class ShellEncryptInvoker extends EncryptInvoker { - private final LookupContext shellContext; - - public ShellEncryptInvoker(LookupContext shellContext) { - super(shellContext.invokerRequest.lookup()); - this.shellContext = shellContext; - } - - @Override - protected EncryptContext createContext(InvokerRequest invokerRequest) throws InvokerException { - EncryptContext result = new EncryptContext(invokerRequest, false); - - result.logger = shellContext.logger; - result.loggerFactory = shellContext.loggerFactory; - result.slf4jConfiguration = shellContext.slf4jConfiguration; - result.loggerLevel = shellContext.loggerLevel; - result.coloredOutput = shellContext.coloredOutput; - result.terminal = shellContext.terminal; - result.writer = shellContext.writer; - - result.installationSettingsPath = shellContext.installationSettingsPath; - result.projectSettingsPath = shellContext.projectSettingsPath; - result.userSettingsPath = shellContext.userSettingsPath; - result.interactive = shellContext.interactive; - result.localRepositoryPath = shellContext.localRepositoryPath; - result.effectiveSettings = shellContext.effectiveSettings; - - result.containerCapsule = shellContext.containerCapsule; - result.lookup = shellContext.lookup; - result.eventSpyDispatcher = shellContext.eventSpyDispatcher; - return result; - } -} diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnsh/builtin/ShellMavenInvoker.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnsh/builtin/ShellMavenInvoker.java deleted file mode 100644 index 4536e54c96c..00000000000 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnsh/builtin/ShellMavenInvoker.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.maven.cling.invoker.mvnsh.builtin; - -import org.apache.maven.api.cli.InvokerException; -import org.apache.maven.api.cli.InvokerRequest; -import org.apache.maven.cling.invoker.LookupContext; -import org.apache.maven.cling.invoker.mvn.MavenContext; -import org.apache.maven.cling.invoker.mvn.MavenInvoker; - -/** - * Shell Maven invoker: passes over relevant context bits. - */ -public class ShellMavenInvoker extends MavenInvoker { - private final LookupContext shellContext; - - public ShellMavenInvoker(LookupContext shellContext) { - super(shellContext.invokerRequest.lookup()); - this.shellContext = shellContext; - } - - @Override - protected MavenContext createContext(InvokerRequest invokerRequest) throws InvokerException { - MavenContext result = new MavenContext(invokerRequest, false); - - result.logger = shellContext.logger; - result.loggerFactory = shellContext.loggerFactory; - result.slf4jConfiguration = shellContext.slf4jConfiguration; - result.loggerLevel = shellContext.loggerLevel; - result.coloredOutput = shellContext.coloredOutput; - result.terminal = shellContext.terminal; - result.writer = shellContext.writer; - - result.installationSettingsPath = shellContext.installationSettingsPath; - result.projectSettingsPath = shellContext.projectSettingsPath; - result.userSettingsPath = shellContext.userSettingsPath; - result.interactive = shellContext.interactive; - result.localRepositoryPath = shellContext.localRepositoryPath; - result.effectiveSettings = shellContext.effectiveSettings; - - result.containerCapsule = shellContext.containerCapsule; - result.lookup = shellContext.lookup; - result.eventSpyDispatcher = shellContext.eventSpyDispatcher; - return result; - } -}