|
| 1 | +package com.devonfw.tools.ide.commandlet; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.Iterator; |
| 5 | + |
| 6 | +import org.fusesource.jansi.AnsiConsole; |
| 7 | +import org.jline.reader.EndOfFileException; |
| 8 | +import org.jline.reader.LineReader; |
| 9 | +import org.jline.reader.LineReaderBuilder; |
| 10 | +import org.jline.reader.MaskingCallback; |
| 11 | +import org.jline.reader.Parser; |
| 12 | +import org.jline.reader.UserInterruptException; |
| 13 | +import org.jline.reader.impl.DefaultParser; |
| 14 | +import org.jline.terminal.Terminal; |
| 15 | +import org.jline.terminal.TerminalBuilder; |
| 16 | +import org.jline.widget.AutosuggestionWidgets; |
| 17 | + |
| 18 | +import com.devonfw.tools.ide.cli.CliArgument; |
| 19 | +import com.devonfw.tools.ide.cli.CliArguments; |
| 20 | +import com.devonfw.tools.ide.cli.CliException; |
| 21 | +import com.devonfw.tools.ide.completion.IdeCompleter; |
| 22 | +import com.devonfw.tools.ide.context.AbstractIdeContext; |
| 23 | +import com.devonfw.tools.ide.context.IdeContext; |
| 24 | +import com.devonfw.tools.ide.property.BooleanProperty; |
| 25 | +import com.devonfw.tools.ide.property.FlagProperty; |
| 26 | +import com.devonfw.tools.ide.property.KeywordProperty; |
| 27 | +import com.devonfw.tools.ide.property.Property; |
| 28 | + |
| 29 | +/** |
| 30 | + * {@link Commandlet} for internal interactive shell with build-in auto-completion and help. |
| 31 | + */ |
| 32 | +public final class ShellCommandlet extends Commandlet { |
| 33 | + |
| 34 | + private static final int AUTOCOMPLETER_MAX_RESULTS = 50; |
| 35 | + |
| 36 | + private static final int RC_EXIT = 987654321; |
| 37 | + |
| 38 | + /** |
| 39 | + * The constructor. |
| 40 | + * |
| 41 | + * @param context the {@link IdeContext}. |
| 42 | + */ |
| 43 | + public ShellCommandlet(IdeContext context) { |
| 44 | + |
| 45 | + super(context); |
| 46 | + addKeyword(getName()); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public String getName() { |
| 51 | + |
| 52 | + return "shell"; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public boolean isIdeHomeRequired() { |
| 57 | + |
| 58 | + return false; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public void run() { |
| 63 | + |
| 64 | + try { |
| 65 | + // TODO: add BuiltIns here, see: https://github.com/devonfw/IDEasy/issues/168 |
| 66 | + |
| 67 | + Parser parser = new DefaultParser(); |
| 68 | + try (Terminal terminal = TerminalBuilder.builder().build()) { |
| 69 | + |
| 70 | + // initialize our own completer here |
| 71 | + IdeCompleter completer = new IdeCompleter((AbstractIdeContext) this.context); |
| 72 | + |
| 73 | + LineReader reader = LineReaderBuilder.builder().terminal(terminal).completer(completer).parser(parser) |
| 74 | + .variable(LineReader.LIST_MAX, AUTOCOMPLETER_MAX_RESULTS).build(); |
| 75 | + |
| 76 | + // Create autosuggestion widgets |
| 77 | + AutosuggestionWidgets autosuggestionWidgets = new AutosuggestionWidgets(reader); |
| 78 | + // Enable autosuggestions |
| 79 | + autosuggestionWidgets.enable(); |
| 80 | + |
| 81 | + // TODO: implement TailTipWidgets, see: https://github.com/devonfw/IDEasy/issues/169 |
| 82 | + |
| 83 | + String prompt = "ide> "; |
| 84 | + String rightPrompt = null; |
| 85 | + String line; |
| 86 | + |
| 87 | + AnsiConsole.systemInstall(); |
| 88 | + while (true) { |
| 89 | + try { |
| 90 | + line = reader.readLine(prompt, rightPrompt, (MaskingCallback) null, null); |
| 91 | + reader.getHistory().add(line); |
| 92 | + int rc = runCommand(line); |
| 93 | + if (rc == RC_EXIT) { |
| 94 | + return; |
| 95 | + } |
| 96 | + } catch (UserInterruptException e) { |
| 97 | + // Ignore CTRL+C |
| 98 | + return; |
| 99 | + } catch (EndOfFileException e) { |
| 100 | + // CTRL+D |
| 101 | + return; |
| 102 | + } finally { |
| 103 | + AnsiConsole.systemUninstall(); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + } catch (IOException e) { |
| 108 | + throw new RuntimeException(e); |
| 109 | + } |
| 110 | + } catch (Exception e) { |
| 111 | + throw new RuntimeException("Unexpected error during interactive auto-completion", e); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Converts String of arguments to array and runs the command |
| 117 | + * |
| 118 | + * @param args String of arguments |
| 119 | + * @return status code |
| 120 | + */ |
| 121 | + private int runCommand(String args) { |
| 122 | + |
| 123 | + if ("exit".equals(args) || "quit".equals(args)) { |
| 124 | + return RC_EXIT; |
| 125 | + } |
| 126 | + String[] arguments = args.split(" ", 0); |
| 127 | + CliArguments cliArgs = new CliArguments(arguments); |
| 128 | + cliArgs.next(); |
| 129 | + return ((AbstractIdeContext) this.context).run(cliArgs); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * @param argument the current {@link CliArgument} (position) to match. |
| 134 | + * @param commandlet the potential {@link Commandlet} to match. |
| 135 | + * @return {@code true} if the given {@link Commandlet} matches to the given {@link CliArgument}(s) and those have |
| 136 | + * been applied (set in the {@link Commandlet} and {@link Commandlet#validate() validated}), {@code false} |
| 137 | + * otherwise (the {@link Commandlet} did not match and we have to try a different candidate). |
| 138 | + */ |
| 139 | + private boolean apply(CliArgument argument, Commandlet commandlet) { |
| 140 | + |
| 141 | + this.context.trace("Trying to match arguments to commandlet {}", commandlet.getName()); |
| 142 | + CliArgument currentArgument = argument; |
| 143 | + Iterator<Property<?>> valueIterator = commandlet.getValues().iterator(); |
| 144 | + Property<?> currentProperty = null; |
| 145 | + boolean endOpts = false; |
| 146 | + while (!currentArgument.isEnd()) { |
| 147 | + if (currentArgument.isEndOptions()) { |
| 148 | + endOpts = true; |
| 149 | + } else { |
| 150 | + String arg = currentArgument.get(); |
| 151 | + this.context.trace("Trying to match argument '{}'", currentArgument); |
| 152 | + if ((currentProperty != null) && (currentProperty.isExpectValue())) { |
| 153 | + currentProperty.setValueAsString(arg, this.context); |
| 154 | + if (!currentProperty.isMultiValued()) { |
| 155 | + currentProperty = null; |
| 156 | + } |
| 157 | + } else { |
| 158 | + Property<?> property = null; |
| 159 | + if (!endOpts) { |
| 160 | + property = commandlet.getOption(currentArgument.getKey()); |
| 161 | + } |
| 162 | + if (property == null) { |
| 163 | + if (!valueIterator.hasNext()) { |
| 164 | + this.context.trace("No option or next value found"); |
| 165 | + return false; |
| 166 | + } |
| 167 | + currentProperty = valueIterator.next(); |
| 168 | + this.context.trace("Next value candidate is {}", currentProperty); |
| 169 | + if (currentProperty instanceof KeywordProperty) { |
| 170 | + KeywordProperty keyword = (KeywordProperty) currentProperty; |
| 171 | + if (keyword.matches(arg)) { |
| 172 | + keyword.setValue(Boolean.TRUE); |
| 173 | + this.context.trace("Keyword matched"); |
| 174 | + } else { |
| 175 | + this.context.trace("Missing keyword"); |
| 176 | + return false; |
| 177 | + } |
| 178 | + } else { |
| 179 | + boolean success = currentProperty.assignValueAsString(arg, this.context, commandlet); |
| 180 | + if (!success && currentProperty.isRequired()) { |
| 181 | + return false; |
| 182 | + } |
| 183 | + } |
| 184 | + if ((currentProperty != null) && !currentProperty.isMultiValued()) { |
| 185 | + currentProperty = null; |
| 186 | + } |
| 187 | + } else { |
| 188 | + this.context.trace("Found option by name"); |
| 189 | + String value = currentArgument.getValue(); |
| 190 | + if (value != null) { |
| 191 | + property.setValueAsString(value, this.context); |
| 192 | + } else if (property instanceof BooleanProperty) { |
| 193 | + ((BooleanProperty) property).setValue(Boolean.TRUE); |
| 194 | + } else { |
| 195 | + currentProperty = property; |
| 196 | + if (property.isEndOptions()) { |
| 197 | + endOpts = true; |
| 198 | + } |
| 199 | + throw new UnsupportedOperationException("not implemented"); |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | + currentArgument = currentArgument.getNext(!endOpts); |
| 205 | + } |
| 206 | + return commandlet.validate(); |
| 207 | + } |
| 208 | +} |
0 commit comments