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

devonfw / IDEasy / 13696924163

06 Mar 2025 10:47AM UTC coverage: 68.467% (+0.2%) from 68.253%
13696924163

Pull #1085

github

web-flow
Merge 19c6a8170 into cb3091c59
Pull Request #1085: #654: improved plugin suppport

3065 of 4919 branches covered (62.31%)

Branch coverage included in aggregate %.

7924 of 11131 relevant lines covered (71.19%)

3.11 hits per line

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

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

3
import java.io.IOException;
4
import java.nio.file.Files;
5
import java.nio.file.LinkOption;
6
import java.nio.file.Path;
7
import java.nio.file.StandardOpenOption;
8
import java.util.Collection;
9
import java.util.Locale;
10
import java.util.Set;
11

12
import com.devonfw.tools.ide.common.Tag;
13
import com.devonfw.tools.ide.context.IdeContext;
14
import com.devonfw.tools.ide.io.FileAccess;
15
import com.devonfw.tools.ide.io.FileCopyMode;
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

47
    return this.context.getSoftwarePath().resolve(getName());
7✔
48
  }
49

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

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

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

69
  }
1✔
70

71
  @Override
72
  public boolean install(boolean silent, EnvironmentContext environmentContext) {
73
    return install(silent, (ProcessContext) environmentContext);
6✔
74
  }
75

76
  /**
77
   * Installs or updates the managed {@link #getName() tool}.
78
   *
79
   * @param silent - {@code true} if called recursively to suppress verbose logging, {@code false} otherwise.
80
   * @param processContext the {@link ProcessContext} used to
81
   *     {@link LocalToolCommandlet#setEnvironment(EnvironmentContext, ToolInstallation, boolean) configure environment variables}.
82
   * @return {@code true} if the tool was newly installed, {@code false} if the tool was already installed before and nothing has changed.
83
   */
84
  public boolean install(boolean silent, ProcessContext processContext) {
85

86
    installDependencies();
2✔
87
    VersionIdentifier configuredVersion = getConfiguredVersion();
3✔
88
    // get installed version before installInRepo actually may install the software
89
    VersionIdentifier installedVersion = getInstalledVersion();
3✔
90
    Step step = this.context.newStep(silent, "Install " + this.tool, configuredVersion);
14✔
91
    try {
92
      // install configured version of our tool in the software repository if not already installed
93
      ToolInstallation installation = installTool(configuredVersion, processContext);
5✔
94

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

126
  }
127

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

137
    if (newlyInstalled) {
2✔
138
      postInstall();
2✔
139
    }
140
  }
1✔
141

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

147
    // nothing to do by default
148
  }
1✔
149

150
  private boolean toolAlreadyInstalled(boolean silent, VersionIdentifier installedVersion, Step step, ProcessContext pc) {
151
    if (!silent) {
2✔
152
      this.context.info("Version {} of tool {} is already installed", installedVersion, getToolWithEdition());
15✔
153
    }
154
    postInstall(false, pc);
4✔
155
    step.success();
2✔
156
    return false;
2✔
157
  }
158

159
  /**
160
   * Determines whether this tool should be installed directly in the software folder or in the software repository.
161
   *
162
   * @return {@code true} if the tool should be installed directly in the software folder, ignoring the central software repository; {@code false} if the tool
163
   *     should be installed in the central software repository (default behavior).
164
   */
165
  protected boolean isIgnoreSoftwareRepo() {
166

167
    return false;
2✔
168
  }
169

170
  /**
171
   * Performs the installation of the {@link #getName() tool} together with the environment context, managed by this
172
   * {@link com.devonfw.tools.ide.commandlet.Commandlet}.
173
   *
174
   * @param version the {@link GenericVersionRange} requested to be installed.
175
   * @param environmentContext the {@link EnvironmentContext} used to
176
   *     {@link #setEnvironment(EnvironmentContext, ToolInstallation, boolean) configure environment variables}.
177
   * @return the {@link ToolInstallation} matching the given {@code version}.
178
   */
179
  public ToolInstallation installTool(GenericVersionRange version, EnvironmentContext environmentContext) {
180

181
    return installTool(version, environmentContext, getConfiguredEdition());
7✔
182
  }
183

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

196
    // if version is a VersionRange, we are not called from install() but directly from installAsDependency() due to a version conflict of a dependency
197
    boolean extraInstallation = (version instanceof VersionRange);
