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

devonfw / IDEasy / 42210500687

06 Nov 2026 11:28AM UTC coverage: 75.383% (+0.08%) from 75.305%
42210500687

Pull #2642

github

web-flow
Merge 8b8032bc9 into 9081e8335
Pull Request #2642: #945: skip redundant installs and CVE checks for shared dependencies

6067 of 8928 branches covered (67.95%)

Branch coverage included in aggregate %.

15748 of 20011 relevant lines covered (78.7%)

3.38 hits per line

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

81.84
cli/src/main/java/com/devonfw/tools/ide/tool/AbstractLocalToolCommandlet.java
1
package com.devonfw.tools.ide.tool;
2

3
import java.nio.file.Files;
4
import java.nio.file.LinkOption;
5
import java.nio.file.Path;
6
import java.util.Collection;
7
import java.util.Set;
8

9
import org.slf4j.Logger;
10
import org.slf4j.LoggerFactory;
11

12
import com.devonfw.tools.ide.cli.CliException;
13
import com.devonfw.tools.ide.cli.CliOfflineException;
14
import com.devonfw.tools.ide.common.Tag;
15
import com.devonfw.tools.ide.context.IdeContext;
16
import com.devonfw.tools.ide.io.FileAccess;
17
import com.devonfw.tools.ide.log.IdeLogLevel;
18
import com.devonfw.tools.ide.process.ProcessContext;
19
import com.devonfw.tools.ide.step.Step;
20
import com.devonfw.tools.ide.url.model.file.json.ToolDependency;
21
import com.devonfw.tools.ide.version.GenericVersionRange;
22
import com.devonfw.tools.ide.version.VersionIdentifier;
23
import com.devonfw.tools.ide.version.VersionRange;
24

25
/**
26
 * {@link AbstractToolCommandlet} that is installed locally into the IDEasy.
27
 */
28
public abstract class AbstractLocalToolCommandlet extends AbstractToolCommandlet implements LocalToolCommandlet {
29

30
  private static final Logger LOG = LoggerFactory.getLogger(AbstractLocalToolCommandlet.class);
4 ✔
31

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

41
    super(context, tool, tags);
5 ✔
42
  }
1 ✔
43

44
  @Override
45
  public Path getToolPath() {
46
    if (this.context.getSoftwarePath() == null) {
4 ✔
47
      return null;
2 ✔
48
    }
49
    return this.context.getSoftwarePath().resolve(getName());
7 ✔
50
  }
51

52
  /**
53
   * @return the {@link Path} where the installed artifact of this tool actually lives, as reported in the install success message. Defaults to
54
   *     {@link #getToolPath() tool path}, which for package-manager based tools is the runtime tool's path and not the location where the managed package
55
   *     is actually installed (e.g. a global npm package lives in the per-project npm prefix, not in {@code software/node}).
56
   */
57
  protected Path getInstalledLocation() {
58

59
    return getToolPath();
3 ✔
60
  }
61

62
  /**
63
   * @return the {@link Path} where the executables of the tool can be found. Typically, a "bin" folder inside {@link #getToolPath() tool path}.
64
   */
65
  public Path getToolBinPath() {
66

67
    Path toolPath = getToolPath();
3 ✔
68
    if (toolPath == null) {
2 !
69
      return null;
×
70
    }
71
    Path binPath = toolPath.resolve(IdeContext.FOLDER_BIN);
4 ✔
72
    if (Files.isDirectory(binPath)) {
5 ✔
73
      return binPath;
2 ✔
74
    }
75
    return toolPath;
2 ✔
76
  }
77

78
  /**
79
   * @return {@code true} to ignore a missing {@link IdeContext#FILE_SOFTWARE_VERSION software version file} in an installation, {@code false} delete the broken
80
   *     installation (default).
81
   */
82
  protected boolean isIgnoreMissingSoftwareVersionFile() {
83

84
    return false;
×
85
  }
86

87
  @Override
88
  protected ToolInstallation doInstall(ToolInstallRequest request) {
89

90
    Step step = request.getStep();
3 ✔
91
    if (step == null) {
2 !
92
      return doInstallStep(request);
4 ✔
93
    } else {
94
      return step.call(() -> doInstallStep(request),
×
95
          () -> createExistingToolInstallation(request));
×
96
    }
97
  }
98

