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

devonfw / IDEasy / 19427001902

17 Nov 2025 10:49AM UTC coverage: 68.834% (-0.03%) from 68.861%
19427001902

push

github

web-flow
#1473: fix --skip-updates option not working (#1567)

Co-authored-by: Jörg Hohwiller <hohwille@users.noreply.github.com>

3495 of 5567 branches covered (62.78%)

Branch coverage included in aggregate %.

9158 of 12815 relevant lines covered (71.46%)

3.14 hits per line

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

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

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

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

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

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

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

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

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

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

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

71
    return false;
×
72
  }
73

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

80
  }
1✔
81

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

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

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

99
    // check if we should skip updates and the configured version matches the installed version
100
    if (context.isSkipUpdatesMode() && configuredVersion.matches(installedVersion) && installedVersion != null) {
10!
101
      return toolAlreadyInstalled(silent, installedVersion, processContext);
6✔
102
    }
103

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

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

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

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

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

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

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

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

177
    return false;
2✔
178
  }
179

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

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

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

206
    // if version is a VersionRange, we are not called from install() but directly from installAsDependency() due to a version conflict of a dependency
207
    boolean extraInstallation = (version instanceof VersionRange);
3✔
208
    ToolRepository toolRepository = getToolRepository();
3✔
209
    VersionIdentifier resolvedVersion = toolRepository.resolveVersion(this.tool, edition, version, this);
8✔
210
    installToolDependencies(resolvedVersion, edition, processContext);
5✔
211

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

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

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

286
  /**
287
   * @param edition the {@link #getConfiguredEdition() tool edition} to download.
288
   * @param toolRepository the {@link ToolRepository} to use.
289
   * @param resolvedVersion the resolved {@link VersionIdentifier version} to download.
290
   * @return the {@link Path} to the downloaded release file.
291
   */
292
  protected Path downloadTool(String edition, ToolRepository toolRepository, VersionIdentifier resolvedVersion) {
293
    return toolRepository.download(this.tool, edition, resolvedVersion, this);
8✔
294
  }
295

296
  /**
297
   * Install this tool as dependency of another tool.
298
   *
299
   * @param version the required {@link VersionRange}. See {@link ToolDependency#versionRange()}.
300
   * @param processContext the {@link ProcessContext}.
301
   * @param toolParent the parent tool name needing the dependency
302
   * @return {@code true} if the tool was newly installed, {@code false} otherwise (installation was already present).
303
   */
304
  public boolean installAsDependency(VersionRange version, ProcessContext processContext, String toolParent) {
305

306
    VersionIdentifier configuredVersion = getConfiguredVersion();
3✔
307
    if (version.contains(configuredVersion)) {
4✔
308
      // prefer configured version if contained in version range
309
      return install(true, processContext, null);
6✔
310
    } else {
311
      if (isIgnoreSoftwareRepo()) {
3!
312
        throw new IllegalStateException(
×
313
            "Cannot satisfy dependency to " + this.tool + " in version " + version + " since it is conflicting with configured version " + configuredVersion
314
                + " and this tool does not support the software repository.");
315
      }
316
      this.context.info(
23✔
317
          "The tool {} requires {} in the version range {}, but your project uses version {}, which does not match."
318
              + " Therefore, we install a compatible version in that range.",
319
          toolParent, this.tool, version, configuredVersion);
320
    }
321
    ToolInstallation toolInstallation = installTool(version, processContext);
5✔
322
    return toolInstallation.newInstallation();
3✔
323
  }
324

325
  /**
326
   * Installs the tool dependencies for the current tool.
327
   *
328
   * @param version the {@link VersionIdentifier} to use.
329
   * @param edition the edition to use.
330
   * @param processContext the {@link ProcessContext} to use.
331
   */
332
  protected void installToolDependencies(VersionIdentifier version, String edition, ProcessContext processContext) {
333
    Collection<ToolDependency> dependencies = getToolRepository().findDependencies(this.tool, edition, version);
8✔
334
    String toolWithEdition = getToolWithEdition(this.tool, edition);
5✔
335
    int size = dependencies.size();
3✔
336
    this.context.debug("Tool {} has {} other tool(s) as dependency", toolWithEdition, size);
15✔
337
    for (ToolDependency dependency : dependencies) {
10✔
338
      this.context.trace("Ensuring dependency {} for tool {}", dependency.tool(), toolWithEdition);
15✔
339
      LocalToolCommandlet dependencyTool = this.context.getCommandletManager().getRequiredLocalToolCommandlet(dependency.tool());
7✔
340
      dependencyTool.installAsDependency(dependency.versionRange(), processContext, toolWithEdition);
7✔
341
    }
1✔
342
  }
