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

devonfw / IDEasy / 17657162730

11 Sep 2025 09:00PM UTC coverage: 68.691% (-0.03%) from 68.725%
17657162730

Pull #1280

github

web-flow
Merge aed4d9120 into a2fce66c4
Pull Request #1280: CVE: adapt IDEasy to consider security.json files to warn user

3405 of 5433 branches covered (62.67%)

Branch coverage included in aggregate %.

8903 of 12485 relevant lines covered (71.31%)

3.14 hits per line

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

80.75
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.CVEFinder;
11
import com.devonfw.tools.ide.common.Tag;
12
import com.devonfw.tools.ide.context.IdeContext;
13
import com.devonfw.tools.ide.environment.EnvironmentVariables;
14
import com.devonfw.tools.ide.io.FileAccess;
15
import com.devonfw.tools.ide.io.FileCopyMode;
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.CVE;
21
import com.devonfw.tools.ide.url.model.file.json.ToolDependency;
22
import com.devonfw.tools.ide.version.GenericVersionRange;
23
import com.devonfw.tools.ide.version.VersionIdentifier;
24
import com.devonfw.tools.ide.version.VersionRange;
25

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

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

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

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

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

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

66
  /**
67
   * @deprecated will be removed once all "dependencies.json" are created in ide-urls.
68
   */
69
  @Deprecated
70
  protected void installDependencies() {
71

72
  }
1✔
73

74
  @Override
75
  public boolean install(boolean silent, ProcessContext processContext, Step step) {
76

77
    return install(silent, processContext, step, false);
7✔
78
  }
79

80
  public boolean install(boolean silent, ProcessContext processContext, Step step, boolean isDependency) {
81
    installDependencies();
2✔
82
    VersionIdentifier configuredVersion = getConfiguredVersion();
3✔
83
    // get installed version before installInRepo actually may install the software
84
    VersionIdentifier installedVersion = getInstalledVersion();
3✔
85
    if (step == null) {
2!
86
      return doInstallStep(configuredVersion, installedVersion, silent, processContext, step, isDependency);
9✔
87
    } else {
88
      return step.call(() -> doInstallStep(configuredVersion, installedVersion, silent, processContext, step, isDependency), Boolean.FALSE);
×
89
    }
90
  }
91

92
  private boolean doInstallStep(VersionIdentifier configuredVersion, VersionIdentifier installedVersion, boolean silent, ProcessContext processContext,
93
      Step step) {
94
    return doInstallStep(configuredVersion, installedVersion, silent, processContext, step, false);
×
95
  }
96

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

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

103
    // check if we already have this version installed (linked) locally in IDE_HOME/software
104
    VersionIdentifier resolvedVersion = installation.resolvedVersion();
3✔
105
    if ((resolvedVersion.equals(installedVersion) && !installation.newInstallation())
9✔
106
        || (configuredVersion.matches(installedVersion) && context.isSkipUpdatesMode())) {
6!
107
      return toolAlreadyInstalled(silent, installedVersion, processContext);
6✔
108
    }
109
    if (!isIgnoreSoftwareRepo()) {
3✔
110
      // we need to link the version or update the link.
111
      Path toolPath = getToolPath();
3✔
112
      FileAccess fileAccess = this.context.getFileAccess();
4✔
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
    this.context.getPath().setPath(this.tool, installation.binDir());
8✔
120
    postInstall(true, processContext);
4✔
121
    if (installedVersion == null) {
2✔
122
      asSuccess(step).log("Successfully installed {} in version {}", this.tool, resolvedVersion);
18✔
123
    } else {
124
      asSuccess(step).log("Successfully installed {} in version {} replacing previous version {}", this.tool, resolvedVersion, installedVersion);
21✔
125
    }
126
    return true;
2✔
127
  }
128

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

138
    if (newlyInstalled) {
2✔
139
      postInstall();
2✔
140
    }
141
  }
1✔
142

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

148
    // nothing to do by default
