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

devonfw / IDEasy / 30885547918

04 Aug 2026 06:52AM UTC coverage: 72.715% (+0.1%) from 72.61%
30885547918

Pull #2258

github

web-flow
Merge bc4337a4b into cd9770b9c
Pull Request #2258: #2250 uninstall commands from package manager

5033 of 7671 branches covered (65.61%)

Branch coverage included in aggregate %.

13126 of 17302 relevant lines covered (75.86%)

3.22 hits per line

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

2.19
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
   * @param action the {@link NativePackageAction} tht is performed - only used for logging.
49
   * @return {@code true} if installation or uninstallation succeeds with any of the package manager commands, {@code false} otherwise.
50
   */
51
  protected boolean runWithPackageManager(boolean silent, NativePackageAction action, String... commandStrings) {
52

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

57
  /**
58
   * Performs the installation or uninstallation of the {@link #getName() tool} via a package manager.
59
   *
60
   * @param silent {@code true} if called recursively to suppress verbose logging, {@code false} otherwise.
61
   * @param pmCommands A list of {@link PackageManagerCommand} to be used for installation or uninstallation. * @param action the
62
   *     {@link NativePackageAction} tht is performed - only used for logging.
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, NativePackageAction action) {
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);
×
72
        continue; // Skip to the next package manager command
×
73
      }
74

75
      if (executePackageManagerCommand(pmCommand, silent, action)) {
×
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
   * @param action the {@link NativePackageAction} tht is performed - only used for logging.
98
   * @return {@code true} if the package manager commands execute successfully, {@code false} otherwise.
99
   */
100
  private boolean executePackageManagerCommand(PackageManagerCommand pmCommand, boolean silent, NativePackageAction action) {
101

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

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

120
  @Override
121
  protected boolean isExtract() {
122

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

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

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

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

185
  /**
186
   * @return the {@link List} of {@link NativePackage}s this tool consists of on Linux - one per supported {@link NativePackageManager}. Override this method
187
   *     instead of {@link #getInstallPackageManagerCommands} so that the commands for installation and uninstallation can both be derived from this
188
   *     declaration. If empty, no package manager installation will be triggered on Linux.
189
   */
190
  protected List<NativePackage> getNativePackages() {
191
    return List.of();
×
192
  }
193

194
  /**
195
   * @return the {@link List} of {@link PackageManagerCommand}s to use on Linux to install this tool. If empty, no package manager installation will be
196
   *     triggered on Linux.
197
   */
198
  protected List<PackageManagerCommand> getInstallPackageManagerCommands(VersionIdentifier resolvedVersion) {
199
    String version = (resolvedVersion == null) ? null : resolvedVersion.toString();
×
200
    return getNativePackages().stream().map(nativePackage -> nativePackage.install(version)).toList();
×
201
  }
202

203
  /**
204
   * @return the {@link List} of {@link PackageManagerCommand}s to use on Linux to uninstall this tool. If empty, no package manager uninstallation will be
205
   *     triggered on Linux.
206
   */
207
  protected List<PackageManagerCommand> getUninstallPackageManagerCommands() {
208
    return getNativePackages().stream().map(NativePackage::uninstall).toList();
×
209
  }
210

211

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

218
  @Override
219
  public String getInstalledEdition() {
220
    //TODO: handle "get-edition <globaltool>"
221
    return null;
×
222
  }
223

224
  @Override
225
  protected Path getInstallationPath(String edition, VersionIdentifier resolvedVersion) {
226

227
    Path toolBinary = Path.of(getBinaryName());
×
228
    Path binaryPath = this.context.getPath().findBinary(toolBinary);
×
229
    if ((binaryPath == toolBinary) || !Files.exists(binaryPath)) {
×
230
      return null;
×
231
    }
232
    Path binPath = binaryPath.getParent();
×
233
    if (binPath == null) {
×
234
      return null;
×
235
    }
236
    return this.context.getFileAccess().getBinParentPath(binPath);
×
237
  }
238

239
  @Override
240
  public void uninstall() {
241
    if (this.context.getSystemInfo().isLinux()) {
×
242
      runWithPackageManager(false, getUninstallPackageManagerCommands(), NativePackageAction.UNINSTALL);
×
243
    } else {
244
      LOG.error("Couldn't uninstall {} on this OS. Please uninstall manually.", this.getName());
×
245
    }
246
  }
×
247
}
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