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

devonfw / IDEasy / 26082439534

19 May 2026 07:19AM UTC coverage: 70.969% (+0.05%) from 70.918%
26082439534

Pull #1891

github

web-flow
Merge 1295e650b into 29ed3245b
Pull Request #1891: #1880: Allow reset of installed plugins when launching IDE in force mode

4446 of 6922 branches covered (64.23%)

Branch coverage included in aggregate %.

11461 of 15492 relevant lines covered (73.98%)

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.AbstractIdeContext;
15
import com.devonfw.tools.ide.context.IdeContext;
16
import com.devonfw.tools.ide.context.IdeStartContextImpl;
17
import com.devonfw.tools.ide.io.FileAccess;
18
import com.devonfw.tools.ide.process.ProcessContext;
19
import com.devonfw.tools.ide.process.ProcessErrorHandling;
20
import com.devonfw.tools.ide.property.FlagProperty;
21
import com.devonfw.tools.ide.step.Step;
22
import com.devonfw.tools.ide.tool.LocalToolCommandlet;
23
import com.devonfw.tools.ide.tool.ToolInstallRequest;
24
import com.devonfw.tools.ide.tool.ide.IdeToolCommandlet;
25

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

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

33
  private ToolPlugins plugins;
34

35
  /** {@link FlagProperty} to force the reset and reinstallation of plugins as configured in the project settings. */
36
  public FlagProperty forcePluginReinstall;
37

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

47
    super(context, tool, tags);
5✔
48
  }
1✔
49

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

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

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

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

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

73
      this.plugins = toolPlugins;
3✔
74
    }
75

76
    return this.plugins;
3✔
77
  }
78

79
  private void loadPluginsFromDirectory(ToolPlugins map, Path pluginsPath) {
80

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

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

94
    return false;
2✔
95
  }
96

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

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

105
  private Path getUserHomePluginsConfigPath() {
106

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

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

115
    return this.context.getPluginsPath().resolve(this.tool);
7✔
116
  }
117

118
  @Override
119
  protected void postInstall(ToolInstallRequest request) {
120

121
    super.postInstall(request);
3✔
122
    Path pluginsInstallationPath = getPluginsInstallationPath();
3✔
123

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

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

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

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

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

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

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

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

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

224
  private void deleteExistingPluginMarkerFiles(FileAccess fileAccess, ToolPluginDescriptor plugin, Path currentMarkerFilePath) {
225

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

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

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

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

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

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

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

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

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

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