99
  private ToolInstallation doInstallStep(ToolInstallRequest request) {
100

101
    if (request.isIgnoreProject() && isIgnoreSoftwareRepo()) {
3 !
102
      throw new CliException(
×
103
          "The tool " + this.tool
104
              + " does not support the software repository and therefore cannot be installed while ignoring the project.");
105
    }
106

107
    // install configured version of our tool in the software repository if not already installed
108
    ToolInstallation installation = installTool(request);
4 ✔
109

110
    // check if we already have this version installed (linked) locally in IDE_HOME/software
111
    VersionIdentifier resolvedVersion = installation.resolvedVersion();
3 ✔
112
    if (request.isAlreadyInstalled()) {
3 ✔
113
      return installation;
2 ✔
114
    } else {
115
      LOG.debug("Installation from {} to {}.", request.getInstalled(), request.getRequested());
7 ✔
116
    }
117
    FileAccess fileAccess = this.context.getFileAccess();
4 ✔
118
    boolean ignoreSoftwareRepo = isIgnoreSoftwareRepo();
3 ✔
119
    Path toolPath = request.getToolPath();
3 ✔
120
    if (!ignoreSoftwareRepo && !request.isIgnoreProject() && toolPath != null) {
7 !
121
      // we need to link the version or update the link.
122
      if (Files.exists(toolPath, LinkOption.NOFOLLOW_LINKS)) {
9 ✔
123
        fileAccess.backup(toolPath);
4 ✔
124
      }
125
      fileAccess.mkdirs(toolPath.getParent());
4 ✔
126
      fileAccess.symlink(installation.linkDir(), toolPath);
6 ✔
127
    } else {
128
      LOG.debug("Skipping symlink creation for tool {} as it is installed in standalone/global mode (ignoreSoftwareRepo={}, ignoreProject={}, toolPath={}).",
13 ✔
129
          this.tool, ignoreSoftwareRepo, request.isIgnoreProject(), toolPath);
12 ✔
130
    }
131
    if (!request.isExtraInstallation() && (installation.binDir() != null)) {
6 !
132
      this.context.getPath().setPath(this.tool, installation.binDir());
8 ✔
133
    }
134
    postInstall(request);
3 ✔
135
    ToolEditionAndVersion installed = request.getInstalled();
3 ✔
136
    GenericVersionRange installedVersion = null;
2 ✔
137
    if (installed != null) {
2 !
138
      installedVersion = installed.getVersion();
3 ✔
139
    }
140
    ToolEditionAndVersion requested = request.getRequested();
3 ✔
141
    ToolEdition toolEdition = requested.getEdition();
3 ✔
142
    Step step = request.getStep();
3 ✔
143
    Path installedLocation = getInstalledLocation();
3 ✔
144
    if (installedVersion == null) {
2 ✔
145
      IdeLogLevel.SUCCESS.log(LOG, "Successfully installed {} in version {} at {}", toolEdition, resolvedVersion, installedLocation);
19 ✔
146
    } else {
147
      IdeLogLevel.SUCCESS.log(LOG, "Successfully installed {} in version {} replacing previous version {} of {} at {}", toolEdition,
21 ✔
148
          resolvedVersion, installedVersion, installed.getEdition(), installedLocation);
6 ✔
149
    }
150
    if (step != null) {
2 !
151
      step.success(true);
×
152
    }
153
    return installation;
2 ✔
154
  }
155

156
  /**
157
   * Determines whether this tool should be installed directly in the software folder or in the software repository.
158
   *
159
   * @return {@code true} if the tool should be installed directly in the software folder, ignoring the central software repository; {@code false} if the tool
160
   *     should be installed in the central software repository (default behavior).
161
   */
162
  protected boolean isIgnoreSoftwareRepo() {
163

164
    return false;
2 ✔
165
  }
166

167
  /**
168
   * Performs the installation of the {@link #getName() tool} together with the environment context  managed by this
169
   * {@link com.devonfw.tools.ide.commandlet.Commandlet}.
170
   *
171
   * @param request the {@link ToolInstallRequest}.
172
   * @return the resulting {@link ToolInstallation}.
173
   */
