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

devonfw / IDEasy / 39247962780

19 Oct 2026 11:53AM UTC coverage: 74.847% (+0.2%) from 74.686%
39247962780

Pull #2586

github

web-flow
Merge b98ec53dc into 553131e8b
Pull Request #2586: #2585: Create npm base commandlet for gemini cli

5871 of 8723 branches covered (67.3%)

Branch coverage included in aggregate %.

15366 of 19651 relevant lines covered (78.19%)

3.36 hits per line

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

81.61
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
    VersionIdentifier resolvedVersion = cveCheck(request);
4 ✔
185
    installToolDependencies(request);
3 ✔
186
    // cveCheck might have changed resolvedVersion so let us re-check...
187
    if (request.isAlreadyInstalled()) {
3 ✔
188
      return toolAlreadyInstalled(request);
4 ✔
189
    } else {
190
      ToolEditionAndVersion installed = request.getInstalled();
3 ✔
191
      LOG.debug("Installation from {} to {}.", installed, requested);
5 ✔
192
    }
193
    Path installationPath = getInstallationPath(edition, resolvedVersion);
5 ✔
194

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

247
  /**
248
   * Performs the installation of the {@link #getName() tool} by using {@link #installDownloadedToolPayload(ToolInstallRequest, Path, Path)} for tool-specific
249
   * logic, backing up any existing installation, and writing the version file.
250
   * <p>
251
   * This method assumes that the version has already been resolved and dependencies installed. It handles the final steps of placing the tool into the
252
   * appropriate installation directory.
253
   *
254
   * @param request the {@link ToolInstallRequest}.
255
   * @param installationPath the target {@link Path} where the {@link #getName() tool} should be installed.
256
   */
257
  protected void performToolInstallation(ToolInstallRequest request, Path installationPath) {
258

259
    FileAccess fileAccess = this.context.getFileAccess();
4 ✔
260
    ToolEditionAndVersion requested = request.getRequested();
3 ✔
261
    VersionIdentifier resolvedVersion = requested.getResolvedVersion();
3 ✔
262
    Path downloadedToolFile = downloadTool(requested.getEdition().edition(), resolvedVersion);
7 ✔
263

264
    if (Files.isDirectory(installationPath)) {
5 !
265
      if (this.tool.equals(IdeasyCommandlet.TOOL_NAME)) {
×
266
        LOG.warn("Your IDEasy installation is missing the version file.");
×
267
      } else {
268
        fileAccess.backup(installationPath);
×
269
      }
270
    }
271
    fileAccess.mkdirs(installationPath.getParent());
4 ✔
272

273
    installDownloadedToolPayload(request, installationPath, downloadedToolFile);
5 ✔
274

275
    this.context.writeVersionFile(resolvedVersion, installationPath);
5 ✔
276
    // fix macOS Gatekeeper blocking - must run after version file is written but before any executables are launched
277
    getMacOsHelper().removeQuarantineAttribute(installationPath);
4 ✔
278
    LOG.debug("Installed {} in version {} at {}", this.tool, resolvedVersion, installationPath);
18 ✔
279
  }
1 ✔
280

281
  /**
282
   * Performs the actual installation of the tool bits.
283
   *
284
   * @param request the {@link ToolInstallRequest}.
285
   * @param installationPath the target {@link Path} where the tool should be installed.
286
   * @param downloadedToolFile the {@link Path} to the downloaded tool file.
287
   */
288
  protected void installDownloadedToolPayload(ToolInstallRequest request, Path installationPath, Path downloadedToolFile) {
289

290
    boolean extract = isExtract();
3 ✔
291
    if (!extract) {
2 ✔
292
      LOG.trace("Extraction is disabled for '{}' hence just moving the downloaded file {}.", this.tool, downloadedToolFile);
6 ✔
293
    }
294
    this.context.getFileAccess().extract(downloadedToolFile, installationPath, this::postExtract, extract);
9 ✔
295
  }
1 ✔
296

297
  /**
298
   * @param edition the {@link #getConfiguredEdition() tool edition} to download.
299
   * @param resolvedVersion the resolved {@link VersionIdentifier version} to download.
300
   * @return the {@link Path} to the downloaded release file.
301
   */
