|
| 1 | +package org.streamflow.plugins; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.JsonNode; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import org.streamflow.bridge.RPaths; |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | +import java.nio.file.Files; |
| 9 | +import java.nio.file.Path; |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.List; |
| 12 | +import java.util.stream.Stream; |
| 13 | + |
| 14 | +/** |
| 15 | + * Discovers R plugins by scanning for {@code <root>/engine/plugins/<id>/plugin.json}. |
| 16 | + * |
| 17 | + * Plugins live in this repo and are bundled only into an R-enabled build (see release.yml's |
| 18 | + * {@code bundle_r}); the default installer is Python-only, so {@link #rAvailable()} is what the UI |
| 19 | + * uses to decide whether to offer them at all. Discovery itself never needs R, which keeps it |
| 20 | + * unit-testable on a machine without an R install. |
| 21 | + */ |
| 22 | +public final class PluginRegistry { |
| 23 | + |
| 24 | + private static final ObjectMapper JSON = new ObjectMapper(); |
| 25 | + |
| 26 | + private PluginRegistry() {} |
| 27 | + |
| 28 | + /** Candidate roots: the packaged app dir first, then the dev repo checkout. */ |
| 29 | + static List<Path> pluginRoots() { |
| 30 | + List<Path> roots = new ArrayList<>(); |
| 31 | + try { roots.add(RPaths.appDir().resolve("engine").resolve("plugins")); } catch (Exception ignored) {} |
| 32 | + try { roots.add(RPaths.repoRoot().resolve("engine").resolve("plugins")); } catch (Exception ignored) {} |
| 33 | + return roots; |
| 34 | + } |
| 35 | + |
| 36 | + /** True when an Rscript executable can be resolved (bundled R-Portable, R_HOME, a system R, or PATH). */ |
| 37 | + public static boolean rAvailable() { |
| 38 | + try { |
| 39 | + Path rs = RPaths.rscript(); |
| 40 | + // A bare "Rscript"/"Rscript.exe" means "hope it's on PATH" — only trust an absolute hit. |
| 41 | + return rs != null && rs.isAbsolute() && Files.exists(rs); |
| 42 | + } catch (Exception e) { |
| 43 | + return false; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /** All plugins found under the first existing root. Never throws; a bad manifest is skipped. */ |
| 48 | + public static List<PluginManifest> discover() { |
| 49 | + for (Path root : pluginRoots()) { |
| 50 | + if (root != null && Files.isDirectory(root)) return discoverIn(root); |
| 51 | + } |
| 52 | + return List.of(); |
| 53 | + } |
| 54 | + |
| 55 | + /** Parse the {@code plugin.json} of every sub-directory. Malformed manifests are skipped, not fatal. */ |
| 56 | + public static List<PluginManifest> discoverIn(Path root) { |
| 57 | + List<PluginManifest> out = new ArrayList<>(); |
| 58 | + try (Stream<Path> dirs = Files.list(root)) { |
| 59 | + for (Path dir : dirs.filter(Files::isDirectory).toList()) { |
| 60 | + Path manifest = dir.resolve("plugin.json"); |
| 61 | + if (!Files.isRegularFile(manifest)) continue; |
| 62 | + try { |
| 63 | + out.add(parse(dir, manifest)); |
| 64 | + } catch (Exception ignored) { |
| 65 | + // A single broken plugin must not hide the healthy ones. |
| 66 | + } |
| 67 | + } |
| 68 | + } catch (IOException ignored) { } |
| 69 | + out.sort(java.util.Comparator.comparing(PluginManifest::name, String.CASE_INSENSITIVE_ORDER)); |
| 70 | + return out; |
| 71 | + } |
| 72 | + |
| 73 | + static PluginManifest parse(Path dir, Path manifestFile) throws IOException { |
| 74 | + JsonNode n = JSON.readTree(Files.readString(manifestFile)); |
| 75 | + JsonNode entry = n.path("entry"); |
| 76 | + String script = entry.path("script").asText(null); |
| 77 | + String function = entry.path("function").asText(null); |
| 78 | + if (script == null || function == null) { |
| 79 | + throw new IOException("plugin.json must declare entry.script and entry.function"); |
| 80 | + } |
| 81 | + String id = n.path("id").asText(dir.getFileName().toString()); |
| 82 | + |
| 83 | + List<String> pkgs = new ArrayList<>(); |
| 84 | + n.path("requires").path("packages").forEach(p -> pkgs.add(p.asText())); |
| 85 | + |
| 86 | + List<PluginManifest.Param> params = new ArrayList<>(); |
| 87 | + for (JsonNode p : n.path("params")) { |
| 88 | + params.add(new PluginManifest.Param( |
| 89 | + p.path("id").asText(), |
| 90 | + p.path("label").asText(p.path("id").asText()), |
| 91 | + p.path("type").asText("string"), |
| 92 | + p.path("required").asBoolean(false), |
| 93 | + p.path("help").asText(null), |
| 94 | + p.has("min") ? p.path("min").asDouble() : null, |
| 95 | + p.has("max") ? p.path("max").asDouble() : null, |
| 96 | + p.has("default") ? toPlain(p.path("default")) : null)); |
| 97 | + } |
| 98 | + |
| 99 | + List<String> outputs = new ArrayList<>(); |
| 100 | + n.path("outputs").forEach(o -> outputs.add(o.asText())); |
| 101 | + |
| 102 | + return new PluginManifest(dir, id, |
| 103 | + n.path("name").asText(id), |
| 104 | + n.path("version").asText("0.0.0"), |
| 105 | + n.path("description").asText(""), |
| 106 | + n.path("status").asText("experimental"), |
| 107 | + script, function, pkgs, params, outputs); |
| 108 | + } |
| 109 | + |
| 110 | + private static Object toPlain(JsonNode v) { |
| 111 | + if (v.isBoolean()) return v.asBoolean(); |
| 112 | + if (v.isInt() || v.isLong()) return v.asLong(); |
| 113 | + if (v.isNumber()) return v.asDouble(); |
| 114 | + return v.asText(); |
| 115 | + } |
| 116 | +} |
0 commit comments