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

devonfw / IDEasy / 12185831515

05 Dec 2024 06:28PM UTC coverage: 66.953% (+0.06%) from 66.892%
12185831515

push

github

web-flow
#846: fix version shortopt (-v) (#847)

2532 of 4134 branches covered (61.25%)

Branch coverage included in aggregate %.

6587 of 9486 relevant lines covered (69.44%)

3.06 hits per line

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

87.86
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 com.devonfw.tools.ide.cli.CliArgument;
12
import com.devonfw.tools.ide.cli.CliArguments;
13
import com.devonfw.tools.ide.completion.CompletionCandidateCollector;
14
import com.devonfw.tools.ide.context.IdeContext;
15
import com.devonfw.tools.ide.property.KeywordProperty;
16
import com.devonfw.tools.ide.property.Property;
17
import com.devonfw.tools.ide.tool.androidstudio.AndroidStudio;
18
import com.devonfw.tools.ide.tool.aws.Aws;
19
import com.devonfw.tools.ide.tool.az.Azure;
20
import com.devonfw.tools.ide.tool.docker.Docker;
21
import com.devonfw.tools.ide.tool.dotnet.DotNet;
22
import com.devonfw.tools.ide.tool.eclipse.Eclipse;
23
import com.devonfw.tools.ide.tool.gcviewer.GcViewer;
24
import com.devonfw.tools.ide.tool.gh.Gh;
25
import com.devonfw.tools.ide.tool.graalvm.GraalVm;
26
import com.devonfw.tools.ide.tool.gradle.Gradle;
27
import com.devonfw.tools.ide.tool.helm.Helm;
28
import com.devonfw.tools.ide.tool.intellij.Intellij;
29
import com.devonfw.tools.ide.tool.jasypt.Jasypt;
30
import com.devonfw.tools.ide.tool.java.Java;
31
import com.devonfw.tools.ide.tool.jmc.Jmc;
32
import com.devonfw.tools.ide.tool.kotlinc.Kotlinc;
33
import com.devonfw.tools.ide.tool.kotlinc.KotlincNative;
34
import com.devonfw.tools.ide.tool.kubectl.KubeCtl;
35
import com.devonfw.tools.ide.tool.lazydocker.LazyDocker;
36
import com.devonfw.tools.ide.tool.mvn.Mvn;
37
import com.devonfw.tools.ide.tool.node.Node;
38
import com.devonfw.tools.ide.tool.npm.Npm;
39
import com.devonfw.tools.ide.tool.oc.Oc;
40
import com.devonfw.tools.ide.tool.pgadmin.PgAdmin;
41
import com.devonfw.tools.ide.tool.quarkus.Quarkus;
42
import com.devonfw.tools.ide.tool.sonar.Sonar;
43
import com.devonfw.tools.ide.tool.terraform.Terraform;
44
import com.devonfw.tools.ide.tool.tomcat.Tomcat;
45
import com.devonfw.tools.ide.tool.vscode.Vscode;
46

47
/**
48
 * Implementation of {@link CommandletManager}.
49
 */
