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

devonfw / IDEasy / 24218122141

09 Apr 2026 11:20PM UTC coverage: 70.464% (-0.006%) from 70.47%
24218122141

Pull #1794

github

web-flow
Merge 7b08d0e94 into c0f5fa9cf
Pull Request #1794: #451: mac gatekeeper quarantine removal

4278 of 6710 branches covered (63.76%)

Branch coverage included in aggregate %.

11098 of 15111 relevant lines covered (73.44%)

3.1 hits per line

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

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

3
import java.io.IOException;
4
import java.nio.file.Files;
5
import java.nio.file.Path;
6
import java.util.ArrayList;
7
import java.util.List;
8
import java.util.Objects;
9
import java.util.Set;
10
import java.util.regex.Matcher;
11
import java.util.regex.Pattern;
12

13
import org.slf4j.Logger;
14
import org.slf4j.LoggerFactory;
15
import org.slf4j.event.Level;
16

17
import com.devonfw.tools.ide.commandlet.Commandlet;
18
import com.devonfw.tools.ide.common.Tag;
19
import com.devonfw.tools.ide.common.Tags;
20
import com.devonfw.tools.ide.context.IdeContext;
21
import com.devonfw.tools.ide.environment.EnvironmentVariables;
22
import com.devonfw.tools.ide.environment.EnvironmentVariablesFiles;
23
import com.devonfw.tools.ide.log.IdeLogLevel;
24
import com.devonfw.tools.ide.nls.NlsBundle;
25
import com.devonfw.tools.ide.os.MacOsHelper;
26
import com.devonfw.tools.ide.process.EnvironmentContext;
27
import com.devonfw.tools.ide.process.ProcessContext;
28
import com.devonfw.tools.ide.process.ProcessErrorHandling;
29
import com.devonfw.tools.ide.process.ProcessMode;
30
import com.devonfw.tools.ide.process.ProcessResult;
31
import com.devonfw.tools.ide.property.StringProperty;
32
import com.devonfw.tools.ide.security.ToolVersionChoice;
33
import com.devonfw.tools.ide.security.ToolVulnerabilities;
34
import com.devonfw.tools.ide.step.Step;
35
import com.devonfw.tools.ide.tool.repository.ToolRepository;
36
import com.devonfw.tools.ide.url.model.file.json.Cve;
37
import com.devonfw.tools.ide.url.model.file.json.ToolDependency;
38
import com.devonfw.tools.ide.url.model.file.json.ToolSecurity;
39
import com.devonfw.tools.ide.variable.IdeVariables;
40
import com.devonfw.tools.ide.version.GenericVersionRange;
41
import com.devonfw.tools.ide.version.VersionIdentifier;
42

43
/**
44
 * {@link Commandlet} for a tool integrated into the IDE.
45
 */
