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

devonfw / IDEasy / 16299443781

15 Jul 2025 04:56PM UTC coverage: 68.439% (-0.007%) from 68.446%
16299443781

Pull #1410

github

web-flow
Merge 6a6965c38 into ab7eb024e
Pull Request #1410: Add support for extra tool installations to run multiple versions in parallel

3301 of 5226 branches covered (63.16%)

Branch coverage included in aggregate %.

8441 of 11931 relevant lines covered (70.75%)

3.12 hits per line

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

78.18
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 extra tool is located (installed).
53
   */
54
  public Path getExtraToolPath() {
55
    if (this.context.getSoftwareExtraPath() == null) {
4!
56
      return null;
×
57
    }
58
    return this.context.getSoftwareExtraPath().resolve(getName());
7✔
59
  }
60

61
  /**
62
   * @return the {@link EnvironmentVariables#getExtraToolVersion(String) extra tool version}.
63
   */
64
  public VersionIdentifier getExtraConfiguredVersion() {
65
    return this.context.getVariables().getExtraToolVersion(getName());
7✔
66
  }
67

68
  /**
69
   * @return the {@link EnvironmentVariables#getExtraToolEdition(String) extra tool edition}.
70
   */
71
  public String getExtraConfiguredEdition() {
72
    String edition = this.context.getVariables().getExtraToolEdition(getName());
7✔
73
    if (edition == null) {
2✔
74
      // fallback to regular edition mechanism
75
      edition = getConfiguredEdition();
3✔
76
    }
77
    return edition;
2✔
78
  }
79

80
  /**
81
   * @return {@code true} if this tool supports extra installations, {@code false} otherwise.
82
   */
83
  public boolean isExtraToolSupported() {
84
    // GraalVM already uses extra path, so extra tool feature is not needed
85
    if ("graalvm".equals(getName())) {
5✔
86
      return false;
2✔
87
    }
88
    // IDEs don't typically need parallel installations
89
    if (getTags().contains(Tag.IDE)) {
5✔
90
      return false;
2✔
91
    }
92
    return true;
2✔
93
  }
94

95
  /**
96
   * @return the {@link Path} where the executables of the tool can be found. Typically a "bin" folder inside {@link #getToolPath() tool path}.
97
   */
98
  public Path getToolBinPath() {
99

100
    Path toolPath = getToolPath();
3✔
101
    Path binPath = this.context.getFileAccess().findFirst(toolPath, path -> path.getFileName().toString().equals("bin"), false);
14✔
102
    if ((binPath != null) && Files.isDirectory(binPath)) {
7!
103
      return binPath;
2✔
104
    }
105
    return toolPath;
2✔
106
  }
107

108
  /**
109
   * @deprecated will be removed once all "dependencies.json" are created in ide-urls.
110
   */
111
  @Deprecated
112
  protected void installDependencies() {
113

114
  }
1✔
115

116
  @Override
117
  public boolean install(boolean silent, ProcessContext processContext, Step step) {
118

119
    installDependencies();
2✔
120
    VersionIdentifier configuredVersion = getConfiguredVersion();
3✔
121
    // get installed version before installInRepo actually may install the software
122
    VersionIdentifier installedVersion = getInstalledVersion();
3✔
123
    if (step == null) {
2!
124
      return doInstallStep(configuredVersion, installedVersion, silent, processContext, step);
8✔
125
    } else {
126
      return step.call(() -> doInstallStep(configuredVersion, installedVersion, silent, processContext, step), Boolean.FALSE);
×
127
    }
128
  }
129