302
  protected Path downloadTool(String edition, VersionIdentifier resolvedVersion) {
303
    return getToolRepository().download(this.tool, edition, resolvedVersion, this);
9 ✔
304
  }
305

306
  /**
307
   * Install this tool as dependency of another tool.
308
   *
309
   * @param versionRange the required {@link VersionRange}. See {@link ToolDependency#versionRange()}.
310
   * @param parentRequest the {@link ToolInstallRequest} of the tool causing this dependency.
311
   * @return the corresponding {@link ToolInstallation}.
312
   */
313
  public ToolInstallation installAsDependency(VersionRange versionRange, ToolInstallRequest parentRequest) {
314
    ToolInstallRequest request = new ToolInstallRequest(parentRequest);
5 ✔
315
    ToolEditionAndVersion requested = new ToolEditionAndVersion(getToolWithConfiguredEdition());
6 ✔
316
    request.setRequested(requested);
3 ✔
317

318
    // If we are not inside a project or if we should ignore the project, do not try to use the configured version
319
    // but instead just use the version range from the dependency.
320
    boolean ignoreProject = request.isIgnoreProject() || this.context.getIdeHome() == null;
11 !
321
    VersionIdentifier configuredVersion = ignoreProject ? null : getConfiguredVersion();
7 ✔
322
    if (!ignoreProject && (configuredVersion != null) && versionRange.contains(configuredVersion)) {
8 !
323
      // prefer configured version if contained in version range
324
      requested.setVersion(configuredVersion);
3 ✔
325
      // return install(true, configuredVersion, processContext, null);
326
      return install(request);
4 ✔
327
    } else {
328
      if (ignoreProject) {
2 ✔
329
        LOG.debug("Ignoring project for dependency {} of tool {}, using version range {}", this.tool, parentRequest.getRequested(), versionRange);
20 ✔
330
      } else if (isIgnoreSoftwareRepo()) {
3 !
331
        throw new IllegalStateException(
×
332
            "Cannot satisfy dependency to " + this.tool + " in version " + versionRange + " for " + parentRequest.getRequested()
×
333
                + " since it is conflicting with configured version "
334
                + configuredVersion
335
                + " and this tool does not support the software repository.");
336
      } else {
337
        LOG.info(
8 ✔
338
            "The tool {} requires {} in the version range {}, but your project uses version {}, which does not match."
339
                + " Therefore, we install a compatible version in that range.",
340
            parentRequest.getRequested().getEdition(), this.tool, versionRange, configuredVersion);
16 ✔
341
      }
342
      requested.setVersion(versionRange);
3 ✔
343
      return installTool(request);
4 ✔
344
    }
345
  }
346

347
  /**
348
   * Installs the tool dependencies for the current tool.
349
   *
350
   * @param request the {@link ToolInstallRequest}.
351
   */
352
  protected void installToolDependencies(ToolInstallRequest request) {
353

354
    ToolEditionAndVersion requested = request.getRequested();
3 ✔
355
    VersionIdentifier version = requested.getResolvedVersion();
3 ✔
356
    ToolEdition toolEdition = requested.getEdition();
3 ✔
357
    Collection<ToolDependency> dependencies = getToolRepository().findDependencies(this.tool, toolEdition.edition(), version);
9 ✔
358
    int size = dependencies.size();
3 ✔
359
    LOG.debug("Tool {} has {} other tool(s) as dependency", toolEdition, size);
6 ✔
360
    for (ToolDependency dependency : dependencies) {
10 ✔
361
      LOG.trace("Ensuring dependency {} for tool {}", dependency.tool(), toolEdition);
6 ✔
362
      AbstractLocalToolCommandlet dependencyTool = this.context.getCommandletManager().getRequiredLocalToolCommandlet(dependency.tool());
7 ✔
363
      dependencyTool.installAsDependency(dependency.versionRange(), request);
6 ✔
364
    }
1 ✔
365
  }
1 ✔
366

367
  /**
368
   * Post-extraction hook that can be overridden to add custom processing after unpacking and before moving to the final destination folder.
369
   *
370
   * @param extractedDir the {@link Path} to the folder with the unpacked tool.
371
   */
