• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

devonfw / IDEasy / 12086012705

29 Nov 2024 01:57PM UTC coverage: 66.998% (-0.1%) from 67.107%
12086012705

push

github

web-flow
#737: Added cd command to shell commandlet (#748)

2505 of 4088 branches covered (61.28%)

Branch coverage included in aggregate %.

6533 of 9402 relevant lines covered (69.49%)

3.06 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

3.18
cli/src/main/java/com/devonfw/tools/ide/commandlet/ShellCommandlet.java
1
package com.devonfw.tools.ide.commandlet;
2

3
import java.io.IOException;
4
import java.nio.file.Path;
5
import java.nio.file.Paths;
6
import java.util.Iterator;
7

8
import org.fusesource.jansi.AnsiConsole;
9
import org.jline.reader.Completer;
10
import org.jline.reader.EndOfFileException;
11
import org.jline.reader.LineReader;
12
import org.jline.reader.LineReaderBuilder;
13
import org.jline.reader.MaskingCallback;
14
import org.jline.reader.Parser;
15
import org.jline.reader.UserInterruptException;
16
import org.jline.reader.impl.DefaultParser;
17
import org.jline.reader.impl.completer.AggregateCompleter;
18
import org.jline.reader.impl.completer.StringsCompleter;
19
import org.jline.terminal.Terminal;
20
import org.jline.terminal.TerminalBuilder;
21
import org.jline.widget.AutosuggestionWidgets;
22

23
import com.devonfw.tools.ide.cli.CliArgument;
24
import com.devonfw.tools.ide.cli.CliArguments;
25
import com.devonfw.tools.ide.completion.IdeCompleter;
26
import com.devonfw.tools.ide.context.AbstractIdeContext;
27
import com.devonfw.tools.ide.context.IdeContext;
28
import com.devonfw.tools.ide.property.BooleanProperty;
29
import com.devonfw.tools.ide.property.KeywordProperty;
30
import com.devonfw.tools.ide.property.Property;
31

32
/**
33
 * {@link Commandlet} for internal interactive shell with build-in auto-completion and help.
34
 */
