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

devonfw / IDEasy / 9698821877

27 Jun 2024 02:51PM UTC coverage: 60.328% (-0.01%) from 60.34%
9698821877

push

github

web-flow
#410: Refactor get version and edition (#411)

1898 of 3456 branches covered (54.92%)

Branch coverage included in aggregate %.

4989 of 7960 relevant lines covered (62.68%)

2.74 hits per line

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

2.22
cli/src/main/java/com/devonfw/tools/ide/tool/GlobalToolCommandlet.java
1
package com.devonfw.tools.ide.tool;
2

3
import com.devonfw.tools.ide.common.Tag;
4
import com.devonfw.tools.ide.context.IdeContext;
5
import com.devonfw.tools.ide.io.FileAccess;
6
import com.devonfw.tools.ide.log.IdeLogLevel;
7
import com.devonfw.tools.ide.process.ProcessContext;
8
import com.devonfw.tools.ide.process.ProcessErrorHandling;
9
import com.devonfw.tools.ide.repo.ToolRepository;
10
import com.devonfw.tools.ide.version.VersionIdentifier;
11

12
import java.nio.file.Files;
13
import java.nio.file.Path;
14
import java.util.Arrays;
15
import java.util.List;
16
import java.util.Set;
17

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

23
  /**
24
   * The constructor.
25
   *
26
   * @param context the {@link IdeContext}.
27
   * @param tool the {@link #getName() tool name}.
28
   * @param tags the {@link #getTags() tags} classifying the tool. Should be created via
29
   *     {@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,
42
   *     {@code false} otherwise.
43
   */
44
  protected boolean runWithPackageManager(boolean silent, String... commandStrings) {
45

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

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

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

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

75
  /**
76
   * Executes the provided package manager command.
77
   *
78
   * @param pmCommand The {@link PackageManagerCommand} containing the commands to execute.
79
   * @param silent {@code true} if called recursively to suppress verbose logging, {@code false} otherwise.
80
   * @return {@code true} if the package manager commands execute successfully, {@code false} otherwise.
81
   */
82
  private boolean executePackageManagerCommand(PackageManagerCommand pmCommand, boolean silent) {
83

84
    String bashPath = this.context.findBash();
×
85
    for (String command : pmCommand.commands()) {
×
86
      ProcessContext pc = this.context.newProcess().errorHandling(ProcessErrorHandling.WARNING).executable(bashPath)
×
87
          .addArgs("-c", command);
×
88
      int exitCode = pc.run();
×
89
      if (exitCode != 0) {
×
90
        this.context.warning("{} command did not execute successfully", command);
×
91
        return false;
×
92
      }
93
    }
×
94

95
    if (!silent) {
×
96
      this.context.success("Successfully installed {}", this.tool);
×
97
    }
98
    return true;
×
99
  }
100

101
  @Override
102
  protected boolean isExtract() {
103

104
    // for global tools we usually download installers and do not want to extract them (e.g. installer.msi file shall
105
    // not be extracted)
106
    return false;
×
107
  }
108

109
  @Override
110
  protected boolean doInstall(boolean silent) {
111

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

150
  @Override
151
  public VersionIdentifier getInstalledVersion() {
152
    //TODO: handle "get-version <globaltool>"
153
    this.context.error("Couldn't get installed version of " + this.getName());
×
154
    return null;
×
155
  }
156

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

164
  @Override
165
  public void uninstall() {
166
    //TODO: handle "uninstall <globaltool>"
167
    this.context.error("Couldn't uninstall " + this.getName());
×
168
  }
×
169
}
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