372
  protected void postExtract(Path extractedDir) {
373

374
  }
1 ✔
375

376
  /**
377
   * 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
378
   * python virtual environment). Does nothing if the file is present or the version is unknown.
379
   *
380
   * @param installationPath the {@link Path} to the installation of this tool.
381
   * @param version the {@link VersionIdentifier} that is actually installed at the given {@code installationPath}.
382
   */
383
  protected void restoreMissingVersionFile(Path installationPath, VersionIdentifier version) {
384

385
    if ((version == null) || !Files.isDirectory(installationPath)) {
7 !
386
      return;
×
387
    }
388
    Path toolVersionFile = installationPath.resolve(IdeContext.FILE_SOFTWARE_VERSION);
4 ✔
389
    if (Files.exists(toolVersionFile)) {
5 !
390
      return;
×
391
    }
392
    LOG.warn("Version file is missing at {} - restoring it with version {} for tool {}", toolVersionFile, version, this.tool);
18 ✔
393
    this.context.writeVersionFile(version, installationPath);
5 ✔
394
    // the installation on disk changed so a previously cached result must not survive
395
    invalidateInstalledEditionAndVersion();
2 ✔
396
  }
1 ✔
397

398
  @Override
399
  protected EditionAndVersion computeInstalledEditionAndVersion() {
400

401
    Path toolPath = getToolPath();
3 ✔
402
    if (isToolNotInstalled(toolPath)) {
4 ✔
403
      return null;
2 ✔
404
    }
405
    // Resolve edition and version from a single tool-path lookup (one pass) instead of two separate lookups.
406
    Path repoPath = getInstalledSoftwareRepoPath(toolPath, true);
5 ✔
407

408
    if (repoPath != null) {
2 ✔
409
      VersionIdentifier version = VersionIdentifier.of(repoPath.getFileName().toString());
5 ✔
410
      return new EditionAndVersion(getEditionFromSoftwareRepoPath(repoPath), version);
8 ✔
411
    }
412
    return computeInstalledEditionAndVersionFromLocalSoftwareFolder();
3 ✔
413
  }
414

415
  /**
416
   * Determines the installed edition and version of a tool that is not installed via the software repository.
417
   *
418
   * @return the installed {@link EditionAndVersion} or {@code null} if the version cannot be determined.
419
   */
420
  protected EditionAndVersion computeInstalledEditionAndVersionFromLocalSoftwareFolder() {
421

422
    Path toolPath = getToolPath();
3 ✔
423
    VersionIdentifier version = getInstalledVersion(toolPath);
4 ✔
424
    if (version == null) {
2 ✔
425
      version = computeInstalledVersionFromLocalSoftwareFolder();
3 ✔
426
      if (version == null) {
2 ✔
427
        return null;
2 ✔
428
      }
429
      restoreMissingVersionFile(toolPath, version);
4 ✔
430
    }
431
    return new EditionAndVersion(computeInstalledEditionFromLocalSoftwareFolder(), version);
7 ✔
432
  }
433

434
  /**
435
   * Determines the installed edition of a tool that is not installed via the software repository.
436
   *
437
   * @return the installed edition of this tool.
438
   */
439
  protected String computeInstalledEditionFromLocalSoftwareFolder() {
440
    return this.tool;
3 ✔
441
  }
442

443
  /**
444
   * Determines the installed version of a tool that is not installed via the software repository, in case the
445
   * {@link IdeContext#FILE_SOFTWARE_VERSION version file} is missing.
446
   * <p>
447
   * 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
448
   * determine the version from the installation itself.
449
   *
450
   * @return the installed {@link VersionIdentifier version} or {@code null} if it cannot be determined.
451
   */
452
  protected VersionIdentifier computeInstalledVersionFromLocalSoftwareFolder() {
453
    return null;
×
454
  }
455

456
  /**
457
   * @param toolPath the installation {@link Path} where to find the version file.
458
   * @return the currently installed {@link VersionIdentifier version} of this tool or {@code null} if not installed.
459
   */
