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

devonfw / IDEasy / 19476547793

18 Nov 2025 06:11PM UTC coverage: 69.018% (+0.1%) from 68.905%
19476547793

Pull #1593

github

web-flow
Merge d675296b0 into 553958662
Pull Request #1593: #1144: #1145: CVE warnings and suggestions

3569 of 5671 branches covered (62.93%)

Branch coverage included in aggregate %.

9307 of 12985 relevant lines covered (71.68%)

3.15 hits per line

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

81.25
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
    // check if we should skip updates and the configured version matches the installed version
100
    if (context.isSkipUpdatesMode() && configuredVersion.matches(installedVersion) && installedVersion != null) {
10!
101
      return toolAlreadyInstalled(silent, installedVersion, processContext);
6✔
102
    }
103

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

107
    // check if we already have this version installed (linked) locally in IDE_HOME/software
108
    VersionIdentifier resolvedVersion = installation.resolvedVersion();
3✔
109
    if (resolvedVersion.equals(installedVersion) && !installation.newInstallation()) {
7✔
110
      return toolAlreadyInstalled(silent, installedVersion, processContext);
6✔
111
    }
112
    FileAccess fileAccess = this.context.getFileAccess();
4✔
113
    boolean ignoreSoftwareRepo = isIgnoreSoftwareRepo();
3✔
114
    if (!ignoreSoftwareRepo) {
2✔
115
      Path toolPath = getToolPath();
3✔
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);
5✔
122
    }
123
    if (installation.binDir() != null) {
3!
124
      this.context.getPath().setPath(this.tool, installation.binDir());
8✔
125
    }
126
    postInstall(true, processContext);
4✔
127
    if (installedVersion == null) {
2✔
128
      asSuccess(step).log("Successfully installed {} in version {}", this.tool, resolvedVersion);
18✔
129
    } else {
130
      asSuccess(step).log("Successfully installed {} in version {} replacing previous version {}", this.tool, resolvedVersion, installedVersion);
21✔
131
    }
132
    return true;
2✔
133
  }
134

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

144
    if (newlyInstalled) {
2✔
145
      postInstall();
2✔
146
    }
147
  }
1✔
148

149
  /**
150
   * This method is called after the tool has been newly installed or updated to a new version.
151
   */
152
  protected void postInstall() {
153

154
    // nothing to do by default
155
  }
1✔
156

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

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

177
    return false;
2✔
178
  }
179

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

191
    return installTool(version, processContext, getToolWithEdition());
7✔
192
  }
193

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

206
    assert (toolEdition.tool().equals(this.tool)) : "Mismatch " + this.tool + " != " + toolEdition.tool();
7!
207
    // if version is a VersionRange, we are not called from install() but directly from installAsDependency() due to a version conflict of a dependency
208
    boolean extraInstallation = (version instanceof VersionRange);
3✔
209
    ToolRepository toolRepository = getToolRepository();
3✔
210
    String edition = toolEdition.edition();
3✔
211
    VersionIdentifier resolvedVersion = toolRepository.resolveVersion(this.tool, edition, version, this);
8✔
212
    GenericVersionRange allwedVersion = VersionIdentifier.LATEST;
2✔
213
    if (version.isPattern()) {
3✔
214
      // TODO this should be discussed...
215
      allwedVersion = version;
2✔
216
    }
217
    resolvedVersion = cveCheck(toolEdition, resolvedVersion, allwedVersion, false);
7✔
218
    installToolDependencies(resolvedVersion, toolEdition, processContext);
5✔
219

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

259
  /**
260
   * Performs the actual installation of the {@link #getName() tool} by downloading its binary, optionally extracting it, backing up any existing installation,
261
   * and writing the version file.
262
   * <p>
263
   * This method assumes that the version has already been resolved and dependencies installed. It handles the final steps of placing the tool into the
264
   * appropriate installation directory.
265
   *
266
   * @param toolRepository the {@link ToolRepository} used to locate and download the tool.
267
   * @param resolvedVersion the resolved {@link VersionIdentifier} of the {@link #getName() tool} to install.
268
   * @param installationPath the target {@link Path} where the {@link #getName() tool} should be installed.
269
   * @param edition the specific edition of the tool to install.
270
   * @param processContext the {@link ProcessContext} used to manage the installation process.
271
   */