149
  }
1✔
150

151
  private boolean toolAlreadyInstalled(boolean silent, VersionIdentifier installedVersion, ProcessContext pc) {
152
    if (!silent) {
2✔
153
      this.context.info("Version {} of tool {} is already installed", installedVersion, getToolWithEdition());
15✔
154
    }
155
    postInstall(false, pc);
4✔
156
    return false;
2✔
157
  }
158

159
  /**
160
   * Determines whether this tool should be installed directly in the software folder or in the software repository.
161
   *
162
   * @return {@code true} if the tool should be installed directly in the software folder, ignoring the central software repository; {@code false} if the tool
163
   *     should be installed in the central software repository (default behavior).
164
   */
165
  protected boolean isIgnoreSoftwareRepo() {
166

167
    return false;
2✔
168
  }
169

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

181
    return installTool(version, processContext, getConfiguredEdition(), isDependency);
8✔
182
  }
183

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

196
    // if version is a VersionRange, we are not called from install() but directly from installAsDependency() due to a version conflict of a dependency
197
    boolean extraInstallation = (version instanceof VersionRange);
3✔
198
    ToolRepository toolRepository = getToolRepository();
3✔
199
    VersionIdentifier resolvedVersion = toolRepository.resolveVersion(this.tool, edition, version, this);
8✔
200
    if (!isDependenny) {
2✔
201
      resolvedVersion = lookForCVEs(resolvedVersion, edition, processContext, null);
7✔
202
    }
203
    installToolDependencies(resolvedVersion, edition, processContext);
5✔
204

205
    Path installationPath;
206
    boolean ignoreSoftwareRepo = isIgnoreSoftwareRepo();
3✔
207
    if (ignoreSoftwareRepo) {
2✔
208
      installationPath = getToolPath();
4✔
209
    } else {
210
      Path softwareRepoPath = this.context.getSoftwareRepositoryPath().resolve(toolRepository.getId()).resolve(this.tool).resolve(edition);
12✔
211
      installationPath = softwareRepoPath.resolve(resolvedVersion.toString());
5✔
212
    }
213
    Path toolVersionFile = installationPath.resolve(IdeContext.FILE_SOFTWARE_VERSION);
4✔
214
    FileAccess fileAccess = this.context.getFileAccess();
4✔
215
    if (Files.isDirectory(installationPath)) {
5✔
216
      if (Files.exists(toolVersionFile)) {
5!
217
        if (!ignoreSoftwareRepo || resolvedVersion.equals(getInstalledVersion())) {
2!
218
          this.context.debug("Version {} of tool {} is already installed at {}", resolvedVersion, getToolWithEdition(this.tool, edition), installationPath);
21✔
219
          return createToolInstallation(installationPath, resolvedVersion, toolVersionFile, false, processContext, extraInstallation);
9✔
220
        }
221
      } else {
222
        // Makes sure that IDEasy will not delete itself
223
        if (this.tool.equals(IdeasyCommandlet.TOOL_NAME)) {
×
224
          this.context.warning("Your IDEasy installation is missing the version file at {}", toolVersionFile);
×
225
        } else {
226
          this.context.warning("Deleting corrupted installation at {}", installationPath);
×
227
          fileAccess.delete(installationPath);
×
228
        }
229
      }
230
    }
231
    performToolInstallation(toolRepository, resolvedVersion, installationPath, fileAccess, edition, processContext);
8✔
232
    return createToolInstallation(installationPath, resolvedVersion, toolVersionFile, true, processContext, extraInstallation);
9✔
233
  }
234

