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

devonfw / IDEasy / 26097176558

19 May 2026 12:28PM UTC coverage: 70.958% (+0.04%) from 70.918%
26097176558

Pull #1891

github

web-flow
Merge db7b1296e into 921801555
Pull Request #1891: #1880: Allow reset of installed plugins when launching IDE in force mode

4446 of 6930 branches covered (64.16%)

Branch coverage included in aggregate %.

11479 of 15513 relevant lines covered (74.0%)

3.13 hits per line

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

86.34
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.property.FlagProperty;
19
import com.devonfw.tools.ide.step.Step;
20
import com.devonfw.tools.ide.tool.LocalToolCommandlet;
21
import com.devonfw.tools.ide.tool.ToolInstallRequest;
22
import com.devonfw.tools.ide.tool.ide.IdeToolCommandlet;
23

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

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

31
  private ToolPlugins plugins;
32

33
  /** {@link FlagProperty} to force the reset and reinstallation of plugins as configured in the project settings. */
34
  public FlagProperty forcePluginReinstall;
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 PluginBasedCommandlet(IdeContext context, String tool, Set<Tag> tags) {
44

45
    super(context, tool, tags);
5✔
46
  }
1✔
47

48
  @Override
49
  protected void initProperties() {
50
    this.forcePluginReinstall = add(new FlagProperty("--force-plugin-reinstall"));
9✔
51
    super.initProperties();
2✔
52
  }
1✔
53

54
  /**
55
   * @return the {@link ToolPlugins} of this {@link PluginBasedCommandlet}.
56
   */
57
  public ToolPlugins getPlugins() {
58

59
    if (this.plugins == null) {
3✔
60
      ToolPlugins toolPlugins = new ToolPlugins();
4✔
61

62
      // Load project-specific plugins
63
      Path pluginsPath = getPluginsConfigPath();
3✔
64
      loadPluginsFromDirectory(toolPlugins, pluginsPath);
4✔
65

66
      // Load user-specific plugins, this is done after loading the project-specific plugins so the user can potentially
67
      // override plugins (e.g. change active flag).
68
      Path userPluginsPath = getUserHomePluginsConfigPath();
3✔
69
      loadPluginsFromDirectory(toolPlugins, userPluginsPath);
4✔
70

71
      this.plugins = toolPlugins;
3✔
72
    }
73

74
    return this.plugins;
3✔
75
  }
76

77
  private void loadPluginsFromDirectory(ToolPlugins map, Path pluginsPath) {
78

79
    List<Path> children = this.context.getFileAccess()
5✔
80
        .listChildren(pluginsPath, p -> p.getFileName().toString().endsWith(IdeContext.EXT_PROPERTIES));
8✔
81
    for (Path child : children) {
10✔
82
      ToolPluginDescriptor descriptor = ToolPluginDescriptor.of(child, this.context, isPluginUrlNeeded());
7✔
83
      map.add(descriptor);
3✔
84
    }
1✔
85
  }
1✔
86

87
  /**
88
   * @return {@code true} if {@link ToolPluginDescriptor#url() plugin URL} property is needed, {@code false} otherwise.
89
   */
90
  protected boolean isPluginUrlNeeded() {
91

92
    return false;
2✔
93
  }
94

95
  /**
96
   * @return the {@link Path} to the folder with the plugin configuration files inside the settings.
97
   */
98
  protected Path getPluginsConfigPath() {
99

100
    return this.context.getSettingsPath().resolve(this.tool).resolve(IdeContext.FOLDER_PLUGINS);
9✔
101
  }
102

103
  private Path getUserHomePluginsConfigPath() {
104

105
    return this.context.getUserHomeIde().resolve(IdeContext.FOLDER_SETTINGS).resolve(this.tool).resolve(IdeContext.FOLDER_PLUGINS);
11✔
106
  }
107

108
  /**
109
   * @return the {@link Path} where the plugins of this {@link IdeToolCommandlet} shall be installed.
110
   */
111
  public Path getPluginsInstallationPath() {
112

113
    return this.context.getPluginsPath().resolve(this.tool);
7✔
114
  }
115

116
  @Override
117
  protected void postInstall(ToolInstallRequest request) {
118

119
    super.postInstall(request);
3✔
120
    Path pluginsInstallationPath = getPluginsInstallationPath();
3✔
121

122
    if (!request.isAlreadyInstalled() || this.forcePluginReinstall.isTrue()) {
7!
123
      LOG.info("Resetting all installed plugins...");
3✔
124
      deleteAllPlugins(pluginsInstallationPath);
3✔
125
    }
126
    this.context.getFileAccess().mkdirs(pluginsInstallationPath);
5✔
127
    installPlugins(request.getProcessContext());
4✔
128
  }
1✔
129

