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

devonfw / IDEasy / 11061200804

26 Sep 2024 10:27PM UTC coverage: 66.053% (-0.05%) from 66.107%
11061200804

push

github

web-flow
#593: #651: #564: #439: fixed bugs, refactored tool dependencies (#652)

2312 of 3848 branches covered (60.08%)

Branch coverage included in aggregate %.

6078 of 8854 relevant lines covered (68.65%)

3.03 hits per line

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

87.5
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.validation.ValidationResult;
16
import com.devonfw.tools.ide.validation.ValidationState;
17
import com.devonfw.tools.ide.version.VersionIdentifier;
18

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

24
  /** The {@link IdeContext} instance. */
25
  protected final IdeContext context;
26

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

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

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

33
  private final List<Property<?>> values;
34

35
  private final Map<String, Property<?>> optionMap;
36

37
  private Property<?> multiValued;
38

39
  private String firstKeyword;
40

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

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

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

62
    return this.properties;
3✔
63
  }
64

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

70
    return this.values;
3✔
71
  }
72

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

78
    for (Property<?> property : this.propertiesList) {
11✔
79
      property.clearValue();
2✔
80
    }
1✔
81
  }
1✔
82

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

89
    return this.optionMap.get(nameOrAlias);
6✔
90
  }
91

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

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

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

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

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

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

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

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

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

160
  /**
161
   * @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").
162
   */
163
  public String getKeyword() {
164

165
    return this.firstKeyword;
3✔
166
  }
167

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

176
    return this.context.getCommandletManager().getCommandlet(commandletType);
6✔
177
  }
178

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

184
    return isIdeRootRequired();
3✔
185
  }
186

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

192
    return true;
2✔
193
  }
194

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

200
    return false;
2✔
201
  }
202

203
  /**
204
   * @return {@code true} if the output of this commandlet is (potentially) processed automatically from outside, {@code false} otherwise. For example
205
   *     {@link CompleteCommandlet} logs the suggestions for auto-completion to a bash script. Also the {@link EnvironmentCommandlet} logs the environment
206
   *     variables 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
207
   *     break the processing of the output.
208
   */
209
  public boolean isProcessableOutput() {
210

211
    return false;
2✔
212
  }
213

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

219
  /**
220
   * @return {@code true} if this {@link Commandlet} is the valid candidate to be {@link #run()}, {@code false} otherwise.
221
   * @see Property#validate()
222
   */
223
  public ValidationResult validate() {
224
    ValidationState state = new ValidationState(null);
5✔
225
    // avoid validation exception if not a candidate to be run.
226
    for (Property<?> property : this.propertiesList) {
11✔
227
      state.add(property.validate());
4✔
228
    }
1✔
229
    return state;
2✔
230
  }
231

232
  /**
233
   * Provide additional usage help of this {@link Commandlet} to the user.
234
   */
235
  public void printHelp(NlsBundle bundle) {
236

237
  }
1✔
238

239
  @Override
240
  public String toString() {
241

242
    return getClass().getSimpleName() + "[" + getName() + "]";
7✔
243
  }
244

245
  /**
246
   * @return the {@link ToolCommandlet} set in a {@link Property} of this commandlet used for auto-completion of a {@link VersionIdentifier} or
247
   *     {@link com.devonfw.tools.ide.tool.plugin.ToolPluginDescriptor}, otherwise {@code null} if not exists or not configured.
248
   */
249
  public ToolCommandlet getToolForCompletion() {
250
    return null;
2✔
251
  }
252
}
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