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

devonfw / IDEasy / 24732790894

21 Apr 2026 04:01PM UTC coverage: 70.599% (+0.009%) from 70.59%
24732790894

push

github

web-flow
#451: mac gatekeeper quarantine removal (#1794)

Co-authored-by: Jörg Hohwiller <hohwille@users.noreply.github.com>

4325 of 6766 branches covered (63.92%)

Branch coverage included in aggregate %.

11197 of 15220 relevant lines covered (73.57%)

3.1 hits per line

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

73.45
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
      linkDir = getMacOsHelper().findLinkDir(rootDir, getBinaryName());
7✔
503
      binDir = this.context.getFileAccess().getBinPath(linkDir);
6✔
504
    }
505
    return createToolInstallation(rootDir, linkDir, binDir, version, newInstallation, environmentContext, additionalInstallation);
10✔
506
  }
507

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

522
    // do not copy the version file into macOS .app bundles: changing the bundle after codesigning breaks the seal.
523
    ToolInstallation toolInstallation = new ToolInstallation(rootDir, linkDir, binDir, version, newInstallation);
9✔
524
    setEnvironment(environmentContext, toolInstallation, additionalInstallation);
5✔
525
    return toolInstallation;
2✔
526
  }
527

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

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

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

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

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

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

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

595
    return true;
2✔
596
  }
597

598
  /**
599
   * 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
600
   * something better, we will suggest this to the user and ask him to make his choice.
601
   *
602
   * @param request the {@link ToolInstallRequest}.
603
   * @return the {@link VersionIdentifier} to install. The will may be asked (unless {@code skipSuggestions} is {@code true}) and might choose a different
604
   *     version than the originally requested one.
605
   */
606
  protected VersionIdentifier cveCheck(ToolInstallRequest request) {
607

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

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

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

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

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

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

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

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

735
    return getInstalledVersion() != null;
7✔
736
  }
737

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

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

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

753
    return this.context.getDefaultToolRepository();
4✔
754
  }
755

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

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

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

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

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

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

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

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

811
    setVersion(version, hint, null);
5✔
812
  }
1✔
813

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

823
    String edition = getConfiguredEdition();
3✔
824
    ToolRepository toolRepository = getToolRepository();
3✔
825

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

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

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

855
    setEdition(edition, true);
4✔
856
  }
1✔
857

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

866
    setEdition(edition, hint, null);
5✔
867
  }
1✔
868

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

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

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

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

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

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

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

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

928
    return null;
×
929
  }
930

931
  /**
932
   * Creates a start script for the tool using the tool name.
933
   *
934
   * @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
935
   *     instead.
936
   * @param binary name of the binary to execute from the start script.
937
   */
938
  protected void createStartScript(Path targetDir, String binary) {
939

940
    createStartScript(targetDir, binary, false);
×
941
  }
×
942

943
  /**
944
   * Creates a start script for the tool using the tool name.
945
   *
946
   * @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
947
   *     instead.
948
   * @param binary name of the binary to execute from the start script.
949
   * @param background {@code true} to run the {@code binary} in background, {@code false} otherwise (foreground).
950
   */
951
  protected void createStartScript(Path targetDir, String binary, boolean background) {
952

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

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

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

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

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

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

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

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