130
  /**
131
   * Deletes all installed plugins for this {@link IdeToolCommandlet} by deleting the plugins installation folder and all plugin marker files.
132
   * @param pluginsInstallationPath the {@link Path} to the plugins installation folder.
133
   */
134
  private void deleteAllPlugins(Path pluginsInstallationPath) {
135

136
    FileAccess fileAccess = this.context.getFileAccess();
4✔
137
    fileAccess.delete(pluginsInstallationPath);
3✔
138
    List<Path> markerFiles = fileAccess.listChildren(this.context.getIdeHome().resolve(IdeContext.FOLDER_DOT_IDE), Files::isRegularFile);
14✔
139
    for (Path path : markerFiles) {
10✔
140
      if (path.getFileName().toString().startsWith("plugin." + getName())) {
8!
141
        fileAccess.delete(path);
3✔
142
        LOG.debug("Plugin marker file {} got deleted.", path);
4✔
143
      }
144
    }
1✔
145
  }
1✔
146

147
  private void installPlugins(ProcessContext pc) {
148
    installPlugins(getPlugins().getPlugins(), pc);
6✔
149
  }
1✔
150

151
  /**
152
   * Method to install active plugins or to handle install for inactive plugins
153
   *
154
   * @param plugins as {@link Collection} of plugins to install.
155
   * @param pc the {@link ProcessContext} to use.
156
   */
157
  protected void installPlugins(Collection<ToolPluginDescriptor> plugins, ProcessContext pc) {
158
    for (ToolPluginDescriptor plugin : plugins) {
10✔
159
      Path pluginMarkerFile = retrievePluginMarkerFilePath(plugin);
4✔
160
      boolean pluginMarkerFileExists = pluginMarkerFile != null && Files.exists(pluginMarkerFile);
11!
161
      if (pluginMarkerFileExists) {
2✔
162
        LOG.debug("Markerfile for IDE {} and plugin '{}' already exists.", getName(), plugin.name());
7✔
163
      }
164
      if (plugin.active()) {
3✔
165
        if (this.context.isForcePlugins() || !pluginMarkerFileExists) {
6✔
166
          Step step = this.context.newStep("Install plugin " + plugin.name());
7✔
167
          step.run(() -> doInstallPluginStep(plugin, step, pc));
14✔
168
        } else {
1✔
169
          LOG.debug("Skipping installation of plugin '{}' due to existing marker file: {}", plugin.name(), pluginMarkerFile);
7✔
170
        }
171
      } else {
172
        if (!pluginMarkerFileExists) {
2!
173
          handleInstallForInactivePlugin(plugin);
3✔
174
        }
175
      }
176
    }
1✔
177
  }
1✔
178

179
  private void doInstallPluginStep(ToolPluginDescriptor plugin, Step step, ProcessContext pc) {
180
    boolean result = installPlugin(plugin, step, pc);
6✔
181
    if (result) {
2!
182
      createPluginMarkerFile(plugin);
3✔
183
    }
184
  }
1✔
185

186
  /**
187
   * @param plugin the {@link ToolPluginDescriptor plugin} to search for.
188
   * @return Path to the plugin marker file.
189
   */
190
  public Path retrievePluginMarkerFilePath(ToolPluginDescriptor plugin) {
191
    if (this.context.getIdeHome() != null) {
4!
192
      String markerFileName = "plugin" + "." + getName() + "." + getInstalledEdition() + "." + plugin.name();
8✔
193
      String version = plugin.version();
3✔
194
      if ((version != null) && !version.isBlank()) {
5!
195
        markerFileName = markerFileName + ".version-" + normalizeMarkerFileSegment(version);
6✔
196
      }
197
      return this.context.getIdeHome().resolve(IdeContext.FOLDER_DOT_IDE).resolve(markerFileName);
8✔
198
    }
199
    return null;
×
200
  }
201

202
  private String normalizeMarkerFileSegment(String value) {
203
    // replace all characters that are not allowed in filenames with "_"
204
    return value.replaceAll("[^A-Za-z0-9._-]", "_");
5✔
205
  }
206

207
  /**
208
   * Creates a marker file for a plugin in $IDE_HOME/.ide/plugin.«ide».«plugin-name»
209
   *
210
   * @param plugin the {@link ToolPluginDescriptor plugin} for which the marker file should be created.
211
   */
212
  public void createPluginMarkerFile(ToolPluginDescriptor plugin) {
213
    Path pluginMarkerFilePath = retrievePluginMarkerFilePath(plugin);
4✔
214
    if (pluginMarkerFilePath != null) {
2!
215
      FileAccess fileAccess = this.context.getFileAccess();
4✔
216
      fileAccess.mkdirs(pluginMarkerFilePath.getParent());
4✔
217
      deleteExistingPluginMarkerFiles(fileAccess, plugin, pluginMarkerFilePath);
5✔
218
      fileAccess.touch(pluginMarkerFilePath);
3✔
219
    }
220
  }