130
  private boolean doInstallStep(VersionIdentifier configuredVersion, VersionIdentifier installedVersion, boolean silent, ProcessContext processContext,
131
      Step step) {
132

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

136
    // check if we already have this version installed (linked) locally in IDE_HOME/software
137
    VersionIdentifier resolvedVersion = installation.resolvedVersion();
3✔
138
    boolean toolWasInstalled = false;
2✔
139
    if ((resolvedVersion.equals(installedVersion) && !installation.newInstallation())
9✔
140
        || (configuredVersion.matches(installedVersion) && context.isSkipUpdatesMode())) {
6!
141
      toolWasInstalled = toolAlreadyInstalled(silent, installedVersion, processContext);
7✔
142
    } else {
143
      if (!isIgnoreSoftwareRepo()) {
3✔
144
        // we need to link the version or update the link.
145
        Path toolPath = getToolPath();
3✔
146
        FileAccess fileAccess = this.context.getFileAccess();
4✔
147
        if (Files.exists(toolPath, LinkOption.NOFOLLOW_LINKS)) {
9✔
148
          fileAccess.backup(toolPath);
4✔
149
        }
150
        fileAccess.mkdirs(toolPath.getParent());
4✔
151
        fileAccess.symlink(installation.linkDir(), toolPath);
5✔
152
      }
153
      this.context.getPath().setPath(this.tool, installation.binDir());
8✔
154
      postInstall(true, processContext);
4✔
155
      if (installedVersion == null) {
2✔
156
        asSuccess(step).log("Successfully installed {} in version {}", this.tool, resolvedVersion);
18✔
157
      } else {
158
        asSuccess(step).log("Successfully installed {} in version {} replacing previous version {}", this.tool, resolvedVersion, installedVersion);
21✔
159
      }
160
      toolWasInstalled = true;
2✔
161
    }
162

163
    // Handle extra tool installation
164
    if (isExtraToolSupported()) {
3✔
165
      VersionIdentifier extraVersion = getExtraConfiguredVersion();
3✔
166
      if (extraVersion != null) {
2!
167
        installExtraTool(extraVersion, processContext, step);
×
168
      }
169
    }
170
    
171
    return toolWasInstalled;
2✔
172
  }
173

174
  /**
175
   * This method is called after a tool was requested to be installed or updated.
176
   *
177
   * @param newlyInstalled {@code true} if the tool was installed or updated (at least link to software folder was created/updated), {@code false} otherwise
178
   *     (configured version was already installed and nothing changed).
179
   * @param pc the {@link ProcessContext} to use.
180
   */
181
  protected void postInstall(boolean newlyInstalled, ProcessContext pc) {
182

183
    if (newlyInstalled) {
2✔
184
      postInstall();
2✔
185
    }
186
  }
1✔
187

188
  /**
189
   * This method is called after the tool has been newly installed or updated to a new version.
190
   */
191
  protected void postInstall() {
192

193
    // nothing to do by default
194
  }
1✔
195

196
  private boolean toolAlreadyInstalled(boolean silent, VersionIdentifier installedVersion, ProcessContext pc) {
197
    if (!silent) {
2✔
198
      this.context.info("Version {} of tool {} is already installed", installedVersion, getToolWithEdition());
15✔
199
    }
200
    postInstall(false, pc);
4✔
201
    return false;
2✔
202
  }
203

204
  /**
205
   * Determines whether this tool should be installed directly in the software folder or in the software repository.
206
   *
207
   * @return {@code true} if the tool should be installed directly in the software folder, ignoring the central software repository; {@code false} if the tool
208
   *     should be installed in the central software repository (default behavior).
209
   */
210
  protected boolean isIgnoreSoftwareRepo() {
211

212
    return false;
2✔
213
  }
214

215
  /**
216
   * Performs the installation of the {@link #getName() tool} together with the environment context, managed by this
217
   * {@link com.devonfw.tools.ide.commandlet.Commandlet}.
218
   *
219
   * @param version the {@link GenericVersionRange} requested to be installed.
220
   * @param processContext the {@link ProcessContext} used to
221
   *     {@link #setEnvironment(EnvironmentContext, ToolInstallation, boolean) configure environment variables}.
222
   * @return the {@link ToolInstallation} matching the given {@code version}.
223
   */
224
  public ToolInstallation installTool(GenericVersionRange version, ProcessContext processContext) {
225

226
    return installTool(version, processContext, getConfiguredEdition());
7✔
227
  }
228