235
  /**
236
   * Performs the actual installation of the {@link #getName() tool} by downloading its binary, optionally extracting it, backing up any existing installation,
237
   * and writing the version file.
238
   * <p>
239
   * This method assumes that the version has already been resolved and dependencies installed. It handles the final steps of placing the tool into the
240
   * appropriate installation directory.
241
   *
242
   * @param toolRepository the {@link ToolRepository} used to locate and download the tool.
243
   * @param resolvedVersion the resolved {@link VersionIdentifier} of the {@link #getName() tool} to install.
244
   * @param installationPath the target {@link Path} where the {@link #getName() tool} should be installed.
245
   * @param fileAccess the {@link FileAccess} utility for file operations such as backup, extraction, and directory creation.
246
   * @param edition the specific edition of the tool to install.
247
   * @param processContext the {@link ProcessContext} used to manage the installation process.
248
   */
249
  protected void performToolInstallation(ToolRepository toolRepository, VersionIdentifier resolvedVersion, Path installationPath, FileAccess fileAccess,
250
      String edition, ProcessContext processContext) {
251

252
    Path downloadedToolFile = downloadTool(edition, toolRepository, resolvedVersion);
6✔
253
    boolean extract = isExtract();
3✔
254
    if (!extract) {
2✔
255
      this.context.trace("Extraction is disabled for '{}' hence just moving the downloaded file {}.", this.tool, downloadedToolFile);
15✔
256
    }
257
    if (Files.exists(installationPath)) {
5!
258
      fileAccess.backup(installationPath);
×
259
    }
260
    fileAccess.mkdirs(installationPath.getParent());
4✔
261
    fileAccess.extract(downloadedToolFile, installationPath, this::postExtract, extract);
7✔
262
    this.context.writeVersionFile(resolvedVersion, installationPath);
5✔
263
    this.context.debug("Installed {} in version {} at {}", this.tool, resolvedVersion, installationPath);
19✔
264
  }
1✔
265

266
  /**
267
   * @param edition the {@link #getConfiguredEdition() tool edition} to download.
268
   * @param toolRepository the {@link ToolRepository} to use.
269
   * @param resolvedVersion the resolved {@link VersionIdentifier version} to download.
270
   * @return the {@link Path} to the downloaded release file.
271
   */
272
  protected Path downloadTool(String edition, ToolRepository toolRepository, VersionIdentifier resolvedVersion) {
273
    return toolRepository.download(this.tool, edition, resolvedVersion, this);
8✔
274
  }
275

276
  /**
277
   * Install this tool as dependency of another tool.
278
   *
279
   * @param version the required {@link VersionRange}. See {@link ToolDependency#versionRange()}.
280
   * @param processContext the {@link ProcessContext}.
281
   * @param toolParent the parent tool name needing the dependency
282
   * @return {@code true} if the tool was newly installed, {@code false} otherwise (installation was already present).
283
   */
284
  public boolean installAsDependency(VersionRange version, ProcessContext processContext, String toolParent) {
285
    VersionIdentifier configuredVersion = getConfiguredVersion();
3✔
286
    if (version.contains(configuredVersion)) {
4✔
287
      VersionIdentifier cveAlternativeVersion = lookForCVEs(configuredVersion, getConfiguredEdition(), processContext, version);
8✔
288
      if (cveAlternativeVersion == configuredVersion) {
3!
289
        return install(false, processContext, null, true);
7✔
290
      } else {
291
        ToolInstallation toolInstallation = installTool(cveAlternativeVersion, processContext, true);
×
292
        return toolInstallation.newInstallation();
×
293
      }
294
    } else {
295
      if (isIgnoreSoftwareRepo()) {
3!
296
        throw new IllegalStateException(
×
297
            "Cannot satisfy dependency to " + this.tool + " in version " + version + " since it is conflicting with configured version " + configuredVersion
298
                + " and this tool does not support the software repository.");
299
      }
300
      this.context.info(
23✔
301
          "The tool {} requires {} in the version range {}, but your project uses version {}, which does not match."
302
              + " Therefore, we install a compatible version in that range.",
303
          toolParent, this.tool, version, configuredVersion);
304
    }
305
    ToolInstallation toolInstallation = installTool(
5✔
306
        lookForCVEs(getToolRepository().resolveVersion(this.tool, getConfiguredEdition(), version, this), getConfiguredEdition(), processContext, version),
15✔
307
        processContext, true);
308
    return toolInstallation.newInstallation();
3✔
309
  }
