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

devonfw / IDEasy / 39419187825

20 Oct 2026 12:01PM UTC coverage: 74.752% (+0.009%) from 74.743%
39419187825

Pull #2246

github

web-flow
Merge aa96636c7 into 5deca5e88
Pull Request #2246: #1965: improve dependent installation

5868 of 8727 branches covered (67.24%)

Branch coverage included in aggregate %.

15346 of 19652 relevant lines covered (78.09%)

3.35 hits per line

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

91.21
cli/src/main/java/com/devonfw/tools/ide/tool/PackageManagerBasedLocalToolCommandlet.java
1
package com.devonfw.tools.ide.tool;
2

3
import java.nio.file.Path;
4
import java.util.List;
5
import java.util.Set;
6

7
import org.slf4j.Logger;
8
import org.slf4j.LoggerFactory;
9

10
import com.devonfw.tools.ide.common.Tag;
11
import com.devonfw.tools.ide.context.IdeContext;
12
import com.devonfw.tools.ide.process.ProcessContext;
13
import com.devonfw.tools.ide.process.ProcessErrorHandling;
14
import com.devonfw.tools.ide.process.ProcessMode;
15
import com.devonfw.tools.ide.process.ProcessResult;
16
import com.devonfw.tools.ide.version.VersionIdentifier;
17

18
/**
19
 * {@link AbstractLocalToolCommandlet} for tools that have their own {@link #getToolRepository() repository} and do not follow standard installation mechanism.
20
 *
21
 * @param <P> type of the {@link AbstractToolCommandlet} acting as {@link #getPackageManagerClass() package manager}.
22
 */
23
public abstract class PackageManagerBasedLocalToolCommandlet<P extends AbstractToolCommandlet> extends AbstractLocalToolCommandlet {
24

25
  private static final Logger LOG = LoggerFactory.getLogger(PackageManagerBasedLocalToolCommandlet.class);
4 ✔
26

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

36
    super(context, tool, tags);
5 ✔
37
  }
1 ✔
38

39
  @Override
40
  protected boolean isIgnoreSoftwareRepo() {
41

42
    // python/pip and node/npm/yarn are messy - see https://github.com/devonfw/IDEasy/issues/352
43
    return true;
2 ✔
44
  }
45

46
  @Override
47
  public boolean isInstalled() {
48

49
    // Check if parent tool is installed first - if not, this tool cannot be installed
50
    AbstractLocalToolCommandlet parentTool = getParentTool();
3 ✔
51
    if (!parentTool.isInstalled()) {
3 !
52
      return false;
2 ✔
53
    }
54

55
    // Check if the tool binary is found
56
    return getBinaryExecutable() != null;
×
57
  }
58

59
  protected abstract Class<P> getPackageManagerClass();
60

61
  /**
62
   * @return the package name of this tool in the underlying repository (e.g. MVN repo, NPM registry, PyPI). Typically, this is the same as the
63
   *     {@link #getName() tool name} but may be overridden in some special cases.
64
   */
65
  public String getPackageName() {
66

67
    return this.tool;
3 ✔
68
  }
69

70
  /**
71
   * @param request the {@link PackageManagerRequest}.
72
   * @return the {@link ProcessResult}.
73
   */
74
  public ProcessResult runPackageManager(PackageManagerRequest request) {
75
    return runPackageManager(request, false);
5 ✔
76
  }
77

78
  /**
79
   * @param request the {@link PackageManagerRequest}.
80
   * @param skipInstallation {@code true} if the caller can guarantee that this package manager tool is already installed, {@code false} otherwise (run
81
   *     install method again to ensure the tool is installed).
82
   * @return the {@link ProcessResult}.
83
   */
84
  public ProcessResult runPackageManager(PackageManagerRequest request, boolean skipInstallation) {
85

86
    completeRequest(request);
3 ✔
87
    ProcessContext pc = request.getProcessContext();
3 ✔
88
    AbstractToolCommandlet pm = request.getPackageManager();
3 ✔
89
    if (!skipInstallation) {
2 ✔
90
      ToolInstallRequest parentRequest = request.getToolInstallRequest();
3 ✔
91
      if (parentRequest != null) {
2 ✔
92
        // this package-manager installation is triggered by a tool installation
93
        // (e.g. "ide install ng" triggers the npm install), so the package-manager's
94
        // install request gets the triggering tool's request as its parent
95
        pm.install(new ToolInstallRequest(parentRequest));
8 ✔
96
      } else {
97
        // no triggering tool installation (e.g. the uninstall path), install standalone
98
        ToolInstallRequest installRequest = new ToolInstallRequest(true);
5 ✔
99
        installRequest.setProcessContext(pc.createChild());
4 ✔
100
        pm.install(installRequest);
4 ✔
101
      }
102
    }
103
    return pm.runTool(pc, request.getProcessMode(), request.getArgs());
8 ✔
104
  }
105

106
  protected void completeRequest(PackageManagerRequest request) {
107

108
    if (request.getProcessContext() == null) {
3 ✔
109
      request.setProcessContext(this.context.newProcess().errorHandling(ProcessErrorHandling.THROW_CLI));
8 ✔
110
    }
111
    if (request.getPackageManager() == null) {
3 ✔
112
      request.setPackageManager(this.context.getCommandletManager().getCommandlet(getPackageManagerClass()));
10 ✔
113
    }
114
    if (request.getProcessMode() == null) {
3 ✔
115
      request.setProcessMode(ProcessMode.DEFAULT);
4 ✔
116
    }
117
    if (request.getArgs().isEmpty()) {
4 ✔
118
      completeRequestArgs(request);
3 ✔
119
    }
120
  }
1 ✔
121

122
  /**
123
   * @param request the {@link PackageManagerRequest} with currently {@link List#isEmpty() empty} {@link PackageManagerRequest#getArgs() args}.
124
   */
125
  protected void completeRequestArgs(PackageManagerRequest request) {
126

127
    String toolWithVersion = request.getTool();
3 ✔
128
    VersionIdentifier version = request.getVersion();
3 ✔
129
    if (version != null) {
2 ✔
130
      toolWithVersion = appendVersion(toolWithVersion, version);
5 ✔
131
    }
132
    request.addArg(request.getType());
5 ✔
133
    String option = completeRequestOption(request);
4 ✔
134
    if (option != null) {
2 ✔
135
      request.addArg(option);
4 ✔
136
    }
137
    request.addArg(toolWithVersion);
4 ✔
138
  }
1 ✔
139

140
  /**
141
   * @param request the {@link PackageManagerRequest}.
142
   * @return the option to {@link PackageManagerRequest#addArg(String) add as argument} to the package manager sub-command (e.g. "-gf") or {@code null} for no
143
   *     option.
144
   */
145
  protected String completeRequestOption(PackageManagerRequest request) {
146
    return null;
2 ✔
147
  }
148

149
  /**
150
   * @param tool the {@link PackageManagerRequest#getTool() tool} to manage (e.g. install).
151
   * @param version the {@link PackageManagerRequest#getVersion() version} to append.
152
   * @return the combination of {@code tool} and {@code version} in the syntax of the package manager.
153
   */
154
  protected String appendVersion(String tool, VersionIdentifier version) {
155
    return tool + '@' + version;
5 ✔
156
  }
157

158
  @Override
159
  protected boolean isIgnoreMissingSoftwareVersionFile() {
160

161
    return true;
×
162
  }
163

164
  @Override
165
  protected EditionAndVersion computeInstalledEditionAndVersion() {
166

167
    try {
168
      VersionIdentifier version = computeInstalledPackageVersion();
3 ✔
169
      if (version == null) {
2 ✔
170
        return null;
2 ✔
171
      }
172
      return new EditionAndVersion(this.tool, version);
7 ✔
173
    } catch (Exception e) {
×
174
      LOG.debug("Failed to compute installed version of {}", this.tool, e);
×
175
      return null;
×
176
    }
177
  }
178

179
  /**
180
   * @return the computed value of the installed version.
181
   * @implNote Implementations of this method should NOT trigger any tool installation or download. If you need to call
182
   *     {@link #runPackageManager(PackageManagerRequest)}, make sure to use {@link #runPackageManager(PackageManagerRequest, boolean)} with
183
   *     {@code skipInstallation=true} to avoid inadvertently triggering installations when only checking the version.
184
   */
185
  protected abstract VersionIdentifier computeInstalledPackageVersion();
186

187
  /**
188
   * Override to ignore the {@code toolPath} parameter and use the package-manager based detection of the actually installed version.
189
   *
190
   * @param toolPath the installation {@link Path} where to find the version file.
191
   * @return the installed version or null if not installed.
192
   */
193
  @Override
194
  protected VersionIdentifier getInstalledVersion(Path toolPath) {
195
    return getInstalledVersion();
3 ✔
196
  }
197

198
  @Override
199
  protected final void performToolInstallation(ToolInstallRequest request, Path installationPath) {
200

201
    PackageManagerRequest packageManagerRequest = new PackageManagerRequest(PackageManagerRequest.TYPE_INSTALL, getPackageName())
7 ✔
202
        .setToolInstallRequest(request).setProcessContext(request.getProcessContext()).setVersion(request.getRequested().getResolvedVersion());
9 ✔
203
    runPackageManager(packageManagerRequest, isSkipInstallation()).failOnError();
6 ✔
204
    invalidateInstalledEditionAndVersion();
2 ✔
205
  }
1 ✔
206

207
  /**
208
   * @return {@code false} if the underlying {@link #getPackageManagerClass() package manager} should also be installed, {@code false} to skip that additional
209
   *     installation (e.g. to prevent infinite loop in case of cyclic dependencies between package manager based tools such as node and npm).
210
   */
211
  protected boolean isSkipInstallation() {
212
    return false;
2 ✔
213
  }
214

215
  /**
216
   * @return {@code true} if the tool can be uninstalled, {@code false} if not.
217
   */
218
  protected boolean canBeUninstalled() {
219
    return true;
2 ✔
220
  }
221

222
  @Override
223
  protected final void performUninstall(Path toolPath) {
224
    if (canBeUninstalled()) {
3 ✔
225
      PackageManagerRequest request = new PackageManagerRequest(PackageManagerRequest.TYPE_UNINSTALL, getPackageName());
7 ✔
226
      runPackageManager(request).failOnError();
4 ✔
227
      invalidateInstalledEditionAndVersion();
2 ✔
228
    } else {
1 ✔
229
      LOG.info("IDEasy does not support uninstalling the tool {} since this will break your installation.\n"
6 ✔
230
          + "If you really want to uninstall it, please uninstall its parent tool via:\n"
231
          + "ide uninstall {}", this.tool, getParentTool().getName());
2 ✔
232
    }
233
  }
1 ✔
234

235
  protected abstract AbstractLocalToolCommandlet getParentTool();
236

237
  @Override
238
  public Path getToolPath() {
239

240
    return getParentTool().getToolPath();
4 ✔
241
  }
242

243
}
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