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

devonfw / IDEasy / 17659177311

11 Sep 2025 10:43PM UTC coverage: 68.76% (+0.04%) from 68.725%
17659177311

Pull #1280

github

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

3417 of 5443 branches covered (62.78%)

Branch coverage included in aggregate %.

8920 of 12499 relevant lines covered (71.37%)

3.14 hits per line

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

80.45
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.equals(configuredVersion)) {
4!
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
    VersionIdentifier resolvedVersion = getToolRepository().resolveVersion(this.tool, getConfiguredEdition(), version, this);
10✔
306
    ToolInstallation toolInstallation = null;
2✔
307
    if (lookForCVEs(resolvedVersion, getConfiguredEdition(), processContext, version) != resolvedVersion) {
9!
308
      toolInstallation = installTool(resolvedVersion, processContext, true);
×
309
    } else {
310
      toolInstallation = installTool(version, processContext, true);
6✔
311
    }
312
    return toolInstallation.newInstallation();
3✔
313
  }
314

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

327
  /**
328
   * Checks tool for CVEs and suggests alternative Version to install.
329
   *
330
   * @param version the required {@link VersionIdentifier}.
331
   * @param edition the specific {@link #getConfiguredEdition() edition} to install.
332
   * @param processContext the {@link ProcessContext}.
333
   * @param allowedVersions the allowed {@link VersionRange}.
334
   * @return {@code true} if the tool was newly installed, {@code false} otherwise (installation was already present).
335
   */
336
  private VersionIdentifier lookForCVEs(VersionIdentifier version, String edition, ProcessContext processContext, VersionRange allowedVersions) {
337
    CVEFinder cveFinder;
338
    if (allowedVersions == null) {
2✔
339
      cveFinder = new CVEFinder(context, this, version);
9✔
340
    } else {
341
      cveFinder = new CVEFinder(context, this, version, allowedVersions);
9✔
342
    }
343
    Collection<CVE> cves = cveFinder.getCVEs(version);
4✔
344
    if (cves.isEmpty()) {
3✔
345
      context.info("No CVEs found for tool {} in version {}", this.getName(), version);
15✔
346
      return version;
2✔
347
    }
348
    VersionIdentifier safestNearestVersion = cveFinder.findSafestNearestVersion();
3✔
349
    VersionIdentifier safestLatestVersion = cveFinder.findSafestLatestVersion();
3✔
350
    String answer = null;
2✔
351
    if (safestNearestVersion.equals(safestLatestVersion.equals(safestNearestVersion) && safestNearestVersion.equals(version))) {
9!
352
      context.info("Current version is the safest version but is affected by the following CVE(s)");
×
353
      cveFinder.listCVEs(version);
×
354
    } else if (safestNearestVersion.equals(safestLatestVersion)) {
4!
355
      context.info("The latest version {} is only affected by the following CVE(s).", safestLatestVersion);
×
356
      cveFinder.listCVEs(safestLatestVersion);
×
357
      answer = context.question(new String[] { "current",
×
358
          "latest" }, "Which version do you want to use?");
359
      cveFinder.listCVEs(version);
×
360
      context.info("The tool {} in version {} is affected by the CVE(s) logged above.", this.getName(), version);
×
361
      context.info("The latest version {} is only affected by the following CVE(s).", safestLatestVersion);
×
362
      cveFinder.listCVEs(safestLatestVersion);
×
363
    } else {
364
      cveFinder.listCVEs(version);
3✔
365
      context.info("The tool {} in version {} is affected by the CVE(s) logged above.", this.getName(), version);
15✔
366
      context.info("The latest version {} is only affected by the following CVE(s).", safestLatestVersion);
10✔
367
      cveFinder.listCVEs(safestLatestVersion);
3✔
368
      context.info("The nearest version {} is only affected by the following CVE(s).", safestNearestVersion);
10✔
369
      cveFinder.listCVEs(safestNearestVersion);
3✔
370
      answer = context.question(new String[] { "current",
22✔
371
          "nearest",
372
          "latest" }, "Which version do you want to use?");
373
    }
374

375
    if (answer.equals("current")) {
4✔
376
      return version;
2✔
377
    }
378
    if (answer.equals("nearest")) {
4✔
379
      return safestNearestVersion;
2✔
380
    }
381
    if (answer.equals("latest")) {
4!
382
      return safestLatestVersion;
2✔
383
    }
384
    return version;
×
385
  }
