Skip to content

Commit d5a355e

Browse files
author
Rob Rudin
committed
#165 Now supporting SJS services and transforms
1 parent f088177 commit d5a355e

File tree

3 files changed

+56
-11
lines changed

3 files changed

+56
-11
lines changed

src/main/groovy/com/marklogic/gradle/MarkLogicPlugin.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ class MarkLogicPlugin implements Plugin<Project> {
117117

118118
String devGroup = "ml-gradle Development"
119119
project.task("mlScaffold", type: GenerateScaffoldTask, group: devGroup, description: "Generate project scaffold for a new project")
120-
project.task("mlCreateResource", type: CreateResourceTask, group: devGroup, description: "Create a new resource extension in the modules services directory")
121-
project.task("mlCreateTransform", type: CreateTransformTask, group: devGroup, description: "Create a new transform in the modules transforms directory")
120+
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)")
121+
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)")
122122
project.task("mlPrepareRestApiDependencies", type: PrepareRestApiDependenciesTask, group: devGroup, dependsOn: project.configurations["mlRestApi"], description: "Downloads (if necessary) and unzips in the build directory all mlRestApi dependencies")
123123

124124
String esGroup = "ml-gradle Entity Services"

src/main/groovy/com/marklogic/gradle/task/client/CreateResourceTask.groovy

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,29 @@ import com.marklogic.gradle.task.MarkLogicTask
66

77
class CreateResourceTask extends MarkLogicTask {
88

9+
final static String SJS_RESOURCE_TEMPLATE =
10+
'''function get(context, params) {
11+
// return zero or more document nodes
12+
};
13+
14+
function post(context, params, input) {
15+
// return zero or more document nodes
16+
};
17+
18+
function put(context, params, input) {
19+
// return at most one document node
20+
};
21+
22+
function deleteFunction(context, params) {
23+
// return at most one document node
24+
};
25+
26+
exports.GET = get;
27+
exports.POST = post;
28+
exports.PUT = put;
29+
exports.DELETE = deleteFunction;
30+
'''
31+
932
final static String RESOURCE_TEMPLATE = '''xquery version "1.0-ml";
1033
1134
module namespace resource = "http://marklogic.com/rest-api/resource/%%RESOURCE_NAME%%";
@@ -65,15 +88,22 @@ declare function delete(
6588

6689
@TaskAction
6790
void createResource() {
68-
String propName = "resourceName"
91+
String propName = "resourceName"
6992
if (getProject().hasProperty(propName)) {
7093
servicesDir = servicesDir ? servicesDir : getAppConfig().getModulePaths().get(0) + "/services"
7194

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

74-
String resource = RESOURCE_TEMPLATE.replace("%%RESOURCE_NAME%%", name)
97+
String template = RESOURCE_TEMPLATE
98+
String fileExtension = ".xqy"
99+
if (getProject().hasProperty("resourceType") && "sjs".equals(getProject().getProperties().get("resourceType"))) {
100+
template = SJS_RESOURCE_TEMPLATE
101+
fileExtension = ".sjs"
102+
}
103+
104+
String resource = template.replace("%%RESOURCE_NAME%%", name)
75105
new File(servicesDir).mkdirs()
76-
def resourceFile = new File(servicesDir, name + ".xqy")
106+
def resourceFile = new File(servicesDir, name + fileExtension)
77107
println "Creating new resource at " + resourceFile.getAbsolutePath()
78108
resourceFile.write(resource)
79109

@@ -84,7 +114,7 @@ declare function delete(
84114
println "Creating new resource metadata file at " + metadataFile.getAbsolutePath()
85115
metadataFile.write(metadata)
86116
} else {
87-
println "Use -PresourceName=your-resource-name when invoking Gradle to specify a resource name"
117+
println "Use -PresourceName=your-resource-name -PresourceType=(xqy|sjs) when invoking Gradle to specify a resource name"
88118
}
89119
}
90120
}

src/main/groovy/com/marklogic/gradle/task/client/CreateTransformTask.groovy

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ declare function transform(
1919
{
2020
()
2121
};
22+
'''
23+
24+
final static String SJS_TEMPLATE =
25+
'''function transform(context, params, content)
26+
{
27+
// Must return the result of the transform
28+
};
29+
exports.transform = transform;
2230
'''
2331

2432
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">
@@ -50,19 +58,26 @@ declare function transform(
5058

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

53-
String defaultType = "xqy"
5461
String type = "xqy"
5562
String propType = "transformType"
5663
if (getProject().hasProperty(propType)) {
5764
type = getProject().getProperties().get(propType)
5865
}
5966

60-
String template = type == "xqy" ? XQUERY_TEMPLATE : XSL_TEMPLATE
61-
String suffix = type == "xqy" ? ".xqy" : ".xsl"
67+
String template = XQUERY_TEMPLATE
68+
String fileExtension = ".xqy"
69+
if ("xsl".equals(type)) {
70+
template = XSL_TEMPLATE
71+
fileExtension = ".xsl"
72+
} else if ("sjs".equals(type)) {
73+
template = SJS_TEMPLATE
74+
fileExtension = ".sjs"
75+
}
76+
6277
String transform = template.replace("%%TRANSFORM_NAME%%", name)
6378

6479
new File(transformsDir).mkdirs()
65-
def transformFile = new File(transformsDir, name + suffix)
80+
def transformFile = new File(transformsDir, name + fileExtension)
6681
println "Creating new transform at " + transformFile.getAbsolutePath()
6782
transformFile.write(transform)
6883

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

0 commit comments

Comments
 (0)