174
  public ToolInstallation installTool(ToolInstallRequest request) {
175

176
    completeRequest(request); // most likely already done, but if installTool was called directly and not from install
3 ✔
177
    if (request.isInstallLoop()) {
3 !
178
      return toolAlreadyInstalled(request);
×
179
    }
180
    ToolEditionAndVersion requested = request.getRequested();
3 ✔
181
    ToolEdition toolEdition = requested.getEdition();
3 ✔
182
    assert (toolEdition.tool().equals(this.tool)) : "Mismatch " + this.tool + " != " + toolEdition.tool();
7 !
183
    String edition = toolEdition.edition();
3 ✔
184
    // The CVE check runs once per tool+version per process (also covering the out-of-range path, which bypasses install() and calls this method directly).
185
    // Extra installations are excluded because they install their own fixed version and must be checked independently.
186
    ToolInstallTracker tracker = request.getTracker();
3 ✔
187
    VersionIdentifier resolvedVersion;
188
    if ((tracker != null) && !request.isExtraInstallation() && tracker.isCveChecked(toolEdition, requested.getResolvedVersion())) {
11 !
189
      LOG.debug("Skipping CVE check of {} (already done during this process).", requested);
4 ✔
190
      resolvedVersion = requested.getResolvedVersion();
4 ✔
191
    } else {
192
      resolvedVersion = cveCheck(request);
4 ✔
193
      if ((tracker != null) && !request.isExtraInstallation()) {
5 !
194
        tracker.recordCveChecked(toolEdition, resolvedVersion);
4 ✔
195
      }
196
    }
197
    installToolDependencies(request);
3 ✔
198
    // cveCheck might have changed resolvedVersion so let us re-check...
199
    if (request.isAlreadyInstalled()) {
3 ✔
200
      return toolAlreadyInstalled(request);
4 ✔
201
    } else {
202
      ToolEditionAndVersion installed = request.getInstalled();
3 ✔
203
      LOG.debug("Installation from {} to {}.", installed, requested);
5 ✔
204
    }
205
    Path installationPath = getInstallationPath(edition, resolvedVersion);
5 ✔
206

207
    ProcessContext processContext = request.getProcessContext();
3 ✔
208
    boolean additionalInstallation = request.isAdditionalInstallation();
3 ✔
209
    boolean ignoreSoftwareRepo = isIgnoreSoftwareRepo();
3 ✔
210
    Path toolVersionFile = installationPath.resolve(IdeContext.FILE_SOFTWARE_VERSION);
4 ✔
211
    FileAccess fileAccess = this.context.getFileAccess();
4 ✔
212
    if (Files.isDirectory(installationPath)) {
5 ✔
213
      if (Files.exists(toolVersionFile)) {
5 ✔
214
        if (!ignoreSoftwareRepo) {
2 ✔
215
          assert resolvedVersion.equals(getInstalledVersion(installationPath)) :
7 !
216
              "Found version " + getInstalledVersion(installationPath) + " in " + toolVersionFile + " but expected " + resolvedVersion;
×
217
          LOG.debug("Version {} of tool {} is already installed at {}", resolvedVersion, toolEdition, installationPath);
17 ✔
218
          return createToolInstallation(installationPath, resolvedVersion, false, processContext, additionalInstallation);
8 ✔
219
        }
220
      } else {
221
        // Makes sure that IDEasy will not delete itself
222
        if (this.tool.equals(IdeasyCommandlet.TOOL_NAME)) {
5 !
223
          LOG.warn("Your IDEasy installation is missing the version file at {}", toolVersionFile);
×
224
          return createToolInstallation(installationPath, resolvedVersion, false, processContext, additionalInstallation);
×
225
        } else if (!isIgnoreMissingSoftwareVersionFile()) {
3 !
226
          LOG.warn("Deleting corrupted installation at {}", installationPath);
×
227
          fileAccess.delete(installationPath);
×
228
        } else {
229
          // the version file is missing but the tool allows this (e.g. because the version is determined from the installation itself). If
230
          // the installed version could be determined, it was already restored to the version file during the determination of the installed
231
          // edition and version (see computeInstalledEditionAndVersionFromLocalSoftwareFolder) and the installation above is kept. Reaching
232
          // this point means the installed version could not be determined, so the installation is treated as broken and reinstalled.
233
          LOG.warn("Could not determine the installed version of {} at {}, the installation is considered broken and will be reinstalled.", this.tool,
6 ✔
234
              installationPath);
235
        }
236
      }
237
    }
238
    VersionIdentifier actualInstalledVersion = resolvedVersion;
2 ✔
239
    try {
240
      performToolInstallation(request, installationPath);
4 ✔
241
    } catch (CliOfflineException e) {
1 ✔
242
      // If we are offline and cannot download, check if we can continue with an existing installation
243
      ToolEditionAndVersion installed = request.getInstalled();
3 ✔
244
      if ((installed != null) && (installed.getResolvedVersion() != null)) {
5 !
245
        LOG.warn("Cannot download {} in version {} because we are offline. Continuing with already installed version {}.", this.tool,
17 ✔
246
            resolvedVersion, installed.getResolvedVersion());
2 ✔
247
        // If offline and could not download, actualInstalledVersion will be the old version, not resolvedVersion
248
        // In that case, we need to recalculate the installation path for the actually installed version
249
        actualInstalledVersion = installed.getResolvedVersion();
3 ✔
250
        installationPath = getInstallationPath(edition, actualInstalledVersion);
6 ✔
251
      } else {
252
        // No existing installation available, re-throw the exception
253
        throw e;
2 ✔
254
      }
255
    }
1 ✔
256
    return createToolInstallation(installationPath, actualInstalledVersion, true, processContext, additionalInstallation);
8 ✔
257
  }
258

259
  /**
260
   * Performs the installation of the {@link #getName() tool} by using {@link #installDownloadedToolPayload(ToolInstallRequest, Path, Path)} for tool-specific
261
   * logic, backing up any existing installation, and writing the version file.
262
   * <p>
263
   * This method assumes that the version has already been resolved and dependencies installed. It handles the final steps of placing the tool into the
264
   * appropriate installation directory.
265
   *
266
   * @param request the {@link ToolInstallRequest}.
267
   * @param installationPath the target {@link Path} where the {@link #getName() tool} should be installed.
268
   */
269
  protected void performToolInstallation(ToolInstallRequest request, Path installationPath) {
270

271
    FileAccess fileAccess = this.context.getFileAccess();
4 ✔
272
    ToolEditionAndVersion requested = request.getRequested();
3 ✔
273
    VersionIdentifier resolvedVersion = requested.getResolvedVersion();
3 ✔
274
    Path downloadedToolFile = downloadTool(requested.getEdition().edition(), resolvedVersion);
7 ✔
275

276
    if (Files.isDirectory(installationPath)) {
5 !
277
      if (this.tool.equals(IdeasyCommandlet.TOOL_NAME)) {
×
278
        LOG.warn("Your IDEasy installation is missing the version file.");
×
279
      } else {
280
        fileAccess.backup(installationPath);
×
281
      }
282
    }
283
    fileAccess.mkdirs(installationPath.getParent());
4 ✔
284

285
    installDownloadedToolPayload(request, installationPath, downloadedToolFile);
5 ✔
286

287
    this.context.writeVersionFile(resolvedVersion, installationPath);
5 ✔
288
    // fix macOS Gatekeeper blocking - must run after version file is written but before any executables are launched
289
    getMacOsHelper().removeQuarantineAttribute(installationPath);
4 ✔
290
    LOG.debug("Installed {} in version {} at {}", this.tool, resolvedVersion, installationPath);
18 ✔
291
  }
1 ✔
292

293
  /**
294
   * Performs the actual installation of the tool bits.
295
   *
296
   * @param request the {@link ToolInstallRequest}.
297
   * @param installationPath the target {@link Path} where the tool should be installed.
298
   * @param downloadedToolFile the {@link Path} to the downloaded tool file.
299
   */
300
  protected void installDownloadedToolPayload(ToolInstallRequest request, Path installationPath, Path downloadedToolFile) {
301

302
    boolean extract = isExtract();
3 ✔
303
    if (!extract) {
2 ✔
304
      LOG.trace("Extraction is disabled for '{}' hence just moving the downloaded file {}.", this.tool, downloadedToolFile);
6 ✔
305
    }
306
    this.context.getFileAccess().extract(downloadedToolFile, installationPath, this::postExtract, extract);
9 ✔
307
  }
1 ✔
308

309
  /**
310
   * @param edition the {@link #getConfiguredEdition() tool edition} to download.
311
   * @param resolvedVersion the resolved {@link VersionIdentifier version} to download.
312
   * @return the {@link Path} to the downloaded release file.
313
   */
