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

devonfw / IDEasy / 11910441375

19 Nov 2024 09:50AM UTC coverage: 67.287% (+0.005%) from 67.282%
11910441375

push

github

web-flow
#751: Moved postInstall() and changed tool installation message (#763)

2461 of 3999 branches covered (61.54%)

Branch coverage included in aggregate %.

6400 of 9170 relevant lines covered (69.79%)

3.08 hits per line

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

2.04
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.EnvironmentContext;
14
import com.devonfw.tools.ide.process.ProcessContext;
15
import com.devonfw.tools.ide.process.ProcessErrorHandling;
16
import com.devonfw.tools.ide.process.ProcessMode;
17
import com.devonfw.tools.ide.repo.ToolRepository;
18
import com.devonfw.tools.ide.version.VersionIdentifier;
19

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

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

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

37
  /**
38
   * Performs the installation or uninstallation of the {@link #getName() tool} via a package manager.
39
   *
40
   * @param silent {@code true} if called recursively to suppress verbose logging, {@code false} otherwise.
41
   * @param commandStrings commandStrings The package manager command strings to execute.
42
   * @return {@code true} if installation or uninstallation succeeds with any of the package manager commands, {@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, {@code false} otherwise.
56
   */
57
  protected boolean runWithPackageManager(boolean silent, List<PackageManagerCommand> pmCommands) {
58

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

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

74
  private void logPackageManagerCommands(PackageManagerCommand pmCommand) {
75

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

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

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

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

110
  @Override
111
  protected boolean isExtract() {
112

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

118
  @Override
119
  public boolean install(boolean silent, EnvironmentContext environmentContext) {
120

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

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

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

172
  @Override
173
  public void uninstall() {
174
    //TODO: handle "uninstall <globaltool>"
175
    this.context.error("Couldn't uninstall " + this.getName());
×
176
  }
×
177
}
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