229
  /**
230
   * Performs the installation of the {@link #getName() tool} together with the environment context  managed by this
231
   * {@link com.devonfw.tools.ide.commandlet.Commandlet}.
232
   *
233
   * @param version the {@link GenericVersionRange} requested to be installed.
234
   * @param processContext the {@link ProcessContext} used to
235
   *     {@link #setEnvironment(EnvironmentContext, ToolInstallation, boolean) configure environment variables}.
236
   * @param edition the specific {@link #getConfiguredEdition() edition} to install.
237
   * @return the {@link ToolInstallation} matching the given {@code version}.
238
   */
239
  public ToolInstallation installTool(GenericVersionRange version, ProcessContext processContext, String edition) {
240

241
    // if version is a VersionRange, we are not called from install() but directly from installAsDependency() due to a version conflict of a dependency
242
    boolean extraInstallation = (version instanceof VersionRange);
3✔
243
    ToolRepository toolRepository = getToolRepository();
3✔
244
    VersionIdentifier resolvedVersion = toolRepository.resolveVersion(this.tool, edition, version, this);
8✔
245
    installToolDependencies(resolvedVersion, edition, processContext);
5✔
246

247
    Path installationPath;
248
    boolean ignoreSoftwareRepo = isIgnoreSoftwareRepo();
3✔
249
    if (ignoreSoftwareRepo) {
2✔
250
      installationPath = getToolPath();
4✔
251
    } else {
252
      Path softwareRepoPath = this.context.getSoftwareRepositoryPath().resolve(toolRepository.getId()).resolve(this.tool).resolve(edition);
12✔
253
      installationPath = softwareRepoPath.resolve(resolvedVersion.toString());
5✔
254
    }
255
    Path toolVersionFile = installationPath.resolve(IdeContext.FILE_SOFTWARE_VERSION);
4✔
256
    FileAccess fileAccess = this.context.getFileAccess();
4✔
257
    if (Files.isDirectory(installationPath)) {
5✔
258
      if (Files.exists(toolVersionFile)) {
5!
259
        if (!ignoreSoftwareRepo || resolvedVersion.equals(getInstalledVersion())) {
2!
260
          this.context.debug("Version {} of tool {} is already installed at {}", resolvedVersion, getToolWithEdition(this.tool, edition), installationPath);
21✔
261
          return createToolInstallation(installationPath, resolvedVersion, toolVersionFile, false, processContext, extraInstallation);
9✔
262
        }
263
      } else {
264
        // Makes sure that IDEasy will not delete itself
265
        if (this.tool.equals(IdeasyCommandlet.TOOL_NAME)) {
×
266
          this.context.warning("Your IDEasy installation is missing the version file at {}", toolVersionFile);
×
267
        } else {
268
          this.context.warning("Deleting corrupted installation at {}", installationPath);
×
269
          fileAccess.delete(installationPath);
×
270
        }
271
      }
272
    }
273
    Path downloadedToolFile = downloadTool(edition, toolRepository, resolvedVersion);
6✔
274
    boolean extract = isExtract();
3✔
275
    if (!extract) {
2✔
276
      this.context.trace("Extraction is disabled for '{}' hence just moving the downloaded file {}.", this.tool, downloadedToolFile);
15✔
277
    }
278
    if (Files.exists(installationPath)) {
5!
279
      fileAccess.backup(installationPath);
×
280
    }
281
    fileAccess.mkdirs(installationPath.getParent());
4✔
282
    fileAccess.extract(downloadedToolFile, installationPath, this::postExtract, extract);
7✔
283
    this.context.writeVersionFile(resolvedVersion, installationPath);
5✔
284
    this.context.debug("Installed {} in version {} at {}", this.tool, resolvedVersion, installationPath);
19✔
285
    return createToolInstallation(installationPath, resolvedVersion, toolVersionFile, true, processContext, extraInstallation);
9✔
286
  }
287

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

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

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

327
  /**
328
   * Installs the extra tool if configured.
329
   *
330
   * @param extraVersion the {@link VersionIdentifier} of the extra tool to install.
331
   * @param processContext the {@link ProcessContext} to use.
332
   * @param step the {@link Step} to track the installation. May be {@code null}.
333
   */