310

311
  private void installToolDependencies(VersionIdentifier version, String edition, ProcessContext processContext) {
312
    Collection<ToolDependency> dependencies = getToolRepository().findDependencies(this.tool, edition, version);
8✔
313
    String toolWithEdition = getToolWithEdition(this.tool, edition);
5✔
314
    int size = dependencies.size();
3✔
315
    this.context.debug("Tool {} has {} other tool(s) as dependency", toolWithEdition, size);
15✔
316
    for (ToolDependency dependency : dependencies) {
10✔
317
      this.context.trace("Ensuring dependency {} for tool {}", dependency.tool(), toolWithEdition);
15✔
318
      LocalToolCommandlet dependencyTool = this.context.getCommandletManager().getRequiredLocalToolCommandlet(dependency.tool());
7✔
319
      dependencyTool.installAsDependency(dependency.versionRange(), processContext, toolWithEdition);
7✔
320
    }
1✔
321
  }
1✔
322

323
  /**
324
   * Checks tool for CVEs and suggests alternative Version to install.
325
   *
326
   * @param version the required {@link VersionIdentifier}.
327
   * @param edition the specific {@link #getConfiguredEdition() edition} to install.
328
   * @param processContext the {@link ProcessContext}.
329
   * @param allowedVersions the allowed {@link VersionRange}.
330
   * @return {@code true} if the tool was newly installed, {@code false} otherwise (installation was already present).
331
   */
332
  private VersionIdentifier lookForCVEs(VersionIdentifier version, String edition, ProcessContext processContext, VersionRange allowedVersions) {
333
    CVEFinder cveFinder;
334
    if (allowedVersions == null) {
2✔
335
      cveFinder = new CVEFinder(context, this, version);
9✔
336
    } else {
337
      cveFinder = new CVEFinder(context, this, version, allowedVersions);
9✔
338
    }
339
    Collection<CVE> cves = cveFinder.getCVEs(version);
4✔
340
    if (cves.isEmpty()) {
3✔
341
      context.info("No CVEs found for tool {} in version {}", this.getName(), version);
15✔
342
      return version;
2✔
343
    }
344
    VersionIdentifier safestNearestVersion = cveFinder.findSafestNearestVersion();
3✔
345
    VersionIdentifier safestLatestVersion = cveFinder.findSafestLatestVersion();
3✔
346
    cveFinder.listCVEs(version);
3✔
347
    context.info("The tool {} in version {} is affected by the CVE(s) logged above.", this.getName(), version);
15✔
348
    context.info("The latest version {} is only affected by the following CVE(s).", safestLatestVersion);
10✔
349
    cveFinder.listCVEs(safestLatestVersion);
3✔
350
    context.info("The nearest version {} is only affected by the following CVE(s).", safestNearestVersion);
10✔
351
    cveFinder.listCVEs(safestNearestVersion);
3✔
352

353
    String answer = context.question(new String[] { "current",
22✔
354
        "nearest",
355
        "latest" }, "Which version do you want to use?");
356
    if (answer.equals("current")) {
4!
357
      return version;
2✔
358
    }
359
    if (answer.equals("nearest")) {
×
360
      return safestNearestVersion;
×
361
    }
362
    if (answer.equals("latest")) {
×
363
      return safestLatestVersion;
×
364
    }
365
    return version;
×
366
  }
367

368

369
  /**
370
   * Post-extraction hook that can be overridden to add custom processing after unpacking and before moving to the final destination folder.
371
   *
372
   * @param extractedDir the {@link Path} to the folder with the unpacked tool.
373
   */
374
  protected void postExtract(Path extractedDir) {
375

376
  }
1✔
377

378
  @Override
379
  public VersionIdentifier getInstalledVersion() {
380

381
    return getInstalledVersion(this.context.getSoftwarePath().resolve(getName()));
9✔
382
  }
