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

devonfw / IDEasy / 7278937169

20 Dec 2023 05:43PM UTC coverage: 47.802% (-0.07%) from 47.873%
7278937169

Pull #151

github

web-flow
Merge 09b6148ea into 1d60d9c17
Pull Request #151: #20: Implement ToolCommandlet for GCViewer

1031 of 2383 branches covered (0.0%)

Branch coverage included in aggregate %.

2721 of 5466 relevant lines covered (49.78%)

2.05 hits per line

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

84.44
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.List;
7
import java.util.Map;
8

9
import com.devonfw.tools.ide.context.AbstractIdeContext;
10
import com.devonfw.tools.ide.context.IdeContext;
11
import com.devonfw.tools.ide.property.KeywordProperty;
12
import com.devonfw.tools.ide.property.Property;
13
import com.devonfw.tools.ide.tool.az.Azure;
14
import com.devonfw.tools.ide.tool.eclipse.Eclipse;
15
import com.devonfw.tools.ide.tool.gcviewer.GcViewer;
16
import com.devonfw.tools.ide.tool.gh.Gh;
17
import com.devonfw.tools.ide.tool.gradle.Gradle;
18
import com.devonfw.tools.ide.tool.helm.Helm;
19
import com.devonfw.tools.ide.tool.java.Java;
20
import com.devonfw.tools.ide.tool.kotlinc.Kotlinc;
21
import com.devonfw.tools.ide.tool.kotlinc.KotlincNative;
22
import com.devonfw.tools.ide.tool.mvn.Mvn;
23
import com.devonfw.tools.ide.tool.node.Node;
24
import com.devonfw.tools.ide.tool.oc.Oc;
25
import com.devonfw.tools.ide.tool.quarkus.Quarkus;
26
import com.devonfw.tools.ide.tool.terraform.Terraform;
27
import com.devonfw.tools.ide.tool.vscode.Vscode;
28

29
/**
30
 * Implementation of {@link CommandletManager}.
31
 */
