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

devonfw / IDEasy / 18646729507

20 Oct 2025 08:33AM UTC coverage: 68.511% (-0.02%) from 68.526%
18646729507

push

github

web-flow
#1530: fixed NPE on ide update with custom tool (#1531)

3466 of 5541 branches covered (62.55%)

Branch coverage included in aggregate %.

9055 of 12735 relevant lines covered (71.1%)

3.13 hits per line

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

82.19
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
import java.util.function.Predicate;
9

10
import com.devonfw.tools.ide.common.Tag;
11
import com.devonfw.tools.ide.context.IdeContext;
12
import com.devonfw.tools.ide.environment.EnvironmentVariables;
13
import com.devonfw.tools.ide.io.FileAccess;
14
import com.devonfw.tools.ide.io.FileCopyMode;
15
import com.devonfw.tools.ide.log.IdeSubLogger;
16
import com.devonfw.tools.ide.process.EnvironmentContext;
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 {
1✔
29

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

39
    super(context, tool, tags);
5✔
40
  }
1✔
41

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

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

57
    Path toolPath = getToolPath();
3✔
58
    Path binPath = this.context.getFileAccess().findFirst(toolPath, path -> path.getFileName().toString().equals("bin"), false);
14✔
59
    if ((binPath != null) && Files.isDirectory(binPath)) {
7!
60
      return binPath;
2✔
61
    }
62
    return toolPath;
2✔
63
  }
64

65
  /**
66
   * @return {@code true} to ignore a missing {@link IdeContext#FILE_SOFTWARE_VERSION software version file} in an installation, {@code false} delete the broken
67
   *     installation (default).
68
   */
69
  protected boolean isIgnoreMissingSoftwareVersionFile() {
70

71
    return false;
×
72
  }
73

74
  /**
75
   * @deprecated will be removed once all "dependencies.json" are created in ide-urls.
76
   */
77
  @Deprecated
78
  protected void installDependencies() {
79

80
  }
1✔
81

82
  @Override
83
  public boolean install(boolean silent, ProcessContext processContext, Step step) {
84

85
    installDependencies();
2✔
86
    VersionIdentifier configuredVersion = getConfiguredVersion();
3✔
87
    // get installed version before installInRepo actually may install the software
88
    VersionIdentifier installedVersion = getInstalledVersion();
3✔
89
    if (step == null) {
2!
90
      return doInstallStep(configuredVersion, installedVersion, silent, processContext, step);
8✔
91
    } else {
92
      return step.call(() -> doInstallStep(configuredVersion, installedVersion, silent, processContext, step), Boolean.FALSE);
×
93
    }
94
  }
95

96
  private boolean doInstallStep(VersionIdentifier configuredVersion, VersionIdentifier installedVersion, boolean silent, ProcessContext processContext,
97
      Step step) {
98

99
    // install configured version of our tool in the software repository if not already installed
100
    ToolInstallation installation = installTool(configuredVersion, processContext);
5✔
101

102
    // check if we already have this version installed (linked) locally in IDE_HOME/software
103
    VersionIdentifier resolvedVersion = installation.resolvedVersion();
3✔
104
    if ((resolvedVersion.equals(installedVersion) && !installation.newInstallation()) || (configuredVersion.matches(installedVersion)
13✔
105
        && context.isSkipUpdatesMode())) {
2!
106
      return toolAlreadyInstalled(silent, installedVersion, processContext);
6✔
107
    }
108
    FileAccess fileAccess = this.context.getFileAccess();
4✔
109
    boolean ignoreSoftwareRepo = isIgnoreSoftwareRepo();
3✔
110
    if (!ignoreSoftwareRepo) {
2✔
111
      Path toolPath = getToolPath();
3✔
112
      // we need to link the version or update the link.
113
      if (Files.exists(toolPath, LinkOption.NOFOLLOW_LINKS)) {
9✔
114
        fileAccess.backup(toolPath);
4✔
115
      }
116
      fileAccess.mkdirs(toolPath.getParent());
4✔
117
      fileAccess.symlink(installation.linkDir(), toolPath);
5✔
118
    }
119
    if (installation.binDir() != null) {
3!
120
      this.context.getPath().setPath(this.tool, installation.binDir());
8✔
121
    }
122
    postInstall(true, processContext);
4✔
123
    if (installedVersion == null) {
2✔
124
      asSuccess(step).log("Successfully installed {} in version {}", this.tool, resolvedVersion);
18✔
125
    } else {
126
      asSuccess(step).log("Successfully installed {} in version {} replacing previous version {}", this.tool, resolvedVersion, installedVersion);
21✔
127
    }
128
    return true;
2✔
129
  }
