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

devonfw / IDEasy / 22300285724

23 Feb 2026 09:32AM UTC coverage: 70.754% (+0.3%) from 70.474%
22300285724

Pull #1714

github

web-flow
Merge caa980897 into 379acdc9d
Pull Request #1714: #404: #1713: advanced logging

4064 of 6348 branches covered (64.02%)

Branch coverage included in aggregate %.

10640 of 14434 relevant lines covered (73.71%)

3.1 hits per line

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

84.93
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
      return this.context.getIdeHome().resolve(IdeContext.FOLDER_DOT_IDE)
7✔
172
          .resolve("plugin" + "." + getName() + "." + getInstalledEdition() + "." + plugin.name());
7✔
173
    }
174
    return null;
×
175
  }
176

177
  /**
178
   * Creates a marker file for a plugin in $IDE_HOME/.ide/plugin.«ide».«plugin-name»
179
   *
180
   * @param plugin the {@link ToolPluginDescriptor plugin} for which the marker file should be created.
181
   */
182
  public void createPluginMarkerFile(ToolPluginDescriptor plugin) {
183
    Path pluginMarkerFilePath = retrievePluginMarkerFilePath(plugin);
4✔
184
    if (pluginMarkerFilePath != null) {
2!
185
      FileAccess fileAccess = this.context.getFileAccess();
4✔
186
      fileAccess.mkdirs(pluginMarkerFilePath.getParent());
4✔
187
      fileAccess.touch(pluginMarkerFilePath);
3✔
188
    }
189
  }
1✔
190

191
  /**
192
   * @param plugin the {@link ToolPluginDescriptor} to install.
193
   * @param step the {@link Step} for the plugin installation.
194
   * @param pc the {@link ProcessContext} to use.
195
   * @return boolean true if the installation of the plugin succeeded, false if not.
196
   */
197
  public abstract boolean installPlugin(ToolPluginDescriptor plugin, Step step, ProcessContext pc);
198

199
  /**
200
   * @param plugin the {@link ToolPluginDescriptor} to install.
201
   * @param step the {@link Step} for the plugin installation.
202
   */
203
  public void installPlugin(ToolPluginDescriptor plugin, final Step step) {
204
    ProcessContext pc = this.context.newProcess().errorHandling(ProcessErrorHandling.THROW_CLI);
6✔
205
    ToolInstallRequest request = new ToolInstallRequest(true);
5✔
206
    request.setProcessContext(pc);
3✔
207
    install(request);
4✔
208
    installPlugin(plugin, step, pc);
6✔
209
  }
1✔
210

211
  /**
212
   * @param plugin the {@link ToolPluginDescriptor} to uninstall.
213
   */
214
  public void uninstallPlugin(ToolPluginDescriptor plugin) {
215

216
    boolean error = false;
2✔
217
    Path pluginsPath = getPluginsInstallationPath();
3✔
218
    if (!Files.isDirectory(pluginsPath)) {
5!
219
      LOG.debug("Omitting to uninstall plugin {} ({}) as plugins folder does not exist at {}",
×
220
          plugin.name(), plugin.id(), pluginsPath);
×
221
      error = true;
×
222
    }
223
    FileAccess fileAccess = this.context.getFileAccess();
4✔
224
    Path match = fileAccess.findFirst(pluginsPath, p -> p.getFileName().toString().startsWith(plugin.id()), false);
7✔
225
    if (match == null) {
2!
226
      LOG.debug("Omitting to uninstall plugin {} ({}) as plugins folder does not contain a match at {}",
8✔
227
          plugin.name(), plugin.id(), pluginsPath);
11✔
228
      error = true;
2✔
229
    }
230
    if (error) {
2!
231
      LOG.error("Could not uninstall plugin {} because we could not find an installation", plugin);
5✔
232
    } else {
233
      fileAccess.delete(match);
×
234
      LOG.info("Successfully uninstalled plugin {}", plugin);
×
235
    }
236
  }
1✔
237

238
  /**
239
   * @param key the filename of the properties file configuring the requested plugin (typically excluding the ".properties" extension).
240
   * @return the {@link ToolPluginDescriptor} for the given {@code key}.
241
   */
242
  public ToolPluginDescriptor getPlugin(String key) {
243

244
    if (key == null) {
2!
245
      return null;
×
246
    }
247
    if (key.endsWith(IdeContext.EXT_PROPERTIES)) {
4!
248
      key = key.substring(0, key.length() - IdeContext.EXT_PROPERTIES.length());
×
249
    }
250

251
    ToolPlugins toolPlugins = getPlugins();
3✔
252
    ToolPluginDescriptor pluginDescriptor = toolPlugins.getByName(key);
4✔
253
    if (pluginDescriptor == null) {
2!
254
      throw new CliException(
×
255
          "Could not find plugin " + key + " at " + getPluginsConfigPath().resolve(key) + ".properties");
×
256
    }
257
    return pluginDescriptor;
2✔
258
  }
259

260
  /**
261
   * @param plugin the in{@link ToolPluginDescriptor#active() active} {@link ToolPluginDescriptor} that is skipped for regular plugin installation.
262
   */
263
  protected void handleInstallForInactivePlugin(ToolPluginDescriptor plugin) {
264

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