272
  protected void performToolInstallation(ToolRepository toolRepository, VersionIdentifier resolvedVersion, Path installationPath,
273
      String edition, ProcessContext processContext) {
274

275
    FileAccess fileAccess = this.context.getFileAccess();
4✔
276
    Path downloadedToolFile = downloadTool(edition, toolRepository, resolvedVersion);
6✔
277
    boolean extract = isExtract();
3✔
278
    if (!extract) {
2✔
279
      this.context.trace("Extraction is disabled for '{}' hence just moving the downloaded file {}.", this.tool, downloadedToolFile);
15✔
280
    }
281
    if (Files.isDirectory(installationPath)) {
5!
282
      if (this.tool.equals(IdeasyCommandlet.TOOL_NAME)) {
×
283
        this.context.warning("Your IDEasy installation is missing the version file.");
×
284
      } else {
285
        fileAccess.backup(installationPath);
×
286
      }
287
    }
288
    fileAccess.mkdirs(installationPath.getParent());
4✔
289
    fileAccess.extract(downloadedToolFile, installationPath, this::postExtract, extract);
7✔
290
    this.context.writeVersionFile(resolvedVersion, installationPath);
5✔
291
    this.context.debug("Installed {} in version {} at {}", this.tool, resolvedVersion, installationPath);
19✔
292
  }
1✔
293

294
  /**
295
   * @param edition the {@link #getConfiguredEdition() tool edition} to download.
296
   * @param toolRepository the {@link ToolRepository} to use.
297
   * @param resolvedVersion the resolved {@link VersionIdentifier version} to download.
298
   * @return the {@link Path} to the downloaded release file.
299
   */
300
  protected Path downloadTool(String edition, ToolRepository toolRepository, VersionIdentifier resolvedVersion) {
301
    return toolRepository.download(this.tool, edition, resolvedVersion, this);
8✔
302
  }
303

304
  /**
305
   * Install this tool as dependency of another tool.
306
   *
307
   * @param versionRange the required {@link VersionRange}. See {@link ToolDependency#versionRange()}.
308
   * @param processContext the {@link ProcessContext}.
309
   * @param toolParent the parent {@link ToolEdition} needing the dependency
310
   * @return {@code true} if the tool was newly installed, {@code false} otherwise (installation was already present).
311
   */
312
  public boolean installAsDependency(VersionRange versionRange, ProcessContext processContext, ToolEdition toolParent) {
313
    VersionIdentifier configuredVersion = getConfiguredVersion();
3✔
314
    if (versionRange.contains(configuredVersion)) {
4✔
315
      ToolEdition toolEdition = getToolWithEdition();
3✔
316
      VersionIdentifier cveAlternativeVersion = cveCheck(toolEdition, configuredVersion, versionRange, false);
7✔
317
      if (cveAlternativeVersion.equals(configuredVersion)) {
4!
318
        return install(true, processContext, null);
6✔
319
      } else {
320
        ToolInstallation toolInstallation = installTool(cveAlternativeVersion, processContext);
×
321
        return toolInstallation.newInstallation();
×
322
      }
323
    } else {
324
      if (isIgnoreSoftwareRepo()) {
3!
325
        throw new IllegalStateException(
×
326
            "Cannot satisfy dependency to " + this.tool + " in version " + versionRange + " since it is conflicting with configured version "
327
                + configuredVersion
328
                + " and this tool does not support the software repository.");
329
      }
330
      this.context.info(
23✔
331
          "The tool {} requires {} in the version range {}, but your project uses version {}, which does not match."
332
              + " Therefore, we install a compatible version in that range.",
333
          toolParent, this.tool, versionRange, configuredVersion);
334
    }
335
    ToolInstallation toolInstallation = installTool(versionRange, processContext);
5✔
336
    return toolInstallation.newInstallation();
3✔
337
  }
338

339
  /**
340
   * Installs the tool dependencies for the current tool.
341
   *
342
   * @param version the {@link VersionIdentifier} to use.
343
   * @param toolEdition the {@link ToolEdition} to use.
344
   * @param processContext the {@link ProcessContext} to use.
345
   */
