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

devonfw / IDEasy / 17576623150

09 Sep 2025 08:28AM UTC coverage: 68.445% (-0.3%) from 68.725%
17576623150

Pull #1463

github

web-flow
Merge 7a2b12bae into 89be58913
Pull Request #1463: #1454: Add ng commandlet

3400 of 5437 branches covered (62.53%)

Branch coverage included in aggregate %.

8851 of 12462 relevant lines covered (71.02%)

3.12 hits per line

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

83.38
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.process.EnvironmentContext;
16
import com.devonfw.tools.ide.process.ProcessContext;
17
import com.devonfw.tools.ide.step.Step;
18
import com.devonfw.tools.ide.tool.repository.ToolRepository;
19
import com.devonfw.tools.ide.url.model.file.json.ToolDependency;
20
import com.devonfw.tools.ide.version.GenericVersionRange;
21
import com.devonfw.tools.ide.version.VersionIdentifier;
22
import com.devonfw.tools.ide.version.VersionRange;
23

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

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

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

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

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

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

64
  /**
65
   * @deprecated will be removed once all "dependencies.json" are created in ide-urls.
66
   */
67
  @Deprecated
68
  protected void installDependencies() {
69

70
  }
1✔
71

72
  @Override
73
  public boolean install(boolean silent, ProcessContext processContext, Step step) {
74

75
    installDependencies();
2✔
76
    VersionIdentifier configuredVersion = getConfiguredVersion();
3✔
77
    // get installed version before installInRepo actually may install the software
78
    VersionIdentifier installedVersion = getInstalledVersion();
3✔
79
    if (step == null) {
2!
80
      return doInstallStep(configuredVersion, installedVersion, silent, processContext, step);
8✔
81
    } else {
82
      return step.call(() -> doInstallStep(configuredVersion, installedVersion, silent, processContext, step), Boolean.FALSE);
×
83
    }
84
  }
85

86
  private boolean doInstallStep(VersionIdentifier configuredVersion, VersionIdentifier installedVersion, boolean silent, ProcessContext processContext,
87
      Step step) {
88

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

92
    // check if we already have this version installed (linked) locally in IDE_HOME/software
93
    VersionIdentifier resolvedVersion = installation.resolvedVersion();
3✔
94
    if ((resolvedVersion.equals(installedVersion) && !installation.newInstallation())
9✔
95
        || (configuredVersion.matches(installedVersion) && context.isSkipUpdatesMode())) {
6!
96
      return toolAlreadyInstalled(silent, installedVersion, processContext);
6✔
97
    }
98
    if (!isIgnoreSoftwareRepo()) {
3✔
99
      // we need to link the version or update the link.
100
      Path toolPath = getToolPath();
3✔
101
      FileAccess fileAccess = this.context.getFileAccess();
4✔
102
      if (Files.exists(toolPath, LinkOption.NOFOLLOW_LINKS)) {
9✔
103
        fileAccess.backup(toolPath);
4✔
104
      }
105
      fileAccess.mkdirs(toolPath.getParent());
4✔
106
      fileAccess.symlink(installation.linkDir(), toolPath);
5✔
107
    }
108
    if (installation.binDir() != null) {
3!
109
      this.context.getPath().setPath(this.tool, installation.binDir());
8✔
110
    }
111
    postInstall(true, processContext);
4✔
112
    if (installedVersion == null) {
2✔
113
      asSuccess(step).log("Successfully installed {} in version {}", this.tool, resolvedVersion);
18✔
114
    } else {
115
      asSuccess(step).log("Successfully installed {} in version {} replacing previous version {}", this.tool, resolvedVersion, installedVersion);
21✔
116
    }
117
    return true;
2✔
118
  }
119

120
  /**
121
   * This method is called after a tool was requested to be installed or updated.
122
   *
123
   * @param newlyInstalled {@code true} if the tool was installed or updated (at least link to software folder was created/updated), {@code false} otherwise
124
   *     (configured version was already installed and nothing changed).
125
   * @param pc the {@link ProcessContext} to use.
126
   */
127
  protected void postInstall(boolean newlyInstalled, ProcessContext pc) {
128

129
    if (newlyInstalled) {
2✔
130
      postInstall();
2✔
131
    }
132
  }
1✔
133

134
  /**
135
   * This method is called after the tool has been newly installed or updated to a new version.
136
   */
137
  protected void postInstall() {
138

139
    // nothing to do by default
140
  }
1✔
141

142
  private boolean toolAlreadyInstalled(boolean silent, VersionIdentifier installedVersion, ProcessContext pc) {
143
    if (!silent) {
2✔
144
      this.context.info("Version {} of tool {} is already installed", installedVersion, getToolWithEdition());
15✔
145
    }
146
    postInstall(false, pc);
4✔
147
    return false;
2✔
148
  }
149

150
  /**
151
   * Determines whether this tool should be installed directly in the software folder or in the software repository.
152
   *
153
   * @return {@code true} if the tool should be installed directly in the software folder, ignoring the central software repository; {@code false} if the tool
154
   *     should be installed in the central software repository (default behavior).
155
   */
156
  protected boolean isIgnoreSoftwareRepo() {
157

158
    return false;
2✔
159
  }
160

161
  /**
162
   * Performs the installation of the {@link #getName() tool} together with the environment context, managed by this
163
   * {@link com.devonfw.tools.ide.commandlet.Commandlet}.
164
   *
165
   * @param version the {@link GenericVersionRange} requested to be installed.
166
   * @param processContext the {@link ProcessContext} used to
167
   *     {@link #setEnvironment(EnvironmentContext, ToolInstallation, boolean) configure environment variables}.
168
   * @return the {@link ToolInstallation} matching the given {@code version}.
169
   */
170
  public ToolInstallation installTool(GenericVersionRange version, ProcessContext processContext) {
171

172
    return installTool(version, processContext, getConfiguredEdition());
7✔
173
  }
174

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

187
    // if version is a VersionRange, we are not called from install() but directly from installAsDependency() due to a version conflict of a dependency
188
    boolean extraInstallation = (version instanceof VersionRange);
3✔
189
    ToolRepository toolRepository = getToolRepository();
3✔
190
    VersionIdentifier resolvedVersion = toolRepository.resolveVersion(this.tool, edition, version, this);
8✔
191
    installToolDependencies(resolvedVersion, edition, processContext);
5✔
192

193
    Path installationPath;
194
    boolean ignoreSoftwareRepo = isIgnoreSoftwareRepo();
3✔
195
    if (ignoreSoftwareRepo) {
2✔
196
      installationPath = getToolPath();
4✔
197
    } else {
198
      Path softwareRepoPath = this.context.getSoftwareRepositoryPath().resolve(toolRepository.getId()).resolve(this.tool).resolve(edition);
12✔
199
      installationPath = softwareRepoPath.resolve(resolvedVersion.toString());
5✔
200
    }
201
    Path toolVersionFile = installationPath.resolve(IdeContext.FILE_SOFTWARE_VERSION);
4✔
202
    FileAccess fileAccess = this.context.getFileAccess();
4✔
203
    if (Files.isDirectory(installationPath)) {
5✔
204
      if (Files.exists(toolVersionFile)) {
5!
205
        if (!ignoreSoftwareRepo || resolvedVersion.equals(getInstalledVersion())) {
7!
206
          this.context.debug("Version {} of tool {} is already installed at {}", resolvedVersion, getToolWithEdition(this.tool, edition), installationPath);
21✔
207
          return createToolInstallation(installationPath, resolvedVersion, toolVersionFile, false, processContext, extraInstallation);
9✔
208
        }
209
      } else {
210
        // Makes sure that IDEasy will not delete itself
211
        if (this.tool.equals(IdeasyCommandlet.TOOL_NAME)) {
×
212
          this.context.warning("Your IDEasy installation is missing the version file at {}", toolVersionFile);
×
213
        } else {
214
          this.context.warning("Deleting corrupted installation at {}", installationPath);
×
215
          fileAccess.delete(installationPath);
×
216
        }
217
      }
218
    }
219
    performToolInstallation(toolRepository, resolvedVersion, installationPath, fileAccess, edition, processContext);
8✔
220
    return createToolInstallation(installationPath, resolvedVersion, toolVersionFile, true, processContext, extraInstallation);
9✔
221
  }
222

223
  /**
224
   * Performs the actual installation of the {@link #getName() tool} by downloading its binary, optionally extracting it, backing up any existing installation,
225
   * and writing the version file.
226
   * <p>
227
   * This method assumes that the version has already been resolved and dependencies installed. It handles the final steps of placing the tool into the
228
   * appropriate installation directory.
229
   *
230
   * @param toolRepository the {@link ToolRepository} used to locate and download the tool.
231
   * @param resolvedVersion the resolved {@link VersionIdentifier} of the {@link #getName() tool} to install.
232
   * @param installationPath the target {@link Path} where the {@link #getName() tool} should be installed.
233
   * @param fileAccess the {@link FileAccess} utility for file operations such as backup, extraction, and directory creation.
234
   * @param edition the specific edition of the tool to install.
235
   * @param processContext the {@link ProcessContext} used to manage the installation process.
236
   */
