From c2d4f5b17e4012d08c641d03a5028d5cb18590de Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Tue, 9 Jan 2024 15:03:37 +0100 Subject: [PATCH] #56: implemented requested change removed CommandletRegistry --- .../tools/ide/cli/CommandletRegistry.java | 178 ------------------ 1 file changed, 178 deletions(-) delete mode 100644 cli/src/main/java/com/devonfw/tools/ide/cli/CommandletRegistry.java diff --git a/cli/src/main/java/com/devonfw/tools/ide/cli/CommandletRegistry.java b/cli/src/main/java/com/devonfw/tools/ide/cli/CommandletRegistry.java deleted file mode 100644 index 63b796288..000000000 --- a/cli/src/main/java/com/devonfw/tools/ide/cli/CommandletRegistry.java +++ /dev/null @@ -1,178 +0,0 @@ -package com.devonfw.tools.ide.cli; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.stream.Collectors; - -import org.jline.console.ArgDesc; -import org.jline.console.CmdDesc; -import org.jline.console.CommandRegistry; -import org.jline.reader.impl.completer.SystemCompleter; -import org.jline.utils.AttributedString; - -import com.devonfw.tools.ide.commandlet.Commandlet; -import com.devonfw.tools.ide.commandlet.ContextCommandlet; -import com.devonfw.tools.ide.commandlet.HelpCommandlet; -import com.devonfw.tools.ide.context.IdeContext; - -/** - * Implements the {@link CommandRegistry} for jline3. Inspired by: picocli - */ -public class CommandletRegistry implements CommandRegistry { - - private final ContextCommandlet cmd; - - private final IdeContext context; - - private final Ide ide; - - private final IdeCompleter ideCompleter; - - private final Set commandlets; - - private final Map aliasCommandlets; - - public CommandletRegistry(ContextCommandlet cmd, Ide ide, IdeContext context) { - - this.ideCompleter = new IdeCompleter(cmd, context); - this.cmd = cmd; - this.context = context; - this.ide = ide; - - Set commandlets = new HashSet<>(); - Collection commandletCollection = context.getCommandletManager().getCommandlets(); - - for (Commandlet commandlet : commandletCollection) { - commandlets.add(commandlet.getName()); - } - - // TODO: get correct aliases, uses command name keywords instead now - Map aliasCommandlets = new HashMap<>(); - Collection aliasCommandletCollection = context.getCommandletManager().getCommandlets(); - - for (Commandlet commandlet : aliasCommandletCollection) { - aliasCommandlets.put(commandlet.getName(), commandlet.getKeyword()); - } - - this.commandlets = commandlets; - this.aliasCommandlets = aliasCommandlets; - } - - @Override - public Set commandNames() { - - return commandlets; - } - - @Override - public Map commandAliases() { - - return aliasCommandlets; - } - - @Override - public List commandInfo(String command) { - - // TODO: disable this functionality? - List out = new ArrayList<>(); - // TODO: take command and get help from Ide - HelpCommandlet helpCommandlet = context.getCommandletManager().getCommandlet(HelpCommandlet.class); - helpCommandlet.commandlet.setValueAsString(command); - helpCommandlet.run(); - // TODO: add our own description for each commandlet here - String description = "placeholder description"; - out.addAll(Arrays.asList(description.split("\\r?\\n"))); - return out; - } - - @Override - public boolean hasCommand(String command) { - - return commandlets.contains(command) || aliasCommandlets.containsKey(command); - } - - @Override - public SystemCompleter compileCompleters() { - - SystemCompleter out = new SystemCompleter(); - List all = new ArrayList<>(); - all.addAll(commandlets); - all.addAll(aliasCommandlets.keySet()); - out.add(all, new IdeCompleter(cmd, context)); - return out; - } - - // For JLine >= 3.16.0 - @Override - public Object invoke(CommandRegistry.CommandSession session, String command, Object[] args) throws Exception { - - List arguments = new ArrayList<>(); - arguments.add(command); - arguments.addAll(Arrays.stream(args).map(Object::toString).toList()); - // TODO: run our commandlet here - runCommand(command, arguments); - return null; - } - - private void runCommand(String command, List arguments) { - - String[] convertedArgs = arguments.toArray(new String[0]); - CliArgument first = CliArgument.of(convertedArgs); - Commandlet firstCandidate = this.context.getCommandletManager().getCommandletByFirstKeyword(command); - // TODO: find better way to run the command - // ide.applyAndRun(first, firstCandidate); - } - - // @Override This method was removed in JLine 3.16.0; keep it in case this component is used with an older version of - // JLine - public Object execute(CommandRegistry.CommandSession session, String command, String[] args) throws Exception { - - List arguments = new ArrayList<>(); - arguments.add(command); - arguments.addAll(Arrays.asList(args)); - // TODO: run our commandlet here - runCommand(command, arguments); - return null; - } - - // @Override This method was removed in JLine 3.16.0; keep it in case this component is used with an older version of - // JLine - public CmdDesc commandDescription(String command) { - - return null; - } - - @Override - public CmdDesc commandDescription(List list) { - - List main = new ArrayList<>(); - Map> options = new HashMap<>(); - // String synopsis = - // AttributedString.stripAnsi(spec.usageMessage().sectionMap().get("synopsis").render(cmdhelp).toString()); - // main.add(Options.HelpException.highlightSyntax(synopsis.trim(), Options.HelpException.defaultStyle())); - - AttributedString attributedString = new AttributedString("PlaceHolderCommandletName"); - main.add(attributedString); - options.put("PlaceHolderCommandletName", main); - // for (OptionSpec o : spec.options()) { - // String key = Arrays.stream(o.names()).collect(Collectors.joining(" ")); - // List val = new ArrayList<>(); - // for (String d: o.description()) { - // val.add(new AttributedString(d)); - // } - // if (o.arity().max() > 0) { - // key += "=" + o.paramLabel(); - // } - // options.put(key, val); - // } - // return new CmdDesc(main, ArgDesc.doArgNames(Arrays.asList("")), options); - // TODO: implement this - return new CmdDesc(main, ArgDesc.doArgNames(Arrays.asList("PlaceHolderCommandletDescription")), options); - } -}