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

devonfw / IDEasy / 19751480105

28 Nov 2025 01:26AM UTC coverage: 69.441% (+0.3%) from 69.136%
19751480105

push

github

web-flow
#1613: fixed duplicated CVE check and refactored installation routine (#1614)

3696 of 5851 branches covered (63.17%)

Branch coverage included in aggregate %.

9620 of 13325 relevant lines covered (72.2%)

3.14 hits per line

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

1.64
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 com.devonfw.tools.ide.cli.CliException;
10
import com.devonfw.tools.ide.common.Tag;
11
import com.devonfw.tools.ide.context.IdeContext;
12
import com.devonfw.tools.ide.io.FileAccess;
13
import com.devonfw.tools.ide.process.ProcessContext;
14
import com.devonfw.tools.ide.process.ProcessErrorHandling;
15
import com.devonfw.tools.ide.process.ProcessMode;
16
import com.devonfw.tools.ide.tool.repository.ToolRepository;
17
import com.devonfw.tools.ide.version.VersionIdentifier;
18

19
/**
20
 * {@link ToolCommandlet} that is installed globally.
21
 */
22
public abstract class GlobalToolCommandlet extends ToolCommandlet {
23

24
  /**
25
   * The constructor.
26
   *
27
   * @param context the {@link IdeContext}.
28
   * @param tool the {@link #getName() tool name}.
29
   * @param tags the {@link #getTags() tags} classifying the tool. Should be created via {@link Set#of(Object) Set.of} method.
30
   */
31
  public GlobalToolCommandlet(IdeContext context, String tool, Set<Tag> tags) {
32

33
    super(context, tool, tags);
5✔
34
  }
1✔
35

36
  /**
37
   * Performs the installation or uninstallation of the {@link #getName() tool} via a package manager.
38
   *
39
   * @param silent {@code true} if called recursively to suppress verbose logging, {@code false} otherwise.
40
   * @param commandStrings commandStrings The package manager command strings to execute.
41
   * @return {@code true} if installation or uninstallation succeeds with any of the package manager commands, {@code false} otherwise.
42
   */
43
  protected boolean runWithPackageManager(boolean silent, String... commandStrings) {
44

45
    List<PackageManagerCommand> pmCommands = Arrays.stream(commandStrings).map(PackageManagerCommand::of).toList();
×
46
    return runWithPackageManager(silent, pmCommands);
×
47
  }
48

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

58
    for (PackageManagerCommand pmCommand : pmCommands) {
×
59
      PackageManager packageManager = pmCommand.packageManager();
×
60
      Path packageManagerPath = this.context.getPath().findBinary(Path.of(packageManager.getBinaryName()));
×
61
      if (packageManagerPath == null || !Files.exists(packageManagerPath)) {
×
62
        this.context.debug("{} is not installed", packageManager.toString());
×
63
        continue; // Skip to the next package manager command
×
64
      }
65

66
      if (executePackageManagerCommand(pmCommand, silent)) {
×
67
        return true; // Success
×
68
      }
69
    }
×
70
    return false; // None of the package manager commands were successful
×
71
  }
72

73
  private void logPackageManagerCommands(PackageManagerCommand pmCommand) {
74

75
    this.context.interaction("We need to run the following privileged command(s):");
×
76
    for (String command : pmCommand.commands()) {
×
77
      this.context.interaction(command);
×
78
    }
×
79
    this.context.interaction("This will require root permissions!");
×
80
  }
×
81

82
  /**
83
   * Executes the provided package manager command.
84
   *
85
   * @param pmCommand The {@link PackageManagerCommand} containing the commands to execute.
86
   * @param silent {@code true} if called recursively to suppress verbose logging, {@code false} otherwise.
87
   * @return {@code true} if the package manager commands execute successfully, {@code false} otherwise.
88
   */
89
  private boolean executePackageManagerCommand(PackageManagerCommand pmCommand, boolean silent) {
90

91
    String bashPath = this.context.findBashRequired().toString();
×
92
    logPackageManagerCommands(pmCommand);
×
93
    for (String command : pmCommand.commands()) {
×
94
      ProcessContext pc = this.context.newProcess().errorHandling(ProcessErrorHandling.LOG_WARNING).executable(bashPath)
×
95
          .addArgs("-c", command);
×
96
      int exitCode = pc.run();
×
97
      if (exitCode != 0) {
×
98
        this.context.warning("{} command did not execute successfully", command);
×
99
        return false;
×
100
      }
101
    }
×
102

103
    if (!silent) {
×
104
      this.context.success("Successfully installed {}", this.tool);
×
105
    }
106
    return true;
×
107
  }
108

109
  @Override
110
  protected boolean isExtract() {
111

112
    // for global tools we usually download installers and do not want to extract them (e.g. installer.msi file shall
113
    // not be extracted)
114
    return false;
×
115
  }
116

117
  @Override
118
  protected ToolInstallation doInstall(ToolInstallRequest request) {
119

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

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

170
  /**
171
   * @return the {@link List} of {@link PackageManagerCommand}s to use on Linux to install this tool. If empty, no package manager installation will be
172
   *     triggered on Linux.
173
   */
174
  protected List<PackageManagerCommand> getInstallPackageManagerCommands() {
175
    return List.of();
×
176
  }
177

178
  @Override
179
  public VersionIdentifier getInstalledVersion() {
180
    //TODO: handle "get-version <globaltool>"
181
    this.context.error("Couldn't get installed version of " + this.getName());
×
182
    return null;
×
183
  }
184

185
  @Override
186
  public String getInstalledEdition() {
187
    //TODO: handle "get-edition <globaltool>"
188
    this.context.error("Couldn't get installed edition of " + this.getName());
×
189
    return null;
×
190
  }
191

192
  @Override
193
  protected Path getInstallationPath(String edition, VersionIdentifier resolvedVersion) {
194

195
    Path toolBinary = Path.of(getBinaryName());
×
196
    Path binaryPath = this.context.getPath().findBinary(toolBinary);
×
197
    if ((binaryPath == toolBinary) || !Files.exists(binaryPath)) {
×
198
      return null;
×
199
    }
200
    Path binPath = binaryPath.getParent();
×
201
    if (binPath == null) {
×
202
      return null;
×
203
    }
204
    return this.context.getFileAccess().getBinParentPath(binPath);
×
205
  }
206

207
  @Override
208
  public void uninstall() {
209
    //TODO: handle "uninstall <globaltool>"
210
    this.context.error("Couldn't uninstall " + this.getName());
×
211
  }
×
212
}
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