3✔
198
    ToolRepository toolRepository = getToolRepository();
3✔
199
    VersionIdentifier resolvedVersion = toolRepository.resolveVersion(this.tool, edition, version, this);
8✔
200
    installToolDependencies(resolvedVersion, edition, environmentContext);
5✔
201

202
    Path installationPath;
203
    boolean ignoreSoftwareRepo = isIgnoreSoftwareRepo();
3✔
204
    if (ignoreSoftwareRepo) {
2✔
205
      installationPath = getToolPath();
4✔
206
    } else {
207
      Path softwareRepoPath = this.context.getSoftwareRepositoryPath().resolve(toolRepository.getId()).resolve(this.tool).resolve(edition);
12✔
208
      installationPath = softwareRepoPath.resolve(resolvedVersion.toString());
5✔
209
    }
210
    Path toolVersionFile = installationPath.resolve(IdeContext.FILE_SOFTWARE_VERSION);
4✔
211
    FileAccess fileAccess = this.context.getFileAccess();
4✔
212
    if (Files.isDirectory(installationPath)) {
5✔
213
      if (Files.exists(toolVersionFile)) {
5!
214
        if (!ignoreSoftwareRepo || resolvedVersion.equals(getInstalledVersion())) {
2!
215
          this.context.debug("Version {} of tool {} is already installed at {}", resolvedVersion, getToolWithEdition(this.tool, edition), installationPath);
21✔
216
          return createToolInstallation(installationPath, resolvedVersion, toolVersionFile, false, environmentContext, extraInstallation);
9✔
217
        }
218
      } else {
219
        this.context.warning("Deleting corrupted installation at {}", installationPath);
×
220
        fileAccess.delete(installationPath);
×
221
      }
222
    }
223
    Path downloadedToolFile = downloadTool(edition, toolRepository, resolvedVersion);
6✔
224
    boolean extract = isExtract();
3✔
225
    if (!extract) {
2✔
226
      this.context.trace("Extraction is disabled for '{}' hence just moving the downloaded file {}.", this.tool, downloadedToolFile);
15✔
227
    }
228
    if (Files.exists(installationPath)) {
5!
229
      fileAccess.backup(installationPath);
×
230
    }
231
    fileAccess.mkdirs(installationPath.getParent());
4✔
232
    fileAccess.extract(downloadedToolFile, installationPath, this::postExtract, extract);
7✔
233
    try {
234
      Files.writeString(toolVersionFile, resolvedVersion.toString(), StandardOpenOption.CREATE_NEW);
11✔
235
    } catch (IOException e) {
×
236
      throw new IllegalStateException("Failed to write version file " + toolVersionFile, e);
×
237
    }
1✔
238
    this.context.debug("Installed {} in version {} at {}", this.tool, resolvedVersion, installationPath);
19✔
239
    return createToolInstallation(installationPath, resolvedVersion, toolVersionFile, true, environmentContext, extraInstallation);
9✔
240
  }
241

242
  /**
243
   * @param edition the {@link #getConfiguredEdition() tool edition} to download.
244
   * @param toolRepository the {@link ToolRepository} to use.
245
   * @param resolvedVersion the resolved {@link VersionIdentifier version} to download.
246
   * @return the {@link Path} to the downloaded release file.
247
   */
248
  protected Path downloadTool(String edition, ToolRepository toolRepository, VersionIdentifier resolvedVersion) {
249
    return toolRepository.download(this.tool, edition, resolvedVersion, this);
8✔
250
  }
251

252
  /**
253
   * Install this tool as dependency of another tool.
254
   *
255
   * @param version the required {@link VersionRange}. See {@link ToolDependency#versionRange()}.
256
   * @param environmentContext the {@link EnvironmentContext}.
257
   * @return {@code true} if the tool was newly installed, {@code false} otherwise (installation was already present).
258
   */
259
  public boolean installAsDependency(VersionRange version, EnvironmentContext environmentContext) {
260

261
    VersionIdentifier configuredVersion = getConfiguredVersion();
3✔
262
    if (version.contains(configuredVersion)) {
4✔
263
      // prefer configured version if contained in version range
264
      return install(false, environmentContext);
5✔
265
    } else {
266
      if (isIgnoreSoftwareRepo()) {
3!
267
        throw new IllegalStateException(
×
268
            "Cannot satisfy dependency to " + this.tool + " in version " + version + " since it is conflicting with configured version " + configuredVersion
269
                + " and this tool does not support the software repository.");
270
      }
271
      this.context.info(
19✔
272
          "Configured version of tool {} is {} but does not match version to install {} - need to use different version from software repository.",
273
          this.tool, configuredVersion, version);
274
    }
275
    ToolInstallation toolInstallation = installTool(version, environmentContext);
5✔
276
    return toolInstallation.newInstallation();
3✔
277
  }
