mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-28 08:44:50 +00:00
Add the platform-neutral command api for module tooling
A module registers a root Command through ToolingRegistry.registerCommand: typed Arguments (word, text, integer, decimal, flag, choice, resource, registered), sub-commands, and a CommandAction that runs against a CommandContext with a ToolPack, a Sender and the platform's registries. ToolPack.edit is the one way to change pack files; ObjectCodec reads and writes the host's object format. The host catalogues the root commands and fails a pack load when two modules claim one root name or alias, naming both.
This commit is contained in:
1 parent
480e5485ac
commit
fc81e54b0c
16 files changed
+874
-5
No files matched your search
@@ -0,0 +1,19 @@
|
||||
package com.volmit.host;
|
||||
|
||||
import com.volmit.tooling.Command;
|
||||
import java.util.Objects;
|
||||
|
||||
/** A root command and the module that registered it, which the host names when two modules claim one root name and
|
||||
* which owns the log and the failures of the command's actions.
|
||||
*/
|
||||
public record CommandRegistration(Command command, DiscoveredModule module) {
|
||||
public CommandRegistration {
|
||||
Objects.requireNonNull(command, "command");
|
||||
Objects.requireNonNull(module, "module");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return command + " of module " + module;
|
||||
}
|
||||
}
|
||||
@@ -39,6 +39,7 @@ public final class FeatureCatalog {
|
||||
private final Map<Class<?>, List<?>> providersByService;
|
||||
private final Optional<PlacementTypeRegistration> placementType;
|
||||
private final List<StudioViewRegistration> studioViews;
|
||||
private final List<CommandRegistration> commands;
|
||||
|
||||
FeatureCatalog(
|
||||
Map<Class<?>, FeatureDescriptor<?>> settings,
|
||||
@@ -48,9 +49,11 @@ public final class FeatureCatalog {
|
||||
List<LifecycleRegistration> lifecycles,
|
||||
List<ServiceRegistration> services,
|
||||
PlacementTypeRegistration placementType,
|
||||
Collection<StudioViewRegistration> studioViews) {
|
||||
Collection<StudioViewRegistration> studioViews,
|
||||
Collection<CommandRegistration> commands) {
|
||||
this.placementType = Optional.ofNullable(placementType);
|
||||
this.studioViews = sorted(studioViews, registration -> registration.view().id());
|
||||
this.commands = sorted(commands, registration -> registration.command().name());
|
||||
this.settings = Map.copyOf(settings);
|
||||
this.mantle = Map.copyOf(mantle);
|
||||
this.resourceKinds = sorted(resourceKinds, registration -> registration.kind().id());
|
||||
@@ -89,6 +92,11 @@ public final class FeatureCatalog {
|
||||
return studioViews;
|
||||
}
|
||||
|
||||
/** The root commands modules registered, by command name; hosts with tooling mount them, other hosts ignore them. */
|
||||
public List<CommandRegistration> commands() {
|
||||
return commands;
|
||||
}
|
||||
|
||||
/** The lifecycle hooks modules registered, by module id, each module's in the order it registered them. */
|
||||
public List<LifecycleRegistration> lifecycles() {
|
||||
return lifecycles;
|
||||
|
||||
@@ -10,6 +10,7 @@ import com.volmit.mantle.MantleLifetime;
|
||||
import com.volmit.resource.ResourceId;
|
||||
import com.volmit.resource.ResourceKind;
|
||||
import com.volmit.service.Providers;
|
||||
import com.volmit.tooling.Command;
|
||||
import com.volmit.tooling.StudioView;
|
||||
import com.volmit.tooling.ToolingRegistry;
|
||||
import com.volmit.validation.Validator;
|
||||
@@ -30,7 +31,8 @@ import java.util.Set;
|
||||
* settings ids must be the registering module's id or start with it and '/' ({@link com.volmit.ModuleInfo#ownsId}).
|
||||
* Resource kinds have unique ids, types and folders, and no kind may read a folder the host reserves for its own
|
||||
* files. Lifecycle hooks and service providers are registered by the module that owns them; a service whose providers
|
||||
* are {@link Providers#ONE} has at most one. Studio views have ids unique across their kinds that the registering module owns. A failed
|
||||
* are {@link Providers#ONE} has at most one. Studio views have ids unique across their kinds that the registering module owns.
|
||||
* Root commands have names and aliases unique across the pack's modules. A failed
|
||||
* registration leaves the registry unchanged. A failed module callback invalidates the host's whole build. This does
|
||||
* not decode JSON, discover modules or instantiate settings.
|
||||
*/
|
||||
@@ -46,6 +48,7 @@ public final class ModuleRegistry implements FeatureRegistry, ToolingRegistry {
|
||||
private final List<LifecycleRegistration> lifecycles = new ArrayList<>();
|
||||
private final List<ServiceRegistration> services = new ArrayList<>();
|
||||
private final Map<ResourceId, StudioViewRegistration> studioViews = new HashMap<>();
|
||||
private final List<CommandRegistration> commands = new ArrayList<>();
|
||||
private final Set<String> reservedFolders;
|
||||
private final List<DiscoveredModule> registered = new ArrayList<>();
|
||||
private PlacementTypeRegistration placementType;
|
||||
@@ -194,6 +197,26 @@ public final class ModuleRegistry implements FeatureRegistry, ToolingRegistry {
|
||||
studioViews.put(id, added);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerCommand(Command command) {
|
||||
checkOpen();
|
||||
Objects.requireNonNull(command, "command");
|
||||
requireRegistering("Command " + command.name());
|
||||
var added = new CommandRegistration(command, registering);
|
||||
for (var existing : commands) {
|
||||
requireDistinctRootNames(existing, added);
|
||||
}
|
||||
commands.add(added);
|
||||
}
|
||||
|
||||
private static void requireDistinctRootNames(CommandRegistration existing, CommandRegistration added) {
|
||||
for (var word : added.command().words()) {
|
||||
if (!existing.command().answersTo(word)) continue;
|
||||
throw new IllegalArgumentException("Command root " + word + " is registered by module " + existing.module()
|
||||
+ " already, and module " + added.module() + " registers it too; give one of them another name or alias");
|
||||
}
|
||||
}
|
||||
|
||||
/** Catalogs a settings type named by a stage's when(...), whose owner {@link StageGraphBuilder}
|
||||
* has checked. Several stages, and the owner's {@link #registerSettings}, may name one type. Call
|
||||
* it after every module has registered, so the owner is known.
|
||||
@@ -215,7 +238,7 @@ public final class ModuleRegistry implements FeatureRegistry, ToolingRegistry {
|
||||
public FeatureCatalog freeze() {
|
||||
if (catalog == null) {
|
||||
catalog = new FeatureCatalog(settings, mantle, resourceKinds.values(), validators, lifecycles, services,
|
||||
placementType, studioViews.values());
|
||||
placementType, studioViews.values(), commands);
|
||||
}
|
||||
return catalog;
|
||||
}
|
||||
|
||||
@@ -132,6 +132,56 @@ class ModuleRegistryTest {
|
||||
assertTrue(views.get(1).toString().startsWith("pattern view iris:objects/footprints of module"), views.get(1).toString());
|
||||
}
|
||||
|
||||
private static IrisModule commandModule(String id, com.volmit.tooling.Command... roots) {
|
||||
return new IrisModule() {
|
||||
public ModuleInfo info() { return ModuleInfo.of(id, id, "1"); }
|
||||
public void setup(ModuleSetup setup) {
|
||||
for (var root : roots) setup.tooling().registerCommand(root);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
void rootCommandsAreCataloguedByNameAndKeepTheirModule() {
|
||||
var registry = new ModuleRegistry();
|
||||
ModuleSetups.run(DiscoveredModule.builtIn(commandModule("test:b",
|
||||
com.volmit.tooling.Command.named("zeta"))), new StageGraphBuilder(), registry);
|
||||
ModuleSetups.run(DiscoveredModule.builtIn(commandModule("test:a",
|
||||
com.volmit.tooling.Command.named("alpha").alias("a"))), new StageGraphBuilder(), registry);
|
||||
|
||||
var commands = registry.freeze().commands();
|
||||
assertEquals(List.of("alpha", "zeta"), commands.stream().map(root -> root.command().name()).toList());
|
||||
assertEquals("test:a", commands.getFirst().module().info().id().toString());
|
||||
assertThrows(IllegalStateException.class,
|
||||
() -> registry.registerCommand(com.volmit.tooling.Command.named("late")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void twoModulesClaimingOneRootNameOrAliasFailNamingBoth() {
|
||||
var registry = new ModuleRegistry();
|
||||
ModuleSetups.run(DiscoveredModule.builtIn(commandModule("test:first",
|
||||
com.volmit.tooling.Command.named("structure").alias("str"))), new StageGraphBuilder(), registry);
|
||||
|
||||
var sameName = assertThrows(IllegalArgumentException.class, () -> ModuleSetups.run(DiscoveredModule.builtIn(
|
||||
commandModule("test:second", com.volmit.tooling.Command.named("structure"))), new StageGraphBuilder(), registry));
|
||||
assertTrue(sameName.getMessage().contains("Command root structure is registered by module"), sameName.getMessage());
|
||||
assertTrue(sameName.getMessage().contains("test:first") && sameName.getMessage().contains("test:second"), sameName.getMessage());
|
||||
|
||||
var aliasOfFirst = assertThrows(IllegalArgumentException.class, () -> ModuleSetups.run(DiscoveredModule.builtIn(
|
||||
commandModule("test:third", com.volmit.tooling.Command.named("str"))), new StageGraphBuilder(), registry));
|
||||
assertTrue(aliasOfFirst.getMessage().contains("Command root str is registered by module"), aliasOfFirst.getMessage());
|
||||
assertEquals(1, registry.freeze().commands().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aPackLoadFailsWhenTwoModulesClaimOneRootName() {
|
||||
var failure = assertThrows(ModuleLoadException.class, () -> LoadedModules.load(List.of(
|
||||
DiscoveredModule.builtIn(commandModule("test:first", com.volmit.tooling.Command.named("tools"))),
|
||||
DiscoveredModule.builtIn(commandModule("test:second", com.volmit.tooling.Command.named("tools")))),
|
||||
Map.of(), List.of(), List.of()));
|
||||
assertTrue(failure.getMessage().contains("test:first") && failure.getMessage().contains("test:second"), failure.getMessage());
|
||||
}
|
||||
|
||||
private static com.volmit.tooling.PatternView patternView(String id) {
|
||||
return new com.volmit.tooling.PatternView() {
|
||||
public com.volmit.resource.ResourceId id() { return com.volmit.resource.ResourceId.parse(id); }
|
||||
|
||||
Reference in new issue
Block a user