237
  protected void performToolInstallation(ToolRepository toolRepository, VersionIdentifier resolvedVersion, Path installationPath, FileAccess fileAccess,
238
      String edition, ProcessContext processContext) {
239

240
    Path downloadedToolFile = downloadTool(edition, toolRepository, resolvedVersion);
6✔
241
    boolean extract = isExtract();
3✔
242
    if (!extract) {
2✔
243
      this.context.trace("Extraction is disabled for '{}' hence just moving the downloaded file {}.", this.tool, downloadedToolFile);
15✔
244
    }
245
    if (Files.exists(installationPath)) {
5!
246
      fileAccess.backup(installationPath);
×
247
    }
248
    fileAccess.mkdirs(installationPath.getParent());
4✔
249
    fileAccess.extract(downloadedToolFile, installationPath, this::postExtract, extract);
7✔
250
    this.context.writeVersionFile(resolvedVersion, installationPath);
5✔
251
    this.context.debug("Installed {} in version {} at {}", this.tool, resolvedVersion, installationPath);
19✔
252
  }
1✔
253

254
  /**
255
   * @param edition the {@link #getConfiguredEdition() tool edition} to download.
256
   * @param toolRepository the {@link ToolRepository} to use.
257
   * @param resolvedVersion the resolved {@link VersionIdentifier version} to download.
258
   * @return the {@link Path} to the downloaded release file.
259
   */
260
  protected Path downloadTool(String edition, ToolRepository toolRepository, VersionIdentifier resolvedVersion) {
261
    return toolRepository.download(this.tool, edition, resolvedVersion, this);
8✔
262
  }
263

264
  /**
265
   * Install this tool as dependency of another tool.
266
   *
267
   * @param version the required {@link VersionRange}. See {@link ToolDependency#versionRange()}.
268
   * @param processContext the {@link ProcessContext}.
269
   * @param toolParent the parent tool name needing the dependency
270
   * @return {@code true} if the tool was newly installed, {@code false} otherwise (installation was already present).
271
   */
272
  public boolean installAsDependency(VersionRange version, ProcessContext processContext, String toolParent) {
273

274
    VersionIdentifier configuredVersion = getConfiguredVersion();
3✔
275
    if (version.contains(configuredVersion)) {
4✔
276
      // prefer configured version if contained in version range
277
      return install(false, processContext, null);
6✔
278
    } else {
279
      if (isIgnoreSoftwareRepo()) {
3!
280
        throw new IllegalStateException(
×
281
            "Cannot satisfy dependency to " + this.tool + " in version " + version + " since it is conflicting with configured version " + configuredVersion
282
                + " and this tool does not support the software repository.");
283
      }
284
      this.context.info(
23✔
285
          "The tool {} requires {} in the version range {}, but your project uses version {}, which does not match."
286
              + " Therefore, we install a compatible version in that range.",
287
          toolParent, this.tool, version, configuredVersion);
288
    }
289
    ToolInstallation toolInstallation = installTool(version, processContext);
5✔
290
    return toolInstallation.newInstallation();
3✔
291
  }
292

293
  /**
294
   * Installs the tool dependencies for the current tool.
295
   *
296
   * @param version the {@link VersionIdentifier} to use.
297
   * @param edition the edition to use.
298
   * @param processContext the {@link ProcessContext} to use.
299
   */
300
  protected void installToolDependencies(VersionIdentifier version, String edition, ProcessContext processContext) {
301
    Collection<ToolDependency> dependencies = getToolRepository().findDependencies(this.tool, edition, version);
8✔
302
    String toolWithEdition = getToolWithEdition(this.tool, edition);
5✔
303
    int size = dependencies.size();
3✔
304
    this.context.debug("Tool {} has {} other tool(s) as dependency", toolWithEdition, size);
15✔
305
    for (ToolDependency dependency : dependencies) {
10✔
306
      this.context.trace("Ensuring dependency {} for tool {}", dependency.tool(), toolWithEdition);
15✔
307
      LocalToolCommandlet dependencyTool = this.context.getCommandletManager().getRequiredLocalToolCommandlet(dependency.tool());
7✔
308
      dependencyTool.installAsDependency(dependency.versionRange(), processContext, toolWithEdition);
7✔
309
    }
1✔
310
  }