460
  protected VersionIdentifier getInstalledVersion(Path toolPath) {
461

462
    if (isToolNotInstalled(toolPath)) {
4 ✔
463
      return null;
2 ✔
464
    }
465
    Path versionLookupPath = getInstalledSoftwareRepoPath(toolPath, false);
5 ✔
466
    if (versionLookupPath == null) {
2 !
467
      versionLookupPath = toolPath;
2 ✔
468
    }
469
    Path toolVersionFile = versionLookupPath.resolve(IdeContext.FILE_SOFTWARE_VERSION);
4 ✔
470
    if (!Files.exists(toolVersionFile)) {
5 ✔
471
      Path legacyToolVersionFile = versionLookupPath.resolve(IdeContext.FILE_LEGACY_SOFTWARE_VERSION);
4 ✔
472
      if (Files.exists(legacyToolVersionFile)) {
5 ✔
473
        toolVersionFile = legacyToolVersionFile;
3 ✔
474
      } else {
475
        LOG.warn("Tool {} is missing version file in {}", getName(), toolVersionFile);
6 ✔
476
        return null;
2 ✔
477
      }
478
    }
479
    String version = this.context.getFileAccess().readFileContent(toolVersionFile).trim();
7 ✔
480
    return VersionIdentifier.of(version);
3 ✔
481
  }
482

483
  /**
484
   * @param toolPath the installation {@link Path} where to find currently installed tool. The name of the parent directory of the real path corresponding
485
   *     to the passed {@link Path path} must be the name of the edition.
486
   * @return the installed edition of this tool or {@code null} if not installed.
487
   */
488
  protected String getInstalledEdition(Path toolPath) {
489

490
    if (isToolNotInstalled(toolPath)) {
×
491
      return null;
×
492
    }
493
    Path repoPath = getInstalledSoftwareRepoPath(toolPath, true);
×
494
    if (repoPath == null) {
×
495
      return computeInstalledEditionFromLocalSoftwareFolder();
×
496
    }
497
    return getEditionFromSoftwareRepoPath(repoPath);
×
498
  }
499

500
  private String getEditionFromSoftwareRepoPath(Path repoPath) {
501

502
    String edition = repoPath.getParent().getFileName().toString();
5 ✔
503
    if (!getToolRepository().getSortedEditions(this.tool).contains(edition)) {
8 ✔
504
      LOG.warn("Undefined edition {} of tool {}", edition, this.tool);
6 ✔
505
    }
506
    return edition;
2 ✔
507
  }
508

509
  private Path getInstalledSoftwareRepoPath(Path toolPath, boolean logIfNotSoftwareRepo) {
510
    if (isToolNotInstalled(toolPath)) {
4 !
511
      return null;
×
512
    }
513
    Path installPath = this.context.getFileAccess().toRealPath(toolPath);
6 ✔
514
    // if the installPath changed, a link has been resolved
515
    if (installPath.equals(toolPath)) {
4 ✔
516
      if (logIfNotSoftwareRepo && !isIgnoreSoftwareRepo()) {
5 ✔
517
        LOG.warn("Tool {} is not installed via software repository (maybe from devonfw-ide). Please consider reinstalling it.", this.tool);
5 ✔
518
      }
519
      // 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
520
      return null;
2 ✔
521
    }
522
    installPath = getValidInstalledSoftwareRepoPath(installPath, context.getSoftwareRepositoryPath());
7 ✔
523
    return installPath;
2 ✔
524
  }
525

526
  Path getValidInstalledSoftwareRepoPath(Path installPath, Path softwareRepoPath) {
527

528
    Path result = this.context.getFileAccess().findAncestor(installPath, softwareRepoPath, 4);
8 ✔
529
    if (result == null) {
2 ✔
530
      LOG.warn("The installation path is not located within the software repository {}.", installPath);
4 ✔
531
    }
532
    return result;
2 ✔
533
  }
534

535
  private boolean isToolNotInstalled(Path toolPath) {
536

537
    if ((toolPath == null) || !Files.isDirectory(toolPath)) {
7 ✔
538
      LOG.debug("Tool {} not installed in {}", this.tool, toolPath);
6 ✔
539
      return true;
2 ✔
540
    }
541
    return false;
2 ✔
542
  }
543

544
  @Override