346
  protected void installToolDependencies(VersionIdentifier version, ToolEdition toolEdition, ProcessContext processContext) {
347
    Collection<ToolDependency> dependencies = getToolRepository().findDependencies(this.tool, toolEdition.edition(), version);
9✔
348
    int size = dependencies.size();
3✔
349
    this.context.debug("Tool {} has {} other tool(s) as dependency", toolEdition, size);
15✔
350
    for (ToolDependency dependency : dependencies) {
10✔
351
      this.context.trace("Ensuring dependency {} for tool {}", dependency.tool(), toolEdition);
15✔
352
      LocalToolCommandlet dependencyTool = this.context.getCommandletManager().getRequiredLocalToolCommandlet(dependency.tool());
7✔
353
      dependencyTool.installAsDependency(dependency.versionRange(), processContext, toolEdition);
7✔
354
    }
1✔
355
  }
1✔
356

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

364
  }
1✔
365

366
  @Override
367
  public VersionIdentifier getInstalledVersion() {
368

369
    return getInstalledVersion(this.context.getSoftwarePath().resolve(getName()));
9✔
370
  }
371

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

378
    if (!Files.isDirectory(toolPath)) {
5✔
379
      this.context.debug("Tool {} not installed in {}", getName(), toolPath);
15✔
380
      return null;
2✔
381
    }
382
    Path toolVersionFile = toolPath.resolve(IdeContext.FILE_SOFTWARE_VERSION);