1✔
311

312
  /**
313
   * Post-extraction hook that can be overridden to add custom processing after unpacking and before moving to the final destination folder.
314
   *
315
   * @param extractedDir the {@link Path} to the folder with the unpacked tool.
316
   */
317
  protected void postExtract(Path extractedDir) {
318

319
  }
1✔
320

321
  @Override
322
  public VersionIdentifier getInstalledVersion() {
323

324
    return getInstalledVersion(this.context.getSoftwarePath().resolve(getName()));
9✔
325
  }
326

327
  /**
328
   * @param toolPath the installation {@link Path} where to find the version file.
329
   * @return the currently installed {@link VersionIdentifier version} of this tool or {@code null} if not installed.
330
   */
331
  protected VersionIdentifier getInstalledVersion(Path toolPath) {
332

333
    if (!Files.isDirectory(toolPath)) {
5✔
334
      this.context.debug("Tool {} not installed in {}", getName(), toolPath);
15✔
335
      return null;
2✔
336
    }
337
    Path toolVersionFile = toolPath.resolve(IdeContext.FILE_SOFTWARE_VERSION);
4✔
338
    if (!Files.exists(toolVersionFile)) {
5✔
339
      Path legacyToolVersionFile = toolPath.resolve(IdeContext.FILE_LEGACY_SOFTWARE_VERSION);
4✔
340
      if (Files.exists(legacyToolVersionFile)) {
5✔
341
        toolVersionFile = legacyToolVersionFile;
3✔
342
      } else {
343
        this.context.warning("Tool {} is missing version file in {}", getName(), toolVersionFile);
15✔
344
        return null;
2✔
345
      }
346
    }
347
    String version = this.context.getFileAccess().readFileContent(toolVersionFile).trim();
7✔
348
    return VersionIdentifier.of(version);
3✔
349
  }
350

351
  @Override
352
  public String getInstalledEdition() {
353

354
    if (this.context.getSoftwarePath() == null) {
4!
355
      return "";
×
356
    }
357
    return getInstalledEdition(this.context.getSoftwarePath().resolve(this.tool));
9✔
358
  }
359

360
  /**
361
   * @param toolPath the installation {@link Path} where to find currently installed tool. The name of the parent directory of the real path corresponding
362
   *     to the passed {@link Path path} must be the name of the edition.
363
   * @return the installed edition of this tool or {@code null} if not installed.
364
   */
365
  private String getInstalledEdition(Path toolPath) {
366
    if (!Files.isDirectory(toolPath)) {
5✔
367
      this.context.debug("Tool {} not installed in {}", this.tool, toolPath);
15✔
368
      return null;
2✔
369
    }
370
    Path realPath = this.context.getFileAccess().toRealPath(toolPath);
6✔
371
    // if the realPath changed, a link has been resolved
372
    if (realPath.equals(toolPath)) {
4✔
373
      if (!isIgnoreSoftwareRepo()) {
3!
374
        this.context.warning("Tool {} is not installed via software repository (maybe from devonfw-ide). Please consider reinstalling it.", this.tool);
11✔
375
      }
376
      // 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
377
      return getConfiguredEdition();
3✔
378
    }
379
    Path toolRepoFolder = context.getSoftwareRepositoryPath().resolve(ToolRepository.ID_DEFAULT).resolve(this.tool);
9✔
380
    String edition = getEdition(toolRepoFolder, realPath);
5✔
381
    if (!getToolRepository().getSortedEditions(this.tool).contains(edition)) {
8✔
382
      this.context.warning("Undefined edition {} of tool {}", edition, this.tool);
15✔
383
    }
384
    return edition;
2✔
385
  }
386

387
  private String getEdition(Path toolRepoFolder, Path toolInstallFolder) {
388

389
    int toolRepoNameCount = toolRepoFolder.getNameCount();
3✔
390
    int toolInstallNameCount = toolInstallFolder.getNameCount();
3✔
391
    if (toolRepoNameCount < toolInstallNameCount) {
3!
392
      // ensure toolInstallFolder starts with $IDE_ROOT/_ide/software/default/«tool»
393
      for (int i = 0; i < toolRepoNameCount; i++) {
7✔
394
        if (!toolRepoFolder.getName(i).toString().equals(toolInstallFolder.getName(i).toString())) {
10!
395
          return null;
×
396
        }
397
      }
398
      return toolInstallFolder.getName(toolRepoNameCount).toString();
5✔
399
    }
400
    return null;
×
401
  }
