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

devonfw / IDEasy / 17061661713

19 Aug 2025 06:38AM UTC coverage: 68.724% (-0.8%) from 69.51%
17061661713

Pull #1201

github

web-flow
Merge 2b8a5d4ec into 42204c624
Pull Request #1201: #103: introduce security module

3380 of 5385 branches covered (62.77%)

Branch coverage included in aggregate %.

8813 of 12357 relevant lines covered (71.32%)

3.13 hits per line

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

82.93
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
    this.context.getPath().setPath(this.tool, installation.binDir());
8✔
109
    postInstall(true, processContext);
4✔
110
    if (installedVersion == null) {
2✔
111
      asSuccess(step).log("Successfully installed {} in version {}", this.tool, resolvedVersion);
18✔
112
    } else {
113
      asSuccess(step).log("Successfully installed {} in version {} replacing previous version {}", this.tool, resolvedVersion, installedVersion);
21✔
114
    }
115
    return true;
2✔
116
  }
117

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

127
    if (newlyInstalled) {
2✔
128
      postInstall();
2✔
129
    }
130
  }
1✔
131

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

137
    // nothing to do by default
138
  }
1✔
139

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

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

156
    return false;
2✔
157
  }
158

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

170
    return installTool(version, processContext, getConfiguredEdition());
7✔
171
  }
172

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

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

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

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

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

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

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

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

291
  private void installToolDependencies(VersionIdentifier version, String edition, ProcessContext processContext) {
292
    Collection<ToolDependency> dependencies = getToolRepository().findDependencies(this.tool, edition, version);
8✔
293
    String toolWithEdition = getToolWithEdition(this.tool, edition);
5✔
294
    int size = dependencies.size();
3✔
295
    this.context.debug("Tool {} has {} other tool(s) as dependency", toolWithEdition, size);
15✔
296
    for (ToolDependency dependency : dependencies) {
10✔
297
      this.context.trace("Ensuring dependency {} for tool {}", dependency.tool(), toolWithEdition);
15✔
298
      LocalToolCommandlet dependencyTool = this.context.getCommandletManager().getRequiredLocalToolCommandlet(dependency.tool());
7✔
299
      dependencyTool.installAsDependency(dependency.versionRange(), processContext, toolWithEdition);
7✔
300
    }
1✔
301
  }
1✔
302

303
  /**
304
   * Post-extraction hook that can be overridden to add custom processing after unpacking and before moving to the final destination folder.
305
   *
306
   * @param extractedDir the {@link Path} to the folder with the unpacked tool.
307
   */
308
  protected void postExtract(Path extractedDir) {
309

310
  }
1✔
311

312
  @Override
313
  public VersionIdentifier getInstalledVersion() {
314

315
    return getInstalledVersion(this.context.getSoftwarePath().resolve(getName()));
9✔
316
  }
317

318
  /**
319
   * @param toolPath the installation {@link Path} where to find the version file.
320
   * @return the currently installed {@link VersionIdentifier version} of this tool or {@code null} if not installed.
321
   */
322
  protected VersionIdentifier getInstalledVersion(Path toolPath) {
323

324
    if (!Files.isDirectory(toolPath)) {
5✔
325
      this.context.debug("Tool {} not installed in {}", getName(), toolPath);
15✔
326
      return null;
2✔
327
    }
328
    Path toolVersionFile = toolPath.resolve(IdeContext.FILE_SOFTWARE_VERSION);
4✔
329
    if (!Files.exists(toolVersionFile)) {
5✔
330
      Path legacyToolVersionFile = toolPath.resolve(IdeContext.FILE_LEGACY_SOFTWARE_VERSION);
4✔
331
      if (Files.exists(legacyToolVersionFile)) {
5✔
332
        toolVersionFile = legacyToolVersionFile;
3✔
333
      } else {
334
        this.context.warning("Tool {} is missing version file in {}", getName(), toolVersionFile);
15✔
335
        return null;
2✔
336
      }
337
    }
338
    String version = this.context.getFileAccess().readFileContent(toolVersionFile).trim();
7✔
339
    return VersionIdentifier.of(version);
3✔
340
  }
