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

devonfw / IDEasy / 19651727463

24 Nov 2025 10:43PM UTC coverage: 69.156% (+0.1%) from 69.024%
19651727463

push

github

web-flow
#1144: #1145: CVE warnings and suggestions (#1593)

Co-authored-by: KianRolf <kian.loroff@capgemini.com>
Co-authored-by: jan-vcapgemini <59438728+jan-vcapgemini@users.noreply.github.com>

3613 of 5721 branches covered (63.15%)

Branch coverage included in aggregate %.

9387 of 13077 relevant lines covered (71.78%)

3.15 hits per line

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

82.52
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 com.devonfw.tools.ide.cli.CliException;
10
import com.devonfw.tools.ide.common.Tag;
11
import com.devonfw.tools.ide.context.IdeContext;
12
import com.devonfw.tools.ide.io.FileAccess;
13
import com.devonfw.tools.ide.process.ProcessContext;
14
import com.devonfw.tools.ide.process.ProcessErrorHandling;
15
import com.devonfw.tools.ide.step.Step;
16
import com.devonfw.tools.ide.tool.LocalToolCommandlet;
17
import com.devonfw.tools.ide.tool.ide.IdeToolCommandlet;
18

19
/**
20
 * Base class for {@link LocalToolCommandlet}s that support plugins. It can automatically install configured plugins for the tool managed by this commandlet.
21
 */