386

387

388
  /**
389
   * Post-extraction hook that can be overridden to add custom processing after unpacking and before moving to the final destination folder.
390
   *
391
   * @param extractedDir the {@link Path} to the folder with the unpacked tool.
392
   */
393
  protected void postExtract(Path extractedDir) {
394

395
  }
1✔
396

397
  @Override
398
  public VersionIdentifier getInstalledVersion() {
399

400
    return getInstalledVersion(this.context.getSoftwarePath().resolve(getName()));
9✔
401
  }
402

403
  /**
404
   * @param toolPath the installation {@link Path} where to find the version file.
405
   * @return the currently installed {@link VersionIdentifier version} of this tool or {@code null} if not installed.
406
   */
407
  protected VersionIdentifier getInstalledVersion(Path toolPath) {
408

409
    if (!Files.isDirectory(toolPath)) {
5✔
410
      this.context.debug("Tool {} not installed in {}", getName(), toolPath);
15✔
411
      return null;
2✔
412
    }
413
    Path toolVersionFile = toolPath.resolve(IdeContext.FILE_SOFTWARE_VERSION);
4✔
414
    if (!Files.exists(toolVersionFile)) {
5✔
415
      Path legacyToolVersionFile = toolPath.resolve(IdeContext.FILE_LEGACY_SOFTWARE_VERSION);
4✔
416
      if (Files.exists(legacyToolVersionFile)) {
5✔
417
        toolVersionFile = legacyToolVersionFile;
3✔
418
      } else {
419
        this.context.warning("Tool {} is missing version file in {}", getName(), toolVersionFile);
15✔
420
        return null;
2✔
421
      }
422
    }
423
    String version = this.context.getFileAccess().readFileContent(toolVersionFile).trim();
7✔
424
    return VersionIdentifier.of(version);
3✔
425
  }
426

427
  @Override
428
  public String getInstalledEdition() {
429

430
    if (this.context.getSoftwarePath() == null) {
4!
431
      return "";
×
432
    }
433
    return getInstalledEdition(this.context.getSoftwarePath().resolve(this.tool));
9✔
434
  }
435

436
  /**
437
   * @param toolPath the installation {@link Path} where to find currently installed tool. The name of the parent directory of the real path corresponding
438
   *     to the passed {@link Path path} must be the name of the edition.
439
   * @return the installed edition of this tool or {@code null} if not installed.
440
   */
441
  private String getInstalledEdition(Path toolPath) {
442
    if (!Files.isDirectory(toolPath)) {
5✔
443
      this.context.debug("Tool {} not installed in {}", this.tool, toolPath);
15✔
444
      return null;
2✔
445
    }
446
    Path realPath = this.context.getFileAccess().toRealPath(toolPath);
6✔
447
    // if the realPath changed, a link has been resolved
448
    if (realPath.equals(toolPath)) {
4✔
449
      if (!isIgnoreSoftwareRepo()) {
3!
450
        this.context.warning("Tool {} is not installed via software repository (maybe from devonfw-ide). Please consider reinstalling it.", this.tool);
11✔
451
      }
452
      // 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
453
      return getConfiguredEdition();
3✔
454
    }
455
    Path toolRepoFolder = context.getSoftwareRepositoryPath().resolve(ToolRepository.ID_DEFAULT).resolve(this.tool);
9✔
456
    String edition = getEdition(toolRepoFolder, realPath);
5✔
457
    if (!getToolRepository().getSortedEditions(this.tool).contains(edition)) {
8✔
458
      this.context.warning("Undefined edition {} of tool {}", edition, this.tool);
15✔
459
    }
460
    return edition;
2✔
461
  }
462

463
  private String getEdition(Path toolRepoFolder, Path toolInstallFolder) {
464

465
    int toolRepoNameCount = toolRepoFolder.getNameCount();
3✔
466
    int toolInstallNameCount = toolInstallFolder.getNameCount();
3✔
467
    if (toolRepoNameCount < toolInstallNameCount) {
3!
468
      // ensure toolInstallFolder starts with $IDE_ROOT/_ide/software/default/«tool»
469
      for (int i = 0; i < toolRepoNameCount; i++) {
7✔
470
        if (!toolRepoFolder.getName(i).toString().equals(toolInstallFolder.getName(i).toString())) {
10!
471
          return null;
×
472
        }
473
      }
474
      return toolInstallFolder.getName(toolRepoNameCount).toString();
5✔
475
    }
476
    return null;
×
477
  }