545
  public void uninstall() {
546
    try {
547
      Path toolPath = getToolPath();
3 ✔
548
      if (!Files.exists(toolPath)) {
5 ✔
549
        LOG.warn("An installed version of {} does not exist.", this.tool);
5 ✔
550
        return;
1 ✔
551
      }
552
      if (this.context.isForceMode() && !isIgnoreSoftwareRepo()) {
7 !
553
        LOG.warn(
6 ✔
554
            "You triggered an uninstall of {} in version {} with force mode!\n"
555
                + "This will physically delete the currently installed version including its plugins from the machine.\n"
556
                + "This may cause issues with other projects, that use the same version of that tool."
557
            , this.tool, getInstalledVersion());
1 ✔
558
        uninstallPluginsOfTool();
2 ✔
559
        uninstallFromSoftwareRepository(toolPath);
3 ✔
560
      }
561
      performUninstall(toolPath);
3 ✔
562
      IdeLogLevel.SUCCESS.log(LOG, "Successfully uninstalled {}", this.tool);
11 ✔
563
    } catch (Exception e) {
×
564
      LOG.error("Failed to uninstall {}", this.tool, e);
×
565
    }
1 ✔
566
  }
1 ✔
567

568
  /**
569
   * Performs the actual uninstallation of this tool.
570
   *
571
   * @param toolPath the current {@link #getToolPath() tool path}.
572
   */
573
  protected void performUninstall(Path toolPath) {
574
    this.context.getFileAccess().delete(toolPath);
5 ✔
575
  }
1 ✔
576

577
  /**
578
   * Deletes the installed version of the tool from the shared software repository.
579
   */
580
  private void uninstallFromSoftwareRepository(Path toolPath) {
581
    Path repoPath = getInstalledSoftwareRepoPath(toolPath, true);
5 ✔
582
    if ((repoPath == null) || !Files.exists(repoPath)) {
7 !
583
      LOG.warn("An installed version of {} does not exist in software repository.", this.tool);
×
584
      return;
×
585
    }
586
    LOG.info("Physically deleting {} as requested by the user via force mode.", repoPath);
4 ✔
587
    this.context.getFileAccess().delete(repoPath);
5 ✔
588
    IdeLogLevel.SUCCESS.log(LOG, "Successfully deleted {} from your computer.", repoPath);
10 ✔
589
  }
1 ✔
590

591
  /**
592
   * Deletes the installed plugins of the tool from the plugins path.
593
   */
594
  private void uninstallPluginsOfTool() {
595

596
    Path toolPluginsPath = this.context.getPluginsPath().resolve(this.tool);
7 ✔
597
    if (!Files.exists(toolPluginsPath)) {
5 !
598
      LOG.info("There are no plugins of {} present to delete.", this.tool);
5 ✔
599
      return;
1 ✔
600
    }
601
    LOG.info("Physically deleting {} as requested by the user via force mode.", toolPluginsPath);
×
602
    this.context.getFileAccess().delete(toolPluginsPath);
×
603
    IdeLogLevel.SUCCESS.log(LOG, "Successfully deleted {} from your computer.", toolPluginsPath);
×
604
  }
×
605

606
  @Override
607
  protected Path getInstallationPath(String edition, VersionIdentifier resolvedVersion) {
608
    Path installationPath;
609
    if (isIgnoreSoftwareRepo()) {
3 ✔
610
      installationPath = getToolPath();
4 ✔
611
    } else {
612
      Path softwareRepositoryPath = this.context.getSoftwareRepositoryPath();
4 ✔
613
      if (softwareRepositoryPath == null) {
2 !
614
        return null;
×
615
      }
616
      Path softwareRepoPath = softwareRepositoryPath.resolve(getToolRepository().getId()).resolve(this.tool).resolve(edition);
11 ✔
617
      installationPath = softwareRepoPath.resolve(resolvedVersion.toString());
5 ✔
618
    }
619
    return installationPath;
2 ✔
620
  }
621

622

623
  /**
624
   * @return Bash completion command for this tool or {@code null} if this tool does not provide Bash completion.
625
   */
626
  public String getBashCompletion() {
627

628
    return null;
×
629
  }
630
}
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