32
public final class CommandletManagerImpl implements CommandletManager {
33

34
  private static CommandletManagerImpl INSTANCE;
35

36
  private final Map<Class<? extends Commandlet>, Commandlet> commandletTypeMap;
37

38
  private final Map<String, Commandlet> commandletNameMap;
39

40
  private final Map<String, Commandlet> firstKeywordMap;
41

42
  private final Collection<Commandlet> commandlets;
43

44
  /**
45
   * The constructor.
46
   *
47
   * @param context the {@link IdeContext}.
48
   */
49
  private CommandletManagerImpl(IdeContext context) {
50

51
    super();
2✔
52
    this.commandletTypeMap = new HashMap<>();
5✔
53
    this.commandletNameMap = new HashMap<>();
5✔
54
    this.firstKeywordMap = new HashMap<>();
5✔
55
    this.commandlets = Collections.unmodifiableCollection(this.commandletTypeMap.values());
6✔
56
    add(new HelpCommandlet(context));
6✔
57
    add(new EnvironmentCommandlet(context));
6✔
58
    add(new InstallCommandlet(context));
6✔
59
    add(new VersionSetCommandlet(context));
6✔
60
    add(new VersionGetCommandlet(context));
6✔
61
    add(new VersionListCommandlet(context));
6✔
62
    add(new Gh(context));
6✔
63
    add(new Helm(context));
6✔
64
    add(new Java(context));
6✔
65
    add(new Node(context));
6✔
66
    add(new Mvn(context));
6✔
67
    add(new GcViewer(context));
6✔
68
    add(new Gradle(context));
6✔
69
    add(new Eclipse(context));
6✔
70
    add(new Terraform(context));
6✔
71
    add(new Oc(context));
6✔
72
    add(new Quarkus(context));
6✔
73
    add(new Kotlinc(context));
6✔
74
    add(new KotlincNative(context));
6✔
75
    add(new Vscode(context));
6✔
76
    add(new Azure(context));
6✔
77
  }
1✔
78

79
  private void add(Commandlet commandlet) {
80

81
    boolean hasRequiredProperty = false;
2✔
82
    List<Property<?>> properties = commandlet.getProperties();
3✔
83
    int propertyCount = properties.size();
3✔
84
    for (int i = 0; i < propertyCount; i++) {
5!
85
      Property<?> property = properties.get(i);
5✔
86
      if (property.isRequired()) {
3!
87
        hasRequiredProperty = true;
2✔
88
        if ((i == 0) && (property instanceof KeywordProperty)) {
5!
89
          String keyword = property.getName();
3✔
90
          this.firstKeywordMap.putIfAbsent(keyword, commandlet);
6✔
91
        }
1✔
92
        break;
93
      }
94
    }
95
    if (!hasRequiredProperty) {
2!
96
      throw new IllegalStateException("Commandlet " + commandlet + " must have at least one mandatory property!");
×
97
    }
98
    this.commandletTypeMap.put(commandlet.getClass(), commandlet);
7✔
99
    Commandlet duplicate = this.commandletNameMap.put(commandlet.getName(), commandlet);
8✔
100
    if (duplicate != null) {
2!
101
      throw new IllegalStateException("Commandlet " + commandlet + " has the same name as " + duplicate);
×
102
    }
103
  }
1✔
104

105
  @Override
106
  public Collection<Commandlet> getCommandlets() {
107

108
    return this.commandlets;
3✔
109
  }
110

111
  @Override
112
  public <C extends Commandlet> C getCommandlet(Class<C> commandletType) {
113

114
    Commandlet commandlet = this.commandletTypeMap.get(commandletType);
6✔
115
    if (commandlet == null) {
2!
116
      throw new IllegalStateException("Commandlet for type " + commandletType + " is not registered!");
×
117
    }
118
    return commandletType.cast(commandlet);
5✔
119
  }
120

121
  @Override
122
  public Commandlet getCommandlet(String name) {
123

124
    Commandlet commandlet = this.commandletNameMap.get(name);
6✔
125
    return commandlet;
2✔
126
  }
127

128
  @Override
129
  public Commandlet getCommandletByFirstKeyword(String keyword) {
130

131
    return this.firstKeywordMap.get(keyword);
×
132
  }
133

134
  /**
135
   * This method gives global access to the {@link CommandletManager} instance. Typically you should have access to
136
   * {@link IdeContext} and use {@link IdeContext#getCommandletManager()} to access the proper instance of
137
   * {@link CommandletManager}. Only in very specific cases where there is no {@link IdeContext} available, you may use
138
   * this method to access it (e.g. from {@link com.devonfw.tools.ide.property.CommandletProperty})
139
   *
140
   * @return the static instance of this {@link CommandletManager} implementation that has already been initialized.
141
   * @throws IllegalStateException if the instance has not been previously initialized via
142
   *         {@link #getOrCreate(IdeContext)}.
143
   */
144
  public static CommandletManager get() {
145

146
    return getOrCreate(null);
3✔
147
  }
148

149
  /**
150
   * This method has to be called initially from {@link IdeContext} to create the instance of this
151
   * {@link CommandletManager} implementation. It will store that instance internally in a static variable so it can
152
   * later be retrieved with {@link #get()}.
153
   *
154
   * @param context the {@link IdeContext}.
155
   * @return the {@link CommandletManager}.
156
   */
157
  public static CommandletManager getOrCreate(IdeContext context) {
158

159
    if (context == null) {
2✔
160
      if (INSTANCE == null) {
2!
161
        throw new IllegalStateException("Not initialized!");
×
162
      }
163
    } else {
164
      if (context instanceof AbstractIdeContext c) {
6!
165
        if (c.isMock()) {
3✔
166
          return new CommandletManagerImpl(context);
5✔
167
        }
168
      }
169
      if (INSTANCE != null) {
2✔
170
        System.out.println("Multiple initializations!");
3✔
171
      }
172
      INSTANCE = new CommandletManagerImpl(context);
5✔
173
    }
174
    return INSTANCE;
2✔
175
  }
176

177
  /**
178
   * Internal method to reset internal instance. May only be used for testing!
179
   */
180
  static void reset() {
181

182
    INSTANCE = null;
2✔
183
  }
1✔
184

185
}
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