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

devonfw / IDEasy / 30638110943

31 Jul 2026 02:19PM UTC coverage: 72.649% (+0.008%) from 72.641%
30638110943

Pull #2144

github

web-flow
Merge 57128bd73 into 468118d46
Pull Request #2144: #1976: extend tool commandlet installation logic

5048 of 7683 branches covered (65.7%)

Branch coverage included in aggregate %.

13102 of 17300 relevant lines covered (75.73%)

3.22 hits per line

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

80.95
cli/src/main/java/com/devonfw/tools/ide/tool/LocalToolCommandlet.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.tool.repository.ToolRepository;
21
import com.devonfw.tools.ide.url.model.file.json.ToolDependency;
22
import com.devonfw.tools.ide.version.GenericVersionRange;
23
import com.devonfw.tools.ide.version.VersionIdentifier;
24
import com.devonfw.tools.ide.version.VersionRange;
25

26
/**
27
 * {@link ToolCommandlet} that is installed locally into the IDEasy.
28
 */
29
public abstract class LocalToolCommandlet extends ToolCommandlet {
30

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

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

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

45
  /**
46
   * @return the {@link Path} where the tool is located (installed).
47
   */
48
  @Override
49
  public Path getToolPath() {
50
    if (this.context.getSoftwarePath() == null) {
4✔
51
      return null;
2✔
52
    }
53
    return this.context.getSoftwarePath().resolve(getName());
7✔
54
  }
55

56
  /**
57
   * @return the {@link Path} where the executables of the tool can be found. Typically, a "bin" folder inside {@link #getToolPath() tool path}.
58
   */
59
  public Path getToolBinPath() {
60

61
    Path toolPath = getToolPath();
3✔
62
    if (toolPath == null) {
2!
63
      return null;
×
64
    }
65
    Path binPath = toolPath.resolve(IdeContext.FOLDER_BIN);
4✔
66
    if (Files.isDirectory(binPath)) {
5✔
67
      return binPath;
2✔
68
    }
69
    return toolPath;
2✔
70
  }
71

72
  /**
73
   * @return {@code true} to ignore a missing {@link IdeContext#FILE_SOFTWARE_VERSION software version file} in an installation, {@code false} delete the broken
74
   *     installation (default).
75
   */
76
  protected boolean isIgnoreMissingSoftwareVersionFile() {
77

78
    return false;
×
79
  }
80

81

82
  @Override
83
  protected ToolInstallation doInstall(ToolInstallRequest request) {
84

85
    Step step = request.getStep();
3✔
86
    if (step == null) {
2!
87
      return doInstallStep(request);
4✔
88
    } else {
89
      return step.call(() -> doInstallStep(request),
×
90
          () -> createExistingToolInstallation(request));
×
91
    }
92
  }
93

94
  private ToolInstallation doInstallStep(ToolInstallRequest request) {
95

96
    if (request.isIgnoreProject() && isIgnoreSoftwareRepo()) {
6!
97
      throw new CliException(
×
98
          "The tool " + this.tool
99
              + " does not support the software repository and therefore cannot be installed with --ignore-project.");
100
    }
101

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

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

150
  /**
151
   * Determines whether this tool should be installed directly in the software folder or in the software repository.
152
   *
153
   * @return {@code true} if the tool should be installed directly in the software folder, ignoring the central software repository; {@code false} if the tool
154
   *     should be installed in the central software repository (default behavior).
155
   */
156
  protected boolean isIgnoreSoftwareRepo() {
157

158
    return false;
2✔
159
  }
160

161
  /**
162
   * Performs the installation of the {@link #getName() tool} together with the environment context  managed by this
163
   * {@link com.devonfw.tools.ide.commandlet.Commandlet}.
164
   *
165
   * @param request the {@link ToolInstallRequest}.
166
   * @return the resulting {@link ToolInstallation}.
167
   */
168
  public ToolInstallation installTool(ToolInstallRequest request) {
169

170
    completeRequest(request); // most likely already done, but if installTool was called directly and not from install
3✔
171
    if (request.isInstallLoop()) {
3!
172
      return toolAlreadyInstalled(request);
×
173
    }
174
    ToolEditionAndVersion requested = request.getRequested();
3✔
175
    ToolEdition toolEdition = requested.getEdition();
3✔
176
    assert (toolEdition.tool().equals(this.tool)) : "Mismatch " + this.tool + " != " + toolEdition.tool();
7!
177
    String edition = toolEdition.edition();
3✔
178
    VersionIdentifier resolvedVersion = cveCheck(request);
4✔
179
    installToolDependencies(request);
3✔
180
    // cveCheck might have changed resolvedVersion so let us re-check...
181
    if (request.isAlreadyInstalled()) {
3✔
182
      return toolAlreadyInstalled(request);
4✔
183
    } else {
184
      ToolEditionAndVersion installed = request.getInstalled();
3✔
185
      LOG.debug("Installation from {} to {}.", installed, requested);
5✔
186
    }
187
    Path installationPath = getInstallationPath(edition, resolvedVersion);
5✔
188

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

234
  /**
235
   * Performs the installation of the {@link #getName() tool} by using {@link #installDownloadedToolPayload(ToolInstallRequest, Path, Path)} for tool-specific
236
   * logic, backing up any existing installation, and writing the version file.
237
   * <p>
238
   * This method assumes that the version has already been resolved and dependencies installed. It handles the final steps of placing the tool into the
239
   * appropriate installation directory.
240
   *
241
   * @param request the {@link ToolInstallRequest}.
242
   * @param installationPath the target {@link Path} where the {@link #getName() tool} should be installed.
243
   */
244
  protected void performToolInstallation(ToolInstallRequest request, Path installationPath) {
245

246
    FileAccess fileAccess = this.context.getFileAccess();
4✔
247
    ToolEditionAndVersion requested = request.getRequested();
3✔
248
    VersionIdentifier resolvedVersion = requested.getResolvedVersion();
3✔
249
    Path downloadedToolFile = downloadTool(requested.getEdition().edition(), resolvedVersion);
7✔
250

251
    if (Files.isDirectory(installationPath)) {
5!
252
      if (this.tool.equals(IdeasyCommandlet.TOOL_NAME)) {
×
253
        LOG.warn("Your IDEasy installation is missing the version file.");
×
254
      } else {
255
        fileAccess.backup(installationPath);
×
256
      }
257
    }
258
    fileAccess.mkdirs(installationPath.getParent());
4✔
259

260
    installDownloadedToolPayload(request, installationPath, downloadedToolFile);
5✔
261

262
    this.context.writeVersionFile(resolvedVersion, installationPath);
5✔
263
    // fix macOS Gatekeeper blocking - must run after version file is written but before any executables are launched
264
    getMacOsHelper().removeQuarantineAttribute(installationPath);
4✔
265
    LOG.debug("Installed {} in version {} at {}", this.tool, resolvedVersion, installationPath);
18✔
266
  }
1✔
267

268
  /**
269
   * Performs the actual installation of the tool bits.
270
   *
271
   * @param request the {@link ToolInstallRequest}.
272
   * @param installationPath the target {@link Path} where the tool should be installed.
273
   * @param downloadedToolFile the {@link Path} to the downloaded tool file.
274
   */
275
  protected void installDownloadedToolPayload(ToolInstallRequest request, Path installationPath, Path downloadedToolFile) {
276

277
    boolean extract = isExtract();
3✔
278
    if (!extract) {
2✔
279
      LOG.trace("Extraction is disabled for '{}' hence just moving the downloaded file {}.", this.tool, downloadedToolFile);
6✔
280
    }
281
    this.context.getFileAccess().extract(downloadedToolFile, installationPath, this::postExtract, extract);
9✔
282
  }
1✔
283

284
  /**
285
   * @param edition the {@link #getConfiguredEdition() tool edition} to download.
286
   * @param resolvedVersion the resolved {@link VersionIdentifier version} to download.
287
   * @return the {@link Path} to the downloaded release file.
288
   */
289
  protected Path downloadTool(String edition, VersionIdentifier resolvedVersion) {
290
    return getToolRepository().download(this.tool, edition, resolvedVersion, this);
9✔
291
  }
292

293
  /**
294
   * Install this tool as dependency of another tool.
295
   *
296
   * @param versionRange the required {@link VersionRange}. See {@link ToolDependency#versionRange()}.
297
   * @param parentRequest the {@link ToolInstallRequest} of the tool causing this dependency.
298
   * @return the corresponding {@link ToolInstallation}.
299
   */
300
  public ToolInstallation installAsDependency(VersionRange versionRange, ToolInstallRequest parentRequest) {
301
    ToolInstallRequest request = new ToolInstallRequest(parentRequest);
5✔
302
    ToolEditionAndVersion requested = new ToolEditionAndVersion(getToolWithConfiguredEdition());
6✔
303
    request.setRequested(requested);
3✔
304

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

334
  /**
335
   * Installs the tool dependencies for the current tool.
336
   *
337
   * @param request the {@link ToolInstallRequest}.
338
   */
339
  protected void installToolDependencies(ToolInstallRequest request) {
340

341
    ToolEditionAndVersion requested = request.getRequested();
3✔
342
    VersionIdentifier version = requested.getResolvedVersion();
3✔
343
    ToolEdition toolEdition = requested.getEdition();
3✔
344
    Collection<ToolDependency> dependencies = getToolRepository().findDependencies(this.tool, toolEdition.edition(), version);
9✔
345
    int size = dependencies.size();
3✔
346
    LOG.debug("Tool {} has {} other tool(s) as dependency", toolEdition, size);
6✔
347
    for (ToolDependency dependency : dependencies) {
10✔
348
      LOG.trace("Ensuring dependency {} for tool {}", dependency.tool(), toolEdition);
6✔
349
      LocalToolCommandlet dependencyTool = this.context.getCommandletManager().getRequiredLocalToolCommandlet(dependency.tool());
7✔
350
      dependencyTool.installAsDependency(dependency.versionRange(), request);
6✔
351
    }
1✔
352
  }
1✔
353

354
  /**
355
   * Post-extraction hook that can be overridden to add custom processing after unpacking and before moving to the final destination folder.
356
   *
357
   * @param extractedDir the {@link Path} to the folder with the unpacked tool.
358
   */
359
  protected void postExtract(Path extractedDir) {
360

361
  }
1✔
362

363
  @Override
364
  public VersionIdentifier getInstalledVersion() {
365

366
    return getInstalledVersion(getToolPath());
5✔
367
  }
368

369
  /**
370
   * @param toolPath the installation {@link Path} where to find the version file.
371
   * @return the currently installed {@link VersionIdentifier version} of this tool or {@code null} if not installed.
372
   */
373
  protected VersionIdentifier getInstalledVersion(Path toolPath) {
374

375
    if (isToolNotInstalled(toolPath)) {
4✔
376
      return null;
2✔
377
    }
378
    Path versionLookupPath = getInstalledSoftwareRepoPath(toolPath, false);
5✔
379
    if (versionLookupPath == null) {
2✔
380
      versionLookupPath = toolPath;
2✔
381
    }
382
    Path toolVersionFile = versionLookupPath.resolve(IdeContext.FILE_SOFTWARE_VERSION);
4✔
383
    if (!Files.exists(toolVersionFile)) {
5✔
384
      Path legacyToolVersionFile = versionLookupPath.resolve(IdeContext.FILE_LEGACY_SOFTWARE_VERSION);
4✔
385
      if (Files.exists(legacyToolVersionFile)) {
5!
386
        toolVersionFile = legacyToolVersionFile;
3✔
387
      } else {
388
        LOG.warn("Tool {} is missing version file in {}", getName(), toolVersionFile);
×
389
        return null;
×
390
      }
391
    }
392
    String version = this.context.getFileAccess().readFileContent(toolVersionFile).trim();
7✔
393
    return VersionIdentifier.of(version);
3✔
394
  }
395

396
  @Override
397
  public String getInstalledEdition() {
398

399
    return getInstalledEdition(getToolPath());
5✔
400
  }
401

402
  /**
403
   * @param toolPath the installation {@link Path} where to find currently installed tool. The name of the parent directory of the real path corresponding
404
   *     to the passed {@link Path path} must be the name of the edition.
405
   * @return the installed edition of this tool or {@code null} if not installed.
406
   */
407
  protected String getInstalledEdition(Path toolPath) {
408
    if (isToolNotInstalled(toolPath)) {
4✔
409
      return null;
2✔
410
    }
411
    Path realPath = this.context.getFileAccess().toRealPath(toolPath);
6✔
412
    // if the realPath changed, a link has been resolved
413
    if (realPath.equals(toolPath)) {
4✔
414
      if (!isIgnoreSoftwareRepo()) {
3✔
415
        LOG.warn("Tool {} is not installed via software repository (maybe from devonfw-ide). Please consider reinstalling it.", this.tool);
5✔
416
      }
417
      // 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
418
      return getConfiguredEdition();
3✔
419
    }
420
    Path toolRepoFolder = context.getSoftwareRepositoryPath().resolve(ToolRepository.ID_DEFAULT).resolve(this.tool);
9✔
421
    String edition = getEdition(toolRepoFolder, realPath);
5✔
422
    if (edition == null) {
2!
423
      edition = this.tool;
×
424
    }
425
    if (!getToolRepository().getSortedEditions(this.tool).contains(edition)) {
8✔
426
      LOG.warn("Undefined edition {} of tool {}", edition, this.tool);
6✔
427
    }
428
    return edition;
2✔
429
  }
430

431
  private String getEdition(Path toolRepoFolder, Path toolInstallFolder) {
432

433
    int toolRepoNameCount = toolRepoFolder.getNameCount();
3✔
434
    int toolInstallNameCount = toolInstallFolder.getNameCount();
3✔
435
    if (toolRepoNameCount < toolInstallNameCount) {
3!
436
      // ensure toolInstallFolder starts with $IDE_ROOT/_ide/software/default/«tool»
437
      for (int i = 0; i < toolRepoNameCount; i++) {
7✔
438
        if (!toolRepoFolder.getName(i).toString().equals(toolInstallFolder.getName(i).toString())) {
10!
439
          return null;
×
440
        }
441
      }
442
      return toolInstallFolder.getName(toolRepoNameCount).toString();
5✔
443
    }
444
    return null;
×
445
  }
446

447
  private Path getInstalledSoftwareRepoPath(Path toolPath) {
448
    return getInstalledSoftwareRepoPath(toolPath, false);
×
449
  }
450

451
  private Path getInstalledSoftwareRepoPath(Path toolPath, boolean logIfNotSoftwareRepo) {
452
    if (isToolNotInstalled(toolPath)) {
4!
453
      return null;
×
454
    }
455
    Path installPath = this.context.getFileAccess().toRealPath(toolPath);
6✔
456
    // if the installPath changed, a link has been resolved
457
    if (installPath.equals(toolPath)) {
4✔
458
      if (logIfNotSoftwareRepo && !isIgnoreSoftwareRepo()) {
2!
459
        LOG.warn("Tool {} is not installed via software repository (maybe from devonfw-ide). Please consider reinstalling it.", this.tool);
×
460
      }
461
      // 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
462
      return null;
2✔
463
    }
464
    installPath = getValidInstalledSoftwareRepoPath(installPath, context.getSoftwareRepositoryPath());
7✔
465
    return installPath;
2✔
466
  }
467

468
  Path getValidInstalledSoftwareRepoPath(Path installPath, Path softwareRepoPath) {
469
    int softwareRepoNameCount = softwareRepoPath.getNameCount();
3✔
470
    int toolInstallNameCount = installPath.getNameCount();
3✔
471
    int targetToolInstallNameCount = softwareRepoNameCount + 4;
4✔
472

473
    // installPath can't be shorter than softwareRepoPath
474
    if (toolInstallNameCount < softwareRepoNameCount) {
3!
475
      LOG.warn("The installation path is not located within the software repository {}.", installPath);
×
476
      return null;
×
477
    }
478
    // ensure installPath starts with $IDE_ROOT/_ide/software/
479
    for (int i = 0; i < softwareRepoNameCount; i++) {
7✔
480
      if (!softwareRepoPath.getName(i).toString().equals(installPath.getName(i).toString())) {
10✔
481
        LOG.warn("The installation path is not located within the software repository {}.", installPath);
4✔
482
        return null;
2✔
483
      }
484
    }
485
    // return $IDE_ROOT/_ide/software/«id»/«tool»/«edition»/«version»
486
    if (toolInstallNameCount == targetToolInstallNameCount) {
3✔
487
      return installPath;
2✔
488
    } else if (toolInstallNameCount > targetToolInstallNameCount) {
3✔
489
      Path validInstallPath = installPath;
2✔
490
      for (int i = 0; i < toolInstallNameCount - targetToolInstallNameCount; i++) {
9✔
491
        validInstallPath = validInstallPath.getParent();
3✔
492
      }
493
      return validInstallPath;
2✔
494
    } else {
495
      LOG.warn("The installation path is faulty {}.", installPath);
4✔
496
      return null;
2✔
497
    }
498
  }
499

500
  private boolean isToolNotInstalled(Path toolPath) {
501

502
    if ((toolPath == null) || !Files.isDirectory(toolPath)) {
7✔
503
      LOG.debug("Tool {} not installed in {}", this.tool, toolPath);
6✔
504
      return true;
2✔
505
    }
506
    return false;
2✔
507
  }
508

509
  @Override
510
  public void uninstall() {
511
    try {
512
      Path toolPath = getToolPath();
3✔
513
      if (!Files.exists(toolPath)) {
5!
514
        LOG.warn("An installed version of {} does not exist.", this.tool);
×
515
        return;
×
516
      }
517
      if (this.context.isForceMode() && !isIgnoreSoftwareRepo()) {
7!
518
        LOG.warn(
6✔
519
            "You triggered an uninstall of {} in version {} with force mode!\n"
520
                + "This will physically delete the currently installed version including its plugins from the machine.\n"
521
                + "This may cause issues with other projects, that use the same version of that tool."
522
            , this.tool, getInstalledVersion());
1✔
523
        uninstallPluginsOfTool();
2✔
524
        uninstallFromSoftwareRepository(toolPath);
3✔
525
      }
526
      performUninstall(toolPath);
3✔
527
      IdeLogLevel.SUCCESS.log(LOG, "Successfully uninstalled {}", this.tool);
11✔
528
    } catch (Exception e) {
×
529
      LOG.error("Failed to uninstall {}", this.tool, e);
×
530
    }
1✔
531
  }
1✔
532

533
  /**
534
   * Performs the actual uninstallation of this tool.
535
   *
536
   * @param toolPath the current {@link #getToolPath() tool path}.
537
   */
538
  protected void performUninstall(Path toolPath) {
539
    this.context.getFileAccess().delete(toolPath);
5✔
540
  }
1✔
541

542
  /**
543
   * Deletes the installed version of the tool from the shared software repository.
544
   */
545
  private void uninstallFromSoftwareRepository(Path toolPath) {
546
    Path repoPath = getInstalledSoftwareRepoPath(toolPath, true);
5✔
547
    if ((repoPath == null) || !Files.exists(repoPath)) {
7!
548
      LOG.warn("An installed version of {} does not exist in software repository.", this.tool);
×
549
      return;
×
550
    }
551
    LOG.info("Physically deleting {} as requested by the user via force mode.", repoPath);
4✔
552
    this.context.getFileAccess().delete(repoPath);
5✔
553
    IdeLogLevel.SUCCESS.log(LOG, "Successfully deleted {} from your computer.", repoPath);
10✔
554
  }
1✔
555

556
  /**
557
   * Deletes the installed plugins of the tool from the plugins path.
558
   */
559
  private void uninstallPluginsOfTool() {
560

561
    Path toolPluginsPath = this.context.getPluginsPath().resolve(this.tool);
7✔
562
    if (!Files.exists(toolPluginsPath)) {
5!
563
      LOG.info("There are no plugins of {} present to delete.", this.tool);
5✔
564
      return;
1✔
565
    }
566
    LOG.info("Physically deleting {} as requested by the user via force mode.", toolPluginsPath);
×
567
    this.context.getFileAccess().delete(toolPluginsPath);
×
568
    IdeLogLevel.SUCCESS.log(LOG, "Successfully deleted {} from your computer.", toolPluginsPath);
×
569
  }
×
570

571
  @Override
572
  protected Path getInstallationPath(String edition, VersionIdentifier resolvedVersion) {
573
    Path installationPath;
574
    if (isIgnoreSoftwareRepo()) {
3✔
575
      installationPath = getToolPath();
4✔
576
    } else {
577
      Path softwareRepositoryPath = this.context.getSoftwareRepositoryPath();
4✔
578
      if (softwareRepositoryPath == null) {
2!
579
        return null;
×
580
      }
581
      Path softwareRepoPath = softwareRepositoryPath.resolve(getToolRepository().getId()).resolve(this.tool).resolve(edition);
11✔
582
      installationPath = softwareRepoPath.resolve(resolvedVersion.toString());
5✔
583
    }
584
    return installationPath;
2✔
585
  }
586

587
  /**
588
   * @return {@link VersionIdentifier} with latest version of the tool}.
589
   */
590
  public VersionIdentifier getLatestToolVersion() {
591

592
    return this.context.getDefaultToolRepository().resolveVersion(this.tool, getConfiguredEdition(), VersionIdentifier.LATEST, this);
×
593
  }
594

595

596
  /**
597
   * Searches for a wrapper file in valid projects (containing a build file f.e. build.gradle or pom.xml) and returns its path.
598
   *
599
   * @param wrapperFileName the name of the wrapper file
600
   * @return Path of the wrapper file or {@code null} if none was found.
601
   */
602
  protected Path findWrapper(String wrapperFileName) {
603
    Path dir = this.context.getCwd();
4✔
604
    // traverse the cwd directory containing a build descriptor up till a wrapper file was found
605
    while ((dir != null) && (findBuildDescriptor(dir) != null)) {
6!
606
      Path wrapper = dir.resolve(wrapperFileName);
4✔
607
      if (Files.exists(wrapper)) {
5✔
608
        LOG.debug("Using wrapper: {}", wrapper);
4✔
609
        return wrapper;
2✔
610
      }
611
      dir = dir.getParent();
3✔
612
    }
1✔
613
    return null;
2✔
614
  }
615

616
  /**
617
   * @param directory the {@link Path} to the build directory.
618
   * @return the build configuration file for this tool or {@code null} if not found (or this is not a build tool).
619
   */
620
  public Path findBuildDescriptor(Path directory) {
621
    return null;
2✔
622
  }
623

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

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