130

131
  /**
132
   * This method is called after a tool was requested to be installed or updated.
133
   *
134
   * @param newlyInstalled {@code true} if the tool was installed or updated (at least link to software folder was created/updated), {@code false} otherwise
135
   *     (configured version was already installed and nothing changed).
136
   * @param pc the {@link ProcessContext} to use.
137
   */
138
  protected void postInstall(boolean newlyInstalled, ProcessContext pc) {
139

140
    if (newlyInstalled) {
2✔
141
      postInstall();
2✔
142
    }
143
  }
1✔
144

145
  /**
146
   * This method is called after the tool has been newly installed or updated to a new version.
147
   */
148
  protected void postInstall() {
149

150
    // nothing to do by default
151
  }
1✔
152

153
  private boolean toolAlreadyInstalled(boolean silent, VersionIdentifier installedVersion, ProcessContext pc) {
154
    IdeSubLogger logger;
155
    if (silent) {
2✔
156
      logger = this.context.debug();
5✔
157
    } else {
158
      logger = this.context.info();
4✔
159
    }
160
    logger.log("Version {} of tool {} is already installed", installedVersion, getToolWithEdition());
15✔
161
    postInstall(false, pc);
4✔
162
    return false;
2✔
163
  }
164

165
  /**
166
   * Determines whether this tool should be installed directly in the software folder or in the software repository.
167
   *
168
   * @return {@code true} if the tool should be installed directly in the software folder, ignoring the central software repository; {@code false} if the tool
169
   *     should be installed in the central software repository (default behavior).
170
   */
171
  protected boolean isIgnoreSoftwareRepo() {
172

173
    return false;
2✔
174
  }
175

176
  /**
177
   * Performs the installation of the {@link #getName() tool} together with the environment context, managed by this
178
   * {@link com.devonfw.tools.ide.commandlet.Commandlet}.
179
   *
180
   * @param version the {@link GenericVersionRange} requested to be installed.
181
   * @param processContext the {@link ProcessContext} used to
182
   *     {@link #setEnvironment(EnvironmentContext, ToolInstallation, boolean) configure environment variables}.
183
   * @return the {@link ToolInstallation} matching the given {@code version}.
184
   */
185
  public ToolInstallation installTool(GenericVersionRange version, ProcessContext processContext) {
186

187
    return installTool(version, processContext, getConfiguredEdition());
7✔
188
  }
189

190
  /**
191
   * Performs the installation of the {@link #getName() tool} together with the environment context  managed by this
192
   * {@link com.devonfw.tools.ide.commandlet.Commandlet}.
193
   *
194
   * @param version the {@link GenericVersionRange} requested to be installed.
195
   * @param processContext the {@link ProcessContext} used to
196
   *     {@link #setEnvironment(EnvironmentContext, ToolInstallation, boolean) configure environment variables}.
197
   * @param edition the specific {@link #getConfiguredEdition() edition} to install.
198
   * @return the {@link ToolInstallation} matching the given {@code version}.
199
   */