383

384
  /**
385
   * @param toolPath the installation {@link Path} where to find the version file.
386
   * @return the currently installed {@link VersionIdentifier version} of this tool or {@code null} if not installed.
387
   */
388
  protected VersionIdentifier getInstalledVersion(Path toolPath) {
389

390
    if (!Files.isDirectory(toolPath)) {
5✔
391
      this.context.debug("Tool {} not installed in {}", getName(), toolPath);
15✔
392
      return null;
2✔
393
    }
394
    Path toolVersionFile = toolPath.resolve(IdeContext.FILE_SOFTWARE_VERSION);
4✔
395
    if (!Files.exists(toolVersionFile)) {
5✔
396
      Path legacyToolVersionFile = toolPath.resolve(IdeContext.FILE_LEGACY_SOFTWARE_VERSION);
4✔
397
      if (Files.exists(legacyToolVersionFile)) {
5✔
398
        toolVersionFile = legacyToolVersionFile;
3✔
399
      } else {
400
        this.context.warning("Tool {} is missing version file in {}", getName(), toolVersionFile);
15✔
401
        return null;
2✔
402
      }
403
    }
404
    String version = this.context.getFileAccess().readFileContent(toolVersionFile).trim();
7✔
405
    return VersionIdentifier.of(version);
3✔
406
  }
407

408
  @Override
409
  public String getInstalledEdition() {
410

411
    if (this.context.getSoftwarePath() == null) {
4!
412
      return "";
×
413
    }
414
    return getInstalledEdition(this.context.getSoftwarePath().resolve(this.tool));
9✔
415
  }
416

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

444
  private String getEdition(Path toolRepoFolder, Path toolInstallFolder) {
445

446
    int toolRepoNameCount = toolRepoFolder.getNameCount();
3✔
447
    int toolInstallNameCount = toolInstallFolder.getNameCount();
3✔
448
    if (toolRepoNameCount < toolInstallNameCount) {
3!
449
      // ensure toolInstallFolder starts with $IDE_ROOT/_ide/software/default/«tool»
450
      for (int i = 0; i < toolRepoNameCount; i++) {
7✔
451
        if (!toolRepoFolder.getName(i).toString().equals(toolInstallFolder.getName(i).toString())) {
10!
452
          return null;
×
453
        }
454
      }
455
      return toolInstallFolder.getName(toolRepoNameCount).toString();
5✔
456
    }
457
    return null;
×
458
  }
459

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

