Skip to content

Commit

Permalink
Implement AbstractInstallCommandTask
Browse files Browse the repository at this point in the history
  • Loading branch information
eschleb committed Dec 13, 2024
1 parent 5320a4d commit 17db109
Showing 1 changed file with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.merkle.oss.magnolia.setup.task.common;

import info.magnolia.commands.MgnlCommand;
import info.magnolia.jcr.nodebuilder.NodeOperation;
import info.magnolia.jcr.nodebuilder.task.ErrorHandling;
import info.magnolia.jcr.util.NodeTypes;
import info.magnolia.module.InstallContext;
import info.magnolia.repository.RepositoryConstants;

import java.lang.invoke.MethodHandles;
import java.text.MessageFormat;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.merkle.oss.magnolia.powernode.NodeOperationFactory;
import com.merkle.oss.magnolia.setup.task.nodebuilder.AbstractPathNodeBuilderTask;

/**
* Base class for command install tasks.
*/
public abstract class AbstractInstallCommandTask extends AbstractPathNodeBuilderTask {
private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

public static final String MODULE_PATH = "modules/{0}";

private final NodeOperationFactory ops;
private final String catalog;
private final String name;
private final Class<? extends MgnlCommand> clazz;

protected AbstractInstallCommandTask(
final NodeOperationFactory nodeOperationFactory,
final String catalog,
final String name,
final Class<? extends MgnlCommand> clazz
) {
super("Install command " + name, "Install command " + name + " in catalog " + catalog, ErrorHandling.strict, RepositoryConstants.CONFIG);
this.ops = nodeOperationFactory;
this.catalog = catalog;
this.name = name;
this.clazz = clazz;
}

@Override
protected NodeOperation[] getNodeOperations(final InstallContext ctx) {
final String moduleName = ctx.getCurrentModuleDefinition().getName();
final String modulePath = MessageFormat.format(MODULE_PATH, moduleName);
LOG.info("installing command '{}' for module {}", name, modulePath);
return new NodeOperation[]{
ops.getOrAddNode(modulePath, NodeTypes.Content.NAME).then(
ops.getOrAddNode("commands", NodeTypes.Content.NAME).then(
ops.getOrAddNode(catalog, NodeTypes.Content.NAME).then(
ops.getOrAddContentNode(name).then(
ops.setClassProperty(clazz)
)
)
)
)
};
}
}

0 comments on commit 17db109

Please sign in to comment.