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

devonfw / IDEasy / 27759873085

18 Jun 2026 12:36PM UTC coverage: 71.321% (+0.02%) from 71.305%
27759873085

push

github

web-flow
#2030: fix plugin install in intellij (#2043)

Co-authored-by: Jörg Hohwiller <hohwille@users.noreply.github.com>

4688 of 7272 branches covered (64.47%)

Branch coverage included in aggregate %.

12101 of 16268 relevant lines covered (74.39%)

3.15 hits per line

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

81.34
cli/src/main/java/com/devonfw/tools/ide/tool/ide/IdeaBasedIdeToolCommandlet.java
1
package com.devonfw.tools.ide.tool.ide;
2

3
import java.nio.file.Path;
4
import java.util.ArrayList;
5
import java.util.Collections;
6
import java.util.List;
7
import java.util.Locale;
8
import java.util.Set;
9

10
import org.slf4j.Logger;
11
import org.slf4j.LoggerFactory;
12

13
import com.devonfw.tools.ide.common.Tag;
14
import com.devonfw.tools.ide.context.IdeContext;
15
import com.devonfw.tools.ide.log.IdeLogLevel;
16
import com.devonfw.tools.ide.process.ProcessContext;
17
import com.devonfw.tools.ide.process.ProcessMode;
18
import com.devonfw.tools.ide.process.ProcessResult;
19
import com.devonfw.tools.ide.step.Step;
20
import com.devonfw.tools.ide.tool.plugin.ToolPluginDescriptor;
21

22
/**
23
 * {@link IdeToolCommandlet} for IDEA based commandlets like: {@link com.devonfw.tools.ide.tool.intellij.Intellij IntelliJ} and
24
 * {@link com.devonfw.tools.ide.tool.androidstudio.AndroidStudio Android Studio}.
25
 */
26
public class IdeaBasedIdeToolCommandlet extends IdeToolCommandlet {
27

28
  private static final Logger LOG = LoggerFactory.getLogger(IdeaBasedIdeToolCommandlet.class);
4✔
29

30
  private static final String VM_OPTIONS_FILE_EXTENSION = ".vmoptions";
31

32
  private static final String VM_ARGS_ENV_SUFFIX = "_VM_ARGS";
33

34
  private static final String VM_OPTIONS_ENV_SUFFIX = "_VM_OPTIONS";
35

36
  /**
37
   * The constructor.
38
   *
39
   * @param context the {@link IdeContext}.
40
   * @param tool the {@link #getName() tool name}.
41
   * @param tags the {@link #getTags() tags} classifying the tool. Should be created via {@link Set#of(Object) Set.of} method.
42
   */
43
  public IdeaBasedIdeToolCommandlet(IdeContext context, String tool, Set<Tag> tags) {
44
    super(context, tool, tags);
5✔
45
  }
1✔
46

47
  @Override
48
  public boolean installPlugin(ToolPluginDescriptor plugin, final Step step, ProcessContext pc) {
49

50
    // In case of plugins with a custom repo url
51
    boolean customRepo = plugin.url() != null;
6!
52
    List<String> args = new ArrayList<>();
4✔
53
    args.add("installPlugins");
4✔
54
    args.add(plugin.id().replace("+", " "));
8✔
55
    if (customRepo) {
2!
56
      args.add(plugin.url());
5✔
57
    }
58
    ProcessResult result = runTool(pc, ProcessMode.DEFAULT, args);
6✔
59
    if (result.isSuccessful()) {
3!
60
      IdeLogLevel.SUCCESS.log(LOG, "Successfully installed plugin: {}", plugin.name());
11✔
61
      step.success();
2✔
62
      return true;
2✔
63
    } else {
64
      step.error("Failed to install plugin {} ({}): exit code was {}", plugin.name(), plugin.id(), result.getExitCode());
×
65
      return false;
×
66
    }
67
  }
68

69
  /**
70
   * Returns the IDE product prefix used in various files inside the {@code bin} directory, e.g. {@code "idea"} for {@code idea64.exe} or {@code "studio"} for
71
   * {@code studio64.vmoptions}.
72
   * <p>
73
   * By default, this method returns the tool name ({@link #getName()}). Subclasses may override this method if the IDE binary or vmoptions file uses a more
74
   * specific or different prefix.
75
   *
76
   * @return the IDE product prefix
77
   */
78
  protected String getIdeProductPrefix() {
79

80
    return getName();
×
81
  }
82

83
  @Override
84
  public ProcessResult runTool(ProcessContext pc, ProcessMode processMode, List<String> args) {
85
    if (!args.contains("installPlugins")) {
4✔
86
      args.add(this.context.getWorkspacePath().toString());
7✔
87
    }
88

89
    String variableName = getName().toUpperCase(Locale.ROOT).replace("-", "_") + VM_ARGS_ENV_SUFFIX;
9✔
90
    String userVmArgsContent = this.context.getVariables().get(variableName);
6✔
91
    if (userVmArgsContent == null || userVmArgsContent.isEmpty()) {
5!
92
      return super.runTool(pc, processMode, args);
6✔
93
    }
94
    String[] userVmArgs = userVmArgsContent.trim().split("\\s+");
5✔
95

96
    String prefix = getIdeProductPrefix();
3✔
97
    Path defaultVmOptionsPath = resolveDefaultVmOptionsPath(this.getToolPath(), prefix);
6✔
98
    String defaultVmArgsContent = this.context.getFileAccess().readFileContent(defaultVmOptionsPath);
6✔
99
    if (defaultVmArgsContent == null || defaultVmArgsContent.isEmpty()) {
5!
100
      LOG.debug("Default {} jvm options not found at: {}", getName(), defaultVmOptionsPath);
6✔
101
      return super.runTool(pc, processMode, args);
6✔
102
    }
103
    String[] defaultVmArgs = defaultVmArgsContent.trim().split("\\s+");
5✔
104

105
    String userOptionsFileName = "." + prefix + VM_OPTIONS_FILE_EXTENSION;
3✔
106
    Path confPath = this.context.getWorkspacePath().resolve(userOptionsFileName);
6✔
107
    this.context.getFileAccess().writeFileContent(mergeVmArgs(defaultVmArgs, userVmArgs), confPath, true);
10✔
108

109
    pc.withEnvVar(prefix.toUpperCase() + VM_OPTIONS_ENV_SUFFIX, confPath.toAbsolutePath().toString());
9✔
110
    return super.runTool(pc, processMode, args);
6✔
111
  }
112

113
  private Path resolveDefaultVmOptionsPath(Path softwarePath, String ideProductPrefix) {
114
    if (ideProductPrefix == null) {
2!
115
      LOG.debug("Binary prefix for tool {} is not set", getName());
×
116
      return null;
×
117
    }
118

119
    if (this.context.getSystemInfo().isWindows()) {
5✔
120
      return softwarePath
3✔
121
          .resolve("bin")
3✔
122
          .resolve(ideProductPrefix + "64.exe" + VM_OPTIONS_FILE_EXTENSION);
1✔
123
    }
124

125
    if (this.context.getSystemInfo().isMac()) {
5✔
126
      try {
127
        return softwarePath.toRealPath()
5✔
128
            .getParent()
2✔
129
            .resolve("bin")
3✔
130
            .resolve(ideProductPrefix + VM_OPTIONS_FILE_EXTENSION);
1✔
131
      } catch (Exception e) {
×
132
        LOG.error("Failed to resolve real path for software path: {}", softwarePath, e);
×
133
      }
134
    }
135

136
    return softwarePath // Linux
3✔
137
        .resolve("bin")
3✔
138
        .resolve(ideProductPrefix + "64" + VM_OPTIONS_FILE_EXTENSION);
1✔
139
  }
140

141
  private String mergeVmArgs(String[] defaults, String[] userArgs) {
142

143
    List<String> result = new ArrayList<>(defaults.length + userArgs.length);
9✔
144
    Collections.addAll(result, defaults);
4✔
145
    for (String userArg : userArgs) {
16✔
146
      boolean replaced = false;
2✔
147
      for (int i = 0; i < result.size(); i++) {
8!
148
        if (isSameJvmKey(result.get(i), userArg)) {
8✔
149
          result.set(i, userArg); //override default arg with user defined arg
5✔
150
          replaced = true;
2✔
151
          break;
1✔
152
        }
153
      }
154
      if (!replaced) { // Extend case: user configured arg does not exist in default options
2!
155
        result.add(userArg);
×
156
      }
157
    }
158

159
    return String.join(System.lineSeparator(), result);
4✔
160
  }
161

162
  private String extractJvmOptionsKey(String arg) {
163

164
    if (arg.startsWith("-Xmx")) {
4✔
165
      return "-Xmx";
2✔
166
    }
167
    if (arg.startsWith("-Xms")) {
4✔
168
      return "-Xms";
2✔
169
    }
170
    if (arg.startsWith("-Xmn")) {
4!
171
      return "-Xmn";
×
172
    }
173
    if (arg.startsWith("-Xss")) {
4!
174
      return "-Xss";
×
175
    }
176
    if (arg.startsWith("-D")) {
4✔
177
      int eq = arg.indexOf('=');
4✔
178
      return eq > 0 ? arg.substring(0, eq) : arg;
8!
179
    }
180
    if (arg.startsWith("-XX:")) {
4✔
181
      String opt = arg.substring(4);
4✔
182
      if (opt.startsWith("+") || opt.startsWith("-")) {
8!
183
        return "-XX:" + opt.substring(1);
×
184
      }
185
      int eq = opt.indexOf('=');
4✔
186
      return eq > 0 ? "-XX:" + opt.substring(0, eq) : arg;
9!
187
    }
188

189
    return arg;
2✔
190
  }
191

192
  private boolean isSameJvmKey(String a, String b) {
193

194
    return extractJvmOptionsKey(a).equals(extractJvmOptionsKey(b));
8✔
195
  }
196
}
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