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

devonfw / IDEasy / 14191433633

01 Apr 2025 08:54AM UTC coverage: 67.747% (-0.008%) from 67.755%
14191433633

Pull #1192

github

web-flow
Merge c83461aa9 into 175374c2c
Pull Request #1192: #1191: Fixed vscode plugin detection

3047 of 4924 branches covered (61.88%)

Branch coverage included in aggregate %.

7867 of 11186 relevant lines covered (70.33%)

3.07 hits per line

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

78.18
cli/src/main/java/com/devonfw/tools/ide/tool/vscode/Vscode.java
1
package com.devonfw.tools.ide.tool.vscode;
2

3
import java.io.BufferedReader;
4
import java.io.IOException;
5
import java.nio.file.Files;
6
import java.nio.file.Path;
7
import java.util.ArrayList;
8
import java.util.HashMap;
9
import java.util.HashSet;
10
import java.util.List;
11
import java.util.Map;
12
import java.util.Set;
13

14
import com.devonfw.tools.ide.common.Tag;
15
import com.devonfw.tools.ide.context.IdeContext;
16
import com.devonfw.tools.ide.process.ProcessContext;
17
import com.devonfw.tools.ide.process.ProcessErrorHandling;
18
import com.devonfw.tools.ide.process.ProcessMode;
19
import com.devonfw.tools.ide.process.ProcessResult;
20
import com.devonfw.tools.ide.step.Step;
21
import com.devonfw.tools.ide.tool.ToolCommandlet;
22
import com.devonfw.tools.ide.tool.ide.IdeToolCommandlet;
23
import com.devonfw.tools.ide.tool.plugin.ToolPluginDescriptor;
24
import com.fasterxml.jackson.databind.ObjectMapper;
25

26
/**
27
 * {@link ToolCommandlet} for <a href="https://code.visualstudio.com/">vscode</a>.
28
 */
29
public class Vscode extends IdeToolCommandlet {
30

31
  /**
32
   * The constructor.
33
   *
34
   * @param context the {@link IdeContext}.
35
   */
36
  public Vscode(IdeContext context) {
37

38
    super(context, "vscode", Set.of(Tag.VS_CODE));
6✔
39
  }
1✔
40

41
  @Override
42
  protected String getBinaryName() {
43

44
    return "code";
2✔
45
  }
46

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

50
    List<String> extensionsCommands = new ArrayList<>();
4✔
51
    extensionsCommands.add("--force");
4✔
52
    extensionsCommands.add("--install-extension");
4✔
53
    extensionsCommands.add(plugin.id());
5✔
54
    ProcessResult result = runTool(ProcessMode.DEFAULT, ProcessErrorHandling.THROW_ERR, pc, extensionsCommands.toArray(String[]::new));
13✔
55
    if (result.isSuccessful()) {
3!
56
      this.context.success("Successfully installed plugin: {}", plugin.name());
11✔
57
      step.success();
2✔
58
      return true;
2✔
59
    } else {
60
      this.context.warning("An error occurred while installing plugin: {}", plugin.name());
×
61
      return false;
×
62
    }
63
  }
64

65

66
  @Override
67
  protected void handleInstallForInactivePlugin(ToolPluginDescriptor plugin) {
68

69
    super.handleInstallForInactivePlugin(plugin);
3✔
70

71
    Step step = this.context.newStep(true, "Add recommendation for " + this.tool + " and plugin: " + plugin.name());
12✔
72
    try {
73
      doAddRecommendation(plugin);
3✔
74
    } catch (RuntimeException e) {
×
75
      step.error(e, true);
×
76
      throw e;
×
77
    } finally {
78
      step.close();
2✔
79
    }
80
  }
1✔
81

82
  private void doAddRecommendation(ToolPluginDescriptor recommendation) {
83
    Path extensionsJsonPath = this.context.getWorkspacePath().resolve(".vscode/extensions.json");
6✔
84

85
    ObjectMapper objectMapper = new ObjectMapper();
4✔
86
    Map<String, Object> recommendationsMap;
87

88
    if (Files.exists(extensionsJsonPath)) {
5!
89
      try (BufferedReader reader = Files.newBufferedReader(extensionsJsonPath)) {
3✔
90
        recommendationsMap = objectMapper.readValue(reader, Map.class);
6✔
91
      } catch (IOException e) {
×
92
        throw new RuntimeException(e);
×
93
      }
1✔
94
    } else {
95
      recommendationsMap = new HashMap<>();
×
96
    }
97

98
    List<String> existingRecommendations = (List<String>) recommendationsMap.getOrDefault("recommendations", new ArrayList<>());
8✔
99

100
    try {
101
      Set<String> existingRecommendationsSet = new HashSet<>(existingRecommendations);
5✔
102

103
      String recommendationId = recommendation.id();
3✔
104
      if (existingRecommendationsSet.add(recommendationId)) {
4✔
105
        existingRecommendations.add(recommendationId);
4✔
106
      }
107
      objectMapper.writeValue(extensionsJsonPath.toFile(), recommendationsMap);
5✔
108

109
    } catch (IOException e) {
×
110
      this.context.error(e);
×
111
    }
1✔
112
  }
1✔
113

114
  @Override
115
  protected void configureToolArgs(ProcessContext pc, ProcessMode processMode, ProcessErrorHandling errorHandling, String... args) {
116

117
    Path vsCodeConf = this.context.getWorkspacePath().resolve(".vscode/.userdata");
6✔
118
    pc.addArg("--new-window");
4✔
119
    pc.addArg("--user-data-dir=" + vsCodeConf);
6✔
120
    Path vsCodeExtensionFolder = this.context.getIdeHome().resolve("plugins/vscode");
6✔
121
    pc.addArg("--extensions-dir=" + vsCodeExtensionFolder);
6✔
122
    pc.addArg(this.context.getWorkspacePath());
6✔
123
    super.configureToolArgs(pc, processMode, errorHandling, args);
6✔
124
  }
1✔
125

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