478
  Path getValidInstalledSoftwareRepoPath(Path installPath, Path softwareRepoPath) {
479
    int softwareRepoNameCount = softwareRepoPath.getNameCount();
3✔
480
    int toolInstallNameCount = installPath.getNameCount();
3✔
481
    int targetToolInstallNameCount = softwareRepoNameCount + 4;
4✔
482

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

510
  @Override
511
  public void uninstall() {
512
    try {
513
      Path toolPath = getToolPath();
3✔
514
      if (!Files.exists(toolPath)) {
5!
515
        this.context.warning("An installed version of " + this.tool + " does not exist.");
×
516
        return;
×
517
      }
518
      if (this.context.isForceMode()) {
4✔
519
        this.context.warning(
10✔
520
            "Sub-command uninstall via force mode will physically delete the currently installed version of " + this.tool + " from the machine.\n"
521
                + "This may cause issues with other projects, that use the same version of " + this.tool + ".\n"
522
                + "Deleting " + this.tool + " version " + getInstalledVersion() + " from your machine.");
3✔
523
        uninstallFromSoftwareRepository(toolPath);
3✔
524
      }
525
      try {
526
        this.context.getFileAccess().delete(toolPath);
5✔
527
        this.context.success("Successfully uninstalled " + this.tool);
6✔
528
      } catch (Exception e) {
×
529
        this.context.error("Couldn't uninstall " + this.tool + ". ", e);
×
530
      }
1✔
531
    } catch (Exception e) {
×
532
      this.context.error(e.getMessage(), e);
×
533
    }
1✔
534
  }
1✔
535

536
  /**
537
   * Deletes the installed version of the tool from the shared software repository.
538
   */
539
  private void uninstallFromSoftwareRepository(Path toolPath) {
540
    try {
541
      Path repoPath = getInstalledSoftwareRepoPath(toolPath);
4✔
542
      if (!Files.exists(repoPath)) {
5!
543
        this.context.warning("An installed version of " + this.tool + " does not exist.");
×
544
        return;
×
545
      }
546
      this.context.info("Physically deleting " + repoPath + " as requested by the user via force mode.");
6✔
547
      try {
548
        this.context.getFileAccess().delete(repoPath);
5✔
549
        this.context.success("Successfully deleted " + repoPath + " from your computer.");
6✔
550
      } catch (Exception e) {
×
551
        this.context.error("Couldn't delete " + this.tool + " from your computer.", e);
×
552
      }
1✔
553
    } catch (Exception e) {
×
554
      throw new IllegalStateException(
×
555
          " Couldn't uninstall " + this.tool + ". Couldn't determine the software repository path for " + this.tool + ".", e);
556
    }
1✔
557
  }
1✔
558

559

560
  private ToolInstallation createToolInstallation(Path rootDir, VersionIdentifier resolvedVersion, Path toolVersionFile,
561
      boolean newInstallation, EnvironmentContext environmentContext, boolean extraInstallation) {
562

563
    Path linkDir = getMacOsHelper().findLinkDir(rootDir, getBinaryName());
7✔
564
    Path binDir = linkDir;
2✔
565
    Path binFolder = binDir.resolve(IdeContext.FOLDER_BIN);
4✔
566
    if (Files.isDirectory(binFolder)) {
5✔
567
      binDir = binFolder;
2✔
568
    }
569
    if (linkDir != rootDir) {
3✔
570
      assert (!linkDir.equals(rootDir));
5!
571
      this.context.getFileAccess().copy(toolVersionFile, linkDir, FileCopyMode.COPY_FILE_OVERRIDE);
7✔
572
    }
573
    ToolInstallation toolInstallation = new ToolInstallation(rootDir, linkDir, binDir, resolvedVersion, newInstallation);
9✔
574
    setEnvironment(environmentContext, toolInstallation, extraInstallation);
5✔
575
    return toolInstallation;
2✔
576
  }
577

578
  /**
579
   * Method to set environment variables for the process context.
580
   *
581
   * @param environmentContext the {@link EnvironmentContext} where to {@link EnvironmentContext#withEnvVar(String, String) set environment variables} for
582
   *     this tool.
583
   * @param toolInstallation the {@link ToolInstallation}.
584
   * @param extraInstallation {@code true} if the {@link ToolInstallation} is an additional installation to the {@link #getConfiguredVersion()} ()
585
   *     configured version} due to a conflicting version of a {@link ToolDependency}, {@code false} otherwise.
586
   */
587
  public void setEnvironment(EnvironmentContext environmentContext, ToolInstallation toolInstallation, boolean extraInstallation) {
588

589
    String pathVariable = EnvironmentVariables.getToolVariablePrefix(this.tool) + "_HOME";
5✔
590
    environmentContext.withEnvVar(pathVariable, toolInstallation.linkDir().toString());
7✔
591
    if (extraInstallation) {
2!
592
      environmentContext.withPathEntry(toolInstallation.binDir());
×
593
    }
594
  }
1✔
595

596
  /**
597
   * @return {@link VersionIdentifier} with latest version of the tool}.
598
   */
599
  public VersionIdentifier getLatestToolVersion() {
600

601
    return this.context.getDefaultToolRepository().resolveVersion(this.tool, getConfiguredEdition(), VersionIdentifier.LATEST, this);
×
602
  }
603

604

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

625

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