478

479
  private Path getInstalledSoftwareRepoPath(Path toolPath) {
480
    if (!Files.isDirectory(toolPath)) {
5!
481
      this.context.debug("Tool {} not installed in {}", this.tool, toolPath);
×
482
      return null;
×
483
    }
484
    Path installPath = this.context.getFileAccess().toRealPath(toolPath);
6✔
485
    // if the installPath changed, a link has been resolved
486
    if (installPath.equals(toolPath)) {
4!
487
      if (!isIgnoreSoftwareRepo()) {
×
488
        this.context.warning("Tool {} is not installed via software repository (maybe from devonfw-ide). Please consider reinstalling it.", this.tool);
×
489
      }
490
      // 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
491
      return null;
×
492
    }
493
    installPath = getValidInstalledSoftwareRepoPath(installPath, context.getSoftwareRepositoryPath());
7✔
494
    return installPath;
2✔
495
  }
496

497
  Path getValidInstalledSoftwareRepoPath(Path installPath, Path softwareRepoPath) {
498
    int softwareRepoNameCount = softwareRepoPath.getNameCount();
3✔
499
    int toolInstallNameCount = installPath.getNameCount();
3✔
500
    int targetToolInstallNameCount = softwareRepoNameCount + 4;
4✔
501

502
    // installPath can't be shorter than softwareRepoPath
503
    if (toolInstallNameCount < softwareRepoNameCount) {
3!
504
      this.context.warning("The installation path is not located within the software repository {}.", installPath);
×
505
      return null;
×
506
    }
507
    // ensure installPath starts with $IDE_ROOT/_ide/software/
508
    for (int i = 0; i < softwareRepoNameCount; i++) {
7✔
509
      if (!softwareRepoPath.getName(i).toString().equals(installPath.getName(i).toString())) {
10✔
510
        this.context.warning("The installation path is not located within the software repository {}.", installPath);
10✔
511
        return null;
2✔
512
      }
513
    }
514
    // return $IDE_ROOT/_ide/software/«id»/«tool»/«edition»/«version»
515
    if (toolInstallNameCount == targetToolInstallNameCount) {
3✔
516
      return installPath;
2✔
517
    } else if (toolInstallNameCount > targetToolInstallNameCount) {
3✔
518
      Path validInstallPath = installPath;
2✔
519
      for (int i = 0; i < toolInstallNameCount - targetToolInstallNameCount; i++) {
9✔
520
        validInstallPath = validInstallPath.getParent();
3✔
521
      }
522
      return validInstallPath;
2✔
523
    } else {
524
      this.context.warning("The installation path is faulty {}.", installPath);
10✔
525
      return null;
2✔
526
    }
527
  }
528

529
  @Override
530
  public void uninstall() {
531
    try {
532
      Path toolPath = getToolPath();
3✔
533
      if (!Files.exists(toolPath)) {
5!
534
        this.context.warning("An installed version of " + this.tool + " does not exist.");
×
535
        return;
×
536
      }
537
      if (this.context.isForceMode()) {
4✔
538
        this.context.warning(
10✔
539
            "Sub-command uninstall via force mode will physically delete the currently installed version of " + this.tool + " from the machine.\n"
540
                + "This may cause issues with other projects, that use the same version of " + this.tool + ".\n"
541
                + "Deleting " + this.tool + " version " + getInstalledVersion() + " from your machine.");
3✔
542
        uninstallFromSoftwareRepository(toolPath);
3✔
543
      }
544
      try {
545
        this.context.getFileAccess().delete(toolPath);
5✔
546
        this.context.success("Successfully uninstalled " + this.tool);
6✔
547
      } catch (Exception e) {
×
548
        this.context.error("Couldn't uninstall " + this.tool + ". ", e);
×
549
      }
1✔
550
    } catch (Exception e) {
×
551
      this.context.error(e.getMessage(), e);
×
552
    }
1✔
553
  }
1✔
554

555
  /**
556
   * Deletes the installed version of the tool from the shared software repository.
557
   */