22
public abstract class PluginBasedCommandlet extends LocalToolCommandlet {
23

24
  private ToolPlugins plugins;
25

26
  /**
27
   * The constructor.
28
   *
29
   * @param context the {@link IdeContext}.
30
   * @param tool the {@link #getName() tool name}.
31
   * @param tags the {@link #getTags() tags} classifying the tool. Should be created via {@link Set#of(Object) Set.of} method.
32
   */
33
  public PluginBasedCommandlet(IdeContext context, String tool, Set<Tag> tags) {
34

35
    super(context, tool, tags);
5 ✔
36
  }
1 ✔
37

38
  /**
39
   * @return the {@link ToolPlugins} of this {@link PluginBasedCommandlet}.
40
   */
41
  public ToolPlugins getPlugins() {
42

43
    if (this.plugins == null) {
3 ✔
44
      ToolPlugins toolPlugins = new ToolPlugins(this.context);
6 ✔
45

46
      // Load project-specific plugins
47
      Path pluginsPath = getPluginsConfigPath();
3 ✔
48
      loadPluginsFromDirectory(toolPlugins, pluginsPath);
4 ✔
49

50
      // Load user-specific plugins, this is done after loading the project-specific plugins so the user can potentially
51
      // override plugins (e.g. change active flag).
52
      Path userPluginsPath = getUserHomePluginsConfigPath();
3 ✔
53
      loadPluginsFromDirectory(toolPlugins, userPluginsPath);
4 ✔
54

55
      this.plugins = toolPlugins;
3 ✔
56
    }
57

58
    return this.plugins;
3 ✔
59
  }
60

61
  private void loadPluginsFromDirectory(ToolPlugins map, Path pluginsPath) {
62

63
    List<Path> children = this.context.getFileAccess()
5 ✔
64
        .listChildren(pluginsPath, p -> p.getFileName().toString().endsWith(IdeContext.EXT_PROPERTIES));
8 ✔
65
    for (Path child : children) {
10 ✔
66
      ToolPluginDescriptor descriptor = ToolPluginDescriptor.of(child, this.context, isPluginUrlNeeded());
7 ✔
67
      map.add(descriptor);
3 ✔
68
    }
1 ✔
69
  }
1 ✔
70

71
  /**
72
   * @return {@code true} if {@link ToolPluginDescriptor#url() plugin URL} property is needed, {@code false} otherwise.
73
   */
74
  protected boolean isPluginUrlNeeded() {
75

76
    return false;
2 ✔
77
  }
78

79
  /**
80
   * @return the {@link Path} to the folder with the plugin configuration files inside the settings.
81
   */
82
  protected Path getPluginsConfigPath() {
83

84
    return this.context.getSettingsPath().resolve(this.tool).resolve(IdeContext.FOLDER_PLUGINS);
9 ✔
85
  }
86

87
  private Path getUserHomePluginsConfigPath() {
88

89
    return this.context.getUserHomeIde().resolve(IdeContext.FOLDER_SETTINGS).resolve(this.tool).resolve(IdeContext.FOLDER_PLUGINS);
11 ✔
90
  }
91

92
  /**
93
   * @return the {@link Path} where the plugins of this {@link IdeToolCommandlet} shall be installed.
94
   */
95
  public Path getPluginsInstallationPath() {
96

97
    return this.context.getPluginsPath().resolve(this.tool);
7 ✔
98
  }
99

100
  @Override
101
  protected void postInstall(boolean newlyInstalled, ProcessContext pc) {
102

103
    super.postInstall(newlyInstalled, pc);
4 ✔
104
    Path pluginsInstallationPath = getPluginsInstallationPath();
3 ✔
105
    FileAccess fileAccess = this.context.getFileAccess();
4 ✔
106
    if (newlyInstalled) {
2 !
107
      fileAccess.delete(pluginsInstallationPath);
3 ✔
108
      List<Path> markerFiles = fileAccess.listChildren(this.context.getIdeHome().resolve(IdeContext.FOLDER_DOT_IDE), Files::isRegularFile);
14 ✔
109
      for (Path path : markerFiles) {
10 ✔
110
        if (path.getFileName().toString().startsWith("plugin." + getName())) {
8 !
111
          this.context.debug("Plugin marker file {} got deleted.", path);
10 ✔
112
          fileAccess.delete(path);
3 ✔
113
        }
114
      }
1 ✔
115
    }
116
    fileAccess.mkdirs(pluginsInstallationPath);
3 ✔
117
    installPlugins(pc);
3 ✔
118
  }
1 ✔
119

120
  private void installPlugins(ProcessContext pc) {
121
    installPlugins(getPlugins().getPlugins(), pc);
6 ✔
122
  }
1 ✔
123

124
  /**
125
   * Method to install active plugins or to handle install for inactive plugins
126
   *
127
   * @param plugins as {@link Collection} of plugins to install.
128
   * @param pc the {@link ProcessContext} to use.
129
   */
130
  protected void installPlugins(Collection<ToolPluginDescriptor> plugins, ProcessContext pc) {
131
    for (ToolPluginDescriptor plugin : plugins) {
10 ✔
132
      Path pluginMarkerFile = retrievePluginMarkerFilePath(plugin);
4 ✔
133
      boolean pluginMarkerFileExists = pluginMarkerFile != null && Files.exists(pluginMarkerFile);
11 !
134
      if (pluginMarkerFileExists) {
2 ✔
135
        this.context.debug("Markerfile for IDE {} and plugin '{}' already exists.", getName(), plugin.name());
16 ✔
136
      }
137
      if (plugin.active()) {
3 ✔
138
        if (this.context.isForcePlugins() || !pluginMarkerFileExists) {
6 !
139
          Step step = this.context.newStep("Install plugin " + plugin.name());
7 ✔
140
          step.run(() -> doInstallPluginStep(plugin, step, pc));
14 ✔
141
        } else {
1 ✔
142
          this.context.debug("Skipping installation of plugin '{}' due to existing marker file: {}", plugin.name(), pluginMarkerFile);
×
143
        }
144
      } else {
145
        if (!pluginMarkerFileExists) {
2 !
146
          handleInstallForInactivePlugin(plugin);
3 ✔
147
        }
148
      }
149
    }
1 ✔
150
  }
1 ✔
151

152
  private void doInstallPluginStep(ToolPluginDescriptor plugin, Step step, ProcessContext pc) {
153
    boolean result = installPlugin(plugin, step, pc);
6 ✔
154
    if (result) {
2 !
155
      createPluginMarkerFile(plugin);
3 ✔
156
    }
157
  }
1 ✔
158

159
  /**
160
   * @param plugin the {@link ToolPluginDescriptor plugin} to search for.
161
   * @return Path to the plugin marker file.
162
   */
163
  public Path retrievePluginMarkerFilePath(ToolPluginDescriptor plugin) {
164
    if (this.context.getIdeHome() != null) {
4 !
165
      return this.context.getIdeHome().resolve(IdeContext.FOLDER_DOT_IDE)
7 ✔
166
          .resolve("plugin" + "." + getName() + "." + getInstalledEdition() + "." + plugin.name());
7 ✔
167
    }
168
    return null;
×
169
  }
170

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

185
  /**
186
   * @param plugin the {@link ToolPluginDescriptor} to install.
187
   * @param step the {@link Step} for the plugin installation.
188
   * @param pc the {@link ProcessContext} to use.
189
   * @return boolean true if the installation of the plugin succeeded, false if not.
190
   */
191
  public abstract boolean installPlugin(ToolPluginDescriptor plugin, Step step, ProcessContext pc);
192

193
  /**
194
   * @param plugin the {@link ToolPluginDescriptor} to install.
195
   * @param step the {@link Step} for the plugin installation.
196
   */
197
  public void installPlugin(ToolPluginDescriptor plugin, final Step step) {
198
    ProcessContext pc = this.context.newProcess().errorHandling(ProcessErrorHandling.THROW_CLI);
6 ✔
199
    install(true, getConfiguredVersion(), pc, null);
8 ✔
200
    installPlugin(plugin, step, pc);
6 ✔
201
  }
1 ✔
202

203
  /**
204
   * @param plugin the {@link ToolPluginDescriptor} to uninstall.
205
   */
206
  public void uninstallPlugin(ToolPluginDescriptor plugin) {
207

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

230
  /**
231
   * @param key the filename of the properties file configuring the requested plugin (typically excluding the ".properties" extension).
232
   * @return the {@link ToolPluginDescriptor} for the given {@code key}.
233
   */
234
  public ToolPluginDescriptor getPlugin(String key) {
235

236
    if (key == null) {
2 !
237
      return null;
×
238
    }
239
    if (key.endsWith(IdeContext.EXT_PROPERTIES)) {
4 !
240
      key = key.substring(0, key.length() - IdeContext.EXT_PROPERTIES.length());
×
241
    }
242

243
    ToolPlugins toolPlugins = getPlugins();
3 ✔
244
    ToolPluginDescriptor pluginDescriptor = toolPlugins.getByName(key);
4 ✔
245
    if (pluginDescriptor == null) {
2 !
246
      throw new CliException(
×
247
          "Could not find plugin " + key + " at " + getPluginsConfigPath().resolve(key) + ".properties");
×
248
    }
249
    return pluginDescriptor;
2 ✔
250
  }
251

252
  /**
253
   * @param plugin the in{@link ToolPluginDescriptor#active() active} {@link ToolPluginDescriptor} that is skipped for regular plugin installation.
254
   */
255
  protected void handleInstallForInactivePlugin(ToolPluginDescriptor plugin) {
256

257
    this.context.debug("Omitting installation of inactive plugin {} ({}).", plugin.name(), plugin.id());
16 ✔
258
  }
1 ✔
259
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc