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

devonfw / IDEasy / 18597783582

17 Oct 2025 03:45PM UTC coverage: 68.522% (+0.09%) from 68.437%
18597783582

Pull #1467

github

web-flow
Merge 4e5b40da9 into 46d29352c
Pull Request #1467: #907 Add yarn and corepack

3467 of 5539 branches covered (62.59%)

Branch coverage included in aggregate %.

9054 of 12734 relevant lines covered (71.1%)

3.13 hits per line

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

82.55
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
    // install configured version of our tool in the software repository if not already installed
100
    ToolInstallation installation = installTool(configuredVersion, processContext);
5✔
101

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

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

140
    if (newlyInstalled) {
2✔
141
      postInstall();
2✔
142
    }
143
  }
1✔
144

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

150
    // nothing to do by default
151
  }
1✔
152

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

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

173
    return false;
2✔
174
  }
175

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

187
    return installTool(version, processContext, getConfiguredEdition());
7✔
188
  }
189

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

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

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

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

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

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

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

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

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

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

347
  }
1✔
348

349
  @Override
350
  public VersionIdentifier getInstalledVersion() {
351

352
    return getInstalledVersion(this.context.getSoftwarePath().resolve(getName()));
9✔
353
  }
354

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

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

379
  @Override
380
  public String getInstalledEdition() {
381

382
    if (this.context.getSoftwarePath() == null) {
4!
383
      return "";
×
384
    }
385
    return getInstalledEdition(this.context.getSoftwarePath().resolve(this.tool));
9✔
386
  }
387

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

415
  private String getEdition(Path toolRepoFolder, Path toolInstallFolder) {
416

417
    int toolRepoNameCount = toolRepoFolder.getNameCount();
3✔
418
    int toolInstallNameCount = toolInstallFolder.getNameCount();
3✔
419
    if (toolRepoNameCount < toolInstallNameCount) {
3!
420
      // ensure toolInstallFolder starts with $IDE_ROOT/_ide/software/default/«tool»
421
      for (int i = 0; i < toolRepoNameCount; i++) {
7✔
422
        if (!toolRepoFolder.getName(i).toString().equals(toolInstallFolder.getName(i).toString())) {
10!
423
          return null;
×
424
        }
425
      }
426
      return toolInstallFolder.getName(toolRepoNameCount).toString();
5✔
427
    }
428
    return null;
×
429
  }
430

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

449
  Path getValidInstalledSoftwareRepoPath(Path installPath, Path softwareRepoPath) {
450
    int softwareRepoNameCount = softwareRepoPath.getNameCount();
3✔
451
    int toolInstallNameCount = installPath.getNameCount();
3✔
452
    int targetToolInstallNameCount = softwareRepoNameCount + 4;
4✔
453

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

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

504
  /**
505
   * Performs the actual uninstallation of this tool.
506
   *
507
   * @param toolPath the current {@link #getToolPath() tool path}.
508
   */
509
  protected void performUninstall(Path toolPath) {
510
    this.context.getFileAccess().delete(toolPath);
5✔
511
  }
1✔
512

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

527

528
  private ToolInstallation createToolInstallation(Path rootDir, VersionIdentifier resolvedVersion, boolean newInstallation,
529
      EnvironmentContext environmentContext, boolean extraInstallation) {
530

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

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

560
    String pathVariable = EnvironmentVariables.getToolVariablePrefix(this.tool) + "_HOME";
5✔
561
    Path toolHomePath = getToolHomePath(toolInstallation);
4✔
562
    if (toolHomePath != null) {
2!
563
      environmentContext.withEnvVar(pathVariable, toolHomePath.toString());
6✔
564
    }
565
    if (extraInstallation) {
2✔
566
      environmentContext.withPathEntry(toolInstallation.binDir());
5✔
567
    }
568
  }
1✔
569

570
  /**
571
   * Method to get the home path of the given {@link ToolInstallation}.
572
   *
573
   * @param toolInstallation the {@link ToolInstallation}.
574
   * @return the Path to the home of the tool
575
   */
576
  protected Path getToolHomePath(ToolInstallation toolInstallation) {
577
    return toolInstallation.linkDir();
3✔
578
  }
579

580
  /**
581
   * @return {@link VersionIdentifier} with latest version of the tool}.
582
   */
583
  public VersionIdentifier getLatestToolVersion() {
584

585
    return this.context.getDefaultToolRepository().resolveVersion(this.tool, getConfiguredEdition(), VersionIdentifier.LATEST, this);
×
586
  }
587

588

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

609

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