1✔
221

222
  private void deleteExistingPluginMarkerFiles(FileAccess fileAccess, ToolPluginDescriptor plugin, Path currentMarkerFilePath) {
223

224
    String markerFilePrefix = "plugin" + "." + getName() + "." + getInstalledEdition() + "." + plugin.name();
8✔
225
    List<Path> markerFiles = fileAccess.listChildren(currentMarkerFilePath.getParent(),
7✔
226
        p -> {
227
          String fileName = p.getFileName().toString();
4✔
228
          return Files.isRegularFile(p) && (fileName.equals(markerFilePrefix) || fileName.startsWith(markerFilePrefix + ".version-"));
18!
229
        });
230
    for (Path markerFile : markerFiles) {
10✔
231
      if (!markerFile.equals(currentMarkerFilePath)) {
4✔
232
        fileAccess.delete(markerFile);
3✔
233
        LOG.debug("Deleted stale plugin marker file {} before creating {}.", markerFile, currentMarkerFilePath);
5✔
234
      }
235
    }
1✔
236
  }
1✔
237

238
  /**
239
   * @param plugin the {@link ToolPluginDescriptor} to install.
240
   * @param step the {@link Step} for the plugin installation.
241
   * @param pc the {@link ProcessContext} to use.
242
   * @return boolean true if the installation of the plugin succeeded, false if not.
243
   */
244
  public abstract boolean installPlugin(ToolPluginDescriptor plugin, Step step, ProcessContext pc);
245

246
  /**
247
   * @param plugin the {@link ToolPluginDescriptor} to install.
248
   * @param step the {@link Step} for the plugin installation.
249
   */
250
  public void installPlugin(ToolPluginDescriptor plugin, final Step step) {
251
    ProcessContext pc = this.context.newProcess().errorHandling(ProcessErrorHandling.THROW_CLI);
6✔
252
    ToolInstallRequest request = new ToolInstallRequest(true);
5✔
253
    request.setProcessContext(pc);
3✔
254
    install(request);
4✔
255
    installPlugin(plugin, step, pc);
6✔
256
  }
1✔
257

258
  /**
259
   * @param plugin the {@link ToolPluginDescriptor} to uninstall.
260
   */
261
  public void uninstallPlugin(ToolPluginDescriptor plugin) {
262

263
    boolean error = false;
2✔
264
    Path pluginsPath = getPluginsInstallationPath();
3✔
265
    if (!Files.isDirectory(pluginsPath)) {
5!
266
      LOG.debug("Omitting to uninstall plugin {} ({}) as plugins folder does not exist at {}",
×
267
          plugin.name(), plugin.id(), pluginsPath);
×
268
      error = true;
×
269
    }
270
    FileAccess fileAccess = this.context.getFileAccess();
4✔
271
    Path match = fileAccess.findFirst(pluginsPath, p -> p.getFileName().toString().startsWith(plugin.id()), false);
7✔
272
    if (match == null) {
2!
273
      LOG.debug("Omitting to uninstall plugin {} ({}) as plugins folder does not contain a match at {}",
8✔
274
          plugin.name(), plugin.id(), pluginsPath);
11✔
275
      error = true;
2✔
276
    }
277
    if (error) {
2!
278
      LOG.error("Could not uninstall plugin {} because we could not find an installation", plugin);
5✔
279
    } else {
280
      fileAccess.delete(match);
×
281
      LOG.info("Successfully uninstalled plugin {}", plugin);
×
282
    }
283
  }
1✔
284

285
  /**
286
   * @param key the filename of the properties file configuring the requested plugin (typically excluding the ".properties" extension).
287
   * @return the {@link ToolPluginDescriptor} for the given {@code key}.
288
   */
289
  public ToolPluginDescriptor getPlugin(String key) {
290

291
    if (key == null) {
2!
292
      return null;
×
293
    }
294
    if (key.endsWith(IdeContext.EXT_PROPERTIES)) {
4!
295
      key = key.substring(0, key.length() - IdeContext.EXT_PROPERTIES.length());
×
296
    }
297

298
    ToolPlugins toolPlugins = getPlugins();
3✔
299
    ToolPluginDescriptor pluginDescriptor = toolPlugins.getByName(key);
4✔
300
    if (pluginDescriptor == null) {
2!
301
      throw new CliException(
×
302
          "Could not find plugin " + key + " at " + getPluginsConfigPath().resolve(key) + ".properties");
×
303
    }
304
    return pluginDescriptor;
2✔
305
  }
306

307
  /**
308
   * @param plugin the in{@link ToolPluginDescriptor#active() active} {@link ToolPluginDescriptor} that is skipped for regular plugin installation.
309
   */
310
  protected void handleInstallForInactivePlugin(ToolPluginDescriptor plugin) {
311

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