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

devonfw / IDEasy / 22303886886

23 Feb 2026 11:19AM UTC coverage: 70.647% (+0.2%) from 70.474%
22303886886

Pull #1714

github

web-flow
Merge f1f7e1e61 into 379acdc9d
Pull Request #1714: #404: #1713: advanced logging

4069 of 6360 branches covered (63.98%)

Branch coverage included in aggregate %.

10644 of 14466 relevant lines covered (73.58%)

3.1 hits per line

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

2.31
cli/src/main/java/com/devonfw/tools/ide/tool/GlobalToolCommandlet.java
1
package com.devonfw.tools.ide.tool;
2

3
import java.nio.file.Files;
4
import java.nio.file.Path;
5
import java.util.Arrays;
6
import java.util.List;
7
import java.util.Set;
8

9
import org.slf4j.Logger;
10
import org.slf4j.LoggerFactory;
11

12
import com.devonfw.tools.ide.cli.CliException;
13
import com.devonfw.tools.ide.common.Tag;
14
import com.devonfw.tools.ide.context.IdeContext;
15
import com.devonfw.tools.ide.io.FileAccess;
16
import com.devonfw.tools.ide.log.IdeLogLevel;
17
import com.devonfw.tools.ide.process.ProcessContext;
18
import com.devonfw.tools.ide.process.ProcessErrorHandling;
19
import com.devonfw.tools.ide.process.ProcessMode;
20
import com.devonfw.tools.ide.step.Step;
21
import com.devonfw.tools.ide.tool.repository.ToolRepository;
22
import com.devonfw.tools.ide.version.VersionIdentifier;
23

24
/**
25
 * {@link ToolCommandlet} that is installed globally.
26
 */
27
public abstract class GlobalToolCommandlet extends ToolCommandlet {
28

29
  private static final Logger LOG = LoggerFactory.getLogger(GlobalToolCommandlet.class);
4✔
30

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

40
    super(context, tool, tags);
5✔
41
  }
1✔
42

43
  /**
44
   * Performs the installation or uninstallation of the {@link #getName() tool} via a package manager.
45
   *
46
   * @param silent {@code true} if called recursively to suppress verbose logging, {@code false} otherwise.
47
   * @param commandStrings commandStrings The package manager command strings to execute.
48
   * @return {@code true} if installation or uninstallation succeeds with any of the package manager commands, {@code false} otherwise.
49
   */
50
  protected boolean runWithPackageManager(boolean silent, String... commandStrings) {
51

52
    List<PackageManagerCommand> pmCommands = Arrays.stream(commandStrings).map(PackageManagerCommand::of).toList();
×
53
    return runWithPackageManager(silent, pmCommands);
×
54
  }
55

56
  /**
57
   * Performs the installation or uninstallation of the {@link #getName() tool} via a package manager.
58
   *
59
   * @param silent {@code true} if called recursively to suppress verbose logging, {@code false} otherwise.
60
   * @param pmCommands A list of {@link PackageManagerCommand} to be used for installation or uninstallation.
61
   * @return {@code true} if installation or uninstallation succeeds with any of the package manager commands, {@code false} otherwise.
62
   */
63
  protected boolean runWithPackageManager(boolean silent, List<PackageManagerCommand> pmCommands) {
64

65
    for (PackageManagerCommand pmCommand : pmCommands) {
×
66
      NativePackageManager packageManager = pmCommand.packageManager();
×
67
      Path packageManagerPath = this.context.getPath().findBinary(Path.of(packageManager.getBinaryName()));
×
68
      if (packageManagerPath == null || !Files.exists(packageManagerPath)) {
×
69
        LOG.debug("{} is not installed", packageManager.toString());
×
70
        continue; // Skip to the next package manager command
×
71
      }
72

73
      if (executePackageManagerCommand(pmCommand, silent)) {
×
74
        return true; // Success
×
75
      }
76
    }
×
77
    return false; // None of the package manager commands were successful
×
78
  }
79

80
  private void logPackageManagerCommands(PackageManagerCommand pmCommand) {
81

82
    IdeLogLevel level = IdeLogLevel.INTERACTION;
×
83
    level.log(LOG, "We need to run the following privileged command(s):");
×
84
    for (String command : pmCommand.commands()) {
×
85
      level.log(LOG, command);
×
86
    }
×
87
    level.log(LOG, "This will require root permissions!");
×
88
  }
×
89

90
  /**
91
   * Executes the provided package manager command.
92
   *
93
   * @param pmCommand The {@link PackageManagerCommand} containing the commands to execute.
94
   * @param silent {@code true} if called recursively to suppress verbose logging, {@code false} otherwise.
95
   * @return {@code true} if the package manager commands execute successfully, {@code false} otherwise.
96
   */
97
  private boolean executePackageManagerCommand(PackageManagerCommand pmCommand, boolean silent) {
98

99
    String bashPath = this.context.findBashRequired().toString();
×
100
    logPackageManagerCommands(pmCommand);
×
101
    for (String command : pmCommand.commands()) {
×
102
      ProcessContext pc = this.context.newProcess().errorHandling(ProcessErrorHandling.LOG_WARNING).executable(bashPath)
×
103
          .addArgs("-c", command);
×
104
      int exitCode = pc.run();
×
105
      if (exitCode != 0) {
×
106
        LOG.warn("{} command did not execute successfully", command);
×
107
        return false;
×
108
      }
109
    }
×
110

111
    if (!silent) {
×
112
      IdeLogLevel.SUCCESS.log(LOG, "Successfully installed {}", this.tool);
×
113
    }
114
    return true;
×
115
  }
116

117
  @Override
118
  protected boolean isExtract() {
119

120
    // for global tools we usually download installers and do not want to extract them (e.g. installer.msi file shall
121
    // not be extracted)
122
    return false;
×
123
  }
124

125
  @Override
126
  protected ToolInstallation doInstall(ToolInstallRequest request) {
127

128
    VersionIdentifier resolvedVersion = request.getRequested().getResolvedVersion();
×
129
    if (this.context.getSystemInfo().isLinux()) {
×
130
      // on Linux global tools are typically installed via the package manager of the OS
131
      // if a global tool implements this method to return at least one PackageManagerCommand, then we install this way.
132
      List<PackageManagerCommand> commands = getInstallPackageManagerCommands();
×
133
      if (!commands.isEmpty()) {
×
134
        boolean newInstallation = runWithPackageManager(request.isSilent(), commands);
×
135
        Path rootDir = getInstallationPath(getConfiguredEdition(), resolvedVersion);
×
136
        return createToolInstallation(rootDir, resolvedVersion, newInstallation, request.getProcessContext(), request.isAdditionalInstallation());
×
137
      }
138
    }
139

140
    ToolEdition toolEdition = getToolWithConfiguredEdition();
×
141
    Path installationPath = getInstallationPath(toolEdition.edition(), resolvedVersion);
×
142
    // if force mode is enabled, go through with the installation even if the tool is already installed
143
    if ((installationPath != null) && !this.context.isForceMode()) {
×
144
      return toolAlreadyInstalled(request);
×
145
    }
146
    String edition = toolEdition.edition();
×
147
    ToolRepository toolRepository = this.context.getDefaultToolRepository();
×
148
    resolvedVersion = cveCheck(request);
×
149
    // download and install the global tool
150
    FileAccess fileAccess = this.context.getFileAccess();
×
151
    Path target = toolRepository.download(this.tool, edition, resolvedVersion, this);
×
152
    Path executable = target;
×
153
    Path tmpDir = null;
×
154
    boolean extract = isExtract();
×
155
    if (extract) {
×
156
      tmpDir = fileAccess.createTempDir(getName());
×
157
      Path downloadBinaryPath = tmpDir.resolve(target.getFileName());
×
158
      fileAccess.extract(target, downloadBinaryPath);
×
159
      executable = fileAccess.findFirst(downloadBinaryPath, Files::isExecutable, false);
×
160
    }
161
    ProcessContext pc = this.context.newProcess().errorHandling(ProcessErrorHandling.LOG_WARNING).executable(executable);
×
162
    int exitCode = pc.run(ProcessMode.BACKGROUND).getExitCode();
×
163
    if (tmpDir != null) {
×
164
      fileAccess.delete(tmpDir);
×
165
    }
166
    if (exitCode == 0) {
×
167
      IdeLogLevel.SUCCESS.log(LOG, "Installation process for {} in version {} has started", this.tool, resolvedVersion);
×
168
      Step step = request.getStep();
×
169
      if (step != null) {
×
170
        step.success(true);
×
171
      }
172
    } else {
×
173
      throw new CliException("Installation process for " + this.tool + " in version " + resolvedVersion + " failed with exit code " + exitCode + "!");
×
174
    }
175
    installationPath = getInstallationPath(toolEdition.edition(), resolvedVersion);
×
176
    if (installationPath == null) {
×
177
      LOG.warn("Could not find binary {} on PATH after installation.", getBinaryName());
×
178
    }
179
    return createToolInstallation(installationPath, resolvedVersion, true, pc, false);
×
180
  }
181

182
  /**
183
   * @return the {@link List} of {@link PackageManagerCommand}s to use on Linux to install this tool. If empty, no package manager installation will be
184
   *     triggered on Linux.
185
   */
186
  protected List<PackageManagerCommand> getInstallPackageManagerCommands() {
187
    return List.of();
×
188
  }
189

190
  @Override
191
  public VersionIdentifier getInstalledVersion() {
192
    //TODO: handle "get-version <globaltool>"
193
    LOG.error("Couldn't get installed version of " + this.getName());
×
194
    return null;
×
195
  }
196

197
  @Override
198
  public String getInstalledEdition() {
199
    //TODO: handle "get-edition <globaltool>"
200
    LOG.error("Couldn't get installed edition of " + this.getName());
×
201
    return null;
×
202
  }
203

204
  @Override
205
  protected Path getInstallationPath(String edition, VersionIdentifier resolvedVersion) {
206

207
    Path toolBinary = Path.of(getBinaryName());
×
208
    Path binaryPath = this.context.getPath().findBinary(toolBinary);
×
209
    if ((binaryPath == toolBinary) || !Files.exists(binaryPath)) {
×
210
      return null;
×
211
    }
212
    Path binPath = binaryPath.getParent();
×
213
    if (binPath == null) {
×
214
      return null;
×
215
    }
216
    return this.context.getFileAccess().getBinParentPath(binPath);
×
217
  }
218

219
  @Override
220
  public void uninstall() {
221
    //TODO: handle "uninstall <globaltool>"
222
    LOG.error("Couldn't uninstall " + this.getName());
×
223
  }
×
224
}
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