200
  public ToolInstallation installTool(GenericVersionRange version, ProcessContext processContext, String edition) {
201

202
    // if version is a VersionRange, we are not called from install() but directly from installAsDependency() due to a version conflict of a dependency
203
    boolean extraInstallation = (version instanceof VersionRange);
3✔
204
    ToolRepository toolRepository = getToolRepository();
3✔
205
    VersionIdentifier resolvedVersion = toolRepository.resolveVersion(this.tool, edition, version, this);
8✔
206
    installToolDependencies(resolvedVersion, edition, processContext);
5✔
207

208
    Path installationPath;
209
    boolean ignoreSoftwareRepo = isIgnoreSoftwareRepo();
3✔
210
    if (ignoreSoftwareRepo) {
2✔
211
      installationPath = getToolPath();
4✔
212
    } else {
213
      Path softwareRepoPath = this.context.getSoftwareRepositoryPath().resolve(toolRepository.getId()).resolve(this.tool).resolve(edition);
12✔
214
      installationPath = softwareRepoPath.resolve(resolvedVersion.toString());
5✔
215
    }
216
    VersionIdentifier installedVersion = getInstalledVersion();
3✔
217
    String installedEdition = getInstalledEdition();
3✔
218
    if (resolvedVersion.equals(installedVersion) && edition.equals(installedEdition)) {
8✔
219
      this.context.debug("Version {} of tool {} is already installed at {}", resolvedVersion, getToolWithEdition(this.tool, edition), installationPath);
21✔
220
      return createToolInstallation(installationPath, resolvedVersion, false, processContext, extraInstallation);
8✔
221
    }
222
    Path toolVersionFile = installationPath.resolve(IdeContext.FILE_SOFTWARE_VERSION);
4✔
223
    FileAccess fileAccess = this.context.getFileAccess();
4✔
224
    if (Files.isDirectory(installationPath)) {
5✔
225
      if (Files.exists(toolVersionFile)) {
5!
226
        if (!ignoreSoftwareRepo) {
2✔
227
          assert resolvedVersion.equals(getInstalledVersion(installationPath)) :
7!
228
              "Found version " + getInstalledVersion(installationPath) + " in " + toolVersionFile + " but expected " + resolvedVersion;
×
229
          this.context.debug("Version {} of tool {} is already installed at {}", resolvedVersion, getToolWithEdition(this.tool, edition), installationPath);
21✔
230
          return createToolInstallation(installationPath, resolvedVersion, false, processContext, extraInstallation);
8✔
231
        }
232
      } else {
233
        // Makes sure that IDEasy will not delete itself
234
        if (this.tool.equals(IdeasyCommandlet.TOOL_NAME)) {
×
235
          this.context.warning("Your IDEasy installation is missing the version file at {}", toolVersionFile);
×
236
          return createToolInstallation(installationPath, resolvedVersion, false, processContext, extraInstallation);
×
237
        } else if (!isIgnoreMissingSoftwareVersionFile()) {
×
238
          this.context.warning("Deleting corrupted installation at {}", installationPath);
×
239
          fileAccess.delete(installationPath);
×
240
        }
241
      }
242
    }
243
    performToolInstallation(toolRepository, resolvedVersion, installationPath, edition, processContext);
7✔
244
    return createToolInstallation(installationPath, resolvedVersion, true, processContext, extraInstallation);
8✔
245
  }
246

247
  /**
248
   * Performs the actual installation of the {@link #getName() tool} by downloading its binary, optionally extracting it, backing up any existing installation,
249
   * 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 toolRepository the {@link ToolRepository} used to locate and download the tool.
255
   * @param resolvedVersion the resolved {@link VersionIdentifier} of the {@link #getName() tool} to install.
256
   * @param installationPath the target {@link Path} where the {@link #getName() tool} should be installed.
257
   * @param edition the specific edition of the tool to install.
258
   * @param processContext the {@link ProcessContext} used to manage the installation process.
259
   */