334
  private void installExtraTool(VersionIdentifier extraVersion, ProcessContext processContext, Step step) {
335
    
336
    String extraEdition = getExtraConfiguredEdition();
×
337
    
338
    // Check if extra version/edition is identical to regular version/edition
339
    VersionIdentifier configuredVersion = getConfiguredVersion();
×
340
    String configuredEdition = getConfiguredEdition();
×
341
    
342
    if (configuredVersion.equals(extraVersion) && configuredEdition.equals(extraEdition)) {
×
343
      this.context.warning("Extra tool configuration for {} is identical to regular configuration (version: {}, edition: {}). "
×
344
          + "This extra installation will be identical to the regular tool installation.", 
345
          this.tool, extraVersion, extraEdition);
346
    }
347
    
348
    // Install extra tool to software repository
349
    ToolInstallation extraInstallation = installTool(extraVersion, processContext, extraEdition);
×
350
    
351
    // Create link to extra tool path
352
    Path extraToolPath = getExtraToolPath();
×
353
    FileAccess fileAccess = this.context.getFileAccess();
×
354
    if (Files.exists(extraToolPath, LinkOption.NOFOLLOW_LINKS)) {
×
355
      fileAccess.backup(extraToolPath);
×
356
    }
357
    fileAccess.mkdirs(extraToolPath.getParent());
×
358
    fileAccess.symlink(extraInstallation.linkDir(), extraToolPath);
×
359
    
360
    // Set extra tool environment variables (but don't add to PATH)
361
    String extraHomeVar = "EXTRA_" + EnvironmentVariables.getToolVariablePrefix(this.tool) + "_HOME";
×
362
    processContext.withEnvVar(extraHomeVar, extraInstallation.linkDir().toString());
×
363
    
364
    this.context.info("Successfully installed extra {} in version {} (edition: {})", this.tool, extraVersion, extraEdition);
×
365
  }
×
366

367
  private void installToolDependencies(VersionIdentifier version, String edition, ProcessContext processContext) {
368
    Collection<ToolDependency> dependencies = getToolRepository().findDependencies(this.tool, edition, version);
8✔
369
    String toolWithEdition = getToolWithEdition(this.tool, edition);
5✔
370
    int size = dependencies.size();
3✔
371
    this.context.debug("Tool {} has {} other tool(s) as dependency", toolWithEdition, size);
15✔
372
    for (ToolDependency dependency : dependencies) {
10✔
373
      this.context.trace("Ensuring dependency {} for tool {}", dependency.tool(), toolWithEdition);
15✔
374
      LocalToolCommandlet dependencyTool = this.context.getCommandletManager().getRequiredLocalToolCommandlet(dependency.tool());
7✔
375
      dependencyTool.installAsDependency(dependency.versionRange(), processContext, toolWithEdition);
7✔
376
    }
1✔
377
  }
1✔
378

379
  /**
380
   * Post-extraction hook that can be overridden to add custom processing after unpacking and before moving to the final destination folder.
381
   *
382
   * @param extractedDir the {@link Path} to the folder with the unpacked tool.
383
   */
384
  protected void postExtract(Path extractedDir) {
385

386
  }
1✔
387

388
  @Override
389
  public VersionIdentifier getInstalledVersion() {
390

391
    return getInstalledVersion(this.context.getSoftwarePath().resolve(getName()));
9✔
392
  }
393

394
  /**
395
   * @return the currently installed {@link VersionIdentifier version} of the extra tool or {@code null} if not installed.
396
   */
397
  public VersionIdentifier getExtraInstalledVersion() {
398
    Path extraToolPath = getExtraToolPath();
3✔
399
    if (extraToolPath == null) {
2!
400
      return null;
×
401
    }
402
    return getInstalledVersion(extraToolPath);
4✔
403
  }
404

405
  /**
406
   * @param toolPath the installation {@link Path} where to find the version file.
407
   * @return the currently installed {@link VersionIdentifier version} of this tool or {@code null} if not installed.
408
   */
409
  protected VersionIdentifier getInstalledVersion(Path toolPath) {
410

411
    if (!Files.isDirectory(toolPath)) {
5✔
412
      this.context.debug("Tool {} not installed in {}", getName(), toolPath);
15✔
413
      return null;
2✔
414
    }
415
    Path toolVersionFile = toolPath.resolve(IdeContext.FILE_SOFTWARE_VERSION);
4✔
416
    if (!Files.exists(toolVersionFile)) {
5✔
417
      Path legacyToolVersionFile = toolPath.resolve(IdeContext.FILE_LEGACY_SOFTWARE_VERSION);
4✔
418
      if (Files.exists(legacyToolVersionFile)) {
5✔
419
        toolVersionFile = legacyToolVersionFile;
3✔
420
      } else {
421
        this.context.warning("Tool {} is missing version file in {}", getName(), toolVersionFile);
15✔
422
        return null;
2✔
423
      }
424
    }
425
    String version = this.context.getFileAccess().readFileContent(toolVersionFile).trim();
7✔
426
    return VersionIdentifier.of(version);
3✔
427
  }
428

429
  @Override
430
  public String getInstalledEdition() {
431

432
    if (this.context.getSoftwarePath() == null) {
4!
433
      return "";
×
434
    }
435
    return getInstalledEdition(this.context.getSoftwarePath().resolve(this.tool));
9✔
436
  }
437

438
  /**
439
   * @param toolPath the installation {@link Path} where to find currently installed tool. The name of the parent directory of the real path corresponding
440
   *     to the passed {@link Path path} must be the name of the edition.
441
   * @return the installed edition of this tool or {@code null} if not installed.
442
   */
443
  private String getInstalledEdition(Path toolPath) {
444
    if (!Files.isDirectory(toolPath)) {
5✔
445
      this.context.debug("Tool {} not installed in {}", this.tool, toolPath);
15✔
446
      return null;
2✔
447
    }
448
    Path realPath = this.context.getFileAccess().toRealPath(toolPath);
6✔
449
    // if the realPath changed, a link has been resolved
450
    if (realPath.equals(toolPath)) {
4✔
451
      if (!isIgnoreSoftwareRepo()) {
3!
452
        this.context.warning("Tool {} is not installed via software repository (maybe from devonfw-ide). Please consider reinstalling it.", this.tool);
11✔
453
      }
454
      // 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
455
      return getConfiguredEdition();
3✔
456
    }
457
    Path toolRepoFolder = context.getSoftwareRepositoryPath().resolve(ToolRepository.ID_DEFAULT).resolve(this.tool);
9✔
458
    String edition = getEdition(toolRepoFolder, realPath);
5✔
459
    if (!getToolRepository().getSortedEditions(this.tool).contains(edition)) {
8✔
460
      this.context.warning("Undefined edition {} of tool {}", edition, this.tool);
15✔
461
    }
462
    return edition;
2✔
463
  }
464

465
  private String getEdition(Path toolRepoFolder, Path toolInstallFolder) {
466

467
    int toolRepoNameCount = toolRepoFolder.getNameCount();
3✔
468
    int toolInstallNameCount = toolInstallFolder.getNameCount();
3✔
469
    if (toolRepoNameCount < toolInstallNameCount) {
3!
470
      // ensure toolInstallFolder starts with $IDE_ROOT/_ide/software/default/«tool»
471
      for (int i = 0; i < toolRepoNameCount; i++) {
7✔
472
        if (!toolRepoFolder.getName(i).toString().equals(toolInstallFolder.getName(i).toString())) {
10!
473
          return null;
×
474
        }
475
      }
476
      return toolInstallFolder.getName(toolRepoNameCount).toString();
5✔
477
    }
478
    return null;
×
479
  }
480

481
  private Path getInstalledSoftwareRepoPath(Path toolPath) {
482
    if (!Files.isDirectory(toolPath)) {
5!
483
      this.context.debug("Tool {} not installed in {}", this.tool, toolPath);
×
484
      return null;
×
485
    }
486
    Path installPath = this.context.getFileAccess().toRealPath(toolPath);
6✔
487
    // if the installPath changed, a link has been resolved
488
    if (installPath.equals(toolPath)) {
4!
489
      if (!isIgnoreSoftwareRepo()) {
×
490
        this.context.warning("Tool {} is not installed via software repository (maybe from devonfw-ide). Please consider reinstalling it.", this.tool);
×
491
      }
492
      // 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
493
      return null;
×
494
    }
495
    installPath = getValidInstalledSoftwareRepoPath(installPath, context.getSoftwareRepositoryPath());
7✔
496
    return installPath;
2✔
497
  }
498

499
  Path getValidInstalledSoftwareRepoPath(Path installPath, Path softwareRepoPath) {
500
    int softwareRepoNameCount = softwareRepoPath.getNameCount();
3✔
501
    int toolInstallNameCount = installPath.getNameCount();
3✔
502
    int targetToolInstallNameCount = softwareRepoNameCount + 4;
4✔
503

504
    // installPath can't be shorter than softwareRepoPath
505
    if (toolInstallNameCount < softwareRepoNameCount) {
3!
506
      this.context.warning("The installation path is not located within the software repository {}.", installPath);
×
507
      return null;
×
508
    }
509
    // ensure installPath starts with $IDE_ROOT/_ide/software/
510
    for (int i = 0; i < softwareRepoNameCount; i++) {
7✔
511
      if (!softwareRepoPath.getName(i).toString().equals(installPath.getName(i).toString())) {
10✔
512
        this.context.warning("The installation path is not located within the software repository {}.", installPath);
10✔
513
        return null;
2✔
514
      }
515
    }
516
    // return $IDE_ROOT/_ide/software/«id»/«tool»/«edition»/«version»
517
    if (toolInstallNameCount == targetToolInstallNameCount) {
3✔
518
      return installPath;
2✔
519
    } else if (toolInstallNameCount > targetToolInstallNameCount) {
3✔
520
      Path validInstallPath = installPath;
2✔
521
      for (int i = 0; i < toolInstallNameCount - targetToolInstallNameCount; i++) {
9✔
522
        validInstallPath = validInstallPath.getParent();
3✔
523
      }
524
      return validInstallPath;
2✔
525
    } else {
526
      this.context.warning("The installation path is faulty {}.", installPath);
10✔
527
      return null;
2✔
528
    }
529
  }
530

531
  @Override
532
  public void uninstall() {
533
    try {
534
      Path toolPath = getToolPath();
3✔
535
      if (!Files.exists(toolPath)) {
5!
536
        this.context.warning("An installed version of " + this.tool + " does not exist.");
×
537
        return;
×
538
      }
539
      if (this.context.isForceMode()) {
4✔
540
        this.context.warning(
10✔
541
            "Sub-command uninstall via force mode will physically delete the currently installed version of " + this.tool + " from the machine.\n"
542
                + "This may cause issues with other projects, that use the same version of " + this.tool + ".\n"
543
                + "Deleting " + this.tool + " version " + getInstalledVersion() + " from your machine.");
3✔
544
        uninstallFromSoftwareRepository(toolPath);
3✔
545
      }
546
      try {
547
        this.context.getFileAccess().delete(toolPath);
5✔
548
        this.context.success("Successfully uninstalled " + this.tool);
6✔
549
      } catch (Exception e) {
×
550
        this.context.error("Couldn't uninstall " + this.tool + ". ", e);
×
551
      }
1✔
552
    } catch (Exception e) {
×
553
      this.context.error(e.getMessage(), e);
×
554
    }
1✔
555
  }
1✔
556

557
  /**
558
   * Deletes the installed version of the tool from the shared software repository.
559
   */
560
  private void uninstallFromSoftwareRepository(Path toolPath) {
561
    try {
562
      Path repoPath = getInstalledSoftwareRepoPath(toolPath);
4✔
563
      if (!Files.exists(repoPath)) {
5!
564
        this.context.warning("An installed version of " + this.tool + " does not exist.");
×
565
        return;
×
566
      }
567
      this.context.info("Physically deleting " + repoPath + " as requested by the user via force mode.");
6✔
568
      try {
569
        this.context.getFileAccess().delete(repoPath);
5✔
570
        this.context.success("Successfully deleted " + repoPath + " from your computer.");
6✔
571
      } catch (Exception e) {
×
572
        this.context.error("Couldn't delete " + this.tool + " from your computer.", e);
×
573
      }
1✔
574
    } catch (Exception e) {
×
575
      throw new IllegalStateException(
×
576
          " Couldn't uninstall " + this.tool + ". Couldn't determine the software repository path for " + this.tool + ".", e);
577
    }
1✔
578
  }
1✔
579

580

581
  private ToolInstallation createToolInstallation(Path rootDir, VersionIdentifier resolvedVersion, Path toolVersionFile,
582
      boolean newInstallation, EnvironmentContext environmentContext, boolean extraInstallation) {
583

584
    Path linkDir = getMacOsHelper().findLinkDir(rootDir, getBinaryName());
7✔
585
    Path binDir = linkDir;
2✔
586
    Path binFolder = binDir.resolve(IdeContext.FOLDER_BIN);
4✔
587
    if (Files.isDirectory(binFolder)) {
5✔
588
      binDir = binFolder;
2✔
589
    }
590
    if (linkDir != rootDir) {
3✔
591
      assert (!linkDir.equals(rootDir));
5!
592
      this.context.getFileAccess().copy(toolVersionFile, linkDir, FileCopyMode.COPY_FILE_OVERRIDE);
7✔
593
    }
594
    ToolInstallation toolInstallation = new ToolInstallation(rootDir, linkDir, binDir, resolvedVersion, newInstallation);
9✔
595
    setEnvironment(environmentContext, toolInstallation, extraInstallation);
5✔
596
    return toolInstallation;
2✔
597
  }
598

599
  /**
600
   * Method to set environment variables for the process context.
601
   *
602
   * @param environmentContext the {@link EnvironmentContext} where to {@link EnvironmentContext#withEnvVar(String, String) set environment variables} for
603
   *     this tool.
604
   * @param toolInstallation the {@link ToolInstallation}.
605
   * @param extraInstallation {@code true} if the {@link ToolInstallation} is an additional installation to the
606
   *     {@link #getConfiguredVersion() configured version} due to a conflicting version of a {@link ToolDependency}, {@code false} otherwise.
607
   */
608
  public void setEnvironment(EnvironmentContext environmentContext, ToolInstallation toolInstallation, boolean extraInstallation) {
609

610
    String pathVariable = EnvironmentVariables.getToolVariablePrefix(this.tool) + "_HOME";
5✔
611
    environmentContext.withEnvVar(pathVariable, toolInstallation.linkDir().toString());
7✔
612
    if (extraInstallation) {
2✔
613
      environmentContext.withPathEntry(toolInstallation.binDir());
5✔
614
    }
615
  }
1✔
616

617
  /**
618
   * @return {@link VersionIdentifier} with latest version of the tool}.
619
   */
620
  public VersionIdentifier getLatestToolVersion() {
621

622
    return this.context.getDefaultToolRepository().resolveVersion(this.tool, getConfiguredEdition(), VersionIdentifier.LATEST, this);
×
623
  }
624

625

626
  /**
627
   * Searches for a wrapper file in valid projects (containing a build file f.e. build.gradle or pom.xml) and returns its path.
628
   *
629
   * @param wrapperFileName the name of the wrapper file
630
   * @param filter the {@link Predicate} to match
631
   * @return Path of the wrapper file or {@code null} if none was found.
632
   */
633
  protected Path findWrapper(String wrapperFileName, Predicate<Path> filter) {
634
    Path dir = context.getCwd();
4✔
635
    // traverse the cwd directory containing a build file up till a wrapper file was found
636
    while ((dir != null) && filter.test(dir)) {
6!
637
      if (Files.exists(dir.resolve(wrapperFileName))) {
7✔
638
        context.debug("Using wrapper file at: {}", dir);
10✔
639
        return dir.resolve(wrapperFileName);
4✔
640
      }
641
      dir = dir.getParent();
4✔
642
    }
643
    return null;
2✔
644
  }
645

646

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