278

279
  private void installToolDependencies(VersionIdentifier version, String edition, EnvironmentContext environmentContext) {
280
    Collection<ToolDependency> dependencies = getToolRepository().findDependencies(this.tool, edition, version);
8✔
281
    String toolWithEdition = getToolWithEdition(this.tool, edition);
5✔
282
    int size = dependencies.size();
3✔
283
    this.context.debug("Tool {} has {} other tool(s) as dependency", toolWithEdition, size);
15✔
284
    for (ToolDependency dependency : dependencies) {
10✔
285
      this.context.trace("Ensuring dependency {} for tool {}", dependency.tool(), toolWithEdition);
15✔
286
      LocalToolCommandlet dependencyTool = this.context.getCommandletManager().getRequiredLocalToolCommandlet(dependency.tool());
7✔
287
      dependencyTool.installAsDependency(dependency.versionRange(), environmentContext);
6✔
288
    }
1✔
289
  }
1✔
290

291
  /**
292
   * Post-extraction hook that can be overridden to add custom processing after unpacking and before moving to the final destination folder.
293
   *
294
   * @param extractedDir the {@link Path} to the folder with the unpacked tool.
295
   */
296
  protected void postExtract(Path extractedDir) {
297

298
  }
1✔
299

300
  @Override
301
  public VersionIdentifier getInstalledVersion() {
302

303
    return getInstalledVersion(this.context.getSoftwarePath().resolve(getName()));
9✔
304
  }
305

306
  /**
307
   * @param toolPath the installation {@link Path} where to find the version file.
308
   * @return the currently installed {@link VersionIdentifier version} of this tool or {@code null} if not installed.
309
   */
310
  protected VersionIdentifier getInstalledVersion(Path toolPath) {
311

312
    if (!Files.isDirectory(toolPath)) {
5✔
313
      this.context.debug("Tool {} not installed in {}", getName(), toolPath);
15✔
314
      return null;
2✔
315
    }
316
    Path toolVersionFile = toolPath.resolve(IdeContext.FILE_SOFTWARE_VERSION);
4✔
317
    if (!Files.exists(toolVersionFile)) {
5✔
318
      Path legacyToolVersionFile = toolPath.resolve(IdeContext.FILE_LEGACY_SOFTWARE_VERSION);
4✔
319
      if (Files.exists(legacyToolVersionFile)) {
5✔
320
        toolVersionFile = legacyToolVersionFile;
3✔
321
      } else {
322
        this.context.warning("Tool {} is missing version file in {}", getName(), toolVersionFile);
15✔
323
        return null;
2✔
324
      }
325
    }
326
    String version = this.context.getFileAccess().readFileContent(toolVersionFile).trim();
7✔
327
    return VersionIdentifier.of(version);
3✔
328
  }
329

330
  @Override
331
  public String getInstalledEdition() {
332

333
    if (this.context.getSoftwarePath() == null) {
4✔
334
      return "";
2✔
335
    }
336
    return getInstalledEdition(this.context.getSoftwarePath().resolve(this.tool));
9✔
337
  }
338

339
  /**
340
   * @param toolPath the installation {@link Path} where to find currently installed tool. The name of the parent directory of the real path corresponding
341
   *     to the passed {@link Path path} must be the name of the edition.
342
   * @return the installed edition of this tool or {@code null} if not installed.
343
   */
344
  private String getInstalledEdition(Path toolPath) {
345

346
    if (!Files.isDirectory(toolPath)) {
5✔
347
      this.context.debug("Tool {} not installed in {}", this.tool, toolPath);
15✔
348
      return null;
2✔
349
    }
350
    Path realPath = this.context.getFileAccess().toRealPath(toolPath);
6✔
351
    // if the realPath changed, a link has been resolved
352
    if (realPath.equals(toolPath)) {
4✔
353
      if (!isIgnoreSoftwareRepo()) {
3!
354
        this.context.warning("Tool {} is not installed via software repository (maybe from devonfw-ide). Please consider reinstalling it.", this.tool);
11✔
355
      }
356
      // 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
357
      return getConfiguredEdition();
3✔
358
    }
359
    Path toolRepoFolder = context.getSoftwareRepositoryPath().resolve(ToolRepository.ID_DEFAULT).resolve(this.tool);
9✔
360
    String edition = getEdition(toolRepoFolder, realPath);
5✔
361
    if (!getToolRepository().getSortedEditions(this.tool).contains(edition)) {
8✔
362
      this.context.warning("Undefined edition {} of tool {}", edition, this.tool);
15✔
363
    }
364
    return edition;
2✔
365
  }