35
public final class ShellCommandlet extends Commandlet {
36

37
  private static final int AUTOCOMPLETER_MAX_RESULTS = 50;
38

39
  private static final int RC_EXIT = 987654321;
40

41
  /**
42
   * The constructor.
43
   *
44
   * @param context the {@link IdeContext}.
45
   */
46
  public ShellCommandlet(IdeContext context) {
47

48
    super(context);
3✔
49
    addKeyword(getName());
4✔
50
  }
1✔
51

52
  @Override
53
  public String getName() {
54

55
    return "shell";
2✔
56
  }
57

58
  @Override
59
  public boolean isIdeHomeRequired() {
60

61
    return false;
2✔
62
  }
63

64
  @Override
65
  public void run() {
66

67
    try {
68
      Parser parser = new DefaultParser();
×
69
      try (Terminal terminal = TerminalBuilder.builder().build()) {
×
70
        // initialize our own completer here and add exit as an autocompletion option
71
        Completer completer = new AggregateCompleter(
×
72
            new StringsCompleter("exit"), new IdeCompleter((AbstractIdeContext) this.context));
73

74
        LineReader reader = LineReaderBuilder.builder().terminal(terminal).completer(completer).parser(parser)
×
75
            .variable(LineReader.LIST_MAX, AUTOCOMPLETER_MAX_RESULTS).build();
×
76

77
        // Create autosuggestion widgets
78
        AutosuggestionWidgets autosuggestionWidgets = new AutosuggestionWidgets(reader);
×
79
        // Enable autosuggestions
80
        autosuggestionWidgets.enable();
×
81

82
        // TODO: implement TailTipWidgets, see: https://github.com/devonfw/IDEasy/issues/169
83

84
        String rightPrompt = null;
×
85
        String line;
86

87
        AnsiConsole.systemInstall();
×
88
        while (true) {
89
          try {
90
            String prompt = context.getCwd() + "$ ide ";
×
91
            line = reader.readLine(prompt, rightPrompt, (MaskingCallback) null, null);
×
92
            line = line.trim();
×
93
            if (line.equals("exit")) {
×
94
              return;
×
95
            }
96
            reader.getHistory().add(line);
×
97
            int rc = runCommand(line);
×
98
            if (rc == RC_EXIT) {
×
99
              return;
×
100
            }
101
          } catch (UserInterruptException e) {
×
102
            // Ignore CTRL+C
103
            return;
×
104
          } catch (EndOfFileException e) {
×
105
            // CTRL+D
106
            return;
×
107
          } finally {
108
            AnsiConsole.systemUninstall();
×
109
          }
×
110
        }
111

112
      } catch (IOException e) {
×
113
        throw new RuntimeException(e);
×
114
      }
115
    } catch (Exception e) {
×
116
      throw new RuntimeException("Unexpected error during interactive auto-completion", e);
×
117
    }
118
  }
119

120
  /**
121
   * Converts String of arguments to array and runs the command
122
   *
123
   * @param args String of arguments
124
   * @return status code
125
   */
126
  private int runCommand(String args) {
127

128
    if ("exit".equals(args) || "quit".equals(args)) {
×
129
      return RC_EXIT;
×
130
    }
131
    String[] arguments = args.split(" ", 0);
×
132
    CliArguments cliArgs = new CliArguments(arguments);
×
133
    cliArgs.next();
×
134

135
    if ("cd".equals(arguments[0])) {
×
136
      return changeDirectory(cliArgs);
×
137
    }
138

139
    return ((AbstractIdeContext) this.context).run(cliArgs);
×
140
  }
141

142
  private int changeDirectory(CliArguments cliArgs) {
143
    if (!cliArgs.hasNext()) {
×
144
      Path homeDir = this.context.getUserHome();
×
145
      context.setCwd(homeDir, context.getWorkspaceName(), context.getIdeHome());
×
146
      return 0;
×
147
    }
148

149
    String targetDir = String.valueOf(cliArgs.next());
×
150
    Path path = Paths.get(targetDir);
×
151

152
    // If the given path is relative, resolve it relative to the current directory
153
    if (!path.isAbsolute()) {
×
154
      path = context.getCwd().resolve(targetDir).normalize();
×
155
    }
156

157
    if (context.getFileAccess().isExpectedFolder(path)) {
×
158
      context.setCwd(path, context.getWorkspaceName(), context.getIdeHome());
×
159
      return 0;
×
160
    } else {
161
      return 1;
×
162
    }
163
  }
164

165
  /**
166
   * @param argument the current {@link CliArgument} (position) to match.
167
   * @param commandlet the potential {@link Commandlet} to match.
168
   * @return {@code true} if the given {@link Commandlet} matches to the given {@link CliArgument}(s) and those have been applied (set in the {@link Commandlet}
169
   *     and {@link Commandlet#validate() validated}), {@code false} otherwise (the {@link Commandlet} did not match and we have to try a different candidate).
170
   */
171
  private boolean apply(CliArgument argument, Commandlet commandlet) {
172

173
    this.context.trace("Trying to match arguments to commandlet {}", commandlet.getName());
×
174
    CliArgument currentArgument = argument;
×
175
    Iterator<Property<?>> valueIterator = commandlet.getValues().iterator();
×
176
    Property<?> currentProperty = null;
×
177
    boolean endOpts = false;
×
178
    while (!currentArgument.isEnd()) {
×
179
      if (currentArgument.isEndOptions()) {
×
180
        endOpts = true;
×
181
      } else {
182
        String arg = currentArgument.get();
×
183
        this.context.trace("Trying to match argument '{}'", currentArgument);
×
184
        if ((currentProperty != null) && (currentProperty.isExpectValue())) {
×
185
          currentProperty.setValueAsString(arg, this.context);
×
186
          if (!currentProperty.isMultiValued()) {
×
187
            currentProperty = null;
×
188
          }
189
        } else {
190
          Property<?> property = null;
×
191
          if (!endOpts) {
×
192
            property = commandlet.getOption(currentArgument.getKey());
×
193
          }
194
          if (property == null) {
×
195
            if (!valueIterator.hasNext()) {
×
196
              this.context.trace("No option or next value found");
×
197
              return false;
×
198
            }
199
            currentProperty = valueIterator.next();
×
200
            this.context.trace("Next value candidate is {}", currentProperty);
×
201
            if (currentProperty instanceof KeywordProperty keyword) {
×
202
              if (keyword.matches(arg)) {
×
203
                keyword.setValue(Boolean.TRUE);
×
204
                this.context.trace("Keyword matched");
×
205
              } else {
206
                this.context.trace("Missing keyword");
×
207
                return false;
×
208
              }
209
            } else {
210
              boolean success = currentProperty.assignValueAsString(arg, this.context, commandlet);
×
211
              if (!success && currentProperty.isRequired()) {
×
212
                return false;
×
213
              }
214
            }
215
            if ((currentProperty != null) && !currentProperty.isMultiValued()) {
×
216
              currentProperty = null;
×
217
            }
218
          } else {
219
            this.context.trace("Found option by name");
×
220
            String value = currentArgument.getValue();
×
221
            if (value != null) {
×
222
              property.setValueAsString(value, this.context);
×
223
            } else if (property instanceof BooleanProperty) {
×
224
              ((BooleanProperty) property).setValue(Boolean.TRUE);
×
225
            } else {
226
              currentProperty = property;
×
227
              if (property.isEndOptions()) {
×
228
                endOpts = true;
×
229
              }
230
              throw new UnsupportedOperationException("not implemented");
×
231
            }
232
          }
233
        }
234
      }
235
      currentArgument = currentArgument.getNext(!endOpts);
×
236
    }
237
    return commandlet.validate().isValid();
×
238
  }
239
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc