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

devonfw / IDEasy / 19534564952

20 Nov 2025 11:00AM UTC coverage: 69.062% (+0.1%) from 68.924%
19534564952

Pull #1593

github

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

3574 of 5673 branches covered (63.0%)

Branch coverage included in aggregate %.

9315 of 12990 relevant lines covered (71.71%)

3.15 hits per line

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

82.11
cli/src/main/java/com/devonfw/tools/ide/tool/LocalToolCommandlet.java
1
package com.devonfw.tools.ide.tool;
2

3
import java.nio.file.Files;
4
import java.nio.file.LinkOption;
5
import java.nio.file.Path;
6
import java.util.Collection;
7
import java.util.Set;
8
import java.util.function.Predicate;
9

10
import com.devonfw.tools.ide.common.Tag;
11
import com.devonfw.tools.ide.context.IdeContext;
12
import com.devonfw.tools.ide.environment.EnvironmentVariables;
13
import com.devonfw.tools.ide.io.FileAccess;
14
import com.devonfw.tools.ide.io.FileCopyMode;
15
import com.devonfw.tools.ide.log.IdeSubLogger;
16
import com.devonfw.tools.ide.process.EnvironmentContext;
17
import com.devonfw.tools.ide.process.ProcessContext;
18
import com.devonfw.tools.ide.step.Step;
19
import com.devonfw.tools.ide.tool.repository.ToolRepository;
20
import com.devonfw.tools.ide.url.model.file.json.ToolDependency;
21
import com.devonfw.tools.ide.version.GenericVersionRange;
22
import com.devonfw.tools.ide.version.VersionIdentifier;
23
import com.devonfw.tools.ide.version.VersionRange;
24

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

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

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

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

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

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

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

71
    return false;
×
72
  }
73

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

80
  }
1✔
81

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

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

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

99
    ToolEdition toolEdition = getToolWithEdition();
3✔
100
    // check if we should skip updates and the configured version matches the installed version
101
    if (context.isSkipUpdatesMode() && configuredVersion.matches(installedVersion)) {
8✔
102
      return toolAlreadyInstalled(silent, toolEdition, installedVersion, processContext, true);
8✔
103
    }
104

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

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

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

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

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

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

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

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

181
    return false;
2✔
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
   * @return the {@link ToolInstallation} matching the given {@code version}.
192
   */
193
  public ToolInstallation installTool(GenericVersionRange version, ProcessContext processContext) {
194

195
    return installTool(version, processContext, getToolWithEdition());
7✔
196
  }
197

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

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

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

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

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

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

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

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

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

362
  }
1✔
363

364
  @Override
365
  public VersionIdentifier getInstalledVersion() {
366

367
    return getInstalledVersion(this.context.getSoftwarePath().resolve(getName()));
9✔
368
  }
369

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

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

394
  @Override
395
  public String getInstalledEdition() {
396

397
    if (this.context.getSoftwarePath() == null) {
4!
398
      return "";
×
399
    }
400
    return getInstalledEdition(this.context.getSoftwarePath().resolve(this.tool));
9✔
401
  }
402

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

433
  private String getEdition(Path toolRepoFolder, Path toolInstallFolder) {
434

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

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

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

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

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

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

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

545

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

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

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

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

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

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

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

606

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

627

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