366

367
  private String getEdition(Path toolRepoFolder, Path toolInstallFolder) {
368

369
    int toolRepoNameCount = toolRepoFolder.getNameCount();
3✔
370
    int toolInstallNameCount = toolInstallFolder.getNameCount();
3✔
371
    if (toolRepoNameCount < toolInstallNameCount) {
3!
372
      // ensure toolInstallFolder starts with $IDE_ROOT/_ide/software/default/«tool»
373
      for (int i = 0; i < toolRepoNameCount; i++) {
7✔
374
        if (!toolRepoFolder.getName(i).toString().equals(toolInstallFolder.getName(i).toString())) {
10!
375
          return null;
×
376
        }
377
      }
378
      return toolInstallFolder.getName(toolRepoNameCount).toString();
5✔
379
    }
380
    return null;
×
381
  }
382

383
  @Override
384
  public void uninstall() {
385

386
    try {
387
      Path softwarePath = getToolPath();
3✔
388
      if (Files.exists(softwarePath)) {
5!
389
        try {
390
          this.context.getFileAccess().delete(softwarePath);
5✔
391
          this.context.success("Successfully uninstalled " + this.tool);
6✔
392
        } catch (Exception e) {
×
393
          this.context.error("Couldn't uninstall " + this.tool);
×
394
        }
1✔
395
      } else {
396
        this.context.warning("An installed version of " + this.tool + " does not exist");
×
397
      }
398
    } catch (Exception e) {
×
399
      this.context.error(e.getMessage());
×
400
    }
1✔
401
  }
1✔
402

403
  private ToolInstallation createToolInstallation(Path rootDir, VersionIdentifier resolvedVersion, Path toolVersionFile,
404
      boolean newInstallation, EnvironmentContext environmentContext, boolean extraInstallation) {
405

406
    Path linkDir = getMacOsHelper().findLinkDir(rootDir, getBinaryName());
7✔
407
    Path binDir = linkDir;
2✔
408
    Path binFolder = binDir.resolve(IdeContext.FOLDER_BIN);
4✔
409
    if (Files.isDirectory(binFolder)) {
5✔
410
      binDir = binFolder;
2✔
411
    }
412
    if (linkDir != rootDir) {
3✔
413
      assert (!linkDir.equals(rootDir));
5!
414
      this.context.getFileAccess().copy(toolVersionFile, linkDir, FileCopyMode.COPY_FILE_OVERRIDE);
7✔
415
    }
416
    ToolInstallation toolInstallation = new ToolInstallation(rootDir, linkDir, binDir, resolvedVersion, newInstallation);
9✔
417
    setEnvironment(environmentContext, toolInstallation, extraInstallation);
5✔
418
    return toolInstallation;
2✔
419
  }
420

421
  /**
422
   * Method to set environment variables for the process context.
423
   *
424
   * @param environmentContext the {@link EnvironmentContext} where to {@link EnvironmentContext#withEnvVar(String, String) set environment variables} for
425
   *     this tool.
426
   * @param toolInstallation the {@link ToolInstallation}.
427
   * @param extraInstallation {@code true} if the {@link ToolInstallation} is an additional installation to the
428
   *     {@link #getConfiguredVersion() configured version} due to a conflicting version of a {@link ToolDependency}, {@code false} otherwise.
429
   */
430
  public void setEnvironment(EnvironmentContext environmentContext, ToolInstallation toolInstallation, boolean extraInstallation) {
431

432
    String pathVariable = this.tool.toUpperCase(Locale.ROOT) + "_HOME";
6✔
433
    environmentContext.withEnvVar(pathVariable, toolInstallation.linkDir().toString());
7✔
434
    if (extraInstallation) {
2✔
435
      environmentContext.withPathEntry(toolInstallation.binDir());
5✔
436
    }
437
  }
1✔
438

439

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