314
  protected Path downloadTool(String edition, VersionIdentifier resolvedVersion) {
315
    return getToolRepository().download(this.tool, edition, resolvedVersion, this);
9 ✔
316
  }
317

318
  /**
319
   * Install this tool as dependency of another tool.
320
   *
321
   * @param versionRange the required {@link VersionRange}. See {@link ToolDependency#versionRange()}.
322
   * @param parentRequest the {@link ToolInstallRequest} of the tool causing this dependency.
323
   * @return the corresponding {@link ToolInstallation}.
324
   */
325
  public ToolInstallation installAsDependency(VersionRange versionRange, ToolInstallRequest parentRequest) {
326
    ToolInstallRequest request = new ToolInstallRequest(parentRequest);
5 ✔
327
    ToolEditionAndVersion requested = new ToolEditionAndVersion(getToolWithConfiguredEdition());
6 ✔
328
    request.setRequested(requested);
3 ✔
329

330
    // If we are not inside a project or if we should ignore the project, do not try to use the configured version
331
    // but instead just use the version range from the dependency.
332
    boolean ignoreProject = request.isIgnoreProject() || this.context.getIdeHome() == null;
11 !
333
    VersionIdentifier configuredVersion = ignoreProject ? null : getConfiguredVersion();
7 ✔
334
    if (!ignoreProject && (configuredVersion != null) && versionRange.contains(configuredVersion)) {
8 !
335
      // prefer configured version if contained in version range
336
      requested.setVersion(configuredVersion);
3 ✔
337
      // return install(true, configuredVersion, processContext, null);
338
      return install(request);
4 ✔
339
    } else {
340
      if (ignoreProject) {
2 ✔
341
        LOG.debug("Ignoring project for dependency {} of tool {}, using version range {}", this.tool, parentRequest.getRequested(), versionRange);
20 ✔
342
      } else if (isIgnoreSoftwareRepo()) {
3 !
343
        throw new IllegalStateException(
×
344
            "Cannot satisfy dependency to " + this.tool + " in version " + versionRange + " for " + parentRequest.getRequested()
×
345
                + " since it is conflicting with configured version "
346
                + configuredVersion
347
                + " and this tool does not support the software repository.");
348
      } else {
349
        LOG.info(
8 ✔
350
            "The tool {} requires {} in the version range {}, but your project uses version {}, which does not match."
351
                + " Therefore, we install a compatible version in that range.",
352
            parentRequest.getRequested().getEdition(), this.tool, versionRange, configuredVersion);
16 ✔
353
      }
354
      requested.setVersion(versionRange);
3 ✔
355
      // Intentionally call installTool instead of install: an additional installation is installed into the software repository
356
      // at a version resolved from the range and must never be skipped by the ToolInstallTracker, which tracks the project version.
357
      return installTool(request);
4 ✔
358
    }
359
  }
360

361
  /**
362
   * Installs the tool dependencies for the current tool.
363
   *
364
   * @param request the {@link ToolInstallRequest}.
365
   */
366
  protected void installToolDependencies(ToolInstallRequest request) {
367

368
    ToolEditionAndVersion requested = request.getRequested();
3 ✔
369
    VersionIdentifier version = requested.getResolvedVersion();
3 ✔
370
    ToolEdition toolEdition = requested.getEdition();
3 ✔
371
    Collection<ToolDependency> dependencies = getToolRepository().findDependencies(this.tool, toolEdition.edition(), version);
9 ✔
372
    int size = dependencies.size();
3 ✔
373
    LOG.debug("Tool {} has {} other tool(s) as dependency", toolEdition, size);
6 ✔
374
    for (ToolDependency dependency : dependencies) {
10 ✔
375
      LOG.trace("Ensuring dependency {} for tool {}", dependency.tool(), toolEdition);
6 ✔
376
      AbstractLocalToolCommandlet dependencyTool = this.context.getCommandletManager().getRequiredLocalToolCommandlet(dependency.tool());
7 ✔
377
      dependencyTool.installAsDependency(dependency.versionRange(), request);
6 ✔
378
    }
1 ✔
379
  }
1 ✔
380

381
  /**
382
   * Post-extraction hook that can be overridden to add custom processing after unpacking and before moving to the final destination folder.
383
   *
384
   * @param extractedDir the {@link Path} to the folder with the unpacked tool.
385
   */