1✔
343

344
  /**
345
   * Post-extraction hook that can be overridden to add custom processing after unpacking and before moving to the final destination folder.
346
   *
347
   * @param extractedDir the {@link Path} to the folder with the unpacked tool.
348
   */
349
  protected void postExtract(Path extractedDir) {
350

351
  }
1✔
352

353
  @Override
354
  public VersionIdentifier getInstalledVersion() {
355

356
    return getInstalledVersion(this.context.getSoftwarePath().resolve(getName()));
9✔
357
  }
358

359
  /**
360
   * @param toolPath the installation {@link Path} where to find the version file.
361
   * @return the currently installed {@link VersionIdentifier version} of this tool or {@code null} if not installed.
362
   */
363
  protected VersionIdentifier getInstalledVersion(Path toolPath) {
364

365
    if (!Files.isDirectory(toolPath)) {
5✔
366
      this.context.debug("Tool {} not installed in {}", getName(), toolPath);
15✔
367
      return null;
2✔
368
    }
369
    Path toolVersionFile = toolPath.resolve(IdeContext.FILE_SOFTWARE_VERSION);
4✔
370
    if (!Files.exists(toolVersionFile)) {
5✔
371
      Path legacyToolVersionFile = toolPath.resolve(IdeContext.FILE_LEGACY_SOFTWARE_VERSION);
4✔
372
      if (Files.exists(legacyToolVersionFile)) {
5!
373
        toolVersionFile = legacyToolVersionFile;
3✔
374
      } else {
375
        this.context.warning("Tool {} is missing version file in {}", getName(), toolVersionFile);
×
376
        return null;
×
377
      }
378
    }
379
    String version = this.context.getFileAccess().readFileContent(toolVersionFile).trim();
7✔
380
    return VersionIdentifier.of(version);
3✔
381
  }
382

383
  @Override
384
  public String getInstalledEdition() {
385

386
    if (this.context.getSoftwarePath() == null) {
4!
387
      return "";
×
388
    }
389
    return getInstalledEdition(this.context.getSoftwarePath().resolve(this.tool));
9✔
390
  }
391

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

422
  private String getEdition(Path toolRepoFolder, Path toolInstallFolder) {
423

424
    int toolRepoNameCount = toolRepoFolder.getNameCount();
3✔
425
    int toolInstallNameCount = toolInstallFolder.getNameCount();
3✔
426
    if (toolRepoNameCount < toolInstallNameCount) {
3!
427
      // ensure toolInstallFolder starts with $IDE_ROOT/_ide/software/default/«tool»
428
      for (int i = 0; i < toolRepoNameCount; i++) {
7✔
429
        if (!toolRepoFolder.getName(i).toString().equals(toolInstallFolder.getName(i).toString())) {
10!
430
          return null;
×
431
        }
432
      }
433
      return toolInstallFolder.getName(toolRepoNameCount).toString();
5✔
434
    }
435
    return null;
×
436
  }
437

438
  private Path getInstalledSoftwareRepoPath(Path toolPath) {
439
    if (!Files.isDirectory(toolPath)) {
5!
440
      this.context.debug("Tool {} not installed in {}", this.tool, toolPath);
×
441
      return null;
×
442
    }
443
    Path installPath = this.context.getFileAccess().toRealPath(toolPath);
6✔
444
    // if the installPath changed, a link has been resolved
445
    if (installPath.equals(toolPath)) {
4!
446
      if (!isIgnoreSoftwareRepo()) {
×
447
        this.context.warning("Tool {} is not installed via software repository (maybe from devonfw-ide). Please consider reinstalling it.", this.tool);
×
448
      }
449
      // 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
450
      return null;
×
451
    }
452
    installPath = getValidInstalledSoftwareRepoPath(installPath, context.getSoftwareRepositoryPath());
7✔
453
    return installPath;
2✔
454
  }
455