46
public abstract class ToolCommandlet extends Commandlet implements Tags {
47

48
  private static final Logger LOG = LoggerFactory.getLogger(ToolCommandlet.class);
4✔
49

50
  /** @see #getName() */
51
  protected final String tool;
52

53
  private final Set<Tag> tags;
54

55
  /** The commandline arguments to pass to the tool. */
56
  public final StringProperty arguments;
57

58
  private Path executionDirectory;
59

60
  private MacOsHelper macOsHelper;
61

62
  /**
63
   * The constructor.
64
   *
65
   * @param context the {@link IdeContext}.
66
   * @param tool the {@link #getName() tool name}.
67
   * @param tags the {@link #getTags() tags} classifying the tool. Should be created via {@link Set#of(Object) Set.of} method.
68
   */
69
  public ToolCommandlet(IdeContext context, String tool, Set<Tag> tags) {
70

71
    super(context);
3✔
72
    this.tool = tool;
3✔
73
    this.tags = tags;
3✔
74
    addKeyword(tool);
3✔
75
    this.arguments = new StringProperty("", false, true, "args");
9✔
76
    initProperties();
2✔
77
  }
1✔
78

79
  /**
80
   * Add initial Properties to the tool
81
   */
82
  protected void initProperties() {
83

84
    add(this.arguments);
5✔
85
  }
1✔
86

87
  /**
88
   * @return the name of the tool (e.g. "java", "mvn", "npm", "node").
89
   */
90
  @Override
91
  public final String getName() {
92

93
    return this.tool;
3✔
94
  }
95

96
  /**
97
   * @return the name of the binary executable for this tool.
98
   */
99
  protected String getBinaryName() {
100

101
    return this.tool;
3✔
102
  }
103

104
  /**
105
   * @return the {@link Path} to the installed {@link #getBinaryName() binary} or {@code null} if not found on the
106
   *     {@link com.devonfw.tools.ide.common.SystemPath}.
107
   */
108
  protected Path getBinaryExecutable() {
109

110
    Path binary = this.context.getPath().findBinaryPathByName(getBinaryName());
×
111
    if (binary.getParent() == null) {
×
112
      return null;
×
113
    }
114
    return binary;
×
115
  }
116

117
  @Override
118
  public final Set<Tag> getTags() {
119

120
    return this.tags;
3✔
121
  }
122

123
  /**
124
   * @return the execution directory where the tool will be executed. Will be {@code null} by default leading to execution in the users current working
125
   *     directory where IDEasy was called.
126
   * @see #setExecutionDirectory(Path)
127
   */
128
  public Path getExecutionDirectory() {
129
    return this.executionDirectory;
×
130
  }
131

132
  /**
133
   * @param executionDirectory the new value of {@link #getExecutionDirectory()}.
134
   */
135
  public void setExecutionDirectory(Path executionDirectory) {
136
    this.executionDirectory = executionDirectory;
×
137
  }
×
138

139
  /**
140
   * @return the {@link EnvironmentVariables#getToolVersion(String) tool version}.
141
   */
142
  public VersionIdentifier getConfiguredVersion() {
143

144
    return this.context.getVariables().getToolVersion(getName());
7✔
145
  }
146

147
  /**
148
   * @return the {@link EnvironmentVariables#getToolEdition(String) tool edition}.
149
   */
150
  public String getConfiguredEdition() {
151

152
    return this.context.getVariables().getToolEdition(getName());
7✔
153
  }
154

155
  /**
156
   * @return the {@link ToolEdition} with {@link #getName() tool} with its {@link #getConfiguredEdition() edition}.
157
   */
158
  protected final ToolEdition getToolWithConfiguredEdition() {
159

160
    return new ToolEdition(this.tool, getConfiguredEdition());
8✔
161
  }
162

163
  @Override
164
  protected void doRun() {
165

166
    runTool(this.arguments.asList());
6✔
167
  }
1✔
168

169
  /**
170
   * @param args the command-line arguments to run the tool.
171
   * @return the {@link ProcessResult result}.
172
   * @see ToolCommandlet#runTool(ProcessMode, GenericVersionRange, List)
173
   */
174
  public ProcessResult runTool(List<String> args) {
175

176
    return runTool(ProcessMode.DEFAULT, null, args);
6✔
177
  }
178

179
  /**
180
   * Ensures the tool is installed and then runs this tool with the given arguments.
181
   *
182
   * @param processMode the {@link ProcessMode}. Should typically be {@link ProcessMode#DEFAULT} or {@link ProcessMode#BACKGROUND}.
183
   * @param toolVersion the explicit {@link GenericVersionRange version} to run. Typically {@code null} to run the
184
   *     {@link #getConfiguredVersion() configured version}. Otherwise, the specified version will be used (from the software repository, if not compatible).
185
   * @param args the command-line arguments to run the tool.
186
   * @return the {@link ProcessResult result}.
187
   */
188
  public final ProcessResult runTool(ProcessMode processMode, GenericVersionRange toolVersion, List<String> args) {
189

190
    return runTool(processMode, toolVersion, ProcessErrorHandling.THROW_CLI, args);
7✔
191
  }
192

193
  /**
194
   * Ensures the tool is installed and then runs this tool with the given arguments.
195
   *
196
   * @param processMode the {@link ProcessMode}. Should typically be {@link ProcessMode#DEFAULT} or {@link ProcessMode#BACKGROUND}.
197
   * @param toolVersion the explicit {@link GenericVersionRange version} to run. Typically {@code null} to run the
198
   *     {@link #getConfiguredVersion() configured version}. Otherwise, the specified version will be used (from the software repository, if not compatible).
199
   * @param errorHandling the {@link ProcessErrorHandling}.
200
   * @param args the command-line arguments to run the tool.
201
   * @return the {@link ProcessResult result}.
202
   */
203
  public ProcessResult runTool(ProcessMode processMode, GenericVersionRange toolVersion, ProcessErrorHandling errorHandling, List<String> args) {
204

205
    ProcessContext pc = this.context.newProcess().errorHandling(errorHandling);
6✔
206
    ToolInstallRequest request = new ToolInstallRequest(true);
5✔
207
    if (toolVersion != null) {
2!
208
      request.setRequested(new ToolEditionAndVersion(toolVersion));
×
209
    }
210
    request.setProcessContext(pc);
3✔
211
    return runTool(request, processMode, args);
6✔
212
  }
213

214
  /**
215
   * Ensures the tool is installed and then runs this tool with the given arguments.
216
   *
217
   * @param request the {@link ToolInstallRequest}.
218
   * @param processMode the {@link ProcessMode}. Should typically be {@link ProcessMode#DEFAULT} or {@link ProcessMode#BACKGROUND}.
219
   * @param args the command-line arguments to run the tool.
220
   * @return the {@link ProcessResult result}.
221
   */
222
  public ProcessResult runTool(ToolInstallRequest request, ProcessMode processMode, List<String> args) {
223

224
    if (request.isCveCheckDone()) {
3!
225
      // if the CVE check has already been done, we can assume that the install(request) has already been called before
226
      // most likely a postInstall* method was overridden calling this method with the same request what is a programming error
227
      // we render this warning so the error gets detected and can be fixed but we do not block the user by skipping the installation.
228
      LOG.warn("Preventing infinity loop during installation of {}", request.getRequested(), new RuntimeException());
×
229
    } else {
230
      install(request);
4✔
231
    }
232
    return runTool(request.getProcessContext(), processMode, args);
7✔
233
  }
234

235
  /**
236
   * @param pc the {@link ProcessContext}.
237
   * @param processMode the {@link ProcessMode}. Should typically be {@link ProcessMode#DEFAULT} or {@link ProcessMode#BACKGROUND}.
238
   * @param args the command-line arguments to run the tool.
239
   * @return the {@link ProcessResult result}.
240
   */
241
  public ProcessResult runTool(ProcessContext pc, ProcessMode processMode, List<String> args) {
242

243
    if (this.executionDirectory != null) {
3!
244
      pc.directory(this.executionDirectory);
×
245
    }
246
    configureToolBinary(pc, processMode);
4✔
247
    configureToolArgs(pc, processMode, args);
5✔
248
    return pc.run(processMode);
4✔
249
  }
250

251
  /**
252
   * @param pc the {@link ProcessContext}.
253
   * @param processMode the {@link ProcessMode}.
254
   */
255
  protected void configureToolBinary(ProcessContext pc, ProcessMode processMode) {
256

257
    pc.executable(Path.of(getBinaryName()));
8✔
258
  }
1✔
259

260
  /**
261
   * @param pc the {@link ProcessContext}.
262
   * @param processMode the {@link ProcessMode}.
263
   * @param args the command-line arguments to {@link ProcessContext#addArgs(List) add}.
264
   */
265
  protected void configureToolArgs(ProcessContext pc, ProcessMode processMode, List<String> args) {
266

267
    pc.addArgs(args);
4✔
268
  }
1✔
269

270
  /**
271
   * Installs or updates the managed {@link #getName() tool}.
272
   *
273
   * @return the {@link ToolInstallation}.
274
   */
275
  public ToolInstallation install() {
276

277
    return install(true);
4✔
278
  }
279

280
  /**
281
   * Performs the installation of the {@link #getName() tool} managed by this {@link com.devonfw.tools.ide.commandlet.Commandlet}.
282
   *
283
   * @param silent - {@code true} if called recursively to suppress verbose logging, {@code false} otherwise.
284
   * @return the {@link ToolInstallation}.
285
   */
286
  public ToolInstallation install(boolean silent) {
287
    return install(new ToolInstallRequest(silent));
7✔
288
  }
289

290
  /**
291
   * Performs the installation (install, update, downgrade) of the {@link #getName() tool} managed by this {@link ToolCommandlet}.
292
   *
293
   * @param request the {@link ToolInstallRequest}.
294
   * @return the {@link ToolInstallation}.
295
   */
296
  public ToolInstallation install(ToolInstallRequest request) {
297

298
    completeRequest(request);
3✔
299
    if (request.isInstallLoop()) {
3!
300
      return toolAlreadyInstalled(request);
×
301
    }
302
    return doInstall(request);
4✔
303
  }
304

305
  /**
306
   * Performs the installation (install, update, downgrade) of the {@link #getName() tool} managed by this {@link ToolCommandlet}.
307
   *
308
   * @param request the {@link ToolInstallRequest}.
309
   * @return the {@link ToolInstallation}.
310
   */
311
  protected abstract ToolInstallation doInstall(ToolInstallRequest request);
312

313
  /**
314
   * @param request the {@link ToolInstallRequest} to complete (fill values that are currently {@code null}).
315
   */
316
  protected void completeRequest(ToolInstallRequest request) {
317

318
    completeRequestInstalled(request);
3✔
319
    completeRequestRequested(request); // depends on completeRequestInstalled
3✔
320
    completeRequestProcessContext(request);
3✔
321
    completeRequestToolPath(request);
3✔
322
  }
1✔
323

324
  private void completeRequestProcessContext(ToolInstallRequest request) {
325
    if (request.getProcessContext() == null) {
3✔
326
      ProcessContext pc = this.context.newProcess().errorHandling(ProcessErrorHandling.THROW_CLI);
6✔
327
      request.setProcessContext(pc);
3✔
328
    }
329
  }
1✔
330

331
  private void completeRequestInstalled(ToolInstallRequest request) {
332

333
    ToolEditionAndVersion installedToolVersion = request.getInstalled();
3✔
334
    if (installedToolVersion == null) {
2✔
335
      installedToolVersion = new ToolEditionAndVersion((GenericVersionRange) null);
6✔
336
      request.setInstalled(installedToolVersion);
3✔
337
    }
338
    Path toolPath = request.getToolPath();
3✔
339
    if (installedToolVersion.getVersion() == null) {
3✔
340
      VersionIdentifier installedVersion;
341
      if ((toolPath != null) && (this instanceof LocalToolCommandlet ltc)) {
10!
342
        installedVersion = ltc.getInstalledVersion(toolPath);
5✔
343
      } else {
344
        installedVersion = getInstalledVersion();
3✔
345
      }
346
      if (installedVersion == null) {
2✔
347
        return;
1✔
348
      }
349
      installedToolVersion.setVersion(installedVersion);
3✔
350
    }
351
    if (installedToolVersion.getEdition() == null) {
3✔
352
      String installedEdition;
353
      if ((toolPath != null) && (this instanceof LocalToolCommandlet ltc)) {
2!
354
        installedEdition = ltc.getInstalledEdition(toolPath);
×
355
      } else {
356
        installedEdition = getInstalledEdition();
3✔
357
      }
358
      installedToolVersion.setEdition(new ToolEdition(this.tool, installedEdition));
8✔
359
    }
360
    assert installedToolVersion.getResolvedVersion() != null;
4!
361
  }
1✔
362

363
  private void completeRequestRequested(ToolInstallRequest request) {
364

365
    ToolEdition edition;
366
    ToolEditionAndVersion requested = request.getRequested();
3✔
367
    if (requested == null) {
2✔
368
      edition = new ToolEdition(this.tool, getConfiguredEdition());
8✔
369
      requested = new ToolEditionAndVersion(edition);
5✔
370
      request.setRequested(requested);
4✔
371
    } else {
372
      edition = requested.getEdition();
3✔
373
      if (edition == null) {
2✔
374
        edition = new ToolEdition(this.tool, getConfiguredEdition());
8✔
375
        requested.setEdition(edition);
3✔
376
      }
377
    }
378
    GenericVersionRange version = requested.getVersion();
3✔
379
    if (version == null) {
2✔
380
      version = getConfiguredVersion();
3✔
381
      requested.setVersion(version);
3✔
382
    }
383
    VersionIdentifier resolvedVersion = requested.getResolvedVersion();
3✔
384
    if (resolvedVersion == null) {
2✔
385
      if (this.context.isSkipUpdatesMode()) {
4✔
386
        ToolEditionAndVersion installed = request.getInstalled();
3✔
387
        if (installed != null) {
2!
388
          VersionIdentifier installedVersion = installed.getResolvedVersion();
3✔
389
          if (version.contains(installedVersion)) {
4✔
390
            resolvedVersion = installedVersion;
2✔
391
          }
392
        }
393
      }
394
      if (resolvedVersion == null) {
2✔
395
        resolvedVersion = getToolRepository().resolveVersion(this.tool, edition.edition(), version, this);
10✔
396
      }
397
      requested.setResolvedVersion(resolvedVersion);
3✔
398
    }
399
  }
1✔
400

401
  private void completeRequestToolPath(ToolInstallRequest request) {
402

403
    Path toolPath = request.getToolPath();
3✔
404
    if (toolPath == null) {
2✔
405
      toolPath = getToolPath();
3✔
406
      request.setToolPath(toolPath);
3✔
407
    }
408
  }
1✔
409

410
  /**
411
   * @return the {@link Path} where the tool is located (installed). Will be {@code null} for global tools that do not know the {@link Path} since it is
412
   *     determined by the installer.
413
   */
414
  public Path getToolPath() {
415
    return null;
×
416
  }
417

418
  /**
419
   * This method is called after a tool was requested to be installed or updated.
420
   *
421
   * @param request {@code true} the {@link ToolInstallRequest}.
422
   */
423
  protected void postInstall(ToolInstallRequest request) {
424

425
    if (!request.isAlreadyInstalled()) {
3✔
426
      postInstallOnNewInstallation(request);
3✔
427
    }
428
  }
1✔
429

430
  /**
431
   * This method is called after a tool was requested to be installed or updated and a new installation was performed.
432
   *
433
   * @param request {@code true} the {@link ToolInstallRequest}.
434
   */
435
  protected void postInstallOnNewInstallation(ToolInstallRequest request) {
436

437
    // nothing to do by default
438
  }
1✔
439

440
  /**
441
   * @param edition the {@link #getInstalledEdition() edition}.
442
   * @param version the {@link #getInstalledVersion() version}.
443
   * @return the {@link Path} where this tool is installed (physically) or {@code null} if not available.
444
   */
445
  protected abstract Path getInstallationPath(String edition, VersionIdentifier version);
446

447
  /**
448
   * @param request the {@link ToolInstallRequest}.
449
   * @return the existing {@link ToolInstallation}.
450
   */
451
  protected ToolInstallation createExistingToolInstallation(ToolInstallRequest request) {
452

453
    ToolEditionAndVersion installed = request.getInstalled();
3✔
454

455
    String edition = this.tool;
3✔
456
    VersionIdentifier resolvedVersion = VersionIdentifier.LATEST;
2✔
457

458
    if (installed != null) {
2!
459
      if (installed.getEdition() != null) {
3!
460
        edition = installed.getEdition().edition();
4✔
461
      }
462
      if (installed.getResolvedVersion() != null) {
3!
463
        resolvedVersion = installed.getResolvedVersion();
3✔
464
      }
465
    }
466

467
    return createExistingToolInstallation(edition, resolvedVersion, request.getProcessContext(),
8✔
468
        request.isAdditionalInstallation());
1✔
469
  }
470

471
  /**
472
   * @param edition the {@link #getConfiguredEdition() edition}.
473
   * @param installedVersion the {@link #getConfiguredVersion() version}.
474
   * @param environmentContext the {@link EnvironmentContext}.
475
   * @param extraInstallation {@code true} if the {@link ToolInstallation} is an additional installation to the
476
   *     {@link #getConfiguredVersion() configured version} due to a conflicting version of a {@link ToolDependency}, {@code false} otherwise.
477
   * @return the {@link ToolInstallation}.
478
   */
479
  protected ToolInstallation createExistingToolInstallation(String edition, VersionIdentifier installedVersion, EnvironmentContext environmentContext,
480
      boolean extraInstallation) {
481

482
    Path installationPath = getInstallationPath(edition, installedVersion);
5✔
483
    return createToolInstallation(installationPath, installedVersion, false, environmentContext, extraInstallation);
8✔
484
  }
485

486
  /**
487
   * @param rootDir the {@link ToolInstallation#rootDir() top-level installation directory}.
488
   * @param version the installed {@link VersionIdentifier}.
489
   * @param newInstallation {@link ToolInstallation#newInstallation() new installation} flag.
490
   * @param environmentContext the {@link EnvironmentContext}.
491
   * @param additionalInstallation {@code true} if the {@link ToolInstallation} is an additional installation to the
492
   *     {@link #getConfiguredVersion() configured version} due to a conflicting version of a {@link ToolDependency}, {@code false} otherwise.
493
   * @return the {@link ToolInstallation}.
494
   */
495
  protected ToolInstallation createToolInstallation(Path rootDir, VersionIdentifier version, boolean newInstallation,
496
      EnvironmentContext environmentContext, boolean additionalInstallation) {
497

498
    Path linkDir = rootDir;
2✔
499
    Path binDir = rootDir;
2✔
500
    if (rootDir != null) {
2!
501
      // on MacOS applications have a very strange structure - see JavaDoc of findLinkDir and ToolInstallation.linkDir for details.
502
      binDir = this.context.getFileAccess().getBinPath(getMacOsHelper().findLinkDir(rootDir, getBinaryName()));
11✔
503
    }
504
    return createToolInstallation(rootDir, linkDir, binDir, version, newInstallation, environmentContext, additionalInstallation);
10✔
505
  }
506

507
  /**
508
   * @param rootDir the {@link ToolInstallation#rootDir() top-level installation directory}.
509
   * @param linkDir the {@link ToolInstallation#linkDir() link directory}.
510
   * @param binDir the {@link ToolInstallation#binDir() bin directory}.
511
   * @param version the installed {@link VersionIdentifier}.
512
   * @param newInstallation {@link ToolInstallation#newInstallation() new installation} flag.
513
   * @param environmentContext the {@link EnvironmentContext}.
514
   * @param additionalInstallation {@code true} if the {@link ToolInstallation} is an additional installation to the
515
   *     {@link #getConfiguredVersion() configured version} due to a conflicting version of a {@link ToolDependency}, {@code false} otherwise.
516
   * @return the {@link ToolInstallation}.
517
   */
518
  protected ToolInstallation createToolInstallation(Path rootDir, Path linkDir, Path binDir, VersionIdentifier version, boolean newInstallation,
519
      EnvironmentContext environmentContext, boolean additionalInstallation) {
520

521
    ToolInstallation toolInstallation = new ToolInstallation(rootDir, linkDir, binDir, version, newInstallation);
9✔
522
    setEnvironment(environmentContext, toolInstallation, additionalInstallation);
5✔
523
    return toolInstallation;
2✔
524
  }
525

526
  /**
527
   * Called if the tool {@link ToolInstallRequest#isAlreadyInstalled() is already installed in the correct edition and version} so we can skip the
528
   * installation.
529
   *
530
   * @param request the {@link ToolInstallRequest}.
531
   * @return the {@link ToolInstallation}.
532
   */
533
  protected ToolInstallation toolAlreadyInstalled(ToolInstallRequest request) {
534

535
    logToolAlreadyInstalled(request);
3✔
536
    cveCheck(request);
4✔
537
    postInstall(request);
3✔
538
    return createExistingToolInstallation(request);
4✔
539
  }
540

541
  /**
542
   * Log that the tool is already installed.
543
   *
544
   * @param request the {@link ToolInstallRequest}.
545
   */
546
  protected void logToolAlreadyInstalled(ToolInstallRequest request) {
547
    Level level;
548
    if (request.isSilent()) {
3✔
549
      level = Level.DEBUG;
3✔
550
    } else {
551
      level = Level.INFO;
2✔
552
    }
553
    ToolEditionAndVersion installed = request.getInstalled();
3✔
554
    LOG.atLevel(level).log("Version {} of tool {} is already installed", installed.getVersion(), installed.getEdition());
9✔
555
  }
1✔
556

557
  /**
558
   * Method to get the home path of the given {@link ToolInstallation}.
559
   *
560
   * @param toolInstallation the {@link ToolInstallation}.
561
   * @return the Path to the home of the tool
562
   */
563
  protected Path getToolHomePath(ToolInstallation toolInstallation) {
564
    return toolInstallation.linkDir();
3✔
565
  }
566

567
  /**
568
   * Method to set environment variables for the process context.
569
   *
570
   * @param environmentContext the {@link EnvironmentContext} where to {@link EnvironmentContext#withEnvVar(String, String) set environment variables} for
571
   *     this tool.
572
   * @param toolInstallation the {@link ToolInstallation}.
573
   * @param additionalInstallation {@code true} if the {@link ToolInstallation} is an additional installation to the
574
   *     {@link #getConfiguredVersion() configured version} due to a conflicting version of a {@link ToolDependency}, {@code false} otherwise.
575
   */
576
  public void setEnvironment(EnvironmentContext environmentContext, ToolInstallation toolInstallation, boolean additionalInstallation) {
577

578
    String pathVariable = EnvironmentVariables.getToolVariablePrefix(this.tool) + "_HOME";
5✔
579
    Path toolHomePath = getToolHomePath(toolInstallation);
4✔
580
    if (toolHomePath != null) {
2!
581
      environmentContext.withEnvVar(pathVariable, toolHomePath.toString());
6✔
582
    }
583
    if (additionalInstallation) {
2✔
584
      environmentContext.withPathEntry(toolInstallation.binDir());
5✔
585
    }
586
  }
1✔
587

588
  /**
589
   * @return {@code true} to extract (unpack) the downloaded binary file, {@code false} otherwise.
590
   */
591
  protected boolean isExtract() {
592

593
    return true;
2✔
594
  }
595

596
  /**
597
   * Checks a version to be installed for {@link Cve}s. If at least one {@link Cve} is found, we try to find better/safer versions as alternative. If we find
598
   * something better, we will suggest this to the user and ask him to make his choice.
599
   *
600
   * @param request the {@link ToolInstallRequest}.
601
   * @return the {@link VersionIdentifier} to install. The will may be asked (unless {@code skipSuggestions} is {@code true}) and might choose a different
602
   *     version than the originally requested one.
603
   */
604
  protected VersionIdentifier cveCheck(ToolInstallRequest request) {
605

606
    ToolEditionAndVersion requested = request.getRequested();
3✔
607
    VersionIdentifier resolvedVersion = requested.getResolvedVersion();
3✔
608
    if (request.isCveCheckDone()) {
3✔
609
      return resolvedVersion;
2✔
610
    }
611
    ToolEdition toolEdition = requested.getEdition();
3✔
612
    GenericVersionRange allowedVersions = requested.getVersion();
3✔
613
    boolean requireStableVersion = true;
2✔
614
    if (allowedVersions instanceof VersionIdentifier vi) {
6✔
615
      requireStableVersion = vi.isStable();
3✔
616
    }
617
    ToolSecurity toolSecurity = this.context.getDefaultToolRepository().findSecurity(this.tool, toolEdition.edition());
9✔
618
    double minSeverity = IdeVariables.CVE_MIN_SEVERITY.get(context);
7✔
619
    ToolVulnerabilities currentVulnerabilities = toolSecurity.findCves(resolvedVersion, minSeverity);
5✔
620
    ToolVersionChoice currentChoice = ToolVersionChoice.ofCurrent(requested, currentVulnerabilities);
4✔
621
    request.setCveCheckDone();
2✔
622
    if (currentChoice.logAndCheckIfEmpty()) {
3✔
623
      return resolvedVersion;
2✔
624
    }
625
    boolean alreadyInstalled = request.isAlreadyInstalled();
3✔
626
    boolean directForceInstall = this.context.isForceMode() && request.isDirect();
6!
627
    if (alreadyInstalled && !directForceInstall) {
2!
628
      // currently for a transitive dependency it does not make sense to suggest alternative versions, since the choice is not stored anywhere,
629
      // and we then would ask the user again every time the tool having this dependency is started. So we only log the problem and the user needs to react
630
      // (e.g. upgrade the tool with the dependency that is causing this).
631
      IdeLogLevel.INTERACTION.log(LOG, "Please run 'ide -f install {}' to check for update suggestions!", this.tool);
×
632
      return resolvedVersion;
×
633
    }
634
    ToolVersionChoice latest = null;
2✔
635
    ToolVulnerabilities latestVulnerabilities = currentVulnerabilities;
2✔
636
    ToolVersionChoice nearest = null;
2✔
637
    ToolVulnerabilities nearestVulnerabilities = currentVulnerabilities;
2✔
638
    List<VersionIdentifier> toolVersions = getVersions();
3✔
639
    for (VersionIdentifier version : toolVersions) {
10✔
640

641
      if (Objects.equals(version, resolvedVersion)) {
4✔
642
        continue; // Skip the entire iteration for resolvedVersion
1✔
643
      }
644

645
      if (acceptVersion(version, allowedVersions, requireStableVersion)) {
5!
646
        ToolVulnerabilities newVulnerabilities = toolSecurity.findCves(version, minSeverity);
5✔
647
        if (newVulnerabilities.isSafer(latestVulnerabilities)) {
4✔
648
          // we found a better/safer version
649
          ToolEditionAndVersion toolEditionAndVersion = new ToolEditionAndVersion(toolEdition, version);
6✔
650
          if (version.isGreater(resolvedVersion)) {
4!
651
            latestVulnerabilities = newVulnerabilities;
2✔
652
            latest = ToolVersionChoice.ofLatest(toolEditionAndVersion, latestVulnerabilities);
4✔
653
            nearest = null;
3✔
654
          } else {
655
            nearestVulnerabilities = newVulnerabilities;
×
656
            nearest = ToolVersionChoice.ofNearest(toolEditionAndVersion, nearestVulnerabilities);
×
657
          }
658
        } else if (newVulnerabilities.isSaferOrEqual(nearestVulnerabilities)) {
5✔
659
          if (newVulnerabilities.isSafer(nearestVulnerabilities) || version.isGreater(resolvedVersion)) {
8!
660
            nearest = ToolVersionChoice.ofNearest(new ToolEditionAndVersion(toolEdition, version), newVulnerabilities);
8✔
661
          }
662
          nearestVulnerabilities = newVulnerabilities;
2✔
663
        }
664
      }
665
    }
1✔
666
    if ((latest == null) && (nearest == null)) {
2!
667
      LOG.warn(
×
668
          "Could not find any other version resolving your CVEs.\nPlease keep attention to this tool and consider updating as soon as security fixes are available.");
669
      if (alreadyInstalled) {
×
670
        // we came here via "ide -f install ..." but no alternative is available
671
        return resolvedVersion;
×
672
      }
673
    }
674
    List<ToolVersionChoice> choices = new ArrayList<>();
4✔
675
    choices.add(currentChoice);
4✔
676
    boolean addSuggestions;
677
    if (this.context.isForceMode() && request.isDirect()) {
4!
678
      addSuggestions = true;
×
679
    } else {
680
      List<String> skipCveFixTools = IdeVariables.SKIP_CVE_FIX.get(this.context);
6✔
681
      addSuggestions = !skipCveFixTools.contains(this.tool);
8!
682
    }
683
    if (nearest != null) {
2!
684
      if (addSuggestions) {
2!
685
        choices.add(nearest);
4✔
686
      }
687
      nearest.logAndCheckIfEmpty();
3✔
688
    }
689
    if (latest != null) {
2!
690
      if (addSuggestions) {
2!
691
        choices.add(latest);
4✔
692
      }
693
      latest.logAndCheckIfEmpty();
3✔
694
    }
695
    ToolVersionChoice[] choicesArray = choices.toArray(ToolVersionChoice[]::new);
8✔
696
    LOG.warn("Please note that by selecting an unsafe version to install, you accept the risk to be attacked.");
3✔
697
    ToolVersionChoice answer = this.context.question(choicesArray, "Which version do you want to install?");
9✔
698
    VersionIdentifier version = answer.toolEditionAndVersion().getResolvedVersion();
4✔
699
    requested.setResolvedVersion(version);
3✔
700
    return version;
2✔
701
  }
702

703
  private static boolean acceptVersion(VersionIdentifier version, GenericVersionRange allowedVersions, boolean requireStableVersion) {
704
    if (allowedVersions.isPattern() && !allowedVersions.contains(version)) {
3!
705
      return false;
×
706
    } else if (requireStableVersion && !version.isStable()) {
5!
707
      return false;
×
708
    }
709
    return true;
2✔
710
  }
711

712
  /**
713
   * @return the {@link MacOsHelper} instance.
714
   */
715
  protected MacOsHelper getMacOsHelper() {
716

717
    if (this.macOsHelper == null) {
3✔
718
      this.macOsHelper = new MacOsHelper(this.context);
7✔
719
    }
720
    return this.macOsHelper;
3✔
721
  }
722

723
  /**
724
   * @return the currently installed {@link VersionIdentifier version} of this tool or {@code null} if not installed.
725
   */
726
  public abstract VersionIdentifier getInstalledVersion();
727

728
  /**
729
   * @return {@code true} if this tool is installed, {@code false} otherwise.
730
   */
731
  public boolean isInstalled() {
732

733
    return getInstalledVersion() != null;
7✔
734
  }
735

736
  /**
737
   * @return the installed edition of this tool or {@code null} if not installed.
738
   */
739
  public abstract String getInstalledEdition();
740

741
  /**
742
   * Uninstalls the {@link #getName() tool}.
743
   */
744
  public abstract void uninstall();
745

746
  /**
747
   * @return the {@link ToolRepository}.
748
   */
749
  public ToolRepository getToolRepository() {
750

751
    return this.context.getDefaultToolRepository();
4✔
752
  }
753

754
  /**
755
   * List the available editions of this tool.
756
   */
757
  public void listEditions() {
758

759
    List<String> editions = getToolRepository().getSortedEditions(getName());
6✔
760
    for (String edition : editions) {
10✔
761
      LOG.info(edition);
3✔
762
    }
1✔
763
  }
1✔
764

765
  /**
766
   * List the available versions of this tool.
767
   */
768
  public void listVersions() {
769

770
    List<VersionIdentifier> versions = getToolRepository().getSortedVersions(getName(), getConfiguredEdition(), this);
9✔
771
    for (VersionIdentifier vi : versions) {
10✔
772
      LOG.info(vi.toString());
4✔
773
    }
1✔
774
  }
1✔
775

776
  /**
777
   * @return the {@link com.devonfw.tools.ide.tool.repository.DefaultToolRepository#getSortedVersions(String, String, ToolCommandlet) sorted versions} of this
778
   *     tool.
779
   */
780
  public List<VersionIdentifier> getVersions() {
781
    return getToolRepository().getSortedVersions(getName(), getConfiguredEdition(), this);
9✔
782
  }
783

784
  /**
785
   * Sets the tool version in the environment variable configuration file.
786
   *
787
   * @param version the version (pattern) to set.
788
   */
789
  public void setVersion(String version) {
790

791
    if ((version == null) || version.isBlank()) {
×
792
      throw new IllegalStateException("Version has to be specified!");
×
793
    }
794
    VersionIdentifier configuredVersion = VersionIdentifier.of(version);
×
795
    if (!configuredVersion.isPattern() && !configuredVersion.isValid()) {
×
796
      LOG.warn("Version {} seems to be invalid", version);
×
797
    }
798
    setVersion(configuredVersion, true);
×
799
  }
×
800

801
  /**
802
   * Sets the tool version in the environment variable configuration file.
803
   *
804
   * @param version the version to set. May also be a {@link VersionIdentifier#isPattern() version pattern}.
805
   * @param hint - {@code true} to print the installation hint, {@code false} otherwise.
806
   */
807
  public void setVersion(VersionIdentifier version, boolean hint) {
808

809
    setVersion(version, hint, null);
5✔
810
  }
1✔
811

812
  /**
813
   * Sets the tool version in the environment variable configuration file.
814
   *
815
   * @param version the version to set. May also be a {@link VersionIdentifier#isPattern() version pattern}.
816
   * @param hint - {@code true} to print the installation hint, {@code false} otherwise.
817
   * @param destination - the destination for the property to be set
818
   */
819
  public void setVersion(VersionIdentifier version, boolean hint, EnvironmentVariablesFiles destination) {
820

821
    String edition = getConfiguredEdition();
3✔
822
    ToolRepository toolRepository = getToolRepository();
3✔
823

824
    EnvironmentVariables variables = this.context.getVariables();
4✔
825
    if (destination == null) {
2✔
826
      //use default location
827
      destination = EnvironmentVariablesFiles.SETTINGS;
2✔
828
    }
829
    EnvironmentVariables settingsVariables = variables.getByType(destination.toType());
5✔
830
    String name = EnvironmentVariables.getToolVersionVariable(this.tool);
4✔
831

832
    toolRepository.resolveVersion(this.tool, edition, version, this); // verify that the version actually exists
8✔
833
    settingsVariables.set(name, version.toString(), false);
7✔
834
    settingsVariables.save();
2✔
835
    EnvironmentVariables declaringVariables = variables.findVariable(name);
4✔
836
    if ((declaringVariables != null) && (declaringVariables != settingsVariables)) {
5!
837
      LOG.warn("The variable {} is overridden in {}. Please remove the overridden declaration in order to make the change affect.", name,
5✔
838
          declaringVariables.getSource());
1✔
839
    }
840
    if (hint) {
2✔
841
      LOG.info("To install that version call the following command:");
3✔
842
      LOG.info("ide install {}", this.tool);
5✔
843
    }
844
  }
1✔
845

846
  /**
847
   * Sets the tool edition in the environment variable configuration file.
848
   *
849
   * @param edition the edition to set.
850
   */
851
  public void setEdition(String edition) {
852

853
    setEdition(edition, true);
4✔
854
  }
1✔
855

856
  /**
857
   * Sets the tool edition in the environment variable configuration file.
858
   *
859
   * @param edition the edition to set
860
   * @param hint - {@code true} to print the installation hint, {@code false} otherwise.
861
   */
862
  public void setEdition(String edition, boolean hint) {
863

864
    setEdition(edition, hint, null);
5✔
865
  }
1✔
866

867
  /**
868
   * Sets the tool edition in the environment variable configuration file.
869
   *
870
   * @param edition the edition to set
871
   * @param hint - {@code true} to print the installation hint, {@code false} otherwise.
872
   * @param destination - the destination for the property to be set
873
   */
874
  public void setEdition(String edition, boolean hint, EnvironmentVariablesFiles destination) {
875

876
    if ((edition == null) || edition.isBlank()) {
5!
877
      throw new IllegalStateException("Edition has to be specified!");
×
878
    }
879

880
    if (destination == null) {
2✔
881
      //use default location
882
      destination = EnvironmentVariablesFiles.SETTINGS;
2✔
883
    }
884

885
    if (!getToolRepository().getSortedEditions(this.tool).contains(edition)) {
8✔
886
      LOG.warn("Edition {} seems to be invalid", edition);
4✔
887
    }
888
    EnvironmentVariables variables = this.context.getVariables();
4✔
889
    EnvironmentVariables settingsVariables = variables.getByType(destination.toType());
5✔
890
    String name = EnvironmentVariables.getToolEditionVariable(this.tool);
4✔
891
    settingsVariables.set(name, edition, false);
6✔
892
    settingsVariables.save();
2✔
893

894
    LOG.info("{}={} has been set in {}", name, edition, settingsVariables.getSource());
18✔
895
    EnvironmentVariables declaringVariables = variables.findVariable(name);
4✔
896
    if ((declaringVariables != null) && (declaringVariables != settingsVariables)) {
5!
897
      LOG.warn("The variable {} is overridden in {}. Please remove the overridden declaration in order to make the change affect.", name,
5✔
898
          declaringVariables.getSource());
1✔
899
    }
900
    if (hint) {
2!
901
      LOG.info("To install that edition call the following command:");
3✔
902
      LOG.info("ide install {}", this.tool);
5✔
903
    }
904
  }
1✔
905

906
  /**
907
   * Runs the tool's help command to provide the user with usage information.
908
   */
909
  @Override
910
  public void printHelp(NlsBundle bundle) {
911

912
    super.printHelp(bundle);
3✔
913
    String toolHelpArgs = getToolHelpArguments();
3✔
914
    if (toolHelpArgs != null && getInstalledVersion() != null) {
5!
915
      ProcessContext pc = this.context.newProcess().errorHandling(ProcessErrorHandling.LOG_WARNING)
6✔
916
          .executable(Path.of(getBinaryName())).addArgs(toolHelpArgs);
13✔
917
      pc.run(ProcessMode.DEFAULT);
4✔
918
    }
919
  }
1✔
920

921
  /**
922
   * @return the tool's specific help command. Usually help, --help or -h. Return null if not applicable.
923
   */
924
  public String getToolHelpArguments() {
925

926
    return null;
×
927
  }
928

929
  /**
930
   * Creates a start script for the tool using the tool name.
931
   *
932
   * @param targetDir the {@link Path} of the installation where to create the script. If a "bin" sub-folder is present, the script will be created there
933
   *     instead.
934
   * @param binary name of the binary to execute from the start script.
935
   */
936
  protected void createStartScript(Path targetDir, String binary) {
937

938
    createStartScript(targetDir, binary, false);
×
939
  }
×
940

941
  /**
942
   * Creates a start script for the tool using the tool name.
943
   *
944
   * @param targetDir the {@link Path} of the installation where to create the script. If a "bin" sub-folder is present, the script will be created there
945
   *     instead.
946
   * @param binary name of the binary to execute from the start script.
947
   * @param background {@code true} to run the {@code binary} in background, {@code false} otherwise (foreground).
948
   */
949
  protected void createStartScript(Path targetDir, String binary, boolean background) {
950

951
    Path binFolder = targetDir.resolve("bin");
×
952
    if (!Files.exists(binFolder)) {
×
953
      if (this.context.getSystemInfo().isMac()) {
×
954
        MacOsHelper macOsHelper = getMacOsHelper();
×
955
        Path appDir = macOsHelper.findAppDir(targetDir);
×
956
        binFolder = macOsHelper.findLinkDir(appDir, binary);
×
957
      } else {
×
958
        binFolder = targetDir;
×
959
      }
960
      assert (Files.exists(binFolder));
×
961
    }
962
    Path bashFile = binFolder.resolve(getName());
×
963
    String bashFileContentStart = "#!/usr/bin/env bash\n\"$(dirname \"$0\")/";
×
964
    String bashFileContentEnd = "\" $@";
×
965
    if (background) {
×
966
      bashFileContentEnd += " &";
×
967
    }
968
    try {
969
      Files.writeString(bashFile, bashFileContentStart + binary + bashFileContentEnd);
×
970
    } catch (IOException e) {
×
971
      throw new RuntimeException(e);
×
972
    }
×
973
    assert (Files.exists(bashFile));
×
974
    context.getFileAccess().makeExecutable(bashFile);
×
975
  }
×
976

977
  @Override
978
  public void reset() {
979
    super.reset();
2✔
980
    this.executionDirectory = null;
3✔
981
  }
1✔
982

983
  /**
984
   * @param command the binary that will be searched in the PATH e.g. docker
985
   * @return true if the command is available to use
986
   */
987
  protected boolean isCommandAvailable(String command) {
988
    return this.context.getPath().hasBinaryOnPath(command);
×
989
  }
990

991
  /**
992
   * @param output the raw output string from executed command e.g. 'docker version'
993
   * @param pattern Regular Expression pattern that filters out the unnecessary texts.
994
   * @return version that has been processed.
995
   */
996
  protected VersionIdentifier resolveVersionWithPattern(String output, Pattern pattern) {
997
    Matcher matcher = pattern.matcher(output);
×
998

999
    if (matcher.find()) {
×
1000
      return VersionIdentifier.of(matcher.group(1));
×
1001
    } else {
1002
      return null;
×
1003
    }
1004
  }
1005

1006
  /**
1007
   * @deprecated directly log success message and then report success on step if not null.
1008
   */
1009
  @Deprecated
1010
  protected void success(Step step, String message, Object... args) {
1011

1012
    if (step == null) {
×
1013
      IdeLogLevel.SUCCESS.log(LOG, message, args);
×
1014
    } else {
1015
      step.success(message, args);
×
1016
    }
1017
  }
×
1018

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