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

devonfw / IDEasy / 29727615224

20 Jul 2026 08:21AM UTC coverage: 72.474% (+0.04%) from 72.436%
29727615224

Pull #2144

github

web-flow
Merge f5492ab08 into 1389cc21a
Pull Request #2144: #1976: extend tool commandlet installation logic

4937 of 7524 branches covered (65.62%)

Branch coverage included in aggregate %.

12698 of 16809 relevant lines covered (75.54%)

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

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

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

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

360
  }
1✔
361

362
  @Override
363
  public VersionIdentifier getInstalledVersion() {
364

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

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

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

395
  @Override
396
  public String getInstalledEdition() {
397

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

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

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

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

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

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

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

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

499
  private boolean isToolNotInstalled(Path toolPath) {
500

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

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

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

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

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

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

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

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

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

594

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

615
  /**
616
   * @param directory the {@link Path} to the build directory.
617
   * @return the build configuration file for this tool or {@code null} if not found (or this is not a build tool).
618
   */
619
  public Path findBuildDescriptor(Path directory) {
620
    return null;
2✔
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