50
public class CommandletManagerImpl implements CommandletManager {
51

52
  private final IdeContext context;
53

54
  private final Map<Class<? extends Commandlet>, Commandlet> commandletTypeMap;
55

56
  private final Map<String, Commandlet> commandletNameMap;
57

58
  private final Map<String, Commandlet> firstKeywordMap;
59

60
  private final Collection<Commandlet> commandlets;
61

62
  /**
63
   * The constructor.
64
   *
65
   * @param context the {@link IdeContext}.
66
   */
67
  public CommandletManagerImpl(IdeContext context) {
68

69
    super();
2✔
70
    this.context = context;
3✔
71
    this.commandletTypeMap = new HashMap<>();
5✔
72
    this.commandletNameMap = new HashMap<>();
5✔
73
    this.firstKeywordMap = new HashMap<>();
5✔
74
    this.commandlets = Collections.unmodifiableCollection(this.commandletTypeMap.values());
6✔
75
    add(new HelpCommandlet(context));
6✔
76
    add(new EnvironmentCommandlet(context));
6✔
77
    add(new CompleteCommandlet(context));
6✔
78
    add(new ShellCommandlet(context));
6✔
79
    add(new InstallCommandlet(context));
6✔
80
    add(new VersionSetCommandlet(context));
6✔
81
    add(new VersionGetCommandlet(context));
6✔
82
    add(new VersionListCommandlet(context));
6✔
83
    add(new EditionGetCommandlet(context));
6✔
84
    add(new EditionSetCommandlet(context));
6✔
85
    add(new EditionListCommandlet(context));
6✔
86
    add(new VersionCommandlet(context));
6✔
87
    add(new StatusCommandlet(context));
6✔
88
    add(new RepositoryCommandlet(context));
6✔
89
    add(new UninstallCommandlet(context));
6✔
90
    add(new UpdateCommandlet(context));
6✔
91
    add(new CreateCommandlet(context));
6✔
92
    add(new BuildCommandlet(context));
6✔
93
    add(new InstallPluginCommandlet(context));
6✔
94
    add(new UninstallPluginCommandlet(context));
6✔
95
    add(new Gh(context));
6✔
96
    add(new Helm(context));
6✔
97
    add(new Java(context));
6✔
98
    add(new Node(context));
6✔
99
    add(new Npm(context));
6✔
100
    add(new Mvn(context));
6✔
101
    add(new GcViewer(context));
6✔
102
    add(new Gradle(context));
6✔
103
    add(new Eclipse(context));
6✔
104
    add(new Terraform(context));
6✔
105
    add(new Oc(context));
6✔
106
    add(new Quarkus(context));
6✔
107
    add(new Kotlinc(context));
6✔
108
    add(new KotlincNative(context));
6✔
109
    add(new KubeCtl(context));
6✔
110
    add(new Tomcat(context));
6✔
111
    add(new Vscode(context));
6✔
112
    add(new Azure(context));
6✔
113
    add(new Aws(context));
6✔
114
    add(new Jmc(context));
6✔
115
    add(new DotNet(context));
6✔
116
    add(new Intellij(context));
6✔
117
    add(new Jasypt(context));
6✔
118
    add(new Docker(context));
6✔
119
    add(new Sonar(context));
6✔
120
    add(new AndroidStudio(context));
6✔
121
    add(new GraalVm(context));
6✔
122
    add(new PgAdmin(context));
6✔
123
    add(new LazyDocker(context));
6✔
124
  }
1✔
125

126
  /**
127
   * @param commandlet the {@link Commandlet} to add.
128
   */
129
  protected void add(Commandlet commandlet) {
130

131
    boolean hasRequiredProperty = false;
2✔
132
    List<Property<?>> properties = commandlet.getProperties();
3✔
133
    int propertyCount = properties.size();
3✔
134
    KeywordProperty keyword = commandlet.getFirstKeyword();
3✔
135
    if (keyword != null) {
2!
136
      String name = keyword.getName();
3✔
137
      this.firstKeywordMap.putIfAbsent(name, commandlet);
6✔
138
      if (name.startsWith("--")) {
4✔
139
        this.firstKeywordMap.putIfAbsent(name.substring(2), commandlet);
8✔
140
      }
141
      String alias = keyword.getAlias();
3✔
142
      if (alias != null) {
2✔
143
        this.firstKeywordMap.putIfAbsent(alias, commandlet);
6✔
144
      }
145
    }
146
    for (int i = 0; i < propertyCount; i++) {
5!
147
      Property<?> property = properties.get(i);
5✔
148
      if (property.isRequired()) {
3!
149
        hasRequiredProperty = true;
2✔
150
        break;
1✔
151
      }
152
    }
153
    if (!hasRequiredProperty) {
2!
154
      throw new IllegalStateException("Commandlet " + commandlet + " must have at least one mandatory property!");
×
155
    }
156
    this.commandletTypeMap.put(commandlet.getClass(), commandlet);
7✔
157
    Commandlet duplicate = this.commandletNameMap.put(commandlet.getName(), commandlet);
8✔
158
    if (duplicate != null) {
2!
159
      throw new IllegalStateException("Commandlet " + commandlet + " has the same name as " + duplicate);
×
160
    }
161
  }
1✔
162

163
  @Override
164
  public Collection<Commandlet> getCommandlets() {
165

166
    return this.commandlets;
3✔
167
  }
168

169
  @Override
170
  public <C extends Commandlet> C getCommandlet(Class<C> commandletType) {
171

172
    Commandlet commandlet = this.commandletTypeMap.get(commandletType);
6✔
173
    if (commandlet == null) {
2!
174
      throw new IllegalStateException("Commandlet for type " + commandletType + " is not registered!");
×
175
    }
176
    return commandletType.cast(commandlet);
5✔
177
  }
178

179
  @Override
180
  public Commandlet getCommandlet(String name) {
181

182
    return this.commandletNameMap.get(name);
6✔
183
  }
184

185
  @Override
186
  public Commandlet getCommandletByFirstKeyword(String keyword) {
187

188
    return this.firstKeywordMap.get(keyword);
6✔
189
  }
190

191
  @Override
192
  public Iterator<Commandlet> findCommandlet(CliArguments arguments, CompletionCandidateCollector collector) {
193

194
    CliArgument current = arguments.current();
3✔
195
    if (current.isEnd()) {
3!
196
      return Collections.emptyIterator();
×
197
    }
198
    String keyword = current.get();
3✔
199
    Commandlet commandlet = getCommandletByFirstKeyword(keyword);
4✔
200
    if ((commandlet == null) && (collector == null)) {
4!
201
      return Collections.emptyIterator();
×
202
    }
203
    return new CommandletFinder(commandlet, arguments.copy(), collector);
9✔
204
  }
205

206
  private final class CommandletFinder implements Iterator<Commandlet> {
1✔
207

208
    private final Commandlet firstCandidate;
209

210
    private final Iterator<Commandlet> commandletIterator;
211

212
    private final CliArguments arguments;
213

214
    private final CompletionCandidateCollector collector;
215

216
    private Commandlet next;
217

218
    private CommandletFinder(Commandlet firstCandidate, CliArguments arguments, CompletionCandidateCollector collector) {
5✔
219

220
      this.firstCandidate = firstCandidate;
3✔
221
      this.commandletIterator = getCommandlets().iterator();
5✔
222
      this.arguments = arguments;
3✔
223
      this.collector = collector;
3✔
224
      if (isSuitable(firstCandidate)) {
4✔
225
        this.next = firstCandidate;
4✔
226
      } else {
227
        this.next = findNext();
4✔
228
      }
229
    }
1✔
230

231
    @Override
232
    public boolean hasNext() {
233

234
      return this.next != null;
7✔
235
    }
236

237
    @Override
238
    public Commandlet next() {
239

240
      if (this.next == null) {
3!
241
        throw new NoSuchElementException();
×
242
      }
243
      Commandlet result = this.next;
3✔
244
      this.next = findNext();
4✔
245
      return result;
2✔
246
    }
247

248
    private boolean isSuitable(Commandlet commandlet) {
249

250
      return (commandlet != null) && (!commandlet.isIdeHomeRequired() || (context.getIdeHome() != null));
14!
251
    }
252

253
    private Commandlet findNext() {
254
      while (this.commandletIterator.hasNext()) {
4✔
255
        Commandlet cmd = this.commandletIterator.next();
5✔
256
        if ((cmd != this.firstCandidate) && isSuitable(cmd)) {
8!
257
          List<Property<?>> properties = cmd.getProperties();
3✔
258
          // validation should already be done in add method and could be removed here...
259
          if (properties.isEmpty()) {
3!
260
            assert false : cmd.getClass().getSimpleName() + " has no properties!";
×
261
          } else {
262
            Property<?> property = properties.get(0);
5✔
263
            if (property instanceof KeywordProperty) {
3!
264
              boolean matches = property.apply(arguments.copy(), context, cmd, this.collector);
12✔
265
              if (matches) {
2✔
266
                return cmd;
2✔
267
              }
268
            } else {
1✔
269
              assert false : cmd.getClass().getSimpleName() + " is invalid as first property must be keyword property but is " + property;
×
270
            }
271
          }
272
        }
273
      }
1✔
274
      return null;
2✔
275
    }
276
  }
277
}
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