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

devonfw / IDEasy / 9907372175

12 Jul 2024 11:49AM UTC coverage: 61.142% (-0.02%) from 61.162%
9907372175

push

github

hohwille
fixed tests

1997 of 3595 branches covered (55.55%)

Branch coverage included in aggregate %.

5296 of 8333 relevant lines covered (63.55%)

2.8 hits per line

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

59.62
cli/src/main/java/com/devonfw/tools/ide/commandlet/Commandlet.java
1
package com.devonfw.tools.ide.commandlet;
2

3
import java.util.ArrayList;
4
import java.util.Collections;
5
import java.util.HashMap;
6
import java.util.List;
7
import java.util.Map;
8
import java.util.Objects;
9

10
import com.devonfw.tools.ide.context.IdeContext;
11
import com.devonfw.tools.ide.nls.NlsBundle;
12
import com.devonfw.tools.ide.property.KeywordProperty;
13
import com.devonfw.tools.ide.property.Property;
14
import com.devonfw.tools.ide.tool.ToolCommandlet;
15
import com.devonfw.tools.ide.version.VersionIdentifier;
16

17
/**
18
 * A {@link Commandlet} is a sub-command of the IDE CLI.
19
 */
20
public abstract class Commandlet {
1✔
21

22
  /** The {@link IdeContext} instance. */
23
  protected final IdeContext context;
24

25
  private final List<Property<?>> propertiesList;
26

27
  private final List<Property<?>> properties;
28

29
  private final List<Property<?>> valuesList;
30

31
  private final List<Property<?>> values;
32

33
  private final Map<String, Property<?>> optionMap;
34

35
  private Property<?> multiValued;
36

37
  private String firstKeyword;
38

39
  /**
40
   * The constructor.
41
   *
42
   * @param context the {@link IdeContext}.
43
   */
44
  public Commandlet(IdeContext context) {
45

46
    super();
2✔
47
    this.context = context;
3✔
48
    this.propertiesList = new ArrayList<>();
5✔
49
    this.properties = Collections.unmodifiableList(this.propertiesList);
5✔
50
    this.valuesList = new ArrayList<>();
5✔
51
    this.values = Collections.unmodifiableList(this.valuesList);
5✔
52
    this.optionMap = new HashMap<>();
5✔
53
  }
1✔
54

55
  /**
56
   * @return the {@link List} with all {@link Property properties} of this {@link Commandlet}.
57
   */
58
  public List<Property<?>> getProperties() {
59

60
    return this.properties;
3✔
61
  }
62

63
  /**
64
   * @return the {@link List} of {@link Property properties} that are {@link Property#isValue() values}.
65
   */
66
  public List<Property<?>> getValues() {
67

68
    return this.values;
3✔
69
  }
70

71
  /**
72
   * Clear the set values on all properties of the {@link Commandlet#propertiesList}
73
   */
74
  public void clearProperties() {
75

76
    for (Property<?> property : this.propertiesList) {
×
77
      property.clearValue();
×
78
    }
×
79
  }
×
80

81
  /**
82
   * @param nameOrAlias the potential {@link Property#getName() name} or {@link Property#getAlias() alias} of the requested {@link Property}.
83
   * @return the requested {@link Property property} or {@code null} if not found.
84
   */
85
  public Property<?> getOption(String nameOrAlias) {
86

87
    return this.optionMap.get(nameOrAlias);
6✔
88
  }
89

90
  /**
91
   * @param keyword the {@link KeywordProperty keyword} to {@link #add(Property) add}.
92
   */
93
  protected void addKeyword(String keyword) {
94

95
    if (this.properties.isEmpty()) {
4✔
96
      this.firstKeyword = keyword;
3✔
97
    }
98
    add(new KeywordProperty(keyword, true, null));
9✔
99
  }
1✔
100

101
  /**
102
   * @param property the keyword {@link Property} to {@link #add(Property) add}.
103
   */
104
  protected void addKeyword(Property<?> property) {
105

106
    if (!this.properties.isEmpty()) {
4!
107
      throw new IllegalStateException();
×
108
    }
109
    this.firstKeyword = property.getNameOrAlias();
4✔
110
    add(property);
4✔
111
  }
1✔
112

113
  /**
114
   * @param <P> type of the {@link Property}.
115
   * @param property the {@link Property} to register.
116
   * @return the given {@link Property}.
117
   */
118
  protected <P extends Property<?>> P add(P property) {
119

120
    if (this.multiValued != null) {
3!
121
      throw new IllegalStateException("The multi-valued property " + this.multiValued + " can not be followed by " + property);
×
122
    }
123
    this.propertiesList.add(property);
5✔
124
    if (property.isOption()) {
3✔
125
      add(property.getName(), property, false);
6✔
126
      add(property.getAlias(), property, true);
6✔
127
    }
128
    if (property.isValue()) {
3✔
129
      this.valuesList.add(property);
5✔
130
    }
131
    if (property.isMultiValued()) {
3✔
132
      this.multiValued = property;
3✔
133
    }
134
    return property;
2✔
135
  }
136

137
  private void add(String name, Property<?> property, boolean alias) {
138

139
    if (alias && (name == null)) {
4✔
140
      return;
1✔
141
    }
142
    Objects.requireNonNull(name);
3✔
143
    assert (name.equals(name.trim()));
6!
144
    if (name.isEmpty() && !alias) {
3!
145
      return;
×
146
    }
147
    Property<?> duplicate = this.optionMap.put(name, property);
7✔
148
    if (duplicate != null) {
2!
149
      throw new IllegalStateException("Duplicate name or alias " + name + " for " + property + " and " + duplicate);
×
150
    }
151
  }
1✔
152

153
  /**
154
   * @return the name of this {@link Commandlet} (e.g. "help").
155
   */
156
  public abstract String getName();
157

158
  /**
159
   * @return the first keyword of this {@link Commandlet}. Typically the same as {@link #getName() name} but may also differ (e.g. "set" vs. "set-version").
160
   */
161
  public String getKeyword() {
162

163
    return this.firstKeyword;
3✔
164
  }
165

166
  /**
167
   * @param <C> type of the {@link Commandlet}.
168
   * @param commandletType the {@link Class} reflecting the requested {@link Commandlet}.
169
   * @return the requested {@link Commandlet}.
170
   * @see CommandletManager#getCommandlet(Class)
171
   */
172
  protected <C extends Commandlet> C getCommandlet(Class<C> commandletType) {
173

174
    return this.context.getCommandletManager().getCommandlet(commandletType);
6✔
175
  }
176

177
  /**
178
   * @return {@code true} if {@link IdeContext#getIdeHome() IDE_HOME} is required for this commandlet, {@code false} otherwise.
179
   */
180
  public boolean isIdeHomeRequired() {
181

182
    if (!isIdeRootRequired()) {
3!
183
      return false;
2✔
184
    }
185
    return true;
×
186
  }
187

188
  /**
189
   * @return {@code true} if {@link IdeContext#getIdeRoot() IDE_ROOT} is required for this commandlet, {@code false} otherwise.
190
   */
191
  public boolean isIdeRootRequired() {
192

193
    return true;
×
194
  }
195

196
  /**
197
   * @return {@code true} to suppress the {@link com.devonfw.tools.ide.step.StepImpl#logSummary(boolean) step summary success message}.
198
   */
199
  public boolean isSuppressStepSuccess() {
200

201
    return false;
×
202
  }
203

204
  /**
205
   * @return {@code true} if the output of this commandlet is (potentially) processed automatically from outside, {@code false} otherwise. For example
206
   * {@link CompleteCommandlet} logs the suggestions for auto-completion to a bash script. Also the {@link EnvironmentCommandlet} logs the environment variables
207
   * for the {@code ide} wrapper script. In such scenarios these logs shall not be spammed with warnings like "IDE_ROOT is not set" that would break the
208
   * processing of the output.
209
   */
210
  public boolean isProcessableOutput() {
211

212
    return false;
×
213
  }
214

215
  /**
216
   * Runs this {@link Commandlet}.
217
   */
218
  public abstract void run();
219

220
  /**
221
   * @return {@code true} if this {@link Commandlet} is the valid candidate to be {@link #run()}, {@code false} otherwise.
222
   * @see Property#validate()
223
   */
224
  public boolean validate() {
225

226
    // avoid validation exception if not a candidate to be run.
227
    for (Property<?> property : this.propertiesList) {
×
228
      if (property.isRequired() && (property.getValue() == null)) {
×
229
        return false;
×
230
      }
231
    }
×
232
    for (Property<?> property : this.propertiesList) {
×
233
      if (!property.validate()) {
×
234
        return false;
×
235
      }
236
    }
×
237
    return true;
×
238
  }
239

240
  /**
241
   * Provide additional usage help of this {@link Commandlet} to the user.
242
   */
243
  public void printHelp(NlsBundle bundle) {
244

245
  }
1✔
246

247
  @Override
248
  public String toString() {
249

250
    return getClass().getSimpleName() + "[" + getName() + "]";
7✔
251
  }
252

253
  /**
254
   * @return the {@link ToolCommandlet} set in a {@link Property} of this commandlet used for auto-completion of a {@link VersionIdentifier} or {@code null} if
255
   * not exists or not configured.
256
   */
257
  public ToolCommandlet getToolForVersionCompletion() {
258

259
    return null;
×
260
  }
261
}
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