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

devonfw / IDEasy / 29090449301

10 Jul 2026 11:46AM UTC coverage: 72.186% (-0.04%) from 72.225%
29090449301

Pull #2147

github

web-flow
Merge 1a8c35908 into 365f379a1
Pull Request #2147: #2140 just install fails since already installed

4876 of 7460 branches covered (65.36%)

Branch coverage included in aggregate %.

12572 of 16711 relevant lines covered (75.23%)

3.18 hits per line

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

80.92
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.CliOfflineException;
13
import com.devonfw.tools.ide.common.Tag;
14
import com.devonfw.tools.ide.context.IdeContext;
15
import com.devonfw.tools.ide.io.FileAccess;
16
import com.devonfw.tools.ide.log.IdeLogLevel;
17
import com.devonfw.tools.ide.process.ProcessContext;
18
import com.devonfw.tools.ide.step.Step;
19
import com.devonfw.tools.ide.tool.repository.ToolRepository;
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 ToolCommandlet} that is installed locally into the IDEasy.
27
 */
28
public abstract class LocalToolCommandlet extends ToolCommandlet {
29

30
  private static final Logger LOG = LoggerFactory.getLogger(LocalToolCommandlet.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 LocalToolCommandlet(IdeContext context, String tool, Set<Tag> tags) {
40

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

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

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

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

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

77
    return false;
×
78
  }
79

80

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

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

93
  private ToolInstallation doInstallStep(ToolInstallRequest request) {
94

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

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

140
  /**
141
   * Determines whether this tool should be installed directly in the software folder or in the software repository.
142
   *
143
   * @return {@code true} if the tool should be installed directly in the software folder, ignoring the central software repository; {@code false} if the tool
144
   *     should be installed in the central software repository (default behavior).
145
   */
146
  protected boolean isIgnoreSoftwareRepo() {
147

148
    return false;
2✔
149
  }
150

151
  /**
152
   * Performs the installation of the {@link #getName() tool} together with the environment context  managed by this
153
   * {@link com.devonfw.tools.ide.commandlet.Commandlet}.
154
   *
155
   * @param request the {@link ToolInstallRequest}.
156
   * @return the resulting {@link ToolInstallation}.
157
   */
158
  public ToolInstallation installTool(ToolInstallRequest request) {
159

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

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

224
  /**
225
   * Performs the installation of the {@link #getName() tool} by using {@link #installDownloadedToolPayload(ToolInstallRequest, Path, Path)} for tool-specific
226
   * logic, backing up any existing installation, and writing the version file.
227
   * <p>
228
   * This method assumes that the version has already been resolved and dependencies installed. It handles the final steps of placing the tool into the
229
   * appropriate installation directory.
230
   *
231
   * @param request the {@link ToolInstallRequest}.
232
   * @param installationPath the target {@link Path} where the {@link #getName() tool} should be installed.
233
   */
234
  protected void performToolInstallation(ToolInstallRequest request, Path installationPath) {
235

236
    FileAccess fileAccess = this.context.getFileAccess();
4✔
237
    ToolEditionAndVersion requested = request.getRequested();
3✔
238
    VersionIdentifier resolvedVersion = requested.getResolvedVersion();
3✔
239
    Path downloadedToolFile = downloadTool(requested.getEdition().edition(), resolvedVersion);
7✔
240

241
    if (Files.isDirectory(installationPath)) {
5!
242
      if (this.tool.equals(IdeasyCommandlet.TOOL_NAME)) {
×
243
        LOG.warn("Your IDEasy installation is missing the version file.");
×
244
      } else {
245
        fileAccess.backup(installationPath);
×
246
      }
247
    }
248
    fileAccess.mkdirs(installationPath.getParent());
4✔
249

250
    installDownloadedToolPayload(request, installationPath, downloadedToolFile);
5✔
251

252
    this.context.writeVersionFile(resolvedVersion, installationPath);
5✔
253
    // fix macOS Gatekeeper blocking - must run after version file is written but before any executables are launched
254
    getMacOsHelper().removeQuarantineAttribute(installationPath);
4✔
255
    LOG.debug("Installed {} in version {} at {}", this.tool, resolvedVersion, installationPath);
18✔
256
  }
1✔
257

258
  /**
259
   * Performs the actual installation of the tool bits.
260
   *
261
   * @param request the {@link ToolInstallRequest}.
262
   * @param installationPath the target {@link Path} where the tool should be installed.
263
   * @param downloadedToolFile the {@link Path} to the downloaded tool file.
264
   */
265
  protected void installDownloadedToolPayload(ToolInstallRequest request, Path installationPath, Path downloadedToolFile) {
266

267
    boolean extract = isExtract();
3✔
268
    if (!extract) {
2✔
269
      LOG.trace("Extraction is disabled for '{}' hence just moving the downloaded file {}.", this.tool, downloadedToolFile);
6✔
270
    }
271
    this.context.getFileAccess().extract(downloadedToolFile, installationPath, this::postExtract, extract);
9✔
272
  }
1✔
273

274
  /**
275
   * @param edition the {@link #getConfiguredEdition() tool edition} to download.
276
   * @param resolvedVersion the resolved {@link VersionIdentifier version} to download.
277
   * @return the {@link Path} to the downloaded release file.
278
   */
279
  protected Path downloadTool(String edition, VersionIdentifier resolvedVersion) {
280
    return getToolRepository().download(this.tool, edition, resolvedVersion, this);
9✔
281
  }
282

283
  /**
284
   * Install this tool as dependency of another tool.
285
   *
286
   * @param versionRange the required {@link VersionRange}. See {@link ToolDependency#versionRange()}.
287
   * @param parentRequest the {@link ToolInstallRequest} of the tool causing this dependency.
288
   * @return the corresponding {@link ToolInstallation}.
289
   */
290
  public ToolInstallation installAsDependency(VersionRange versionRange, ToolInstallRequest parentRequest) {
291
    ToolInstallRequest request = new ToolInstallRequest(parentRequest);
5✔
292
    ToolEditionAndVersion requested = new ToolEditionAndVersion(getToolWithConfiguredEdition());
6✔
293
    request.setRequested(requested);
3✔
294
    VersionIdentifier configuredVersion = getConfiguredVersion();
3✔
295
    if (versionRange.contains(configuredVersion)) {
4✔
296
      // prefer configured version if contained in version range
297
      requested.setVersion(configuredVersion);
3✔
298
      // return install(true, configuredVersion, processContext, null);
299
      return install(request);
4✔
300
    } else {
301
      if (isIgnoreSoftwareRepo()) {
3!
302
        throw new IllegalStateException(
×
303
            "Cannot satisfy dependency to " + this.tool + " in version " + versionRange + " for " + parentRequest.getRequested()
×
304
                + " since it is conflicting with configured version "
305
                + configuredVersion
306
                + " and this tool does not support the software repository.");
307
      }
308
      LOG.info(
8✔
309
          "The tool {} requires {} in the version range {}, but your project uses version {}, which does not match."
310
              + " Therefore, we install a compatible version in that range.",
311
          parentRequest.getRequested().getEdition(), this.tool, versionRange, configuredVersion);
16✔
312
      requested.setVersion(versionRange);
3✔
313
      return installTool(request);
4✔
314
    }
315
  }
316

317
  /**
318
   * Installs the tool dependencies for the current tool.
319
   *
320
   * @param request the {@link ToolInstallRequest}.
321
   */
322
  protected void installToolDependencies(ToolInstallRequest request) {
323

324
    ToolEditionAndVersion requested = request.getRequested();
3✔
325
    VersionIdentifier version = requested.getResolvedVersion();
3✔
326
    ToolEdition toolEdition = requested.getEdition();
3✔
327
    Collection<ToolDependency> dependencies = getToolRepository().findDependencies(this.tool, toolEdition.edition(), version);
9✔
328
    int size = dependencies.size();
3✔
329
    LOG.debug("Tool {} has {} other tool(s) as dependency", toolEdition, size);
6✔
330
    for (ToolDependency dependency : dependencies) {
10✔
331
      LOG.trace("Ensuring dependency {} for tool {}", dependency.tool(), toolEdition);
6✔
332
      LocalToolCommandlet dependencyTool = this.context.getCommandletManager().getRequiredLocalToolCommandlet(dependency.tool());
7✔
333
      dependencyTool.installAsDependency(dependency.versionRange(), request);
6✔
334
    }
1✔
335
  }
1✔
336

337
  /**
338
   * Post-extraction hook that can be overridden to add custom processing after unpacking and before moving to the final destination folder.
339
   *
340
   * @param extractedDir the {@link Path} to the folder with the unpacked tool.
341
   */
342
  protected void postExtract(Path extractedDir) {
343

344
  }
1✔
345

346
  @Override
347
  public VersionIdentifier getInstalledVersion() {
348

349
    return getInstalledVersion(getToolPath());
5✔
350
  }
351

352
  /**
353
   * @param toolPath the installation {@link Path} where to find the version file.
354
   * @return the currently installed {@link VersionIdentifier version} of this tool or {@code null} if not installed.
355
   */
356
  protected VersionIdentifier getInstalledVersion(Path toolPath) {
357

358
    if (isToolNotInstalled(toolPath)) {
4✔
359
      return null;
2✔
360
    }
361
    Path versionLookupPath = getInstalledSoftwareRepoPath(toolPath, false);
5✔
362
    if (versionLookupPath == null) {
2✔
363
      versionLookupPath = toolPath;
2✔
364
    }
365
    Path toolVersionFile = versionLookupPath.resolve(IdeContext.FILE_SOFTWARE_VERSION);
4✔
366
    if (!Files.exists(toolVersionFile)) {
5✔
367
      Path legacyToolVersionFile = versionLookupPath.resolve(IdeContext.FILE_LEGACY_SOFTWARE_VERSION);
4✔
368
      if (Files.exists(legacyToolVersionFile)) {
5!
369
        toolVersionFile = legacyToolVersionFile;
3✔
370
      } else {
371
        LOG.warn("Tool {} is missing version file in {}", getName(), toolVersionFile);
×
372
        return null;
×
373
      }
374
    }
375
    String version = this.context.getFileAccess().readFileContent(toolVersionFile).trim();
7✔
376
    return VersionIdentifier.of(version);
3✔
377
  }
378

379
  @Override
380
  public String getInstalledEdition() {
381

382
    return getInstalledEdition(getToolPath());
5✔
383
  }
384

385
  /**
386
   * @param toolPath the installation {@link Path} where to find currently installed tool. The name of the parent directory of the real path corresponding
387
   *     to the passed {@link Path path} must be the name of the edition.
388
   * @return the installed edition of this tool or {@code null} if not installed.
389
   */
390
  protected String getInstalledEdition(Path toolPath) {
391
    if (isToolNotInstalled(toolPath)) {
4✔
392
      return null;
2✔
393
    }
394
    Path realPath = this.context.getFileAccess().toRealPath(toolPath);
6✔
395
    // if the realPath changed, a link has been resolved
396
    if (realPath.equals(toolPath)) {
4✔
397
      if (!isIgnoreSoftwareRepo()) {
3✔
398
        LOG.warn("Tool {} is not installed via software repository (maybe from devonfw-ide). Please consider reinstalling it.", this.tool);
5✔
399
      }
400
      // 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
401
      return getConfiguredEdition();
3✔
402
    }
403
    Path toolRepoFolder = context.getSoftwareRepositoryPath().resolve(ToolRepository.ID_DEFAULT).resolve(this.tool);
9✔
404
    String edition = getEdition(toolRepoFolder, realPath);
5✔
405
    if (edition == null) {
2!
406
      edition = this.tool;
×
407
    }
408
    if (!getToolRepository().getSortedEditions(this.tool).contains(edition)) {
8✔
409
      LOG.warn("Undefined edition {} of tool {}", edition, this.tool);
6✔
410
    }
411
    return edition;
2✔
412
  }
413

414
  private String getEdition(Path toolRepoFolder, Path toolInstallFolder) {
415

416
    int toolRepoNameCount = toolRepoFolder.getNameCount();
3✔
417
    int toolInstallNameCount = toolInstallFolder.getNameCount();
3✔
418
    if (toolRepoNameCount < toolInstallNameCount) {
3!
419
      // ensure toolInstallFolder starts with $IDE_ROOT/_ide/software/default/«tool»
420
      for (int i = 0; i < toolRepoNameCount; i++) {
7✔
421
        if (!toolRepoFolder.getName(i).toString().equals(toolInstallFolder.getName(i).toString())) {
10!
422
          return null;
×
423
        }
424
      }
425
      return toolInstallFolder.getName(toolRepoNameCount).toString();
5✔
426
    }
427
    return null;
×
428
  }
429

430
  private Path getInstalledSoftwareRepoPath(Path toolPath) {
431
    return getInstalledSoftwareRepoPath(toolPath, false);
×
432
  }
433

434
  private Path getInstalledSoftwareRepoPath(Path toolPath, boolean logIfNotSoftwareRepo) {
435
    if (isToolNotInstalled(toolPath)) {
4!
436
      return null;
×
437
    }
438
    Path installPath = this.context.getFileAccess().toRealPath(toolPath);
6✔
439
    // if the installPath changed, a link has been resolved
440
    if (installPath.equals(toolPath)) {
4✔
441
      if (logIfNotSoftwareRepo && !isIgnoreSoftwareRepo()) {
2!
442
        LOG.warn("Tool {} is not installed via software repository (maybe from devonfw-ide). Please consider reinstalling it.", this.tool);
×
443
      }
444
      // 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
445
      return null;
2✔
446
    }
447
    installPath = getValidInstalledSoftwareRepoPath(installPath, context.getSoftwareRepositoryPath());
7✔
448
    return installPath;
2✔
449
  }
450

451
  Path getValidInstalledSoftwareRepoPath(Path installPath, Path softwareRepoPath) {
452
    int softwareRepoNameCount = softwareRepoPath.getNameCount();
3✔
453
    int toolInstallNameCount = installPath.getNameCount();
3✔
454
    int targetToolInstallNameCount = softwareRepoNameCount + 4;
4✔
455

456
    // installPath can't be shorter than softwareRepoPath
457
    if (toolInstallNameCount < softwareRepoNameCount) {
3!
458
      LOG.warn("The installation path is not located within the software repository {}.", installPath);
×
459
      return null;
×
460
    }
461
    // ensure installPath starts with $IDE_ROOT/_ide/software/
462
    for (int i = 0; i < softwareRepoNameCount; i++) {
7✔
463
      if (!softwareRepoPath.getName(i).toString().equals(installPath.getName(i).toString())) {
10✔
464
        LOG.warn("The installation path is not located within the software repository {}.", installPath);
4✔
465
        return null;
2✔
466
      }
467
    }
468
    // return $IDE_ROOT/_ide/software/«id»/«tool»/«edition»/«version»
469
    if (toolInstallNameCount == targetToolInstallNameCount) {
3✔
470
      return installPath;
2✔
471
    } else if (toolInstallNameCount > targetToolInstallNameCount) {
3✔
472
      Path validInstallPath = installPath;
2✔
473
      for (int i = 0; i < toolInstallNameCount - targetToolInstallNameCount; i++) {
9✔
474
        validInstallPath = validInstallPath.getParent();
3✔
475
      }
476
      return validInstallPath;
2✔
477
    } else {
478
      LOG.warn("The installation path is faulty {}.", installPath);
4✔
479
      return null;
2✔
480
    }
481
  }
482

483
  private boolean isToolNotInstalled(Path toolPath) {
484

485
    if ((toolPath == null) || !Files.isDirectory(toolPath)) {
7✔
486
      LOG.debug("Tool {} not installed in {}", this.tool, toolPath);
6✔
487
      return true;
2✔
488
    }
489
    return false;
2✔
490
  }
491

492
  @Override
493
  public void uninstall() {
494
    try {
495
      Path toolPath = getToolPath();
3✔
496
      if (!Files.exists(toolPath)) {
5!
497
        LOG.warn("An installed version of {} does not exist.", this.tool);
×
498
        return;
×
499
      }
500
      if (this.context.isForceMode() && !isIgnoreSoftwareRepo()) {
7!
501
        LOG.warn(
6✔
502
            "You triggered an uninstall of {} in version {} with force mode!\n"
503
                + "This will physically delete the currently installed version including its plugins from the machine.\n"
504
                + "This may cause issues with other projects, that use the same version of that tool."
505
            , this.tool, getInstalledVersion());
1✔
506
        uninstallPluginsOfTool();
2✔
507
        uninstallFromSoftwareRepository(toolPath);
3✔
508
      }
509
      performUninstall(toolPath);
3✔
510
      IdeLogLevel.SUCCESS.log(LOG, "Successfully uninstalled {}", this.tool);
11✔
511
    } catch (Exception e) {
×
512
      LOG.error("Failed to uninstall {}", this.tool, e);
×
513
    }
1✔
514
  }
1✔
515

516
  /**
517
   * Performs the actual uninstallation of this tool.
518
   *
519
   * @param toolPath the current {@link #getToolPath() tool path}.
520
   */
521
  protected void performUninstall(Path toolPath) {
522
    this.context.getFileAccess().delete(toolPath);
5✔
523
  }
1✔
524

525
  /**
526
   * Deletes the installed version of the tool from the shared software repository.
527
   */
528
  private void uninstallFromSoftwareRepository(Path toolPath) {
529
    Path repoPath = getInstalledSoftwareRepoPath(toolPath, true);
5✔
530
    if ((repoPath == null) || !Files.exists(repoPath)) {
7!
531
      LOG.warn("An installed version of {} does not exist in software repository.", this.tool);
×
532
      return;
×
533
    }
534
    LOG.info("Physically deleting {} as requested by the user via force mode.", repoPath);
4✔
535
    this.context.getFileAccess().delete(repoPath);
5✔
536
    IdeLogLevel.SUCCESS.log(LOG, "Successfully deleted {} from your computer.", repoPath);
10✔
537
  }
1✔
538

539
  /**
540
   * Deletes the installed plugins of the tool from the plugins path.
541
   */
542
  private void uninstallPluginsOfTool() {
543

544
    Path toolPluginsPath = this.context.getPluginsPath().resolve(this.tool);
7✔
545
    if (!Files.exists(toolPluginsPath)) {
5!
546
      LOG.info("There are no plugins of {} present to delete.", this.tool);
5✔
547
      return;
1✔
548
    }
549
    LOG.info("Physically deleting {} as requested by the user via force mode.", toolPluginsPath);
×
550
    this.context.getFileAccess().delete(toolPluginsPath);
×
551
    IdeLogLevel.SUCCESS.log(LOG, "Successfully deleted {} from your computer.", toolPluginsPath);
×
552
  }
×
553

554
  @Override
555
  protected Path getInstallationPath(String edition, VersionIdentifier resolvedVersion) {
556
    Path installationPath;
557
    if (isIgnoreSoftwareRepo()) {
3✔
558
      installationPath = getToolPath();
4✔
559
    } else {
560
      Path softwareRepositoryPath = this.context.getSoftwareRepositoryPath();
4✔
561
      if (softwareRepositoryPath == null) {
2!
562
        return null;
×
563
      }
564
      Path softwareRepoPath = softwareRepositoryPath.resolve(getToolRepository().getId()).resolve(this.tool).resolve(edition);
11✔
565
      installationPath = softwareRepoPath.resolve(resolvedVersion.toString());
5✔
566
    }
567
    return installationPath;
2✔
568
  }
569

570
  /**
571
   * @return {@link VersionIdentifier} with latest version of the tool}.
572
   */
573
  public VersionIdentifier getLatestToolVersion() {
574

575
    return this.context.getDefaultToolRepository().resolveVersion(this.tool, getConfiguredEdition(), VersionIdentifier.LATEST, this);
×
576
  }
577

578

579
  /**
580
   * Searches for a wrapper file in valid projects (containing a build file f.e. build.gradle or pom.xml) and returns its path.
581
   *
582
   * @param wrapperFileName the name of the wrapper file
583
   * @return Path of the wrapper file or {@code null} if none was found.
584
   */
585
  protected Path findWrapper(String wrapperFileName) {
586
    Path dir = this.context.getCwd();
4✔
587
    // traverse the cwd directory containing a build descriptor up till a wrapper file was found
588
    while ((dir != null) && (findBuildDescriptor(dir) != null)) {
6!
589
      Path wrapper = dir.resolve(wrapperFileName);
4✔
590
      if (Files.exists(wrapper)) {
5✔
591
        LOG.debug("Using wrapper: {}", wrapper);
4✔
592
        return wrapper;
2✔
593
      }
594
      dir = dir.getParent();
3✔
595
    }
1✔
596
    return null;
2✔
597
  }
598

599
  /**
600
   * @param directory the {@link Path} to the build directory.
601
   * @return the build configuration file for this tool or {@code null} if not found (or this is not a build tool).
602
   */
603
  public Path findBuildDescriptor(Path directory) {
604
    return null;
2✔
605
  }
606

607
  /**
608
   * @return Bash completion command for this tool or {@code null} if this tool does not provide Bash completion.
609
   */
610
  public String getBashCompletion() {
611

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

© 2026 Coveralls, Inc