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

devonfw / IDEasy / 24069879495

07 Apr 2026 07:32AM UTC coverage: 70.609% (-0.03%) from 70.643%
24069879495

Pull #1782

github

web-flow
Merge 2040cb5c4 into 420453a8c
Pull Request #1782: #1724: Create GUI commandlet

4196 of 6560 branches covered (63.96%)

Branch coverage included in aggregate %.

10889 of 14804 relevant lines covered (73.55%)

3.1 hits per line

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

90.16
cli/src/main/java/com/devonfw/tools/ide/commandlet/CommandletManagerImpl.java
1
package com.devonfw.tools.ide.commandlet;
2

3
import java.util.Collection;
4
import java.util.Collections;
5
import java.util.HashMap;
6
import java.util.Iterator;
7
import java.util.List;
8
import java.util.Map;
9
import java.util.NoSuchElementException;
10

11
import org.slf4j.Logger;
12
import org.slf4j.LoggerFactory;
13

14
import com.devonfw.tools.ide.cli.CliArgument;
15
import com.devonfw.tools.ide.cli.CliArguments;
16
import com.devonfw.tools.ide.completion.CompletionCandidateCollector;
17
import com.devonfw.tools.ide.context.IdeContext;
18
import com.devonfw.tools.ide.git.repository.RepositoryCommandlet;
19
import com.devonfw.tools.ide.property.KeywordProperty;
20
import com.devonfw.tools.ide.property.Property;
21
import com.devonfw.tools.ide.tool.androidstudio.AndroidStudio;
22
import com.devonfw.tools.ide.tool.aws.Aws;
23
import com.devonfw.tools.ide.tool.az.Azure;
24
import com.devonfw.tools.ide.tool.corepack.Corepack;
25
import com.devonfw.tools.ide.tool.docker.Docker;
26
import com.devonfw.tools.ide.tool.dotnet.DotNet;
27
import com.devonfw.tools.ide.tool.eclipse.Eclipse;
28
import com.devonfw.tools.ide.tool.gcviewer.GcViewer;
29
import com.devonfw.tools.ide.tool.gh.Gh;
30
import com.devonfw.tools.ide.tool.go.Go;
31
import com.devonfw.tools.ide.tool.graalvm.GraalVm;
32
import com.devonfw.tools.ide.tool.gradle.Gradle;
33
import com.devonfw.tools.ide.tool.gui.Gui;
34
import com.devonfw.tools.ide.tool.helm.Helm;
35
import com.devonfw.tools.ide.tool.intellij.Intellij;
36
import com.devonfw.tools.ide.tool.jasypt.Jasypt;
37
import com.devonfw.tools.ide.tool.java.Java;
38
import com.devonfw.tools.ide.tool.jmc.Jmc;
39
import com.devonfw.tools.ide.tool.kotlinc.Kotlinc;
40
import com.devonfw.tools.ide.tool.kotlinc.KotlincNative;
41
import com.devonfw.tools.ide.tool.kubectl.KubeCtl;
42
import com.devonfw.tools.ide.tool.lazydocker.LazyDocker;
43
import com.devonfw.tools.ide.tool.mvn.Mvn;
44
import com.devonfw.tools.ide.tool.ng.Ng;
45
import com.devonfw.tools.ide.tool.node.Node;
46
import com.devonfw.tools.ide.tool.npm.Npm;
47
import com.devonfw.tools.ide.tool.oc.Oc;
48
import com.devonfw.tools.ide.tool.pgadmin.PgAdmin;
49
import com.devonfw.tools.ide.tool.pip.Pip;
50
import com.devonfw.tools.ide.tool.pycharm.Pycharm;
51
import com.devonfw.tools.ide.tool.python.Python;
52
import com.devonfw.tools.ide.tool.quarkus.Quarkus;
53
import com.devonfw.tools.ide.tool.sonar.Sonar;
54
import com.devonfw.tools.ide.tool.spring.Spring;
55
import com.devonfw.tools.ide.tool.terraform.Terraform;
56
import com.devonfw.tools.ide.tool.tomcat.Tomcat;
57
import com.devonfw.tools.ide.tool.uv.Uv;
58
import com.devonfw.tools.ide.tool.vscode.Vscode;
59
import com.devonfw.tools.ide.tool.yarn.Yarn;
60

61
/**
62
 * Implementation of {@link CommandletManager}.
63
 */
64
public class CommandletManagerImpl implements CommandletManager {
65

66
  private static final Logger LOG = LoggerFactory.getLogger(CommandletManagerImpl.class);
4✔
67

68
  private final IdeContext context;
69

70
  private final Map<Class<? extends Commandlet>, Commandlet> commandletTypeMap;
71

72
  private final Map<String, Commandlet> commandletNameMap;
73

74
  private final Map<String, Commandlet> firstKeywordMap;
75

76
  private final Collection<Commandlet> commandlets;
77

78
  /**
79
   * The constructor.
80
   *
81
   * @param context the {@link IdeContext}.
82
   */
83
  public CommandletManagerImpl(IdeContext context) {
84

85
    super();
2✔
86
    this.context = context;
3✔
87
    this.commandletTypeMap = new HashMap<>();
5✔
88
    this.commandletNameMap = new HashMap<>();
5✔
89
    this.firstKeywordMap = new HashMap<>();
5✔
90
    this.commandlets = Collections.unmodifiableCollection(this.commandletTypeMap.values());
6✔
91
    add(new HelpCommandlet(context));
6✔
92
    add(new EnvironmentCommandlet(context));
6✔
93
    add(new CompleteCommandlet(context));
6✔
94
    add(new ShellCommandlet(context));
6✔
95
    add(new InstallCommandlet(context));
6✔
96
    add(new VersionSetCommandlet(context));
6✔
97
    add(new VersionGetCommandlet(context));
6✔
98
    add(new VersionListCommandlet(context));
6✔
99
    add(new EditionGetCommandlet(context));
6✔
100
    add(new EditionSetCommandlet(context));
6✔
101
    add(new EditionListCommandlet(context));
6✔
102
    add(new VersionCommandlet(context));
6✔
103
    add(new StatusCommandlet(context));
6✔
104
    add(new RepositoryCommandlet(context));
6✔
105
    add(new UninstallCommandlet(context));
6✔
106
    add(new UpdateCommandlet(context));
6✔
107
    add(new UpgradeSettingsCommandlet(context));
6✔
108
    add(new CreateCommandlet(context));
6✔
109
    add(new BuildCommandlet(context));
6✔
110
    add(new InstallPluginCommandlet(context));
6✔
111
    add(new UninstallPluginCommandlet(context));
6✔
112
    add(new UpgradeCommandlet(context));
6✔
113
    add(new Gh(context));
6✔
114
    add(new Helm(context));
6✔
115
    add(new Java(context));
6✔
116
    add(new Ng(context));
6✔
117
    add(new Node(context));
6✔
118
    add(new Npm(context));
6✔
119
    add(new Mvn(context));
6✔
120
    add(new GcViewer(context));
6✔
121
    add(new Gradle(context));
6✔
122
    add(new Eclipse(context));
6✔
123
    add(new Terraform(context));
6✔
124
    add(new Oc(context));
6✔
125
    add(new Quarkus(context));
6✔
126
    add(new Kotlinc(context));
6✔
127
    add(new KotlincNative(context));
6✔
128
    add(new KubeCtl(context));
6✔
129
    add(new Tomcat(context));
6✔
130
    add(new Vscode(context));
6✔
131
    add(new Azure(context));
6✔
132
    add(new Aws(context));
6✔
133
    add(new Jmc(context));
6✔
134
    add(new DotNet(context));
6✔
135
    add(new Intellij(context));
6✔
136
    add(new Jasypt(context));
6✔
137
    add(new Docker(context));
6✔
138
    add(new Sonar(context));
6✔
139
    add(new AndroidStudio(context));
6✔
140
    add(new GraalVm(context));
6✔
141
    add(new PgAdmin(context));
6✔
142
    add(new LazyDocker(context));
6✔
143
    add(new Python(context));
6✔
144
    add(new Pycharm(context));
6✔
145
    add(new Spring(context));
6✔
146
    add(new Uv(context));
6✔
147
    add(new Yarn(context));
6✔
148
    add(new Corepack(context));
6✔
149
    add(new Pip(context));
6✔
150
    add(new Go(context));
6✔
151
    add(new Gui(context));
6✔
152
  }
1✔
153

154
  /**
155
   * @param commandlet the {@link Commandlet} to add.
156
   */
157
  protected void add(Commandlet commandlet) {
158

159
    boolean hasRequiredProperty = false;
2✔
160
    List<Property<?>> properties = commandlet.getProperties();
3✔
161
    int propertyCount = properties.size();
3✔
162
    KeywordProperty keyword = commandlet.getFirstKeyword();
3✔
163
    if (keyword != null) {
2!
164
      String name = keyword.getName();
3✔
165
      registerKeyword(name, commandlet);
4✔
166
      String optionName = keyword.getOptionName();
3✔
167
      if (!optionName.equals(name)) {
4✔
168
        registerKeyword(optionName, commandlet);
4✔
169
      }
170
      String alias = keyword.getAlias();
3✔
171
      if (alias != null) {
2✔
172
        registerKeyword(alias, commandlet);
4✔
173
      }
174
    }
175
    for (int i = 0; i < propertyCount; i++) {
5!
176
      Property<?> property = properties.get(i);
5✔
177
      if (property.isRequired()) {
3!
178
        hasRequiredProperty = true;
2✔
179
        break;
1✔
180
      }
181
    }
182
    if (!hasRequiredProperty) {
2!
183
      throw new IllegalStateException("Commandlet " + commandlet + " must have at least one mandatory property!");
×
184
    }
185
    this.commandletTypeMap.put(commandlet.getClass(), commandlet);
7✔
186
    Commandlet duplicate = this.commandletNameMap.put(commandlet.getName(), commandlet);
8✔
187
    if (duplicate != null) {
2!
188
      throw new IllegalStateException("Commandlet " + commandlet + " has the same name as " + duplicate);
×
189
    }
190
  }
1✔
191

192
  private void registerKeyword(String keyword, Commandlet commandlet) {
193

194
    Commandlet duplicate = this.firstKeywordMap.putIfAbsent(keyword, commandlet);
7✔
195
    if (duplicate != null) {
2!
196
      LOG.debug("Duplicate keyword {} already used by {} so it cannot be associated also with {}", keyword, duplicate, commandlet);
×
197
    }
198
  }
1✔
199

200
  @Override
201
  public Collection<Commandlet> getCommandlets() {
202

203
    return this.commandlets;
3✔
204
  }
205

206
  @Override
207
  public <C extends Commandlet> C getCommandlet(Class<C> commandletType) {
208

209
    Commandlet commandlet = this.commandletTypeMap.get(commandletType);
6✔
210
    if (commandlet == null) {
2!
211
      throw new IllegalStateException("Commandlet for type " + commandletType + " is not registered!");
×
212
    }
213
    return commandletType.cast(commandlet);
5✔
214
  }
215

216
  @Override
217
  public Commandlet getCommandlet(String name) {
218

219
    return this.commandletNameMap.get(name);
6✔
220
  }
221

222
  @Override
223
  public Commandlet getCommandletByFirstKeyword(String keyword) {
224

225
    return this.firstKeywordMap.get(keyword);
6✔
226
  }
227

228
  @Override
229
  public Iterator<Commandlet> findCommandlet(CliArguments arguments, CompletionCandidateCollector collector) {
230

231
    CliArgument current = arguments.current();
3✔
232
    if (current.isEnd()) {
3!
233
      return Collections.emptyIterator();
×
234
    }
235
    String keyword = current.get();
3✔
236
    Commandlet commandlet = getCommandletByFirstKeyword(keyword);
4✔
237
    if ((commandlet == null) && (collector == null)) {
4✔
238
      return Collections.emptyIterator();
2✔
239
    }
240
    return new CommandletFinder(commandlet, arguments.copy(), collector);
9✔
241
  }
242

243
  private final class CommandletFinder implements Iterator<Commandlet> {
1✔
244

245
    private final Commandlet firstCandidate;
246

247
    private final Iterator<Commandlet> commandletIterator;
248

249
    private final CliArguments arguments;
250

251
    private final CompletionCandidateCollector collector;
252

253
    private Commandlet next;
254

255
    private CommandletFinder(Commandlet firstCandidate, CliArguments arguments, CompletionCandidateCollector collector) {
8✔
256

257
      this.firstCandidate = firstCandidate;
3✔
258
      this.commandletIterator = getCommandlets().iterator();
5✔
259
      this.arguments = arguments;
3✔
260
      this.collector = collector;
3✔
261
      if (isSuitable(firstCandidate)) {
4✔
262
        this.next = firstCandidate;
4✔
263
      } else {
264
        this.next = findNext();
4✔
265
      }
266
    }
1✔
267

268
    @Override
269
    public boolean hasNext() {
270

271
      return this.next != null;
7✔
272
    }
273

274
    @Override
275
    public Commandlet next() {
276

277
      if (this.next == null) {
3!
278
        throw new NoSuchElementException();
×
279
      }
280
      Commandlet result = this.next;
3✔
281
      this.next = findNext();
4✔
282
      return result;
2✔
283
    }
284

285
    private boolean isSuitable(Commandlet commandlet) {
286

287
      return (commandlet != null) && (!commandlet.isIdeHomeRequired() || (context.getIdeHome() != null));
14✔
288
    }
289

290
    private Commandlet findNext() {
291

292
      while (this.commandletIterator.hasNext()) {
4✔
293
        Commandlet cmd = this.commandletIterator.next();
5✔
294
        if ((cmd != this.firstCandidate) && isSuitable(cmd)) {
8✔
295
          List<Property<?>> properties = cmd.getProperties();
3✔
296
          // validation should already be done in add method and could be removed here...
297
          if (properties.isEmpty()) {
3!
298
            assert false : cmd.getClass().getSimpleName() + " has no properties!";
×
299
          } else {
300
            Property<?> property = properties.getFirst();
4✔
301
            if (property instanceof KeywordProperty) {
3!
302
              boolean matches = property.apply(arguments.copy(), context, cmd, this.collector);
12✔
303
              if (matches) {
2✔
304
                return cmd;
2✔
305
              }
306
            } else {
1✔
307
              assert false : cmd.getClass().getSimpleName() + " is invalid as first property must be keyword property but is " + property;
×
308
            }
309
          }
310
        }
311
      }
1✔
312
      return null;
2✔
313
    }
314
  }
315
}
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

© 2026 Coveralls, Inc