402

403
  private Path getInstalledSoftwareRepoPath(Path toolPath) {
404
    if (!Files.isDirectory(toolPath)) {
5!
405
      this.context.debug("Tool {} not installed in {}", this.tool, toolPath);
×
406
      return null;
×
407
    }
408
    Path installPath = this.context.getFileAccess().toRealPath(toolPath);
6✔
409
    // if the installPath changed, a link has been resolved
410
    if (installPath.equals(toolPath)) {
4!
411
      if (!isIgnoreSoftwareRepo()) {
×
412
        this.context.warning("Tool {} is not installed via software repository (maybe from devonfw-ide). Please consider reinstalling it.", this.tool);
×
413
      }
414
      // 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
415
      return null;
×
416
    }
417
    installPath = getValidInstalledSoftwareRepoPath(installPath, context.getSoftwareRepositoryPath());
7✔
418
    return installPath;
2✔
419
  }
420

421
  Path getValidInstalledSoftwareRepoPath(Path installPath, Path softwareRepoPath) {
422
    int softwareRepoNameCount = softwareRepoPath.getNameCount();
3✔
423
    int toolInstallNameCount = installPath.getNameCount();
3✔
424
    int targetToolInstallNameCount = softwareRepoNameCount + 4;
4✔
425

426
    // installPath can't be shorter than softwareRepoPath
427
    if (toolInstallNameCount < softwareRepoNameCount) {
3!
428
      this.context.warning("The installation path is not located within the software repository {}.", installPath);
×
429
      return null;
×
430
    }
431
    // ensure installPath starts with $IDE_ROOT/_ide/software/
432
    for (int i = 0; i < softwareRepoNameCount; i++) {
7✔
433
      if (!softwareRepoPath.getName(i).toString().equals(installPath.getName(i).toString())) {
10✔
434
        this.context.warning("The installation path is not located within the software repository {}.", installPath);
10✔
435
        return null;
2✔
436
      }
437
    }
438
    // return $IDE_ROOT/_ide/software/«id»/«tool»/«edition»/«version»
439
    if (toolInstallNameCount == targetToolInstallNameCount) {
3✔
440
      return installPath;
2✔
441
    } else if (toolInstallNameCount > targetToolInstallNameCount) {
3✔
442
      Path validInstallPath = installPath;
2✔
443
      for (int i = 0; i < toolInstallNameCount - targetToolInstallNameCount; i++) {
9✔
444
        validInstallPath = validInstallPath.getParent();
3✔
445
      }
446
      return validInstallPath;
2✔
447
    } else {
448
      this.context.warning("The installation path is faulty {}.", installPath);
10✔
449
      return null;
2✔
450
    }
451
  }
452

453
  @Override
454
  public void uninstall() {
455
    try {
456
      Path toolPath = getToolPath();
3✔
457
      if (!Files.exists(toolPath)) {
5!
458
        this.context.warning("An installed version of " + this.tool + " does not exist.");
×
459
        return;
×
460
      }
461
      if (this.context.isForceMode()) {
4✔
462
        this.context.warning(
10✔
463
            "Sub-command uninstall via force mode will physically delete the currently installed version of " + this.tool + " from the machine.\n"
464
                + "This may cause issues with other projects, that use the same version of " + this.tool + ".\n"
465
                + "Deleting " + this.tool + " version " + getInstalledVersion() + " from your machine.");
3✔
466
        uninstallFromSoftwareRepository(toolPath);
3✔
467
      }
468
      try {
469
        this.context.getFileAccess().delete(toolPath);
5✔
470
        this.context.success("Successfully uninstalled " + this.tool);
6✔
471
      } catch (Exception e) {
×
472
        this.context.error("Couldn't uninstall " + this.tool + ". ", e);
×
473
      }
1✔
474
    } catch (Exception e) {
×
475
      this.context.error(e.getMessage(), e);
×
476
    }
1✔
477
  }
1✔
478

479
  /**
480
   * Deletes the installed version of the tool from the shared software repository.
481
   */
