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

devonfw / IDEasy / 30883639185

04 Aug 2026 06:20AM UTC coverage: 72.6% (+0.01%) from 72.587%
30883639185

Pull #2244

github

web-flow
Merge e6d08b34b into cd9770b9c
Pull Request #2244: #1870: Add generic get-version implementation for global tools under windows

5014 of 7653 branches covered (65.52%)

Branch coverage included in aggregate %.

13072 of 17259 relevant lines covered (75.74%)

3.22 hits per line

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

8.76
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.os.WindowsAppInstallation;
18
import com.devonfw.tools.ide.os.WindowsHelper;
19
import com.devonfw.tools.ide.process.ProcessContext;
20
import com.devonfw.tools.ide.process.ProcessErrorHandling;
21
import com.devonfw.tools.ide.process.ProcessMode;
22
import com.devonfw.tools.ide.step.Step;
23
import com.devonfw.tools.ide.tool.repository.ToolRepository;
24
import com.devonfw.tools.ide.version.VersionIdentifier;
25

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

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

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

42
    super(context, tool, tags);
5✔
43
  }
1✔
44

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

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

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

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

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

82
  private void logPackageManagerCommands(PackageManagerCommand pmCommand) {
83

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

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

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

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

119
  @Override
120
  protected boolean isExtract() {
121

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

127
  @Override
128
  protected ToolInstallation doInstall(ToolInstallRequest request) {
129

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

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

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

192
  /**
193
   * @return the app name to look for in the Windows registry
194
   */
195
  public String getWindowsRegistryAppName() {
196

197
    return this.tool;
3✔
198
  }
199

200
  @Override
201
  public VersionIdentifier getInstalledVersion() {
202

203
    if (this.context.getSystemInfo().isWindows()) {
5!
204
      WindowsAppInstallation installation = WindowsHelper.get(this.context).getAppInstallationFromRegistry(getWindowsRegistryAppName());
7✔
205
      if (installation != null) {
2✔
206
        return VersionIdentifier.of(installation.version());
4✔
207
      }
208
    }
209
    return null;
2✔
210
  }
211

212
  @Override
213
  public String getInstalledEdition() {
214
    //TODO: handle "get-edition <globaltool>"
215
    return null;
×
216
  }
217

218
  @Override
219
  protected Path getInstallationPath(String edition, VersionIdentifier resolvedVersion) {
220

221
    Path toolBinary = Path.of(getBinaryName());
×
222
    Path binaryPath = this.context.getPath().findBinary(toolBinary);
×
223
    if ((binaryPath == toolBinary) || !Files.exists(binaryPath)) {
×
224
      return null;
×
225
    }
226
    Path binPath = binaryPath.getParent();
×
227
    if (binPath == null) {
×
228
      return null;
×
229
    }
230
    return this.context.getFileAccess().getBinParentPath(binPath);
×
231
  }
232

233
  @Override
234
  public void uninstall() {
235
    //TODO: handle "uninstall <globaltool>"
236
    LOG.error("Couldn't uninstall " + this.getName());
×
237
  }
×
238
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc