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

devonfw / IDEasy / 9778043214

03 Jul 2024 12:41PM UTC coverage: 60.631% (+0.5%) from 60.142%
9778043214

push

github

web-flow
#36: Tool Commandlet for tomcat (#250)

1939 of 3515 branches covered (55.16%)

Branch coverage included in aggregate %.

5133 of 8149 relevant lines covered (62.99%)

2.76 hits per line

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

67.18
cli/src/main/java/com/devonfw/tools/ide/tool/ToolCommandlet.java
1
package com.devonfw.tools.ide.tool;
2

3
import com.devonfw.tools.ide.commandlet.Commandlet;
4
import com.devonfw.tools.ide.common.Tag;
5
import com.devonfw.tools.ide.common.Tags;
6
import com.devonfw.tools.ide.context.IdeContext;
7
import com.devonfw.tools.ide.environment.EnvironmentVariables;
8
import com.devonfw.tools.ide.environment.EnvironmentVariablesType;
9
import com.devonfw.tools.ide.os.MacOsHelper;
10
import com.devonfw.tools.ide.process.ProcessContext;
11
import com.devonfw.tools.ide.process.ProcessErrorHandling;
12
import com.devonfw.tools.ide.process.ProcessMode;
13
import com.devonfw.tools.ide.property.StringProperty;
14
import com.devonfw.tools.ide.version.VersionIdentifier;
15

16
import java.nio.file.Files;
17
import java.nio.file.Path;
18
import java.util.List;
19
import java.util.Set;
20

21
/**
22
 * {@link Commandlet} for a tool integrated into the IDE.
23
 */