482
  private void uninstallFromSoftwareRepository(Path toolPath) {
483
    try {
484
      Path repoPath = getInstalledSoftwareRepoPath(toolPath);
4✔
485
      if (!Files.exists(repoPath)) {
5!
486
        this.context.warning("An installed version of " + this.tool + " does not exist.");
×
487
        return;
×
488
      }
489
      this.context.info("Physically deleting " + repoPath + " as requested by the user via force mode.");
6✔
490
      try {
491
        this.context.getFileAccess().delete(repoPath);
5✔
492
        this.context.success("Successfully deleted " + repoPath + " from your computer.");
6✔
493
      } catch (Exception e) {
×
494
        this.context.error("Couldn't delete " + this.tool + " from your computer.", e);
×
495
      }
1✔
496
    } catch (Exception e) {
×
497
      throw new IllegalStateException(
×
498
          " Couldn't uninstall " + this.tool + ". Couldn't determine the software repository path for " + this.tool + ".", e);
499
    }
1✔
500
  }
1✔
501

502

503
  private ToolInstallation createToolInstallation(Path rootDir, VersionIdentifier resolvedVersion, Path toolVersionFile,
504
      boolean newInstallation, EnvironmentContext environmentContext, boolean extraInstallation) {
505

506
    Path linkDir = getMacOsHelper().findLinkDir(rootDir, getBinaryName());
7✔
507
    Path binDir = linkDir;
2✔
508
    Path binFolder = binDir.resolve(IdeContext.FOLDER_BIN);
4✔
509
    if (Files.isDirectory(binFolder)) {
5✔
510
      binDir = binFolder;
2✔
511
    }
512
    if (linkDir != rootDir) {
3✔
513
      assert (!linkDir.equals(rootDir));
5!
514
      this.context.getFileAccess().copy(toolVersionFile, linkDir, FileCopyMode.COPY_FILE_OVERRIDE);
7✔
515
    }
516
    ToolInstallation toolInstallation = new ToolInstallation(rootDir, linkDir, binDir, resolvedVersion, newInstallation);
9✔
517
    setEnvironment(environmentContext, toolInstallation, extraInstallation);
5✔
518
    return toolInstallation;
2✔
519
  }
520

521
  /**
522
   * Method to set environment variables for the process context.
523
   *
524
   * @param environmentContext the {@link EnvironmentContext} where to {@link EnvironmentContext#withEnvVar(String, String) set environment variables} for
525
   *     this tool.
526
   * @param toolInstallation the {@link ToolInstallation}.
527
   * @param extraInstallation {@code true} if the {@link ToolInstallation} is an additional installation to the
528
   *     {@link #getConfiguredVersion() configured version} due to a conflicting version of a {@link ToolDependency}, {@code false} otherwise.
529
   */
530
  public void setEnvironment(EnvironmentContext environmentContext, ToolInstallation toolInstallation, boolean extraInstallation) {
531

532
    String pathVariable = EnvironmentVariables.getToolVariablePrefix(this.tool) + "_HOME";
5✔
533
    environmentContext.withEnvVar(pathVariable, toolInstallation.linkDir().toString());
7✔
534
    if (extraInstallation) {
2✔
535
      environmentContext.withPathEntry(toolInstallation.binDir());
5✔
536
    }
537
  }
1✔
538

539
  /**
540
   * @return {@link VersionIdentifier} with latest version of the tool}.
541
   */
542
  public VersionIdentifier getLatestToolVersion() {
543

544
    return this.context.getDefaultToolRepository().resolveVersion(this.tool, getConfiguredEdition(), VersionIdentifier.LATEST, this);
×
545
  }
546

547

548
  /**
549
   * Searches for a wrapper file in valid projects (containing a build file f.e. build.gradle or pom.xml) and returns its path.
550
   *
551
   * @param wrapperFileName the name of the wrapper file
552
   * @param filter the {@link Predicate} to match
553
   * @return Path of the wrapper file or {@code null} if none was found.
554
   */
555
  protected Path findWrapper(String wrapperFileName, Predicate<Path> filter) {
556
    Path dir = context.getCwd();
4✔
557
    // traverse the cwd directory containing a build file up till a wrapper file was found
558
    while ((dir != null) && filter.test(dir)) {
6!
559
      if (Files.exists(dir.resolve(wrapperFileName))) {
7✔
560
        context.debug("Using wrapper file at: {}", dir);
10✔
561
        return dir.resolve(wrapperFileName);
4✔
562
      }
563
      dir = dir.getParent();
4✔
564
    }
565
    return null;
2✔
566
  }
567

568

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