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

devonfw / IDEasy / 17121363550

21 Aug 2025 08:23AM UTC coverage: 69.146% (+0.06%) from 69.091%
17121363550

Pull #1280

github

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

3406 of 5375 branches covered (63.37%)

Branch coverage included in aggregate %.

8886 of 12402 relevant lines covered (71.65%)

3.15 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
  private VersionIdentifier lookForCVEs(VersionIdentifier version, String edition, ProcessContext processContext, VersionRange allowedVersions) {
324
    CVEFinder cveFinder;
325
    if (allowedVersions == null) {
2✔
326
      cveFinder = new CVEFinder(context, this, version);
9✔
327
    } else {
328
      cveFinder = new CVEFinder(context, this, version, allowedVersions);
9✔
329
    }
330
    Collection<CVE> cves = cveFinder.getCVEs(version);
4✔
331
    if (cves.isEmpty()) {
3✔
332
      context.info("No CVEs found for tool {} in version {}", this.getName(), version);
15✔
333
      return version;
2✔
334
    }
335
    VersionIdentifier safestNearestVersion = cveFinder.findSafestNearestVersion();
3✔
336
    VersionIdentifier safestLatestVersion = cveFinder.findSafestLatestVersion();
3✔
337
    cveFinder.listCVEs(version);
3✔
338
    context.info("The tool {} in version {} is affected by the CVE(s) logged above.", this.getName(), version);
15✔
339
    context.info("The latest version {} is only affected by the following CVE(s).", safestLatestVersion);
10✔
340
    cveFinder.listCVEs(safestLatestVersion);
3✔
341
    context.info("The nearest version {} is only affected by the following CVE(s).", safestNearestVersion);
10✔
342
    cveFinder.listCVEs(safestNearestVersion);
3✔
343

344
    String answer = context.question(new String[] { "current",
22✔
345
        "nearest",
346
        "latest" }, "Which version do you want to use?");
347
    if (answer.equals("current")) {
4!
348
      return version;
2✔
349
    }
350
    if (answer.equals("nearest")) {
×
351
      return safestNearestVersion;
×
352
    }
353
    if (answer.equals("latest")) {
×
354
      return safestLatestVersion;
×
355
    }
356
    return version;
×
357
  }
358

359

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

367
  }
1✔
368

369
  @Override
370
  public VersionIdentifier getInstalledVersion() {
371

372
    return getInstalledVersion(this.context.getSoftwarePath().resolve(getName()));
9✔
373
  }
374

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

381
    if (!Files.isDirectory(toolPath)) {
5✔
382
      this.context.debug("Tool {} not installed in {}", getName(), toolPath);
15✔
383
      return null;
2✔
384
    }
385
    Path toolVersionFile = toolPath.resolve(IdeContext.FILE_SOFTWARE_VERSION);
4✔
386
    if (!Files.exists(toolVersionFile)) {
5✔
387
      Path legacyToolVersionFile = toolPath.resolve(IdeContext.FILE_LEGACY_SOFTWARE_VERSION);
4✔
388
      if (Files.exists(legacyToolVersionFile)) {
5✔
389
        toolVersionFile = legacyToolVersionFile;
3✔
390
      } else {
391
        this.context.warning("Tool {} is missing version file in {}", getName(), toolVersionFile);
15✔
392
        return null;
2✔
393
      }
394
    }
395
    String version = this.context.getFileAccess().readFileContent(toolVersionFile).trim();
7✔
396
    return VersionIdentifier.of(version);
3✔
397
  }
398

399
  @Override
400
  public String getInstalledEdition() {
401

402
    if (this.context.getSoftwarePath() == null) {
4!
403
      return "";
×
404
    }
405
    return getInstalledEdition(this.context.getSoftwarePath().resolve(this.tool));
9✔
406
  }
407

408
  /**
409
   * @param toolPath the installation {@link Path} where to find currently installed tool. The name of the parent directory of the real path corresponding
410
   *     to the passed {@link Path path} must be the name of the edition.
411
   * @return the installed edition of this tool or {@code null} if not installed.
412
   */
413
  private String getInstalledEdition(Path toolPath) {
414
    if (!Files.isDirectory(toolPath)) {
5✔
415
      this.context.debug("Tool {} not installed in {}", this.tool, toolPath);
15✔
416
      return null;
2✔
417
    }
418
    Path realPath = this.context.getFileAccess().toRealPath(toolPath);
6✔
419
    // if the realPath changed, a link has been resolved
420
    if (realPath.equals(toolPath)) {
4✔
421
      if (!isIgnoreSoftwareRepo()) {
3!
422
        this.context.warning("Tool {} is not installed via software repository (maybe from devonfw-ide). Please consider reinstalling it.", this.tool);
11✔
423
      }
424
      // 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
425
      return getConfiguredEdition();
3✔
426
    }
427
    Path toolRepoFolder = context.getSoftwareRepositoryPath().resolve(ToolRepository.ID_DEFAULT).resolve(this.tool);
9✔
428
    String edition = getEdition(toolRepoFolder, realPath);
5✔
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 " + this.tool + " does not exist.");
×
507
        return;
×
508
      }
509
      if (this.context.isForceMode()) {
4✔
510
        this.context.warning(
10✔
511
            "Sub-command uninstall via force mode will physically delete the currently installed version of " + this.tool + " from the machine.\n"
512
                + "This may cause issues with other projects, that use the same version of " + this.tool + ".\n"
513
                + "Deleting " + this.tool + " version " + getInstalledVersion() + " from your machine.");
3✔
514
        uninstallFromSoftwareRepository(toolPath);
3✔
515
      }
516
      try {
517
        this.context.getFileAccess().delete(toolPath);
5✔
518
        this.context.success("Successfully uninstalled " + this.tool);
6✔
519
      } catch (Exception e) {
×
520
        this.context.error("Couldn't uninstall " + this.tool + ". ", e);
×
521
      }
1✔
522
    } catch (Exception e) {
×
523
      this.context.error(e.getMessage(), e);
×
524
    }
1✔
525
  }
1✔
526

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

550

551
  private ToolInstallation createToolInstallation(Path rootDir, VersionIdentifier resolvedVersion, Path toolVersionFile,
552
      boolean newInstallation, EnvironmentContext environmentContext, boolean extraInstallation) {
553

554
    Path linkDir = getMacOsHelper().findLinkDir(rootDir, getBinaryName());
7✔
555
    Path binDir = linkDir;
2✔
556
    Path binFolder = binDir.resolve(IdeContext.FOLDER_BIN);
4✔
557
    if (Files.isDirectory(binFolder)) {
5✔
558
      binDir = binFolder;
2✔
559
    }
560
    if (linkDir != rootDir) {
3✔
561
      assert (!linkDir.equals(rootDir));
5!
562
      this.context.getFileAccess().copy(toolVersionFile, linkDir, FileCopyMode.COPY_FILE_OVERRIDE);
7✔
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
    environmentContext.withEnvVar(pathVariable, toolInstallation.linkDir().toString());
7✔
582
    if (extraInstallation) {
2!
583
      environmentContext.withPathEntry(toolInstallation.binDir());
×
584
    }
585
  }
1✔
586

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

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

595

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

616

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