260
  protected void performToolInstallation(ToolRepository toolRepository, VersionIdentifier resolvedVersion, Path installationPath,
261
      String edition, ProcessContext processContext) {
262

263
    FileAccess fileAccess = this.context.getFileAccess();
4✔
264
    Path downloadedToolFile = downloadTool(edition, toolRepository, resolvedVersion);
6✔
265
    boolean extract = isExtract();
3✔
266
    if (!extract) {
2✔
267
      this.context.trace("Extraction is disabled for '{}' hence just moving the downloaded file {}.", this.tool, downloadedToolFile);
15✔
268
    }
269
    if (Files.isDirectory(installationPath)) {
5!
270
      if (this.tool.equals(IdeasyCommandlet.TOOL_NAME)) {
×
271
        this.context.warning("Your IDEasy installation is missing the version file.");
×
272
      } else {
273
        fileAccess.backup(installationPath);
×
274
      }
275
    }
276
    fileAccess.mkdirs(installationPath.getParent());
4✔
277
    fileAccess.extract(downloadedToolFile, installationPath, this::postExtract, extract);
7✔
278
    this.context.writeVersionFile(resolvedVersion, installationPath);
5✔
279
    this.context.debug("Installed {} in version {} at {}", this.tool, resolvedVersion, installationPath);
19✔
280
  }
1✔
281

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

292
  /**
293
   * Install this tool as dependency of another tool.
294
   *
295
   * @param version the required {@link VersionRange}. See {@link ToolDependency#versionRange()}.
296
   * @param processContext the {@link ProcessContext}.
297
   * @param toolParent the parent tool name needing the dependency
298
   * @return {@code true} if the tool was newly installed, {@code false} otherwise (installation was already present).
299
   */
300
  public boolean installAsDependency(VersionRange version, ProcessContext processContext, String toolParent) {
301

302
    VersionIdentifier configuredVersion = getConfiguredVersion();
3✔
303
    if (version.contains(configuredVersion)) {
4✔
304
      // prefer configured version if contained in version range
305
      return install(true, processContext, null);
6✔
306
    } else {
307
      if (isIgnoreSoftwareRepo()) {
3!
308
        throw new IllegalStateException(
×
309
            "Cannot satisfy dependency to " + this.tool + " in version " + version + " since it is conflicting with configured version " + configuredVersion
310
                + " and this tool does not support the software repository.");
311
      }
312
      this.context.info(
23✔
313
          "The tool {} requires {} in the version range {}, but your project uses version {}, which does not match."
314
              + " Therefore, we install a compatible version in that range.",
315
          toolParent, this.tool, version, configuredVersion);
316
    }
317
    ToolInstallation toolInstallation = installTool(version, processContext);
5✔
318
    return toolInstallation.newInstallation();
3✔
319
  }
320

321
  /**
322
   * Installs the tool dependencies for the current tool.
323
   *
324
   * @param version the {@link VersionIdentifier} to use.
325
   * @param edition the edition to use.
326
   * @param processContext the {@link ProcessContext} to use.
327
   */
328
  protected void installToolDependencies(VersionIdentifier version, String edition, ProcessContext processContext) {
329
    Collection<ToolDependency> dependencies = getToolRepository().findDependencies(this.tool, edition, version);
8✔
330
    String toolWithEdition = getToolWithEdition(this.tool, edition);
5✔
331
    int size = dependencies.size();
3✔
332
    this.context.debug("Tool {} has {} other tool(s) as dependency", toolWithEdition, size);
15✔
333
    for (ToolDependency dependency : dependencies) {
10✔
334
      this.context.trace("Ensuring dependency {} for tool {}", dependency.tool(), toolWithEdition);
15✔
335
      LocalToolCommandlet dependencyTool = this.context.getCommandletManager().getRequiredLocalToolCommandlet(dependency.tool());
7✔
336
      dependencyTool.installAsDependency(dependency.versionRange(), processContext, toolWithEdition);
7✔
337
    }
1✔
338
  }
1✔
339

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

347
  }
1✔
348

349
  @Override
350
  public VersionIdentifier getInstalledVersion() {
351

352
    return getInstalledVersion(this.context.getSoftwarePath().resolve(getName()));
9✔
353
  }
354

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

361
    if (!Files.isDirectory(toolPath)) {
5✔
362
      this.context.debug("Tool {} not installed in {}", getName(), toolPath);
15✔
363
      return null;
2✔
364
    }
365
    Path toolVersionFile = toolPath.resolve(IdeContext.FILE_SOFTWARE_VERSION);
4✔
366
    if (!Files.exists(toolVersionFile)) {
5✔
367
      Path legacyToolVersionFile = toolPath.resolve(IdeContext.FILE_LEGACY_SOFTWARE_VERSION);
4✔
368
      if (Files.exists(legacyToolVersionFile)) {
5✔
369
        toolVersionFile = legacyToolVersionFile;
3✔
370
      } else {
371
        this.context.warning("Tool {} is missing version file in {}", getName(), toolVersionFile);
15✔
372
        return null;
2✔
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
    if (this.context.getSoftwarePath() == null) {
4!
383
      return "";
×
384
    }
385
    return getInstalledEdition(this.context.getSoftwarePath().resolve(this.tool));
9✔
386
  }
387

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

418
  private String getEdition(Path toolRepoFolder, Path toolInstallFolder) {
419

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

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

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

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

484
  @Override
485
  public void uninstall() {
486
    try {
487
      Path toolPath = getToolPath();
3✔
488
      if (!Files.exists(toolPath)) {
5!
489
        this.context.warning("An installed version of {} does not exist.", this.tool);
×
490
        return;
×
491
      }
492
      if (this.context.isForceMode() && !isIgnoreSoftwareRepo()) {
7!
493
        this.context.warning(
14✔
494
            "You triggered an uninstall of {} in version {} with force mode!\n"
495
                + "This will physically delete the currently installed version from the machine.\n"
496
                + "This may cause issues with other projects, that use the same version of that tool."
497
            , this.tool, getInstalledVersion());
2✔
498
        uninstallFromSoftwareRepository(toolPath);
3✔
499
      }
500
      performUninstall(toolPath);
3✔
501
      this.context.success("Successfully uninstalled {}", this.tool);
11✔
502
    } catch (Exception e) {
×
503
      this.context.error(e, "Failed to uninstall {}", this.tool);
×
504
    }
1✔
505
  }
1✔
506

507
  /**
508
   * Performs the actual uninstallation of this tool.
509
   *
510
   * @param toolPath the current {@link #getToolPath() tool path}.
511
   */
512
  protected void performUninstall(Path toolPath) {
513
    this.context.getFileAccess().delete(toolPath);
5✔
514
  }
1✔
515

516
  /**
517
   * Deletes the installed version of the tool from the shared software repository.
518
   */
519
  private void uninstallFromSoftwareRepository(Path toolPath) {
520
    Path repoPath = getInstalledSoftwareRepoPath(toolPath);
4✔
521
    if ((repoPath == null) || !Files.exists(repoPath)) {
7!
522
      this.context.warning("An installed version of {} does not exist in software repository.", this.tool);
×
523
      return;
×
524
    }
525
    this.context.info("Physically deleting {} as requested by the user via force mode.", repoPath);
10✔
526
    this.context.getFileAccess().delete(repoPath);
5✔
527
    this.context.success("Successfully deleted {} from your computer.", repoPath);
10✔
528
  }
1✔
529

530

531
  private ToolInstallation createToolInstallation(Path rootDir, VersionIdentifier resolvedVersion, boolean newInstallation,
532
      EnvironmentContext environmentContext, boolean extraInstallation) {
533

534
    Path linkDir = getMacOsHelper().findLinkDir(rootDir, getBinaryName());
7✔
535
    Path binDir = linkDir;
2✔
536
    Path binFolder = binDir.resolve(IdeContext.FOLDER_BIN);
4✔
537
    if (Files.isDirectory(binFolder)) {
5✔
538
      binDir = binFolder;
2✔
539
    }
540
    if (linkDir != rootDir) {
3✔
541
      assert (!linkDir.equals(rootDir));
5!
542
      Path toolVersionFile = rootDir.resolve(IdeContext.FILE_SOFTWARE_VERSION);
4✔
543
      if (Files.exists(toolVersionFile)) {
5!
544
        this.context.getFileAccess().copy(toolVersionFile, linkDir, FileCopyMode.COPY_FILE_OVERRIDE);
7✔
545
      }
546
    }
547
    ToolInstallation toolInstallation = new ToolInstallation(rootDir, linkDir, binDir, resolvedVersion, newInstallation);
9✔
548
    setEnvironment(environmentContext, toolInstallation, extraInstallation);
5✔
549
    return toolInstallation;
2✔
550
  }
551

552
  /**
553
   * Method to set environment variables for the process context.
554
   *
555
   * @param environmentContext the {@link EnvironmentContext} where to {@link EnvironmentContext#withEnvVar(String, String) set environment variables} for
556
   *     this tool.
557
   * @param toolInstallation the {@link ToolInstallation}.
558
   * @param extraInstallation {@code true} if the {@link ToolInstallation} is an additional installation to the
559
   *     {@link #getConfiguredVersion() configured version} due to a conflicting version of a {@link ToolDependency}, {@code false} otherwise.
560
   */
561
  public void setEnvironment(EnvironmentContext environmentContext, ToolInstallation toolInstallation, boolean extraInstallation) {
562

563
    String pathVariable = EnvironmentVariables.getToolVariablePrefix(this.tool) + "_HOME";
5✔
564
    Path toolHomePath = getToolHomePath(toolInstallation);
4✔
565
    if (toolHomePath != null) {
2!
566
      environmentContext.withEnvVar(pathVariable, toolHomePath.toString());
6✔
567
    }
568
    if (extraInstallation) {
2✔
569
      environmentContext.withPathEntry(toolInstallation.binDir());
5✔
570
    }
571
  }
1✔
572

573
  /**
574
   * Method to get the home path of the given {@link ToolInstallation}.
575
   *
576
   * @param toolInstallation the {@link ToolInstallation}.
577
   * @return the Path to the home of the tool
578
   */
579
  protected Path getToolHomePath(ToolInstallation toolInstallation) {
580
    return toolInstallation.linkDir();
3✔
581
  }
582

583
  /**
584
   * @return {@link VersionIdentifier} with latest version of the tool}.
585
   */
586
  public VersionIdentifier getLatestToolVersion() {
587

588
    return this.context.getDefaultToolRepository().resolveVersion(this.tool, getConfiguredEdition(), VersionIdentifier.LATEST, this);
×
589
  }
590

591

592
  /**
593
   * Searches for a wrapper file in valid projects (containing a build file f.e. build.gradle or pom.xml) and returns its path.
594
   *
595
   * @param wrapperFileName the name of the wrapper file
596
   * @param filter the {@link Predicate} to match
597
   * @return Path of the wrapper file or {@code null} if none was found.
598
   */
599
  protected Path findWrapper(String wrapperFileName, Predicate<Path> filter) {
600
    Path dir = context.getCwd();
4✔
601
    // traverse the cwd directory containing a build file up till a wrapper file was found
602
    while ((dir != null) && filter.test(dir)) {
6!
603
      if (Files.exists(dir.resolve(wrapperFileName))) {
7✔
604
        context.debug("Using wrapper file at: {}", dir);
10✔
605
        return dir.resolve(wrapperFileName);
4✔
606
      }
607
      dir = dir.getParent();
4✔
608
    }
609
    return null;
2✔
610
  }
611

612

613
}
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

© 2025 Coveralls, Inc