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

devonfw / IDEasy / 13772844222

10 Mar 2025 07:10PM UTC coverage: 68.619% (+0.1%) from 68.471%
13772844222

push

github

web-flow
#654: improved plugin suppport (#1085)

Co-authored-by: Jörg Hohwiller <hohwille@users.noreply.github.com>

3067 of 4915 branches covered (62.4%)

Branch coverage included in aggregate %.

7934 of 11117 relevant lines covered (71.37%)

3.11 hits per line

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

81.82
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 (retrievePluginMarkerFilePath(plugin) != null && Files.exists(retrievePluginMarkerFilePath(plugin))) {
11!
131
          this.context.debug("Markerfile for IDE: {} and active plugin: {} already exists.", getName(), plugin.name());
17✔
132
        } else {
133
          try (Step step = this.context.newStep("Install plugin " + plugin.name())) {
8✔
134
            boolean result = installPlugin(plugin, step, pc);
6✔
135
            if (result) {
2!
136
              createPluginMarkerFile(plugin);
3✔
137
            }
138
          }
139
        }
140
      } else {
141
        if (retrievePluginMarkerFilePath(plugin) != null && Files.exists(retrievePluginMarkerFilePath(plugin))) {
11!
142
          this.context.debug("Markerfile for IDE: {} and inactive plugin: {} already exists.", getName(), plugin.name());
×
143
        } else {
144
          handleInstallForInactivePlugin(plugin);
3✔
145
        }
146
      }
147
    }
1✔
148
  }
1✔
149

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

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

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

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

193
  /**
194
   * @param plugin the {@link ToolPluginDescriptor} to uninstall.
195
   */
196
  public void uninstallPlugin(ToolPluginDescriptor plugin) {
197

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

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

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

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

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

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

© 2025 Coveralls, Inc