386
  protected void postExtract(Path extractedDir) {
387

388
  }
1 ✔
389

390
  /**
391
   * Restores the {@link IdeContext#FILE_SOFTWARE_VERSION version file} of an existing installation in case it got lost (e.g. because {@code uv} recreated the
392
   * python virtual environment). Does nothing if the file is present or the version is unknown.
393
   *
394
   * @param installationPath the {@link Path} to the installation of this tool.
395
   * @param version the {@link VersionIdentifier} that is actually installed at the given {@code installationPath}.
396
   */
397
  protected void restoreMissingVersionFile(Path installationPath, VersionIdentifier version) {
398

399
    if ((version == null) || !Files.isDirectory(installationPath)) {
7 !
400
      return;
×
401
    }
402
    Path toolVersionFile = installationPath.resolve(IdeContext.FILE_SOFTWARE_VERSION);
4 ✔
403
    if (Files.exists(toolVersionFile)) {
5 !
404
      return;
×
405
    }
406
    LOG.warn("Version file is missing at {} - restoring it with version {} for tool {}", toolVersionFile, version, this.tool);
18 ✔
407
    this.context.writeVersionFile(version, installationPath);
5 ✔
408
    // the installation on disk changed so a previously cached result must not survive
409
    invalidateInstalledEditionAndVersion();
2 ✔
410
  }
1 ✔
411

412
  @Override
413
  protected EditionAndVersion computeInstalledEditionAndVersion() {
414

415
    Path toolPath = getToolPath();
3 ✔
416
    if (isToolNotInstalled(toolPath)) {
4 ✔
417
      return null;
2 ✔
418
    }
419
    // Resolve edition and version from a single tool-path lookup (one pass) instead of two separate lookups.
420
    Path repoPath = getInstalledSoftwareRepoPath(toolPath, true);
5 ✔
421

422
    if (repoPath != null) {
2 ✔
423
      VersionIdentifier version = VersionIdentifier.of(repoPath.getFileName().toString());
5 ✔
424
      return new EditionAndVersion(getEditionFromSoftwareRepoPath(repoPath), version);
8 ✔
425
    }
426
    return computeInstalledEditionAndVersionFromLocalSoftwareFolder();
3 ✔
427
  }
428

429
  /**
430
   * Determines the installed edition and version of a tool that is not installed via the software repository.
431
   *
432
   * @return the installed {@link EditionAndVersion} or {@code null} if the version cannot be determined.
433
   */
434
  protected EditionAndVersion computeInstalledEditionAndVersionFromLocalSoftwareFolder() {
435

436
    Path toolPath = getToolPath();
3 ✔
437
    VersionIdentifier version = getInstalledVersion(toolPath);
4 ✔
438
    if (version == null) {
2 ✔
439
      version = computeInstalledVersionFromLocalSoftwareFolder();
3 ✔
440
      if (version == null) {
2 ✔
441
        return null;
2 ✔
442
      }
443
      restoreMissingVersionFile(toolPath, version);
4 ✔
444
    }
445
    return new EditionAndVersion(computeInstalledEditionFromLocalSoftwareFolder(), version);
7 ✔
446
  }
447

448
  /**
449
   * Determines the installed edition of a tool that is not installed via the software repository.
450
   *
451
   * @return the installed edition of this tool.
452
   */
453
  protected String computeInstalledEditionFromLocalSoftwareFolder() {
454
    return this.tool;
3 ✔
455
  }
456

457
  /**
458
   * Determines the installed version of a tool that is not installed via the software repository, in case the
459
   * {@link IdeContext#FILE_SOFTWARE_VERSION version file} is missing.
460
   * <p>
461
   * By default {@code null} is returned since there is no tool-specific way to determine the version without the version file. Subclasses may override this to
462
   * determine the version from the installation itself.
463
   *
464
   * @return the installed {@link VersionIdentifier version} or {@code null} if it cannot be determined.
465
   */
466
  protected VersionIdentifier computeInstalledVersionFromLocalSoftwareFolder() {
467
    return null;
×
468
  }
469

470
  /**
471
   * @param toolPath the installation {@link Path} where to find the version file.
472
   * @return the currently installed {@link VersionIdentifier version} of this tool or {@code null} if not installed.
473
   */
474
  protected VersionIdentifier getInstalledVersion(Path toolPath) {
475

476
    if (isToolNotInstalled(toolPath)) {
4 ✔
477
      return null;
2 ✔
478
    }
479
    Path versionLookupPath = getInstalledSoftwareRepoPath(toolPath, false);
5 ✔
480
    if (versionLookupPath == null) {
2 !
481
      versionLookupPath = toolPath;
2 ✔
482
    }
483
    Path toolVersionFile = versionLookupPath.resolve(IdeContext.FILE_SOFTWARE_VERSION);
4 ✔
484
    if (!Files.exists(toolVersionFile)) {
5 ✔
485
      Path legacyToolVersionFile = versionLookupPath.resolve(IdeContext.FILE_LEGACY_SOFTWARE_VERSION);
4 ✔
486
      if (Files.exists(legacyToolVersionFile)) {
5 ✔
487
        toolVersionFile = legacyToolVersionFile;
3 ✔
488
      } else {
489
        LOG.warn("Tool {} is missing version file in {}", getName(), toolVersionFile);
6 ✔
490
        return null;
2 ✔
491
      }
492
    }
493
    String version = this.context.getFileAccess().readFileContent(toolVersionFile).trim();
7 ✔
494
    return VersionIdentifier.of(version);
3 ✔
495
  }
496

497
  /**
498
   * @param toolPath the installation {@link Path} where to find currently installed tool. The name of the parent directory of the real path corresponding
499
   *     to the passed {@link Path path} must be the name of the edition.
500
   * @return the installed edition of this tool or {@code null} if not installed.
501
   */
502
  protected String getInstalledEdition(Path toolPath) {
503

504
    if (isToolNotInstalled(toolPath)) {
×
505
      return null;
×
506
    }
507
    Path repoPath = getInstalledSoftwareRepoPath(toolPath, true);
×
508
    if (repoPath == null) {
×
509
      return computeInstalledEditionFromLocalSoftwareFolder();
×
510
    }
511
    return getEditionFromSoftwareRepoPath(repoPath);
×
512
  }
513

514
  private String getEditionFromSoftwareRepoPath(Path repoPath) {
515

516
    String edition = repoPath.getParent().getFileName().toString();
5 ✔
517
    if (!getToolRepository().getSortedEditions(this.tool).contains(edition)) {
8 ✔
518
      LOG.warn("Undefined edition {} of tool {}", edition, this.tool);
6 ✔
519
    }
520
    return edition;
2 ✔
521
  }
522

523
  private Path getInstalledSoftwareRepoPath(Path toolPath, boolean logIfNotSoftwareRepo) {
524
    if (isToolNotInstalled(toolPath)) {
4 !
525
      return null;
×
526
    }
527
    Path installPath = this.context.getFileAccess().toRealPath(toolPath);
6 ✔
528
    // if the installPath changed, a link has been resolved
529
    if (installPath.equals(toolPath)) {
4 ✔
530
      if (logIfNotSoftwareRepo && !isIgnoreSoftwareRepo()) {
5 ✔
531
        LOG.warn("Tool {} is not installed via software repository (maybe from devonfw-ide). Please consider reinstalling it.", this.tool);
5 ✔
532
      }
533
      // I do not see any reliable way how we could determine the edition of a tool that does not use software repo or that was installed by devonfw-ide
534
      return null;
2 ✔
535
    }
536
    installPath = getValidInstalledSoftwareRepoPath(installPath, context.getSoftwareRepositoryPath());
7 ✔
537
    return installPath;
2 ✔
538
  }
539

540
  Path getValidInstalledSoftwareRepoPath(Path installPath, Path softwareRepoPath) {
541

542
    Path result = this.context.getFileAccess().findAncestor(installPath, softwareRepoPath, 4);
8 ✔
543
    if (result == null) {
2 ✔
544
      LOG.warn("The installation path is not located within the software repository {}.", installPath);
4 ✔
545
    }
546
    return result;
2 ✔
547
  }
548

549
  private boolean isToolNotInstalled(Path toolPath) {
550

551
    if ((toolPath == null) || !Files.isDirectory(toolPath)) {
7 ✔
552
      LOG.debug("Tool {} not installed in {}", this.tool, toolPath);
6 ✔
553
      return true;
2 ✔
554
    }
555
    return false;
2 ✔
556
  }
