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

devonfw / IDEasy / 28658444441

03 Jul 2026 11:45AM UTC coverage: 71.286% (-0.07%) from 71.351%
28658444441

Pull #2105

github

web-flow
Merge be70c5cc3 into 2504d944a
Pull Request #2105: #2056 Replaced progress bar with progress information for plugin installation

4695 of 7284 branches covered (64.46%)

Branch coverage included in aggregate %.

12095 of 16269 relevant lines covered (74.34%)

3.15 hits per line

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

86.63
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
   *
133
   * @param pluginsInstallationPath the {@link Path} to the plugins installation folder.
134
   */
135
  private void deleteAllPlugins(Path pluginsInstallationPath) {
136

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

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

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

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

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

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

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

227
  private void deleteExistingPluginMarkerFiles(FileAccess fileAccess, ToolPluginDescriptor plugin, Path currentMarkerFilePath) {
228

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

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

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

263
  /**
264
   * @param plugin the {@link ToolPluginDescriptor} to uninstall.
265
   */
266
  public void uninstallPlugin(ToolPluginDescriptor plugin) {
267

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

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

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

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

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

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