24
public abstract class ToolCommandlet extends Commandlet implements Tags {
25

26
  /** @see #getName() */
27
  protected final String tool;
28

29
  private final Set<Tag> tags;
30

31
  /** The commandline arguments to pass to the tool. */
32
  public final StringProperty arguments;
33

34
  private MacOsHelper macOsHelper;
35

36
  /**
37
   * The constructor.
38
   *
39
   * @param context the {@link IdeContext}.
40
   * @param tool the {@link #getName() tool name}.
41
   * @param tags the {@link #getTags() tags} classifying the tool. Should be created via {@link Set#of(Object) Set.of} method.
42
   */
43
  public ToolCommandlet(IdeContext context, String tool, Set<Tag> tags) {
44

45
    super(context);
3✔
46
    this.tool = tool;
3✔
47
    this.tags = tags;
3✔
48
    addKeyword(tool);
3✔
49
    this.arguments = new StringProperty("", false, true, "args");
9✔
50
    initProperties();
2✔
51
  }
1✔
52

53
  /**
54
   * Add initial Properties to the tool
55
   */
56
  protected void initProperties() {
57

58
    add(this.arguments);
5✔
59
  }
1✔
60

61
  /**
62
   * @return the name of the tool (e.g. "java", "mvn", "npm", "node").
63
   */
64
  @Override
65
  public String getName() {
66

67
    return this.tool;
3✔
68
  }
69

70
  /**
71
   * @return the name of the binary executable for this tool.
72
   */
73
  protected String getBinaryName() {
74

75
    return this.tool;
3✔
76
  }
77

78
  @Override
79
  public final Set<Tag> getTags() {
80

81
    return this.tags;
×
82
  }
83

84
  @Override
85
  public void run() {
86

87
    runTool(ProcessMode.DEFAULT, null, this.arguments.asArray());
7✔
88
  }
1✔
89

90
  /**
91
   * Ensures the tool is installed and then runs this tool with the given arguments.
92
   *
93
   * @param processMode see {@link ProcessMode}
94
   * @param toolVersion the explicit version (pattern) to run. Typically {@code null} to ensure the configured version is installed and use that one. Otherwise,
95
   * the specified version will be installed in the software repository without touching and IDE installation and used to run.
96
   * @param args the command-line arguments to run the tool.
97
   */
98
  public void runTool(ProcessMode processMode, VersionIdentifier toolVersion, String... args) {
99

100
    Path binaryPath;
101
    Path toolPath = Path.of(getBinaryName());
×
102
    if (toolVersion == null) {
×
103
      install(true);
×
104
      binaryPath = toolPath;
×
105
    } else {
106
      throw new UnsupportedOperationException("Not yet implemented!");
×
107
    }
108
    ProcessContext pc = this.context.newProcess().errorHandling(ProcessErrorHandling.WARNING).executable(binaryPath).addArgs(args);
×
109

110
    pc.run(processMode);
×
111
  }
×
112

113
  /**
114
   * @param toolVersion the explicit {@link VersionIdentifier} of the tool to run.
115
   * @param args the command-line arguments to run the tool.
116
   * @see ToolCommandlet#runTool(ProcessMode, VersionIdentifier, String...)
117
   */
118
  public void runTool(VersionIdentifier toolVersion, String... args) {
119

120
    runTool(ProcessMode.DEFAULT, toolVersion, args);
5✔
121
  }
1✔
122

123
  /**
124
   * @return the {@link EnvironmentVariables#getToolEdition(String) tool edition}.
125
   */
126
  public String getEdition() {
127

128
    return this.context.getVariables().getToolEdition(getName());
7✔
129
  }
130

131
  /**
132
   * @return the {@link #getName() tool} with its {@link #getEdition() edition}. The edition will be omitted if same as tool.
133
   * @see #getToolWithEdition(String, String)
134
   */
135
  protected final String getToolWithEdition() {
136

137
    return getToolWithEdition(getName(), getEdition());
6✔
138
  }
139

140
  /**
141
   * @param tool the tool name.
142
   * @param edition the edition.
143
   * @return the {@link #getName() tool} with its {@link #getEdition() edition}. The edition will be omitted if same as tool.
144
   */
145
  protected final static String getToolWithEdition(String tool, String edition) {
146

147
    if (tool.equals(edition)) {
4!
148
      return tool;
2✔
149
    }
150
    return tool + "/" + edition;
×
151
  }
152

153
  /**
154
   * @return the {@link EnvironmentVariables#getToolVersion(String) tool version}.
155
   */
156
  public VersionIdentifier getConfiguredVersion() {
157

158
    return this.context.getVariables().getToolVersion(getName());
7✔
159
  }
160

161
  /**
162
   * Method to be called for {@link #install(boolean)} from dependent {@link com.devonfw.tools.ide.commandlet.Commandlet}s.
163
   *
164
   * @return {@code true} if the tool was newly installed, {@code false} if the tool was already installed before and nothing has changed.
165
   */
166
  public boolean install() {
167

168
    return install(true);
4✔
169
  }
170

171
  /**
172
   * Performs the installation of the {@link #getName() tool} managed by this {@link com.devonfw.tools.ide.commandlet.Commandlet}.
173
   *
174
   * @param silent - {@code true} if called recursively to suppress verbose logging, {@code false} otherwise.
175
   * @return {@code true} if the tool was newly installed, {@code false} if the tool was already installed before and nothing has changed.
176
   */
177
  public boolean install(boolean silent) {
178

179
    return doInstall(silent);
4✔
180
  }
181

182
  /**
183
   * Installs or updates the managed {@link #getName() tool}.
184
   *
185
   * @param silent - {@code true} if called recursively to suppress verbose logging, {@code false} otherwise.
186
   * @return {@code true} if the tool was newly installed, {@code false} if the tool was already installed before and nothing has changed.
187
   */
188
  protected abstract boolean doInstall(boolean silent);
189

190
  /**
191
   * This method is called after the tool has been newly installed or updated to a new version.
192
   */
193
  protected void postInstall() {
194

195
    // nothing to do by default
196
  }
1✔
197

198
  /**
199
   * @return {@code true} to extract (unpack) the downloaded binary file, {@code false} otherwise.
200
   */
201
  protected boolean isExtract() {
202

203
    return true;
2✔
204
  }
205

206
  /**
207
   * @return the {@link MacOsHelper} instance.
208
   */
209
  protected MacOsHelper getMacOsHelper() {
210

211
    if (this.macOsHelper == null) {
3✔
212
      this.macOsHelper = new MacOsHelper(this.context);
7✔
213
    }
214
    return this.macOsHelper;
3✔
215
  }
216

217
  /**
218
   * @return the currently installed {@link VersionIdentifier version} of this tool or {@code null} if not installed.
219
   */
220
  public abstract VersionIdentifier getInstalledVersion();
221

222
  /**
223
   * @return the installed edition of this tool or {@code null} if not installed.
224
   */
225
  public abstract String getInstalledEdition();
226

227
  /**
228
   * Uninstalls the {@link #getName() tool}.
229
   */
230
  public abstract void uninstall();
231

232
  /**
233
   * List the available editions of this tool.
234
   */
235
  public void listEditions() {
236

237
    List<String> editions = this.context.getUrls().getSortedEditions(getName());
7✔
238
    for (String edition : editions) {
10✔
239
      this.context.info(edition);
4✔
240
    }
1✔
241
  }
1✔
242

243
  /**
244
   * List the available versions of this tool.
245
   */
246
  public void listVersions() {
247

248
    List<VersionIdentifier> versions = this.context.getUrls().getSortedVersions(getName(), getEdition());
9✔
249
    for (VersionIdentifier vi : versions) {
10✔
250
      this.context.info(vi.toString());
5✔
251
    }
1✔
252
  }
1✔
253

254
  /**
255
   * Sets the tool version in the environment variable configuration file.
256
   *
257
   * @param version the version (pattern) to set.
258
   */
259
  public void setVersion(String version) {
260

261
    if ((version == null) || version.isBlank()) {
×
262
      throw new IllegalStateException("Version has to be specified!");
×
263
    }
264
    VersionIdentifier configuredVersion = VersionIdentifier.of(version);
×
265
    if (!configuredVersion.isPattern() && !configuredVersion.isValid()) {
×
266
      this.context.warning("Version {} seems to be invalid", version);
×
267
    }
268
    setVersion(configuredVersion, true);
×
269
  }
×
270

271
  /**
272
   * Sets the tool version in the environment variable configuration file.
273
   *
274
   * @param version the version to set. May also be a {@link VersionIdentifier#isPattern() version pattern}.
275
   * @param hint - {@code true} to print the installation hint, {@code false} otherwise.
276
   */
277
  public void setVersion(VersionIdentifier version, boolean hint) {
278

279
    String edition = getEdition();
3✔
280
    this.context.getUrls().getVersionFolder(tool, edition, version); // CliException is thrown if the version is not existing
9✔
281

282
    EnvironmentVariables variables = this.context.getVariables();
4✔
283
    EnvironmentVariables settingsVariables = variables.getByType(EnvironmentVariablesType.SETTINGS);
4✔
284
    String name = EnvironmentVariables.getToolVersionVariable(this.tool);
4✔
285
    VersionIdentifier resolvedVersion = this.context.getUrls().getVersion(this.tool, edition, version);
9✔
286
    if (version.isPattern()) {
3!
287
      this.context.debug("Resolved version {} to {} for tool {}/{}", version, resolvedVersion, this.tool, edition);
×
288
    }
289
    settingsVariables.set(name, resolvedVersion.toString(), false);
7✔
290
    settingsVariables.save();
2✔
291
    this.context.info("{}={} has been set in {}", name, version, settingsVariables.getSource());
19✔
292
    EnvironmentVariables declaringVariables = variables.findVariable(name);
4✔
293
    if ((declaringVariables != null) && (declaringVariables != settingsVariables)) {
5!
294
      this.context.warning("The variable {} is overridden in {}. Please remove the overridden declaration in order to make the change affect.", name,
×
295
          declaringVariables.getSource());
×
296
    }
297
    if (hint) {
2✔
298
      this.context.info("To install that version call the following command:");
4✔
299
      this.context.info("ide install {}", this.tool);
11✔
300
    }
301
  }
1✔
302

303
  /**
304
   * Sets the tool edition in the environment variable configuration file.
305
   *
306
   * @param edition the edition to set.
307
   */
308
  public void setEdition(String edition) {
309

310
    setEdition(edition, true);
4✔
311
  }
1✔
312

313
  /**
314
   * Sets the tool edition in the environment variable configuration file.
315
   *
316
   * @param edition the edition to set
317
   * @param hint - {@code true} to print the installation hint, {@code false} otherwise.
318
   */
319
  public void setEdition(String edition, boolean hint) {
320

321
    if ((edition == null) || edition.isBlank()) {
5!
322
      throw new IllegalStateException("Edition has to be specified!");
×
323
    }
324

325
    if (!Files.exists(this.context.getUrls().getEdition(getName(), edition).getPath())) {
12!
326
      this.context.warning("Edition {} seems to be invalid", edition);
10✔
327

328
    }
329
    EnvironmentVariables variables = this.context.getVariables();
4✔
330
    EnvironmentVariables settingsVariables = variables.getByType(EnvironmentVariablesType.SETTINGS);
4✔
331
    String name = EnvironmentVariables.getToolEditionVariable(this.tool);
4✔
332
    settingsVariables.set(name, edition, false);
6✔
333
    settingsVariables.save();
2✔
334

335
    this.context.info("{}={} has been set in {}", name, edition, settingsVariables.getSource());
19✔
336
    EnvironmentVariables declaringVariables = variables.findVariable(name);
4✔
337
    if ((declaringVariables != null) && (declaringVariables != settingsVariables)) {
5!
338
      this.context.warning("The variable {} is overridden in {}. Please remove the overridden declaration in order to make the change affect.", name,
×
339
          declaringVariables.getSource());
×
340
    }
341
    if (hint) {
2!
342
      this.context.info("To install that edition call the following command:");
4✔
343
      this.context.info("ide install {}", this.tool);
11✔
344
    }
345
  }
1✔
346

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