456
  Path getValidInstalledSoftwareRepoPath(Path installPath, Path softwareRepoPath) {
457
    int softwareRepoNameCount = softwareRepoPath.getNameCount();
3✔
458
    int toolInstallNameCount = installPath.getNameCount();
3✔
459
    int targetToolInstallNameCount = softwareRepoNameCount + 4;
4✔
460

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

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

511
  /**
512
   * Performs the actual uninstallation of this tool.
513
   *
514
   * @param toolPath the current {@link #getToolPath() tool path}.
515
   */
516
  protected void performUninstall(Path toolPath) {
517
    this.context.getFileAccess().delete(toolPath);
5✔
518
  }
1✔
519

520
  /**
521
   * Deletes the installed version of the tool from the shared software repository.
522
   */
523
  private void uninstallFromSoftwareRepository(Path toolPath) {
524
    Path repoPath = getInstalledSoftwareRepoPath(toolPath);
4✔
525
    if ((repoPath == null) || !Files.exists(repoPath)) {
7!
526
      this.context.warning("An installed version of {} does not exist in software repository.", this.tool);
×
527
      return;
×
528
    }
529
    this.context.info("Physically deleting {} as requested by the user via force mode.", repoPath);
10✔
530
    this.context.getFileAccess().delete(repoPath);
5✔
531
    this.context.success("Successfully deleted {} from your computer.", repoPath);
10✔
532
  }
1✔
533

534

535
  private ToolInstallation createToolInstallation(Path rootDir, VersionIdentifier resolvedVersion, boolean newInstallation,
536
      EnvironmentContext environmentContext, boolean extraInstallation) {
537

538
    Path linkDir = getMacOsHelper().findLinkDir(rootDir, getBinaryName());
7✔
539
    Path binDir = linkDir;
2✔
540
    Path binFolder = binDir.resolve(IdeContext.FOLDER_BIN);
4✔
541
    if (Files.isDirectory(binFolder)) {
5✔
542
      binDir = binFolder;
2✔
543
    }
544
    if (linkDir != rootDir) {
3✔
545
      assert (!linkDir.equals(rootDir));
5!
546
      Path toolVersionFile = rootDir.resolve(IdeContext.FILE_SOFTWARE_VERSION);
4✔
547
      if (Files.exists(toolVersionFile)) {
5!
548
        this.context.getFileAccess().copy(toolVersionFile, linkDir, FileCopyMode.COPY_FILE_OVERRIDE);
7✔
549
      }
550
    }
551
    ToolInstallation toolInstallation = new ToolInstallation(rootDir, linkDir, binDir, resolvedVersion, newInstallation);
9✔
552
    setEnvironment(environmentContext, toolInstallation, extraInstallation);
5✔
553
    return toolInstallation;
2✔
554
  }
555

556
  /**
557
   * Method to set environment variables for the process context.
558
   *
559
   * @param environmentContext the {@link EnvironmentContext} where to {@link EnvironmentContext#withEnvVar(String, String) set environment variables} for
560
   *     this tool.
561
   * @param toolInstallation the {@link ToolInstallation}.
562
   * @param extraInstallation {@code true} if the {@link ToolInstallation} is an additional installation to the
563
   *     {@link #getConfiguredVersion() configured version} due to a conflicting version of a {@link ToolDependency}, {@code false} otherwise.
564
   */
565
  public void setEnvironment(EnvironmentContext environmentContext, ToolInstallation toolInstallation, boolean extraInstallation) {
566

567
    String pathVariable = EnvironmentVariables.getToolVariablePrefix(this.tool) + "_HOME";
5✔
568
    Path toolHomePath = getToolHomePath(toolInstallation);
4✔
569
    if (toolHomePath != null) {
2!
570
      environmentContext.withEnvVar(pathVariable, toolHomePath.toString());
6✔
571
    }
572
    if (extraInstallation) {
2✔
573
      environmentContext.withPathEntry(toolInstallation.binDir());
5✔
574
    }
575
  }
1✔
576

577
  /**
578
   * Method to get the home path of the given {@link ToolInstallation}.
579
   *
580
   * @param toolInstallation the {@link ToolInstallation}.
581
   * @return the Path to the home of the tool
582
   */
583
  protected Path getToolHomePath(ToolInstallation toolInstallation) {
584
    return toolInstallation.linkDir();
3✔
585
  }
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

© 2025 Coveralls, Inc