diff --git a/src/main/groovy/com/marklogic/gradle/task/PreviewDeployTask.groovy b/src/main/groovy/com/marklogic/gradle/task/PreviewDeployTask.groovy index b041428c1..bd7258313 100644 --- a/src/main/groovy/com/marklogic/gradle/task/PreviewDeployTask.groovy +++ b/src/main/groovy/com/marklogic/gradle/task/PreviewDeployTask.groovy @@ -1,6 +1,9 @@ package com.marklogic.gradle.task - +import com.marklogic.appdeployer.AppDeployer +import com.marklogic.appdeployer.command.Command +import com.marklogic.appdeployer.command.modules.LoadModulesCommand +import com.marklogic.appdeployer.command.schemas.LoadSchemasCommand import com.marklogic.appdeployer.command.security.DeployRolesCommand import com.marklogic.appdeployer.impl.SimpleAppDeployer import com.marklogic.mgmt.util.ObjectMapperFactory @@ -16,20 +19,51 @@ class PreviewDeployTask extends DeployAppTask { @TaskAction void deployApp() { + modifyAppConfigBeforePreview() + modifyAppDeployerBeforePreview() + + PreviewInterceptor interceptor = configurePreviewInterceptor() + + super.deployApp() + + println "\nPREVIEW OF DEPLOYMENT:\n" + println ObjectMapperFactory.getObjectMapper().writeValueAsString(interceptor.getResults()) + } + + void modifyAppConfigBeforePreview() { // Disable loading of any modules getAppConfig().setModulePaths(new ArrayList()) - // Disable loading of any schemas + // Disable loading of schemas from the default path + // Database-specific schema paths are handled by removing instances of LoadSchemasCommand getAppConfig().setSchemasPath(null) + } + + void modifyAppDeployerBeforePreview() { + AppDeployer deployer = getAppDeployer() - SimpleAppDeployer deployer = getAppDeployer() + if (deployer instanceof SimpleAppDeployer) { + SimpleAppDeployer simpleAppDeployer = (SimpleAppDeployer) deployer - // Loading roles in two phases breaks the preview feature, so it's disabled - DeployRolesCommand deployRolesCommand = deployer.getCommandOfType(DeployRolesCommand.class) - if (deployRolesCommand != null) { - deployRolesCommand.setDeployRolesInTwoPhases(false) + List newCommands = new ArrayList<>() + for (Command c : simpleAppDeployer.getCommands()) { + if (c instanceof LoadSchemasCommand || c instanceof LoadModulesCommand) { + // Don't include these; no need to load schemas or modules during a preview + } + // Loading roles in two phases breaks the preview feature, so it's disabled + else if (c instanceof DeployRolesCommand) { + DeployRolesCommand deployRolesCommand = (DeployRolesCommand) c + deployRolesCommand.setDeployRolesInTwoPhases(false) + newCommands.add(c) + } else { + newCommands.add(c) + } + } + simpleAppDeployer.setCommands(newCommands) } + } + PreviewInterceptor configurePreviewInterceptor() { PreviewInterceptor interceptor = new PreviewInterceptor(getManageClient()) getManageClient().getRestTemplate().getInterceptors().add(interceptor) getManageClient().getRestTemplate().setErrorHandler(interceptor) @@ -37,11 +71,6 @@ class PreviewDeployTask extends DeployAppTask { getManageClient().getSecurityUserRestTemplate().getInterceptors().add(interceptor) getManageClient().getSecurityUserRestTemplate().setErrorHandler(interceptor) } - - super.deployApp() - - println "\nPREVIEW OF DEPLOYMENT:\n" - println ObjectMapperFactory.getObjectMapper().writeValueAsString(interceptor.getResults()) + return interceptor } - }