557

558
  @Override
559
  public void uninstall() {
560
    try {
561
      Path toolPath = getToolPath();
3 ✔
562
      if (!Files.exists(toolPath, LinkOption.NOFOLLOW_LINKS)) {
9 ✔
563
        LOG.warn("An installed version of {} does not exist.", this.tool);
5 ✔
564
        return;
1 ✔
565
      }
566
      if (this.context.isForceMode() && !isIgnoreSoftwareRepo()) {
7 !
567
        LOG.warn(
6 ✔
568
            "You triggered an uninstall of {} in version {} with force mode!\n"
569
                + "This will physically delete the currently installed version including its plugins from the machine.\n"
570
                + "This may cause issues with other projects, that use the same version of that tool."
571
            , this.tool, getInstalledVersion());
1 ✔
572
        uninstallPluginsOfTool();
2 ✔
573
        uninstallFromSoftwareRepository(toolPath);
3 ✔
574
      }
575
      performUninstall(toolPath);
3 ✔
576
      IdeLogLevel.SUCCESS.log(LOG, "Successfully uninstalled {}", this.tool);
11 ✔
577
    } catch (Exception e) {
×
578
      LOG.error("Failed to uninstall {}", this.tool, e);
×
579
    }
1 ✔
580
  }
1 ✔
581

582
  /**
583
   * Performs the actual uninstallation of this tool.
584
   *
585
   * @param toolPath the current {@link #getToolPath() tool path}.
586
   */
587
  protected void performUninstall(Path toolPath) {
588
    this.context.getFileAccess().delete(toolPath);
5 ✔
589
  }
1 ✔
590

591
  /**
592
   * Deletes the installed version of the tool from the shared software repository.
593
   */
594
  private void uninstallFromSoftwareRepository(Path toolPath) {
595
    Path repoPath = getInstalledSoftwareRepoPath(toolPath, true);
5 ✔
596
    if ((repoPath == null) || !Files.exists(repoPath)) {
7 !
597
      LOG.warn("An installed version of {} does not exist in software repository.", this.tool);
×
598
      return;
×
599
    }
600
    LOG.info("Physically deleting {} as requested by the user via force mode.", repoPath);
4 ✔
601
    this.context.getFileAccess().delete(repoPath);
5 ✔
602
    IdeLogLevel.SUCCESS.log(LOG, "Successfully deleted {} from your computer.", repoPath);
10 ✔
603
  }
1 ✔
604

605
  /**
606
   * Deletes the installed plugins of the tool from the plugins path.
607
   */
608
  private void uninstallPluginsOfTool() {
609

610
    Path toolPluginsPath = this.context.getPluginsPath().resolve(this.tool);
7 ✔
611
    if (!Files.exists(toolPluginsPath)) {
5 !
612
      LOG.info("There are no plugins of {} present to delete.", this.tool);
5 ✔
613
      return;
1 ✔
614
    }
615
    LOG.info("Physically deleting {} as requested by the user via force mode.", toolPluginsPath);
×
616
    this.context.getFileAccess().delete(toolPluginsPath);
×
617
    IdeLogLevel.SUCCESS.log(LOG, "Successfully deleted {} from your computer.", toolPluginsPath);
×
618
  }
×
619

620
  @Override
621
  protected Path getInstallationPath(String edition, VersionIdentifier resolvedVersion) {
622
    Path installationPath;
623
    if (isIgnoreSoftwareRepo()) {
3 ✔
624
      installationPath = getToolPath();
4 ✔
625
    } else {
626
      Path softwareRepositoryPath = this.context.getSoftwareRepositoryPath();
4 ✔
627
      if (softwareRepositoryPath == null) {
2 !
628
        return null;
×
629
      }
630
      Path softwareRepoPath = softwareRepositoryPath.resolve(getToolRepository().getId()).resolve(this.tool).resolve(edition);
11 ✔
631
      installationPath = softwareRepoPath.resolve(resolvedVersion.toString());
5 ✔
632
    }
633
    return installationPath;
2 ✔
634
  }
635

636

637
  /**
638
   * @return Bash completion command for this tool or {@code null} if this tool does not provide Bash completion.
639
   */
640
  public String getBashCompletion() {
641

642
    return null;
×
643
  }
644
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc