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

devonfw / IDEasy / 25362807967

05 May 2026 07:09AM UTC coverage: 70.716% (+0.06%) from 70.657%
25362807967

Pull #1832

github

web-flow
Merge 9a27bd7a6 into 84fe4e9bf
Pull Request #1832: #1754: Add support for VS Code plugin versions

4417 of 6890 branches covered (64.11%)

Branch coverage included in aggregate %.

11337 of 15388 relevant lines covered (73.67%)

3.12 hits per line

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

86.29
cli/src/main/java/com/devonfw/tools/ide/tool/plugin/PluginBasedCommandlet.java
1
package com.devonfw.tools.ide.tool.plugin;
2

3
import java.nio.file.Files;
4
import java.nio.file.Path;
5
import java.util.Collection;
6
import java.util.List;
7
import java.util.Set;
8

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

12
import com.devonfw.tools.ide.cli.CliException;
13
import com.devonfw.tools.ide.common.Tag;
14
import com.devonfw.tools.ide.context.IdeContext;
15
import com.devonfw.tools.ide.io.FileAccess;
16
import com.devonfw.tools.ide.process.ProcessContext;
17
import com.devonfw.tools.ide.process.ProcessErrorHandling;
18
import com.devonfw.tools.ide.step.Step;
19
import com.devonfw.tools.ide.tool.LocalToolCommandlet;
20
import com.devonfw.tools.ide.tool.ToolInstallRequest;
21
import com.devonfw.tools.ide.tool.ide.IdeToolCommandlet;
22

23
/**
24
 * Base class for {@link LocalToolCommandlet}s that support plugins. It can automatically install configured plugins for the tool managed by this commandlet.
25
 */
26
public abstract class PluginBasedCommandlet extends LocalToolCommandlet {
27

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

30
  private ToolPlugins plugins;
31

32
  /**
33
   * The constructor.
34
   *
35
   * @param context the {@link IdeContext}.
36
   * @param tool the {@link #getName() tool name}.
37
   * @param tags the {@link #getTags() tags} classifying the tool. Should be created via {@link Set#of(Object) Set.of} method.
38
   */
39
  public PluginBasedCommandlet(IdeContext context, String tool, Set<Tag> tags) {
40

41
    super(context, tool, tags);
5✔
42
  }
1✔
43

44
  /**
45
   * @return the {@link ToolPlugins} of this {@link PluginBasedCommandlet}.
46
   */
47
  public ToolPlugins getPlugins() {
48

49
    if (this.plugins == null) {
3✔
50
      ToolPlugins toolPlugins = new ToolPlugins();
4✔
51

52
      // Load project-specific plugins
53
      Path pluginsPath = getPluginsConfigPath();
3✔
54
      loadPluginsFromDirectory(toolPlugins, pluginsPath);
4✔
55

56
      // Load user-specific plugins, this is done after loading the project-specific plugins so the user can potentially
57
      // override plugins (e.g. change active flag).
58
      Path userPluginsPath = getUserHomePluginsConfigPath();
3✔
59
      loadPluginsFromDirectory(toolPlugins, userPluginsPath);
4✔
60

61
      this.plugins = toolPlugins;
3✔
62
    }
63

64
    return this.plugins;
3✔
65
  }
66

67
  private void loadPluginsFromDirectory(ToolPlugins map, Path pluginsPath) {
68

69
    List<Path> children = this.context.getFileAccess()
5✔
70
        .listChildren(pluginsPath, p -> p.getFileName().toString().endsWith(IdeContext.EXT_PROPERTIES));
8✔
71
    for (Path child : children) {
10✔
72
      ToolPluginDescriptor descriptor = ToolPluginDescriptor.of(child, this.context, isPluginUrlNeeded());
7✔
73
      map.add(descriptor);
3✔
74
    }
1✔
75
  }
1✔
76

77
  /**
78
   * @return {@code true} if {@link ToolPluginDescriptor#url() plugin URL} property is needed, {@code false} otherwise.
79
   */
80
  protected boolean isPluginUrlNeeded() {
81

82
    return false;
2✔
83
  }
84

85
  /**
86
   * @return the {@link Path} to the folder with the plugin configuration files inside the settings.
87
   */
88
  protected Path getPluginsConfigPath() {
89

90
    return this.context.getSettingsPath().resolve(this.tool).resolve(IdeContext.FOLDER_PLUGINS);
9✔
91
  }
92

93
  private Path getUserHomePluginsConfigPath() {
94

95
    return this.context.getUserHomeIde().resolve(IdeContext.FOLDER_SETTINGS).resolve(this.tool).resolve(IdeContext.FOLDER_PLUGINS);
11✔
96
  }
97

98
  /**
99
   * @return the {@link Path} where the plugins of this {@link IdeToolCommandlet} shall be installed.
100
   */
101
  public Path getPluginsInstallationPath() {
102

103
    return this.context.getPluginsPath().resolve(this.tool);
7✔
104
  }
105

106
  @Override
107
  protected void postInstall(ToolInstallRequest request) {
108

109
    super.postInstall(request);
3✔
110
    Path pluginsInstallationPath = getPluginsInstallationPath();
3✔
111
    FileAccess fileAccess = this.context.getFileAccess();
4✔
112
    if (!request.isAlreadyInstalled()) {
3✔
113
      fileAccess.delete(pluginsInstallationPath);
3✔
114
      List<Path> markerFiles = fileAccess.listChildren(this.context.getIdeHome().resolve(IdeContext.FOLDER_DOT_IDE), Files::isRegularFile);
14✔
115
      for (Path path : markerFiles) {
10✔
116
        if (path.getFileName().toString().startsWith("plugin." + getName())) {
8!
117
          LOG.debug("Plugin marker file {} got deleted.", path);
4✔
118
          fileAccess.delete(path);
3✔
119
        }
120
      }
1✔
121
    }
122
    fileAccess.mkdirs(pluginsInstallationPath);
3✔
123
    installPlugins(request.getProcessContext());
4✔
124
  }
1✔
125

126
  private void installPlugins(ProcessContext pc) {
127
    installPlugins(getPlugins().getPlugins(), pc);
6✔
128
  }
1✔
129

130
  /**
131
   * Method to install active plugins or to handle install for inactive plugins
132
   *
133
   * @param plugins as {@link Collection} of plugins to install.
134
   * @param pc the {@link ProcessContext} to use.
135
   */
136
  protected void installPlugins(Collection<ToolPluginDescriptor> plugins, ProcessContext pc) {
137
    for (ToolPluginDescriptor plugin : plugins) {
10✔
138
      Path pluginMarkerFile = retrievePluginMarkerFilePath(plugin);
4✔
139
      boolean pluginMarkerFileExists = pluginMarkerFile != null && Files.exists(pluginMarkerFile);
11!
140
      if (pluginMarkerFileExists) {
2✔
141
        LOG.debug("Markerfile for IDE {} and plugin '{}' already exists.", getName(), plugin.name());
7✔
142
      }
143
      if (plugin.active()) {
3✔
144
        if (this.context.isForcePlugins() || !pluginMarkerFileExists) {
6✔
145
          Step step = this.context.newStep("Install plugin " + plugin.name());
7✔
146
          step.run(() -> doInstallPluginStep(plugin, step, pc));
14✔
147
        } else {
1✔
148
          LOG.debug("Skipping installation of plugin '{}' due to existing marker file: {}", plugin.name(), pluginMarkerFile);
7✔
149
        }
150
      } else {
151
        if (!pluginMarkerFileExists) {
2!
152
          handleInstallForInactivePlugin(plugin);
3✔
153
        }
154
      }
155
    }
1✔
156
  }
1✔
157

158
  private void doInstallPluginStep(ToolPluginDescriptor plugin, Step step, ProcessContext pc) {
159
    boolean result = installPlugin(plugin, step, pc);
6✔
160
    if (result) {
2!
161
      createPluginMarkerFile(plugin);
3✔
162
    }
163
  }
1✔
164

165
  /**
166
   * @param plugin the {@link ToolPluginDescriptor plugin} to search for.
167
   * @return Path to the plugin marker file.
168
   */
169
  public Path retrievePluginMarkerFilePath(ToolPluginDescriptor plugin) {
170
    if (this.context.getIdeHome() != null) {
4!
171
      String markerFileName = "plugin" + "." + getName() + "." + getInstalledEdition() + "." + plugin.name();
8✔
172
      String version = plugin.version();
3✔
173
      if ((version != null) && !version.isBlank()) {
5!
174
        markerFileName = markerFileName + ".version-" + normalizeMarkerFileSegment(version);
6✔
175
      }
176
      return this.context.getIdeHome().resolve(IdeContext.FOLDER_DOT_IDE).resolve(markerFileName);
8✔
177
    }
178
    return null;
×
179
  }
180

181
  private String normalizeMarkerFileSegment(String value) {
182
    // replace all characters that are not allowed in filenames with "_"
183
    return value.replaceAll("[^A-Za-z0-9._-]", "_");
5✔
184
  }
185

186
  /**
187
   * Creates a marker file for a plugin in $IDE_HOME/.ide/plugin.«ide».«plugin-name»
188
   *
189
   * @param plugin the {@link ToolPluginDescriptor plugin} for which the marker file should be created.
190
   */
191
  public void createPluginMarkerFile(ToolPluginDescriptor plugin) {
192
    Path pluginMarkerFilePath = retrievePluginMarkerFilePath(plugin);
4✔
193
    if (pluginMarkerFilePath != null) {
2!
194
      FileAccess fileAccess = this.context.getFileAccess();
4✔
195
      fileAccess.mkdirs(pluginMarkerFilePath.getParent());
4✔
196
      deleteExistingPluginMarkerFiles(fileAccess, plugin, pluginMarkerFilePath);
5✔
197
      fileAccess.touch(pluginMarkerFilePath);
3✔
198
    }
199
  }
1✔
200

201
  private void deleteExistingPluginMarkerFiles(FileAccess fileAccess, ToolPluginDescriptor plugin, Path currentMarkerFilePath) {
202

203
    String markerFilePrefix = "plugin" + "." + getName() + "." + getInstalledEdition() + "." + plugin.name();
8✔
204
    List<Path> markerFiles = fileAccess.listChildren(currentMarkerFilePath.getParent(),
7✔
205
        p -> {
206
          String fileName = p.getFileName().toString();
4✔
207
          return Files.isRegularFile(p) && (fileName.equals(markerFilePrefix) || fileName.startsWith(markerFilePrefix + ".version-"));
18!
208
        });
209
    for (Path markerFile : markerFiles) {
10✔
210
      if (!markerFile.equals(currentMarkerFilePath)) {
4✔
211
        fileAccess.delete(markerFile);
3✔
212
        LOG.debug("Deleted stale plugin marker file {} before creating {}.", markerFile, currentMarkerFilePath);
5✔
213
      }
214
    }
1✔
215
  }
1✔
216

217
  /**
218
   * @param plugin the {@link ToolPluginDescriptor} to install.
219
   * @param step the {@link Step} for the plugin installation.
220
   * @param pc the {@link ProcessContext} to use.
221
   * @return boolean true if the installation of the plugin succeeded, false if not.
222
   */
223
  public abstract boolean installPlugin(ToolPluginDescriptor plugin, Step step, ProcessContext pc);
224

225
  /**
226
   * @param plugin the {@link ToolPluginDescriptor} to install.
227
   * @param step the {@link Step} for the plugin installation.
228
   */
229
  public void installPlugin(ToolPluginDescriptor plugin, final Step step) {
230
    ProcessContext pc = this.context.newProcess().errorHandling(ProcessErrorHandling.THROW_CLI);
6✔
231
    ToolInstallRequest request = new ToolInstallRequest(true);
5✔
232
    request.setProcessContext(pc);
3✔
233
    install(request);
4✔
234
    installPlugin(plugin, step, pc);
6✔
235
  }
1✔
236

237
  /**
238
   * @param plugin the {@link ToolPluginDescriptor} to uninstall.
239
   */
240
  public void uninstallPlugin(ToolPluginDescriptor plugin) {
241

242
    boolean error = false;
2✔
243
    Path pluginsPath = getPluginsInstallationPath();
3✔
244
    if (!Files.isDirectory(pluginsPath)) {
5!
245
      LOG.debug("Omitting to uninstall plugin {} ({}) as plugins folder does not exist at {}",
×
246
          plugin.name(), plugin.id(), pluginsPath);
×
247
      error = true;
×
248
    }
249
    FileAccess fileAccess = this.context.getFileAccess();
4✔
250
    Path match = fileAccess.findFirst(pluginsPath, p -> p.getFileName().toString().startsWith(plugin.id()), false);
7✔
251
    if (match == null) {
2!
252
      LOG.debug("Omitting to uninstall plugin {} ({}) as plugins folder does not contain a match at {}",
8✔
253
          plugin.name(), plugin.id(), pluginsPath);
11✔
254
      error = true;
2✔
255
    }
256
    if (error) {
2!
257
      LOG.error("Could not uninstall plugin {} because we could not find an installation", plugin);
5✔
258
    } else {
259
      fileAccess.delete(match);
×
260
      LOG.info("Successfully uninstalled plugin {}", plugin);
×
261
    }
262
  }
1✔
263

264
  /**
265
   * @param key the filename of the properties file configuring the requested plugin (typically excluding the ".properties" extension).
266
   * @return the {@link ToolPluginDescriptor} for the given {@code key}.
267
   */
268
  public ToolPluginDescriptor getPlugin(String key) {
269

270
    if (key == null) {
2!
271
      return null;
×
272
    }
273
    if (key.endsWith(IdeContext.EXT_PROPERTIES)) {
4!
274
      key = key.substring(0, key.length() - IdeContext.EXT_PROPERTIES.length());
×
275
    }
276

277
    ToolPlugins toolPlugins = getPlugins();
3✔
278
    ToolPluginDescriptor pluginDescriptor = toolPlugins.getByName(key);
4✔
279
    if (pluginDescriptor == null) {
2!
280
      throw new CliException(
×
281
          "Could not find plugin " + key + " at " + getPluginsConfigPath().resolve(key) + ".properties");
×
282
    }
283
    return pluginDescriptor;
2✔
284
  }
285

286
  /**
287
   * @param plugin the in{@link ToolPluginDescriptor#active() active} {@link ToolPluginDescriptor} that is skipped for regular plugin installation.
288
   */
289
  protected void handleInstallForInactivePlugin(ToolPluginDescriptor plugin) {
290

291
    LOG.debug("Omitting installation of inactive plugin {} ({}).", plugin.name(), plugin.id());
7✔
292
  }
1✔
293
}
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