4✔
383
    if (!Files.exists(toolVersionFile)) {
5✔
384
      Path legacyToolVersionFile = toolPath.resolve(IdeContext.FILE_LEGACY_SOFTWARE_VERSION);
4✔
385
      if (Files.exists(legacyToolVersionFile)) {
5!
386
        toolVersionFile = legacyToolVersionFile;
3✔
387
      } else {
388
        this.context.warning("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
    if (this.context.getSoftwarePath() == null) {
4!
400
      return "";
×
401
    }
402
    return getInstalledEdition(this.context.getSoftwarePath().resolve(this.tool));
9✔
403
  }
404

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

435
  private String getEdition(Path toolRepoFolder, Path toolInstallFolder) {
436

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

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

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

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

501
  @Override
502
  public void uninstall() {
503
    try {
504
      Path toolPath = getToolPath();
3✔
505
      if (!Files.exists(toolPath)) {
5!
506
        this.context.warning("An installed version of {} does not exist.", this.tool);
×
507
        return;
×
508
      }
509
      if (this.context.isForceMode() && !isIgnoreSoftwareRepo()) {
7!
510
        this.context.warning(
14✔
511
            "You triggered an uninstall of {} in version {} with force mode!\n"
512
                + "This will physically delete the currently installed version from the machine.\n"
513
                + "This may cause issues with other projects, that use the same version of that tool."
514
            , this.tool, getInstalledVersion());
2✔
515
        uninstallFromSoftwareRepository(toolPath);
3✔
516
      }
517
      performUninstall(toolPath);
3✔
518
      this.context.success("Successfully uninstalled {}", this.tool);
11✔
519
    } catch (Exception e) {
×
520
      this.context.error(e, "Failed to uninstall {}", this.tool);
×
521
    }
1✔
522
  }
1✔
523

524
  /**
525
   * Performs the actual uninstallation of this tool.
526
   *
527
   * @param toolPath the current {@link #getToolPath() tool path}.
528
   */
529
  protected void performUninstall(Path toolPath) {
530
    this.context.getFileAccess().delete(toolPath);
5✔
531
  }
1✔
532

533
  /**
534
   * Deletes the installed version of the tool from the shared software repository.
535
   */
536
  private void uninstallFromSoftwareRepository(Path toolPath) {
537
    Path repoPath = getInstalledSoftwareRepoPath(toolPath);
4✔
538
    if ((repoPath == null) || !Files.exists(repoPath)) {
7!
539
      this.context.warning("An installed version of {} does not exist in software repository.", this.tool);
×
540
      return;
×
541
    }
542
    this.context.info("Physically deleting {} as requested by the user via force mode.", repoPath);
10✔
543
    this.context.getFileAccess().delete(repoPath);
5✔
544
    this.context.success("Successfully deleted {} from your computer.", repoPath);
10✔
545
  }
1✔
546

547

548
  private ToolInstallation createToolInstallation(Path rootDir, VersionIdentifier resolvedVersion, boolean newInstallation,
549
      EnvironmentContext environmentContext, boolean extraInstallation) {
550

551
    Path linkDir = getMacOsHelper().findLinkDir(rootDir, getBinaryName());
7✔
552
    Path binDir = linkDir;
2✔
553
    Path binFolder = binDir.resolve(IdeContext.FOLDER_BIN);
4✔
554
    if (Files.isDirectory(binFolder)) {
5✔
555
      binDir = binFolder;
2✔
556
    }
557
    if (linkDir != rootDir) {
3✔
558
      assert (!linkDir.equals(rootDir));
5!
559
      Path toolVersionFile = rootDir.resolve(IdeContext.FILE_SOFTWARE_VERSION);
4✔
560
      if (Files.exists(toolVersionFile)) {
5!
561
        this.context.getFileAccess().copy(toolVersionFile, linkDir, FileCopyMode.COPY_FILE_OVERRIDE);
7✔
562
      }
563
    }
564
    ToolInstallation toolInstallation = new ToolInstallation(rootDir, linkDir, binDir, resolvedVersion, newInstallation);
9✔
565
    setEnvironment(environmentContext, toolInstallation, extraInstallation);
5✔
566
    return toolInstallation;
2✔
567
  }
568

569
  /**
570
   * Method to set environment variables for the process context.
571
   *
572
   * @param environmentContext the {@link EnvironmentContext} where to {@link EnvironmentContext#withEnvVar(String, String) set environment variables} for
573
   *     this tool.
574
   * @param toolInstallation the {@link ToolInstallation}.
575
   * @param extraInstallation {@code true} if the {@link ToolInstallation} is an additional installation to the {@link #getConfiguredVersion()} ()
576
   *     configured version} due to a conflicting version of a {@link ToolDependency}, {@code false} otherwise.
577
   */
578
  public void setEnvironment(EnvironmentContext environmentContext, ToolInstallation toolInstallation, boolean extraInstallation) {
579

580
    String pathVariable = EnvironmentVariables.getToolVariablePrefix(this.tool) + "_HOME";
5✔
581
    Path toolHomePath = getToolHomePath(toolInstallation);
4✔
582
    if (toolHomePath != null) {
2!
583
      environmentContext.withEnvVar(pathVariable, toolHomePath.toString());
6✔
584
    }
585
    if (extraInstallation) {
2✔
586
      environmentContext.withPathEntry(toolInstallation.binDir());
5✔
587
    }
588
  }
1✔
589

590
  /**
591
   * Method to get the home path of the given {@link ToolInstallation}.
592
   *
593
   * @param toolInstallation the {@link ToolInstallation}.
594
   * @return the Path to the home of the tool
595
   */
596
  protected Path getToolHomePath(ToolInstallation toolInstallation) {
597
    return toolInstallation.linkDir();
3✔
598
  }
599

600
  /**
601
   * @return {@link VersionIdentifier} with latest version of the tool}.
602
   */
603
  public VersionIdentifier getLatestToolVersion() {
604

605
    return this.context.getDefaultToolRepository().resolveVersion(this.tool, getConfiguredEdition(), VersionIdentifier.LATEST, this);
×
606
  }
607

608

609
  /**
610
   * Searches for a wrapper file in valid projects (containing a build file f.e. build.gradle or pom.xml) and returns its path.
611
   *
612
   * @param wrapperFileName the name of the wrapper file
613
   * @param filter the {@link Predicate} to match
614
   * @return Path of the wrapper file or {@code null} if none was found.
615
   */
616
  protected Path findWrapper(String wrapperFileName, Predicate<Path> filter) {
617
    Path dir = context.getCwd();
4✔
618
    // traverse the cwd directory containing a build file up till a wrapper file was found
619
    while ((dir != null) && filter.test(dir)) {
6!
620
      if (Files.exists(dir.resolve(wrapperFileName))) {
7✔
621
        context.debug("Using wrapper file at: {}", dir);
10✔
622
        return dir.resolve(wrapperFileName);
4✔
623
      }
624
      dir = dir.getParent();
4✔
625
    }
626
    return null;
2✔
627
  }
628

629

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