Skip to content

Commit

Permalink
#165 Now supporting SJS services and transforms
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Rudin committed Mar 3, 2017
1 parent f088177 commit d5a355e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/main/groovy/com/marklogic/gradle/MarkLogicPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ class MarkLogicPlugin implements Plugin<Project> {

String devGroup = "ml-gradle Development"
project.task("mlScaffold", type: GenerateScaffoldTask, group: devGroup, description: "Generate project scaffold for a new project")
project.task("mlCreateResource", type: CreateResourceTask, group: devGroup, description: "Create a new resource extension in the modules services directory")
project.task("mlCreateTransform", type: CreateTransformTask, group: devGroup, description: "Create a new transform in the modules transforms directory")
project.task("mlCreateResource", type: CreateResourceTask, group: devGroup, description: "Create a new resource extension in the modules services directory; use -PresourceName and -PresourceType to set the resource name and type (either xqy or sjs)")
project.task("mlCreateTransform", type: CreateTransformTask, group: devGroup, description: "Create a new transform in the modules transforms directory; use -PtranssformName and -PtransformType to set the transform name and type (xqy, xsl, or sjs)")
project.task("mlPrepareRestApiDependencies", type: PrepareRestApiDependenciesTask, group: devGroup, dependsOn: project.configurations["mlRestApi"], description: "Downloads (if necessary) and unzips in the build directory all mlRestApi dependencies")

String esGroup = "ml-gradle Entity Services"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,29 @@ import com.marklogic.gradle.task.MarkLogicTask

class CreateResourceTask extends MarkLogicTask {

final static String SJS_RESOURCE_TEMPLATE =
'''function get(context, params) {
// return zero or more document nodes
};
function post(context, params, input) {
// return zero or more document nodes
};
function put(context, params, input) {
// return at most one document node
};
function deleteFunction(context, params) {
// return at most one document node
};
exports.GET = get;
exports.POST = post;
exports.PUT = put;
exports.DELETE = deleteFunction;
'''

final static String RESOURCE_TEMPLATE = '''xquery version "1.0-ml";
module namespace resource = "http://marklogic.com/rest-api/resource/%%RESOURCE_NAME%%";
Expand Down Expand Up @@ -65,15 +88,22 @@ declare function delete(

@TaskAction
void createResource() {
String propName = "resourceName"
String propName = "resourceName"
if (getProject().hasProperty(propName)) {
servicesDir = servicesDir ? servicesDir : getAppConfig().getModulePaths().get(0) + "/services"

String name = getProject().getProperties().get(propName)

String resource = RESOURCE_TEMPLATE.replace("%%RESOURCE_NAME%%", name)
String template = RESOURCE_TEMPLATE
String fileExtension = ".xqy"
if (getProject().hasProperty("resourceType") && "sjs".equals(getProject().getProperties().get("resourceType"))) {
template = SJS_RESOURCE_TEMPLATE
fileExtension = ".sjs"
}

String resource = template.replace("%%RESOURCE_NAME%%", name)
new File(servicesDir).mkdirs()
def resourceFile = new File(servicesDir, name + ".xqy")
def resourceFile = new File(servicesDir, name + fileExtension)
println "Creating new resource at " + resourceFile.getAbsolutePath()
resourceFile.write(resource)

Expand All @@ -84,7 +114,7 @@ declare function delete(
println "Creating new resource metadata file at " + metadataFile.getAbsolutePath()
metadataFile.write(metadata)
} else {
println "Use -PresourceName=your-resource-name when invoking Gradle to specify a resource name"
println "Use -PresourceName=your-resource-name -PresourceType=(xqy|sjs) when invoking Gradle to specify a resource name"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ declare function transform(
{
()
};
'''

final static String SJS_TEMPLATE =
'''function transform(context, params, content)
{
// Must return the result of the transform
};
exports.transform = transform;
'''

final static String XSL_TEMPLATE = '''<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:map="http://marklogic.com/xdmp/map">
Expand Down Expand Up @@ -50,19 +58,26 @@ declare function transform(

String name = getProject().getProperties().get(propName)

String defaultType = "xqy"
String type = "xqy"
String propType = "transformType"
if (getProject().hasProperty(propType)) {
type = getProject().getProperties().get(propType)
}

String template = type == "xqy" ? XQUERY_TEMPLATE : XSL_TEMPLATE
String suffix = type == "xqy" ? ".xqy" : ".xsl"
String template = XQUERY_TEMPLATE
String fileExtension = ".xqy"
if ("xsl".equals(type)) {
template = XSL_TEMPLATE
fileExtension = ".xsl"
} else if ("sjs".equals(type)) {
template = SJS_TEMPLATE
fileExtension = ".sjs"
}

String transform = template.replace("%%TRANSFORM_NAME%%", name)

new File(transformsDir).mkdirs()
def transformFile = new File(transformsDir, name + suffix)
def transformFile = new File(transformsDir, name + fileExtension)
println "Creating new transform at " + transformFile.getAbsolutePath()
transformFile.write(transform)

Expand All @@ -73,7 +88,7 @@ declare function transform(
println "Creating new transform metadata file at " + metadataFile.getAbsolutePath()
metadataFile.write(metadata)
} else {
println "Use -PtransformName=your-transform-name [-PtransformType=(xqy|xslt)] when invoking Gradle to specify a transform name"
println "Use -PtransformName=your-transform-name [-PtransformType=(xqy|xsl|sjs)] when invoking Gradle to specify a transform name"
}
}
}

0 comments on commit d5a355e

Please sign in to comment.