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

devonfw / IDEasy / 15217656699

23 May 2025 07:22PM UTC coverage: 67.562% (-0.3%) from 67.89%
15217656699

push

github

web-flow
#1332: fixed bug pattern, proper Step usage, allow running tool if plugin failed (#1334)

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

3151 of 5064 branches covered (62.22%)

Branch coverage included in aggregate %.

8048 of 11512 relevant lines covered (69.91%)

3.07 hits per line

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

82.48
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
  public ToolPlugins getPlugins() {
39

40
    if (this.plugins == null) {
3✔
41
      ToolPlugins toolPlugins = new ToolPlugins(this.context);
6✔
42

43
      // Load project-specific plugins
44
      Path pluginsPath = getPluginsConfigPath();
3✔
45
      loadPluginsFromDirectory(toolPlugins, pluginsPath);
4✔
46

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

52
      this.plugins = toolPlugins;
3✔
53
    }
54

55
    return this.plugins;
3✔
56
  }
57

58
  private void loadPluginsFromDirectory(ToolPlugins map, Path pluginsPath) {
59

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

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

73
    return false;
2✔
74
  }
75

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

81
    return this.context.getSettingsPath().resolve(this.tool).resolve(IdeContext.FOLDER_PLUGINS);
9✔
82
  }
83

84
  private Path getUserHomePluginsConfigPath() {
85

86
    return this.context.getUserHomeIde().resolve("settings").resolve(this.tool).resolve(IdeContext.FOLDER_PLUGINS);
11✔
87
  }
88

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

94
    return this.context.getPluginsPath().resolve(this.tool);
7✔
95
  }
96

97
  @Override
98
  protected void postInstall(boolean newlyInstalled, ProcessContext pc) {
99

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

117
  private void installPlugins(ProcessContext pc) {
118
    installPlugins(getPlugins().getPlugins(), pc);
6✔
119
  }
1✔
120

121
  /**
122
   * Method to install active plugins or to handle install for inactive plugins
123
   *
124
   * @param plugins as {@link Collection} of plugins to install.
125
   * @param pc the {@link ProcessContext} to use.
126
   */
127
  protected void installPlugins(Collection<ToolPluginDescriptor> plugins, ProcessContext pc) {
128
    for (ToolPluginDescriptor plugin : plugins) {
10✔
129
      if (plugin.active()) {
3✔
130
        if (!this.context.isForcePlugins() && retrievePluginMarkerFilePath(plugin) != null && Files.exists(retrievePluginMarkerFilePath(plugin))) {
15!
131
          this.context.debug("Markerfile for IDE: {} and active plugin: {} already exists.", getName(), plugin.name());
17✔
132
        } else {
133
          Step step = this.context.newStep("Install plugin " + plugin.name());
7✔
134
          step.run(() -> doInstallPluginStep(plugin, step, pc));
14✔
135
        }
1✔
136
      } else {
137
        if (retrievePluginMarkerFilePath(plugin) != null && Files.exists(retrievePluginMarkerFilePath(plugin))) {
11!
138
          this.context.debug("Markerfile for IDE: {} and inactive plugin: {} already exists.", getName(), plugin.name());
×
139
        } else {
140
          handleInstallForInactivePlugin(plugin);
3✔
141
        }
142
      }
143
    }
1✔
144
  }
1✔
145

146
  private void doInstallPluginStep(ToolPluginDescriptor plugin, Step step, ProcessContext pc) {
147
    boolean result = installPlugin(plugin, step, pc);
6✔
148
    if (result) {
2!
149
      createPluginMarkerFile(plugin);
3✔
150
    }
151
  }
1✔
152

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

165
  /**
166
   * Creates a marker file for a plugin in $IDE_HOME/.ide/plugin.«ide».«plugin-name»
167
   *
168
   * @param plugin the {@link ToolPluginDescriptor plugin} for which the marker file should be created.
169
   */
170
  public void createPluginMarkerFile(ToolPluginDescriptor plugin) {
171
    if (this.context.getIdeHome() != null) {
4!
172
      Path hiddenIdePath = this.context.getIdeHome().resolve(IdeContext.FOLDER_DOT_IDE);
6✔
173
      this.context.getFileAccess().mkdirs(hiddenIdePath);
5✔
174
      this.context.getFileAccess().touch(hiddenIdePath.resolve("plugin" + "." + getName() + "." + getInstalledEdition() + "." + plugin.name()));
13✔
175
    }
176
  }
1✔
177

178
  /**
179
   * @param plugin the {@link ToolPluginDescriptor} to install.
180
   * @param step the {@link Step} for the plugin installation.
181
   * @param pc the {@link ProcessContext} to use.
182
   * @return boolean true if the installation of the plugin succeeded, false if not.
183
   */
184
  public abstract boolean installPlugin(ToolPluginDescriptor plugin, Step step, ProcessContext pc);
185

186
  /**
187
   * @param plugin the {@link ToolPluginDescriptor} to install.
188
   * @param step the {@link Step} for the plugin installation.
189
   */
190
  public void installPlugin(ToolPluginDescriptor plugin, final Step step) {
191
    ProcessContext pc = this.context.newProcess().errorHandling(ProcessErrorHandling.THROW_CLI);
6✔
192
    install(true, pc, null);
6✔
193
    installPlugin(plugin, step, pc);
6✔
194
  }
1✔
195

196
  /**
197
   * @param plugin the {@link ToolPluginDescriptor} to uninstall.
198
   */
199
  public void uninstallPlugin(ToolPluginDescriptor plugin) {
200

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

223
  /**
224
   * @param key the filename of the properties file configuring the requested plugin (typically excluding the ".properties" extension).
225
   * @return the {@link ToolPluginDescriptor} for the given {@code key}.
226
   */
227
  public ToolPluginDescriptor getPlugin(String key) {
228

229
    if (key == null) {
2!
230
      return null;
×
231
    }
232
    if (key.endsWith(IdeContext.EXT_PROPERTIES)) {
4!
233
      key = key.substring(0, key.length() - IdeContext.EXT_PROPERTIES.length());
×
234
    }
235

236
    ToolPlugins toolPlugins = getPlugins();
3✔
237
    ToolPluginDescriptor pluginDescriptor = toolPlugins.getByName(key);
4✔
238
    if (pluginDescriptor == null) {
2!
239
      throw new CliException(
×
240
          "Could not find plugin " + key + " at " + getPluginsConfigPath().resolve(key) + ".properties");
×
241
    }
242
    return pluginDescriptor;
2✔
243
  }
244

245
  /**
246
   * @param plugin the in{@link ToolPluginDescriptor#active() active} {@link ToolPluginDescriptor} that is skipped for regular plugin installation.
247
   */
248
  protected void handleInstallForInactivePlugin(ToolPluginDescriptor plugin) {
249

250
    this.context.debug("Omitting installation of inactive plugin {} ({}).", plugin.name(), plugin.id());
16✔
251
  }
1✔
252
}
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