forked from mulesoft/data-weave-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle
170 lines (152 loc) · 6.22 KB
/
build.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
buildscript {
repositories {
// mavenLocal()
mavenCentral()
maven {
url "https://plugins.gradle.org/m2/"
}
maven {
name "mule-releases"
url "https://repository.mulesoft.org/nexus/content/repositories/releases/"
}
maven {
name "mule-snapshots"
url "https://repository.mulesoft.org/nexus/content/repositories/snapshots/"
}
}
dependencies {
classpath "org.mule.weave:weave-gradle-plugin:" + weavedocVersion
classpath "com.monochromeroad.gradle-plugins:gradle-aws-s3-sync:0.10"
classpath group: 'org.mule.weave', name: 'runtime', version: weaveVersion
classpath group: 'org.mule.weave', name: 'core-modules', version: weaveVersion
classpath group: 'org.mule.weave', name: 'java-module', version: weaveVersion
classpath group: 'org.mule.weave', name: 'yaml-module', version: weaveVersion
classpath group: 'org.mule.weave', name: 'file-module', version: fileModuleVersion
}
}
apply plugin: 'java'
apply plugin: 'dataweave'
group = "org.mule.weave"
version = "0.0.1-SNAPSHOT"
repositories {
// mavenLocal()
mavenCentral()
maven {
name "mule-releases"
url "https://repository.mulesoft.org/nexus/content/repositories/releases/"
}
maven {
name "mule-snapshots"
url "https://repository.mulesoft.org/nexus/content/repositories/snapshots/"
}
}
dependencies {
testCompile group: 'org.mule.weave', name: 'runtime', version: weaveVersion
testCompile group: 'org.mule.weave', name: 'core-modules', version: weaveVersion
testCompile group: 'org.mule.weave', name: 'java-module', version: weaveVersion
testCompile group: 'org.mule.weave', name: 'data-weave-testing-framework', version: wtfVersion
testCompile group: 'org.mule.weave', name: 'yaml-module', version: weaveVersion
}
task docs(type: Zip) {
from 'src/main/docs/'
}
ext {
aws = [
accessKey: System.getenv("AWS_ACCESS_KEY_ID"),
secretKey: System.getenv("AWS_SECRET_KEY")
]
}
import com.monochromeroad.gradle.plugin.aws.s3.S3Sync
import org.mule.weave.v2.completion.DataFormatDescriptor$
import org.mule.weave.v2.completion.DataFormatProperty
import org.mule.weave.v2.editor.*
import org.mule.weave.v2.model.EmptyWeaveServicesProvider$
import org.mule.weave.v2.model.ServiceManager$
import org.mule.weave.v2.runtime.DataWeaveScriptingEngine
import org.mule.weave.v2.runtime.ScriptingBindings
import org.mule.weave.v2.ts.WeaveType
import scala.None$
import scala.Some
task packageDistribution(type: Zip) {
classifier = "content"
from "$buildDir/distributions/content"
}
task tutorialContentToJson(type: TutorialContentToJsonTask)
docs.dependsOn(weavetest)
build.dependsOn(docs)
build.dependsOn(tutorialContentToJson)
packageDistribution.dependsOn(tutorialContentToJson)
build.dependsOn(packageDistribution)
class WeaveTypeFactory {
static def dataFormats() {
def dfd = DataFormatDescriptor$.MODULE$
DataFormatProperty[] dfp = []
def dataFormats = [
dfd.apply("application/dw", "weave", dfp, dfp)
]
dataFormats
}
static def parse(String content) {
VirtualFileSystem fs = EmptyVirtualFileSystem$.MODULE$
fs.updateContent("/dummy.dwl", content)
def file = fs.file("/dummy.dwl")
def service = new WeaveToolingService(fs, {dataFormats()})
ImplicitInput inputs = ImplicitInput$.MODULE$.apply()
scala.Option<WeaveType> none = None$.MODULE$
def document = service.openInMemory(file, inputs, none)
document.typeOfMapping().get()
}
}
class TutorialContentToJsonTask extends DefaultTask {
@TaskAction
def run() {
def oldUserDir = System.getProperty("user.dir")
try {
//Change to user dir that is expected on the scripts
System.setProperty("user.dir", "$project.projectDir")
def engine = new DataWeaveScriptingEngine()
def compile = engine.compile(new File("$project.projectDir/scripts/transform.dwl"))
def serviceManager = ServiceManager$.MODULE$.apply(org.mule.weave.v2.model.service.StdOutputLoggingService$.MODULE$)
def outputFolder = new File("$project.buildDir/distributions/content/")
outputFolder.mkdirs()
def outputFile = new File(outputFolder, "tutorial_content.json")
compile.write(new ScriptingBindings(), serviceManager, Some.apply(outputFile))
def compile2 = engine.compile(new File("$project.projectDir/scripts/weaveTypes.dwl"))
def bindings = new ScriptingBindings().addBinding("payload", outputFile, "application/json")
Map<String, Map<String, String>> types = compile2.write(bindings, serviceManager).content
String contentStr = typesToJson(types)
def outputFile3 = new File(outputFolder, "weaveTypes.json")
outputFile3.write contentStr
} finally {
System.setProperty("user.dir", oldUserDir)
}
}
private static String typesToJson(Map<String, Map<String, String>> types) {
def builder = new StringBuilder()
def initialStr = "{\n"
builder.append(initialStr)
types.entrySet().forEach { entry ->
def path = entry.getKey()
def inputs = entry.getValue()
if (builder.length() > initialStr.length()) {
builder.append(",\n")
}
builder.append(' "').append(path).append('": {\n')
def inputsBuilder = new StringBuilder()
inputs.entrySet().forEach { it ->
if (inputsBuilder.length() > 0) {
inputsBuilder.append(",\n")
}
def inputName = it.getKey()
def content = it.getValue()
def weaveType = WeaveTypeFactory.parse(content)
inputsBuilder.append(' "').append(inputName).append('": "').append(weaveType.toString().replaceAll('"', "'").replaceAll("\n", "")).append('"')
}
builder.append(inputsBuilder.toString())
builder.append("\n }")
}
builder.append("\n}")
def contentStr = builder.toString()
contentStr
}
}