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

devonfw / IDEasy / 25566036672

08 May 2026 04:08PM UTC coverage: 70.645% (-0.04%) from 70.683%
25566036672

Pull #1891

github

web-flow
Merge 7960ecc5f into 83b160b38
Pull Request #1891: #1880: Allow reset of installed plugins when launching IDE in force mode

4404 of 6886 branches covered (63.96%)

Branch coverage included in aggregate %.

11354 of 15420 relevant lines covered (73.63%)

3.11 hits per line

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

81.65
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
      deleteAllPlugins(pluginsInstallationPath);
4✔
114
    } else if (this.context.isForceMode()) {
4!
115
      // Prompt user if they want to reset all plugins
116
      boolean resetPlugins = this.context.question(
×
117
        "You are launching " + getName() + " in force mode. Do you want to reset all plugins for " + getName() + "? "
×
118
        + "This will uninstall all currently installed plugins and reinstall them as configured in your IDEasy project settings.");
119
      if (resetPlugins) {
×
120
        deleteAllPlugins(pluginsInstallationPath);
×
121
      }
122
    }
123
    fileAccess.mkdirs(pluginsInstallationPath);
3✔
124
    installPlugins(request.getProcessContext());
4✔
125
  }
1✔
126

127
  private void deleteAllPlugins(Path pluginsInstallationPath) {
128

129
    FileAccess fileAccess = this.context.getFileAccess();
4✔
130
    fileAccess.delete(pluginsInstallationPath);
3✔
131
      List<Path> markerFiles = fileAccess.listChildren(this.context.getIdeHome().resolve(IdeContext.FOLDER_DOT_IDE), Files::isRegularFile);
14✔
132
      for (Path path : markerFiles) {
10✔
133
        if (path.getFileName().toString().startsWith("plugin." + getName())) {
8!
134
          fileAccess.delete(path);
3✔
135
          LOG.debug("Plugin marker file {} got deleted.", path);
4✔
136
        }
137
      }
1✔
138

139
  }
1✔
140

141
  private void installPlugins(ProcessContext pc) {
142
    installPlugins(getPlugins().getPlugins(), pc);
6✔
143
  }
1✔
144

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

173
  private void doInstallPluginStep(ToolPluginDescriptor plugin, Step step, ProcessContext pc) {
174
    boolean result = installPlugin(plugin, step, pc);
6✔
175
    if (result) {
2!
176
      createPluginMarkerFile(plugin);
3✔
177
    }
178
  }
1✔
179

180
  /**
181
   * @param plugin the {@link ToolPluginDescriptor plugin} to search for.
182
   * @return Path to the plugin marker file.
183
   */
184
  public Path retrievePluginMarkerFilePath(ToolPluginDescriptor plugin) {
185
    if (this.context.getIdeHome() != null) {
4!
186
      return this.context.getIdeHome().resolve(IdeContext.FOLDER_DOT_IDE)
7✔
187
          .resolve("plugin" + "." + getName() + "." + getInstalledEdition() + "." + plugin.name());
7✔
188
    }
189
    return null;
×
190
  }
191

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

206
  /**
207
   * @param plugin the {@link ToolPluginDescriptor} to install.
208
   * @param step the {@link Step} for the plugin installation.
209
   * @param pc the {@link ProcessContext} to use.
210
   * @return boolean true if the installation of the plugin succeeded, false if not.
211
   */
212
  public abstract boolean installPlugin(ToolPluginDescriptor plugin, Step step, ProcessContext pc);
213

214
  /**
215
   * @param plugin the {@link ToolPluginDescriptor} to install.
216
   * @param step the {@link Step} for the plugin installation.
217
   */
218
  public void installPlugin(ToolPluginDescriptor plugin, final Step step) {
219
    ProcessContext pc = this.context.newProcess().errorHandling(ProcessErrorHandling.THROW_CLI);
6✔
220
    ToolInstallRequest request = new ToolInstallRequest(true);
5✔
221
    request.setProcessContext(pc);
3✔
222
    install(request);
4✔
223
    installPlugin(plugin, step, pc);
6✔
224
  }
1✔
225

226
  /**
227
   * @param plugin the {@link ToolPluginDescriptor} to uninstall.
228
   */
229
  public void uninstallPlugin(ToolPluginDescriptor plugin) {
230

231
    boolean error = false;
2✔
232
    Path pluginsPath = getPluginsInstallationPath();
3✔
233
    if (!Files.isDirectory(pluginsPath)) {
5!
234
      LOG.debug("Omitting to uninstall plugin {} ({}) as plugins folder does not exist at {}",
×
235
          plugin.name(), plugin.id(), pluginsPath);
×
236
      error = true;
×
237
    }
238
    FileAccess fileAccess = this.context.getFileAccess();
4✔
239
    Path match = fileAccess.findFirst(pluginsPath, p -> p.getFileName().toString().startsWith(plugin.id()), false);
7✔
240
    if (match == null) {
2!
241
      LOG.debug("Omitting to uninstall plugin {} ({}) as plugins folder does not contain a match at {}",
8✔
242
          plugin.name(), plugin.id(), pluginsPath);
11✔
243
      error = true;
2✔
244
    }
245
    if (error) {
2!
246
      LOG.error("Could not uninstall plugin {} because we could not find an installation", plugin);
5✔
247
    } else {
248
      fileAccess.delete(match);
×
249
      LOG.info("Successfully uninstalled plugin {}", plugin);
×
250
    }
251
  }
1✔
252

253
  /**
254
   * @param key the filename of the properties file configuring the requested plugin (typically excluding the ".properties" extension).
255
   * @return the {@link ToolPluginDescriptor} for the given {@code key}.
256
   */
257
  public ToolPluginDescriptor getPlugin(String key) {
258

259
    if (key == null) {
2!
260
      return null;
×
261
    }
262
    if (key.endsWith(IdeContext.EXT_PROPERTIES)) {
4!
263
      key = key.substring(0, key.length() - IdeContext.EXT_PROPERTIES.length());
×
264
    }
265

266
    ToolPlugins toolPlugins = getPlugins();
3✔
267
    ToolPluginDescriptor pluginDescriptor = toolPlugins.getByName(key);
4✔
268
    if (pluginDescriptor == null) {
2!
269
      throw new CliException(
×
270
          "Could not find plugin " + key + " at " + getPluginsConfigPath().resolve(key) + ".properties");
×
271
    }
272
    return pluginDescriptor;
2✔
273
  }
274

275
  /**
276
   * @param plugin the in{@link ToolPluginDescriptor#active() active} {@link ToolPluginDescriptor} that is skipped for regular plugin installation.
277
   */
278
  protected void handleInstallForInactivePlugin(ToolPluginDescriptor plugin) {
279

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