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

devonfw / IDEasy / 14705641546

28 Apr 2025 10:34AM UTC coverage: 67.522% (+0.07%) from 67.448%
14705641546

Pull #1219

github

web-flow
Merge ede93deb7 into 232e6f193
Pull Request #1219: #1190: Added pycharm support

3086 of 4976 branches covered (62.02%)

Branch coverage included in aggregate %.

7941 of 11355 relevant lines covered (69.93%)

3.06 hits per line

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

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

50
/**
51
 * Implementation of {@link CommandletManager}.
52
 */
53
public class CommandletManagerImpl implements CommandletManager {
54

55
  private final IdeContext context;
56

57
  private final Map<Class<? extends Commandlet>, Commandlet> commandletTypeMap;
58

59
  private final Map<String, Commandlet> commandletNameMap;
60

61
  private final Map<String, Commandlet> firstKeywordMap;
62

63
  private final Collection<Commandlet> commandlets;
64

65
  /**
66
   * The constructor.
67
   *
68
   * @param context the {@link IdeContext}.
69
   */
70
  public CommandletManagerImpl(IdeContext context) {
71

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

133
  /**
134
   * @param commandlet the {@link Commandlet} to add.
135
   */
136
  protected void add(Commandlet commandlet) {
137

138
    boolean hasRequiredProperty = false;
2✔
139
    List<Property<?>> properties = commandlet.getProperties();
3✔
140
    int propertyCount = properties.size();
3✔
141
    KeywordProperty keyword = commandlet.getFirstKeyword();
3✔
142
    if (keyword != null) {
2!
143
      String name = keyword.getName();
3✔
144
      registerKeyword(name, commandlet);
4✔
145
      String optionName = keyword.getOptionName();
3✔
146
      if (!optionName.equals(name)) {
4✔
147
        registerKeyword(optionName, commandlet);
4✔
148
      }
149
      String alias = keyword.getAlias();
3✔
150
      if (alias != null) {
2✔
151
        registerKeyword(alias, commandlet);
4✔
152
      }
153
    }
154
    for (int i = 0; i < propertyCount; i++) {
5!
155
      Property<?> property = properties.get(i);
5✔
156
      if (property.isRequired()) {
3!
157
        hasRequiredProperty = true;
2✔
158
        break;
1✔
159
      }
160
    }
161
    if (!hasRequiredProperty) {
2!
162
      throw new IllegalStateException("Commandlet " + commandlet + " must have at least one mandatory property!");
×
163
    }
164
    this.commandletTypeMap.put(commandlet.getClass(), commandlet);
7✔
165
    Commandlet duplicate = this.commandletNameMap.put(commandlet.getName(), commandlet);
8✔
166
    if (duplicate != null) {
2!
167
      throw new IllegalStateException("Commandlet " + commandlet + " has the same name as " + duplicate);
×
168
    }
169
  }
1✔
170

171
  private void registerKeyword(String keyword, Commandlet commandlet) {
172

173
    Commandlet duplicate = this.firstKeywordMap.putIfAbsent(keyword, commandlet);
7✔
174
    if (duplicate != null) {
2!
175
      this.context.debug("Duplicate keyword {} already used by {} so it cannot be associated also with {}", keyword, duplicate, commandlet);
×
176
    }
177
  }
1✔
178

179
  @Override
180
  public Collection<Commandlet> getCommandlets() {
181

182
    return this.commandlets;
3✔
183
  }
184

185
  @Override
186
  public <C extends Commandlet> C getCommandlet(Class<C> commandletType) {
187

188
    Commandlet commandlet = this.commandletTypeMap.get(commandletType);
6✔
189
    if (commandlet == null) {
2!
190
      throw new IllegalStateException("Commandlet for type " + commandletType + " is not registered!");
×
191
    }
192
    return commandletType.cast(commandlet);
5✔
193
  }
194

195
  @Override
196
  public Commandlet getCommandlet(String name) {
197

198
    return this.commandletNameMap.get(name);
6✔
199
  }
200

201
  @Override
202
  public Commandlet getCommandletByFirstKeyword(String keyword) {
203

204
    return this.firstKeywordMap.get(keyword);
6✔
205
  }
206

207
  @Override
208
  public Iterator<Commandlet> findCommandlet(CliArguments arguments, CompletionCandidateCollector collector) {
209

210
    CliArgument current = arguments.current();
3✔
211
    if (current.isEnd()) {
3!
212
      return Collections.emptyIterator();
×
213
    }
214
    String keyword = current.get();
3✔
215
    Commandlet commandlet = getCommandletByFirstKeyword(keyword);
4✔
216
    if ((commandlet == null) && (collector == null)) {
4!
217
      return Collections.emptyIterator();
×
218
    }
219
    return new CommandletFinder(commandlet, arguments.copy(), collector);
9✔
220
  }
221

222
  private final class CommandletFinder implements Iterator<Commandlet> {
1✔
223

224
    private final Commandlet firstCandidate;
225

226
    private final Iterator<Commandlet> commandletIterator;
227

228
    private final CliArguments arguments;
229

230
    private final CompletionCandidateCollector collector;
231

232
    private Commandlet next;
233

234
    private CommandletFinder(Commandlet firstCandidate, CliArguments arguments, CompletionCandidateCollector collector) {
5✔
235

236
      this.firstCandidate = firstCandidate;
3✔
237
      this.commandletIterator = getCommandlets().iterator();
5✔
238
      this.arguments = arguments;
3✔
239
      this.collector = collector;
3✔
240
      if (isSuitable(firstCandidate)) {
4✔
241
        this.next = firstCandidate;
4✔
242
      } else {
243
        this.next = findNext();
4✔
244
      }
245
    }
1✔
246

247
    @Override
248
    public boolean hasNext() {
249

250
      return this.next != null;
7✔
251
    }
252

253
    @Override
254
    public Commandlet next() {
255

256
      if (this.next == null) {
3!
257
        throw new NoSuchElementException();
×
258
      }
259
      Commandlet result = this.next;
3✔
260
      this.next = findNext();
4✔
261
      return result;
2✔
262
    }
263

264
    private boolean isSuitable(Commandlet commandlet) {
265

266
      return (commandlet != null) && (!commandlet.isIdeHomeRequired() || (context.getIdeHome() != null));
14✔
267
    }
268

269
    private Commandlet findNext() {
270
      while (this.commandletIterator.hasNext()) {
4✔
271
        Commandlet cmd = this.commandletIterator.next();
5✔
272
        if ((cmd != this.firstCandidate) && isSuitable(cmd)) {
8✔
273
          List<Property<?>> properties = cmd.getProperties();
3✔
274
          // validation should already be done in add method and could be removed here...
275
          if (properties.isEmpty()) {
3!
276
            assert false : cmd.getClass().getSimpleName() + " has no properties!";
×
277
          } else {
278
            Property<?> property = properties.get(0);
5✔
279
            if (property instanceof KeywordProperty) {
3!
280
              boolean matches = property.apply(arguments.copy(), context, cmd, this.collector);
12✔
281
              if (matches) {
2✔
282
                return cmd;
2✔
283
              }
284
            } else {
1✔
285
              assert false : cmd.getClass().getSimpleName() + " is invalid as first property must be keyword property but is " + property;
×
286
            }
287
          }
288
        }
289
      }
1✔
290
      return null;
2✔
291
    }
292
  }
293
}
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