558
  private void uninstallFromSoftwareRepository(Path toolPath) {
559
    try {
560
      Path repoPath = getInstalledSoftwareRepoPath(toolPath);
4✔
561
      if (!Files.exists(repoPath)) {
5!
562
        this.context.warning("An installed version of " + this.tool + " does not exist.");
×
563
        return;
×
564
      }
565
      this.context.info("Physically deleting " + repoPath + " as requested by the user via force mode.");
6✔
566
      try {
567
        this.context.getFileAccess().delete(repoPath);
5✔
568
        this.context.success("Successfully deleted " + repoPath + " from your computer.");
6✔
569
      } catch (Exception e) {
×
570
        this.context.error("Couldn't delete " + this.tool + " from your computer.", e);
×
571
      }
1✔
572
    } catch (Exception e) {
×
573
      throw new IllegalStateException(
×
574
          " Couldn't uninstall " + this.tool + ". Couldn't determine the software repository path for " + this.tool + ".", e);
575
    }
1✔
576
  }
1✔
577

578

579
  private ToolInstallation createToolInstallation(Path rootDir, VersionIdentifier resolvedVersion, Path toolVersionFile,
580
      boolean newInstallation, EnvironmentContext environmentContext, boolean extraInstallation) {
581

582
    Path linkDir = getMacOsHelper().findLinkDir(rootDir, getBinaryName());
7✔
583
    Path binDir = linkDir;
2✔
584
    Path binFolder = binDir.resolve(IdeContext.FOLDER_BIN);
4✔
585
    if (Files.isDirectory(binFolder)) {
5✔
586
      binDir = binFolder;
2✔
587
    }
588
    if (linkDir != rootDir) {
3✔
589
      assert (!linkDir.equals(rootDir));
5!
590
      this.context.getFileAccess().copy(toolVersionFile, linkDir, FileCopyMode.COPY_FILE_OVERRIDE);
7✔
591
    }
592
    ToolInstallation toolInstallation = new ToolInstallation(rootDir, linkDir, binDir, resolvedVersion, newInstallation);
9✔
593
    setEnvironment(environmentContext, toolInstallation, extraInstallation);
5✔
594
    return toolInstallation;
2✔
595
  }
596

597
  /**
598
   * Method to set environment variables for the process context.
599
   *
600
   * @param environmentContext the {@link EnvironmentContext} where to {@link EnvironmentContext#withEnvVar(String, String) set environment variables} for
601
   *     this tool.
602
   * @param toolInstallation the {@link ToolInstallation}.
603
   * @param extraInstallation {@code true} if the {@link ToolInstallation} is an additional installation to the {@link #getConfiguredVersion()} ()
604
   *     configured version} due to a conflicting version of a {@link ToolDependency}, {@code false} otherwise.
605
   */
606
  public void setEnvironment(EnvironmentContext environmentContext, ToolInstallation toolInstallation, boolean extraInstallation) {
607

608
    String pathVariable = EnvironmentVariables.getToolVariablePrefix(this.tool) + "_HOME";
5✔
609
    environmentContext.withEnvVar(pathVariable, toolInstallation.linkDir().toString());
7✔
610
    if (extraInstallation) {
2✔
611
      environmentContext.withPathEntry(toolInstallation.binDir());
5✔
612
    }
613
  }
1✔
614

615
  /**
616
   * @return {@link VersionIdentifier} with latest version of the tool}.
617
   */
618
  public VersionIdentifier getLatestToolVersion() {
619

620
    return this.context.getDefaultToolRepository().resolveVersion(this.tool, getConfiguredEdition(), VersionIdentifier.LATEST, this);
×
621
  }
622

623

624
  /**
625
   * Searches for a wrapper file in valid projects (containing a build file f.e. build.gradle or pom.xml) and returns its path.
626
   *
627
   * @param wrapperFileName the name of the wrapper file
628
   * @param filter the {@link Predicate} to match
629
   * @return Path of the wrapper file or {@code null} if none was found.
630
   */
631
  protected Path findWrapper(String wrapperFileName, Predicate<Path> filter) {
632
    Path dir = context.getCwd();
4✔
633
    // traverse the cwd directory containing a build file up till a wrapper file was found
634
    while ((dir != null) && filter.test(dir)) {
6!
635
      if (Files.exists(dir.resolve(wrapperFileName))) {
7✔
636
        context.debug("Using wrapper file at: {}", dir);
10✔
637
        return dir.resolve(wrapperFileName);
4✔
638
      }
639
      dir = dir.getParent();
4✔
640
    }
641
    return null;
2✔
642
  }
643

644

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