341

342
  @Override
343
  public String getInstalledEdition() {
344

345
    if (this.context.getSoftwarePath() == null) {
4!
346
      return "";
×
347
    }
348
    return getInstalledEdition(this.context.getSoftwarePath().resolve(this.tool));
9✔
349
  }
350

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

378
  private String getEdition(Path toolRepoFolder, Path toolInstallFolder) {
379

380
    int toolRepoNameCount = toolRepoFolder.getNameCount();
3✔
381
    int toolInstallNameCount = toolInstallFolder.getNameCount();
3✔
382
    if (toolRepoNameCount < toolInstallNameCount) {
3!
383
      // ensure toolInstallFolder starts with $IDE_ROOT/_ide/software/default/«tool»
384
      for (int i = 0; i < toolRepoNameCount; i++) {
7✔
385
        if (!toolRepoFolder.getName(i).toString().equals(toolInstallFolder.getName(i).toString())) {
10!
386
          return null;
×
387
        }
388
      }
389
      return toolInstallFolder.getName(toolRepoNameCount).toString();
5✔
390
    }
391
    return null;
×
392
  }
393

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

412
  Path getValidInstalledSoftwareRepoPath(Path installPath, Path softwareRepoPath) {
413
    int softwareRepoNameCount = softwareRepoPath.getNameCount();
3✔
414
    int toolInstallNameCount = installPath.getNameCount();
3✔
415
    int targetToolInstallNameCount = softwareRepoNameCount + 4;
4✔
416

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

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

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

493

494
  private ToolInstallation createToolInstallation(Path rootDir, VersionIdentifier resolvedVersion, Path toolVersionFile,
495
      boolean newInstallation, EnvironmentContext environmentContext, boolean extraInstallation) {
496

497
    Path linkDir = getMacOsHelper().findLinkDir(rootDir, getBinaryName());
7✔
498
    Path binDir = linkDir;
2✔
499
    Path binFolder = binDir.resolve(IdeContext.FOLDER_BIN);
4✔
500
    if (Files.isDirectory(binFolder)) {
5✔
501
      binDir = binFolder;
2✔
502
    }
503
    if (linkDir != rootDir) {
3✔
504
      assert (!linkDir.equals(rootDir));
5!
505
      this.context.getFileAccess().copy(toolVersionFile, linkDir, FileCopyMode.COPY_FILE_OVERRIDE);
7✔
506
    }
507
    ToolInstallation toolInstallation = new ToolInstallation(rootDir, linkDir, binDir, resolvedVersion, newInstallation);
9✔
508
    setEnvironment(environmentContext, toolInstallation, extraInstallation);
5✔
509
    return toolInstallation;
2✔
510
  }
511

512
  /**
513
   * Method to set environment variables for the process context.
514
   *
515
   * @param environmentContext the {@link EnvironmentContext} where to {@link EnvironmentContext#withEnvVar(String, String) set environment variables} for
516
   *     this tool.
517
   * @param toolInstallation the {@link ToolInstallation}.
518
   * @param extraInstallation {@code true} if the {@link ToolInstallation} is an additional installation to the
519
   *     {@link #getConfiguredVersion() configured version} due to a conflicting version of a {@link ToolDependency}, {@code false} otherwise.
520
   */
521
  public void setEnvironment(EnvironmentContext environmentContext, ToolInstallation toolInstallation, boolean extraInstallation) {
522

523
    String pathVariable = EnvironmentVariables.getToolVariablePrefix(this.tool) + "_HOME";
5✔
524
    environmentContext.withEnvVar(pathVariable, toolInstallation.linkDir().toString());
7✔
525
    if (extraInstallation) {
2✔
526
      environmentContext.withPathEntry(toolInstallation.binDir());
5✔
527
    }
528
  }
1✔
529

530
  /**
531
   * @return {@link VersionIdentifier} with latest version of the tool}.
532
   */
533
  public VersionIdentifier getLatestToolVersion() {
534

535
    return this.context.getDefaultToolRepository().resolveVersion(this.tool, getConfiguredEdition(), VersionIdentifier.LATEST, this);
×
536
  }
537

538

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

559

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