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

devonfw / IDEasy / 14856597402

06 May 2025 09:45AM UTC coverage: 67.59% (-0.08%) from 67.672%
14856597402

Pull #1242

github

web-flow
Merge d3d39f777 into a0320eff8
Pull Request #1242: #809: Enhance uninstall with --force to remove from the software repo

3108 of 5006 branches covered (62.09%)

Branch coverage included in aggregate %.

7997 of 11424 relevant lines covered (70.0%)

3.06 hits per line

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

1.96
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.common.Tag;
10
import com.devonfw.tools.ide.context.IdeContext;
11
import com.devonfw.tools.ide.io.FileAccess;
12
import com.devonfw.tools.ide.log.IdeLogLevel;
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();
×
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
  public boolean install(boolean silent, ProcessContext processContext) {
119

120
    Path binaryPath = this.context.getPath().findBinary(Path.of(getBinaryName()));
×
121
    // if force mode is enabled, go through with the installation even if the tool is already installed
122
    if (binaryPath != null && Files.exists(binaryPath) && !this.context.isForceMode()) {
×
123
      IdeLogLevel level = silent ? IdeLogLevel.DEBUG : IdeLogLevel.INFO;
×
124
      this.context.level(level).log("{} is already installed at {}", this.tool, binaryPath);
×
125
      return false;
×
126
    }
127
    String edition = getConfiguredEdition();
×
128
    ToolRepository toolRepository = this.context.getDefaultToolRepository();
×
129
    VersionIdentifier configuredVersion = getConfiguredVersion();
×
130
    VersionIdentifier resolvedVersion = toolRepository.resolveVersion(this.tool, edition, configuredVersion, this);
×
131
    // download and install the global tool
132
    FileAccess fileAccess = this.context.getFileAccess();
×
133
    Path target = toolRepository.download(this.tool, edition, resolvedVersion, this);
×
134
    Path executable = target;
×
135
    Path tmpDir = null;
×
136
    boolean extract = isExtract();
×
137
    if (extract) {
×
138
      tmpDir = fileAccess.createTempDir(getName());
×
139
      Path downloadBinaryPath = tmpDir.resolve(target.getFileName());
×
140
      fileAccess.extract(target, downloadBinaryPath);
×
141
      executable = fileAccess.findFirst(downloadBinaryPath, Files::isExecutable, false);
×
142
    }
143
    ProcessContext pc = this.context.newProcess().errorHandling(ProcessErrorHandling.LOG_WARNING).executable(executable);
×
144
    int exitCode = pc.run(ProcessMode.BACKGROUND).getExitCode();
×
145
    if (tmpDir != null) {
×
146
      fileAccess.delete(tmpDir);
×
147
    }
148
    if (exitCode == 0) {
×
149
      this.context.success("Installation process for {} in version {} has started", this.tool, resolvedVersion);
×
150
    } else {
151
      this.context.warning("{} in version {} was not successfully installed", this.tool, resolvedVersion);
×
152
      return false;
×
153
    }
154
    return true;
×
155
  }
156

157
  @Override
158
  public VersionIdentifier getInstalledVersion() {
159
    //TODO: handle "get-version <globaltool>"
160
    this.context.error("Couldn't get installed version of " + this.getName());
×
161
    return null;
×
162
  }
163

164
  @Override
165
  public String getInstalledEdition() {
166
    //TODO: handle "get-edition <globaltool>"
167
    this.context.error("Couldn't get installed edition of " + this.getName());
×
168
    return null;
×
169
  }
170

171
  @Override
172
  public Path getInstalledSoftwareRepoPath() {
173
    //TODO: handle "--force uninstall <globaltool>"
174
    this.context.error("Couldn't get installed repository path of " + this.getName());
×
175
    return null;
×
176
  }
177

178
  @Override
179
  public void uninstall() {
180
    //TODO: handle "uninstall <globaltool>"
181
    this.context.error("Couldn't uninstall " + this.getName());
×
182
  }
×
183

184
  @Override
185
  public void forceUninstall() {
186
    //TODO: handle "uninstall <globaltool>"
187
    this.context.error("Couldn't uninstall " + this.getName() + " from your computer.");
×
188
  }
×
189
}
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

© 2025 Coveralls, Inc