Skip to content

Commit 20e76ae

Browse files
authored
Adding dep management (#47)
* Adding dep management * Cleanup print * Removing local maven * Adding logs * Using tmp folder * It should fail when it can not resolve * It should fail when it can not resolve * Fixing logging * Adding more logs * Trying to fix build * Cleanup
1 parent 842bbe6 commit 20e76ae

19 files changed

Lines changed: 570 additions & 74 deletions

File tree

README.md

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
# DataWeave CLI
22

3-
**DataWeave CLI** is a command-line interface that allows `querying`, `filtering`, and `mapping` structured data from different data sources like `JSON`, `XML`, `CSV`, `YML` to other data formats. It also allows to easily create data in such formats.
3+
**DataWeave CLI** is a command-line interface that allows `querying`, `filtering`, and `mapping` structured data from
4+
different data sources like `JSON`, `XML`, `CSV`, `YML` to other data formats. It also allows to easily create data in
5+
such formats.
46

5-
For more info about the `DataWeave` language visit the [documenation site](https://docs.mulesoft.com/mule-runtime/latest/dataweave)
7+
For more info about the `DataWeave` language visit
8+
the [documenation site](https://docs.mulesoft.com/mule-runtime/latest/dataweave)
69

710
## What is Included?
8-
The binary distribution already ships with a set of modules and data formats that makes this useful for a very interesting and broad set of use cases.
11+
12+
The binary distribution already ships with a set of modules and data formats that makes this useful for a very
13+
interesting and broad set of use cases.
914

1015
### Included Modules
16+
1117
- [DataWeave Standard Library](https://github.com/mulesoft/data-weave/tree/master/wlang)
1218

1319
### Supported Data Formats
@@ -25,7 +31,7 @@ The binary distribution already ships with a set of modules and data formats tha
2531
| `text/plain` | `text` | [Text Plain Format](https://docs.mulesoft.com/dataweave/latest/dataweave-formats-text) |
2632
| `text/x-java-properties` | `properties` | [Text Java Properties](https://docs.mulesoft.com/dataweave/latest/dataweave-formats-properties) |
2733

28-
## Installation
34+
## Installation
2935

3036
### Homebrew (Mac)
3137

@@ -35,18 +41,22 @@ brew install dw
3541
```
3642

3743
### Manual Installation
44+
3845
1. Download the latest [release version](https://github.com/mulesoft-labs/data-weave-cli/releases) according to your OS.
3946
2. Unzip the file on your `<user.home>/.dw`
4047
3. Add `<user.home>/.dw/bin` to your **PATH**
4148

4249
### Build and Install
4350

44-
To build the project, you need to run gradlew with the graalVM distribution based on Java 11. You can download it at https://github.com/graalvm/graalvm-ce-builds/releases
51+
To build the project, you need to run gradlew with the graalVM distribution based on Java 11. You can download it
52+
at https://github.com/graalvm/graalvm-ce-builds/releases
4553
Set:
54+
4655
```bash
4756
export GRAALVM_HOME=<pathToGraalVMFolder>/graalvm-ce-java11-21.2.0/Contents/Home
4857
export JAVA_HOME=<pathToGraalVMFolder>/graalvm-ce-java11-21.2.0/Contents/Home
4958
```
59+
5060
Execute the gradle task `nativeCompile`
5161

5262
```bash
@@ -59,11 +69,12 @@ Once it finishes you will find the `dw` binary in `native-cli/build/native/nativ
5969

6070
## How to Use It
6171

62-
If the directory containing the `dw` executable is in your _PATH_, you can run `dw` from anywhere.
72+
If the directory containing the `dw` executable is in your _PATH_, you can run `dw` from anywhere.
6373

6474
If it is not, go to the `bin` directory referenced in the installation instructions and run `dw` from there.
6575

66-
The following example shows the DataWeave CLI documentation
76+
The following example shows the DataWeave CLI documentation
77+
6778
```bash
6879
dw --help
6980
```
@@ -141,6 +152,28 @@ The following are the DataWeave CLI environment variables that you can set in yo
141152
| `DW_DEFAULT_INPUT_MIMETYPE` | The default `mimeType` that is going to be used for the standard input. If not defined `application/json` will be used. |
142153
| `DW_DEFAULT_OUTPUT_MIMETYPE` | The default output `mimeType` that is going to be if not defined. If not defined `application/json` will be used. |
143154
155+
## Dependency Manager
156+
157+
In order for a spell to depend on a library it can include a library it can use the dependencies.dwl to specify the list of dependencies that it should be included and download
158+
159+
```data-weave
160+
%dw 2.0
161+
var mavenRepositories = [{
162+
url: "https://maven.anypoint.mulesoft.com/api/v3/maven"
163+
}]
164+
---
165+
{
166+
dependencies: [
167+
{
168+
kind: "maven",
169+
artifactId: "data-weave-analytics-library",
170+
groupId: "68ef9520-24e9-4cf2-b2f5-620025690913",
171+
version: "1.0.1",
172+
repositories: mavenRepositories // By default mulesoft, exchange and central are being added
173+
}
174+
]
175+
}
176+
```
144177
145178
### Querying Content From a File
146179
@@ -198,7 +231,6 @@ dw -i payload <fullpathToUsers.json> "output application/json --- payload filter
198231
cat <fullpathToUser.json> | dw "output application/json --- payload filter (item) -> item.age > 17"
199232
```
200233
201-
202234
### Redirecting the Output to a File
203235
204236
```bash
@@ -226,9 +258,11 @@ curl "https://api.github.com/repos/mulesoft/mule/commits?per_page=5" | dw "{mess
226258
```
227259
228260
### Generate a Request with Body
261+
229262
This example uses the [jsonplaceholder API](https://jsonplaceholder.typicode.com/) to update a resource.
230263
231264
Steps:
265+
232266
1. Search the post resource with the `id = 1`.
233267
2. Use DataWeave CLI to create a JSON output changing the post title `My new title`.
234268
3. Finally, update the post resource.
@@ -238,6 +272,7 @@ curl https://jsonplaceholder.typicode.com/posts/1 | dw "output application/json
238272
```
239273
240274
#### Output
275+
241276
```json
242277
{
243278
"id": 1,
@@ -258,4 +293,5 @@ Before creating a pull request review the following:
258293
* [SECURITY](SECURITY.md)
259294
* [CODE_OF_CONDUCT](CODE_OF_CONDUCT.md)
260295
261-
When you submit your pull request, you are asked to sign a contributor license agreement (CLA) if we don't have one on file for you.
296+
When you submit your pull request, you are asked to sign a contributor license agreement (CLA) if we don't have one on
297+
file for you.

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ weaveVersion=2.5.0-SNAPSHOT
22
nativeVersion=100.100.100
33
scalaVersion=2.12.11
44
ioVersion=1.0.0-SNAPSHOT
5-
graalvmVersion=22.0.0.2
5+
graalvmVersion=22.2.0
66
#Libaries
77
scalaTestVersion=3.0.1
88
scalaTestPluginVersion=0.32

native-cli-integration-tests/src/test/scala/org/mule/weave/native/NativeCliRuntimeIT.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ class NativeCliRuntimeIT extends FunSpec
363363
"module-singleton",
364364
"multipart-write-binary",
365365
"read-binary-files",
366+
"underflow",
366367
"try",
367368
"urlEncodeDecode") ++
368369
// Uses resource name that is different on Cli than in the Tests

native-cli/build.gradle

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ plugins {
33
id "com.github.maiflai.scalatest" version "${scalaTestPluginVersion}"
44
id 'application'
55
// Apply GraalVM Native Image plugin
6-
id 'org.graalvm.buildtools.native' version '0.9.13'
6+
id 'org.graalvm.buildtools.native' version '0.9.14'
77
}
88

99
sourceSets {
@@ -19,6 +19,9 @@ mainClassName = 'org.mule.weave.dwnative.cli.DataWeaveCLI'
1919
dependencies {
2020
api group: 'org.mule.weave', name: 'runtime', version: weaveVersion
2121
compileOnly group: 'org.graalvm.sdk', name: 'graal-sdk', version: graalvmVersion
22+
implementation group: 'io.get-coursier', name: 'coursier-core_2.12', version: '1.1.0-M14-7'
23+
implementation group: 'io.get-coursier', name: 'coursier_2.12', version: '1.1.0-M14-7'
24+
implementation group: 'io.get-coursier', name: 'coursier-cache_2.12', version: '1.1.0-M14-7'
2225
implementation group: 'org.mule.weave', name: 'core-modules', version: weaveVersion
2326
implementation group: 'org.mule.weave', name: 'yaml-module', version: weaveVersion
2427
implementation group: 'org.mule.weave', name: 'http-module', version: ioVersion
@@ -116,6 +119,7 @@ graalvmNative {
116119
"org.asynchttpclient," +
117120
"org.mule.weave.v2.module.http.netty.HttpAsyncClientService," +
118121
"scala.util.Random," +
122+
"coursier.," +
119123
"org.mule.weave.v2.sdk.SPIBasedModuleLoaderProvider\$")
120124
buildArgs.add("--initialize-at-build-time=" +
121125
"sun.instrument.InstrumentationImpl," +
@@ -143,6 +147,7 @@ graalvmNative {
143147
// "org.mule.weave.v2.model.types.,"
144148
// "org.mule.weave.v2.core.functions."
145149
// option "-H:+TraceClassInitialization"
150+
buildArgs.add("--trace-class-initialization=coursier.core.Type\$")
146151
buildArgs.add("-H:DeadlockWatchdogInterval=1000")
147152
buildArgs.add("--report-unsupported-elements-at-runtime")
148153
buildArgs.add("-H:CompilationExpirationPeriod=0")

native-cli/src/main/scala/org/mule/weave/dwnative/NativeRuntime.scala

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import org.mule.weave.v2.runtime.ModuleComponentsFactory
4242
import org.mule.weave.v2.runtime.ParserConfiguration
4343
import org.mule.weave.v2.runtime.ScriptingBindings
4444
import org.mule.weave.v2.runtime.ScriptingEngineSetupException
45+
import org.mule.weave.v2.sdk.NameIdentifierHelper
4546
import org.mule.weave.v2.sdk.SPIBasedModuleLoaderProvider
4647
import org.mule.weave.v2.sdk.TwoLevelWeaveResourceResolver
4748
import org.mule.weave.v2.sdk.WeaveResourceResolver
@@ -58,7 +59,7 @@ class NativeRuntime(libDir: File, path: Array[File], console: Console) {
5859
private val dataWeaveUtils = new DataWeaveUtils(console)
5960

6061
private val pathBasedResourceResolver: PathBasedResourceResolver = PathBasedResourceResolver(path ++ Option(libDir.listFiles()).getOrElse(new Array[File](0)))
61-
62+
6263
private val weaveScriptingEngine: DataWeaveScriptingEngine = {
6364
setupEnv()
6465
DataWeaveScriptingEngine(new NativeModuleComponentFactory(() => pathBasedResourceResolver, systemFirst = true), ParserConfiguration())
@@ -68,6 +69,10 @@ class NativeRuntime(libDir: File, path: Array[File], console: Console) {
6869
weaveScriptingEngine.enableProfileParsing()
6970
}
7071

72+
def addJarToClassPath(file: File): Unit = {
73+
pathBasedResourceResolver.addContent(ContentResolver(file))
74+
}
75+
7176
/**
7277
* Setup initialization properties
7378
*/
@@ -103,9 +108,9 @@ class NativeRuntime(libDir: File, path: Array[File], console: Console) {
103108
private def compileScript(script: String, inputs: ScriptingBindings, nameIdentifier: NameIdentifier, defaultOutputMimeType: String) = {
104109
weaveScriptingEngine.compile(script, nameIdentifier, inputs.entries().map(wi => new InputType(wi, None)).toArray, defaultOutputMimeType)
105110
}
106-
111+
107112
private def createServiceManager(maybePrivileges: Option[Seq[String]] = None): ServiceManager = {
108-
113+
109114
val charsetProviderService = new CharsetProviderService {
110115
override def defaultCharset(): Charset = {
111116
StandardCharsets.UTF_8
@@ -120,7 +125,7 @@ class NativeRuntime(libDir: File, path: Array[File], console: Console) {
120125
if (maybePrivileges.isDefined) {
121126
val privileges = maybePrivileges.get
122127
val weaveRuntimePrivileges = privileges.map(WeaveRuntimePrivilege(_)).toArray
123-
customServices = customServices + (classOf[SecurityManagerService] -> new DefaultSecurityManagerService(weaveRuntimePrivileges))
128+
customServices = customServices + (classOf[SecurityManagerService] -> new DefaultSecurityManagerService(weaveRuntimePrivileges))
124129
}
125130
ServiceManager(new ConsoleLogger(console), customServices)
126131
}
@@ -143,12 +148,7 @@ class WeavePathProtocolHandler(path: PathBasedResourceResolver) extends ReadFunc
143148

144149
override def createSourceProvider(url: String, locatable: LocationCapable, charset: Charset): SourceProvider = {
145150
val uri = url.stripPrefix(CLASSPATH_PREFIX)
146-
val wellFormedUri = if (uri.startsWith("/")) {
147-
uri.substring(1)
148-
} else {
149-
uri
150-
}
151-
val maybeResource = path.resolve(wellFormedUri)
151+
val maybeResource = path.resolve(uri)
152152
maybeResource match {
153153
case Some(value) => {
154154
SourceProvider(value, charset)

native-cli/src/main/scala/org/mule/weave/dwnative/PathBasedResourceResolver.scala

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ class PathBasedResourceResolver(paths: mutable.ArrayBuffer[ContentResolver]) ext
2222
}
2323

2424
override def resolve(name: NameIdentifier): Option[WeaveResource] = {
25-
val filePath = NameIdentifierHelper.toWeaveFilePath(name)
25+
2626
val iterator = paths.iterator
2727
while (iterator.hasNext) {
28-
val maybeResource = iterator.next().resolve(filePath)
28+
val maybeResource: Option[InputStream] = iterator.next().resolve(name)
2929
if (maybeResource.isDefined) {
30+
val filePath = NameIdentifierHelper.toWeaveFilePath(name, "/") //Use unix based system
3031
return Some(WeaveResource(filePath, toString(maybeResource.get)))
3132
}
3233
}
@@ -43,9 +44,10 @@ class PathBasedResourceResolver(paths: mutable.ArrayBuffer[ContentResolver]) ext
4344
}
4445

4546
def resolve(filePath: String): Option[InputStream] = {
47+
val ni = NameIdentifierHelper.fromWeaveFilePath(filePath, "/")
4648
val iterator = paths.iterator
4749
while (iterator.hasNext) {
48-
val maybeResource = iterator.next().resolve(filePath)
50+
val maybeResource = iterator.next().resolve(ni)
4951
if (maybeResource.isDefined) {
5052
return maybeResource
5153
}
@@ -55,29 +57,20 @@ class PathBasedResourceResolver(paths: mutable.ArrayBuffer[ContentResolver]) ext
5557

5658

5759
override def resolveAll(name: NameIdentifier): Seq[WeaveResource] = {
58-
val filePath = NameIdentifierHelper.toWeaveFilePath(name)
5960
paths
60-
.flatMap(_.resolve(filePath))
61-
.map((content) => WeaveResource(filePath, toString(content)))
61+
.flatMap(_.resolve(name))
62+
.map((content) => {
63+
val path = NameIdentifierHelper.toWeaveFilePath(name, "/")
64+
WeaveResource(path, toString(content))
65+
})
6266
}
6367
}
6468

6569
/**
6670
*
6771
*/
6872
trait ContentResolver {
69-
def resolve(path: String): Option[InputStream]
70-
}
71-
72-
class CompositeContentResolver(contents: Seq[ContentResolver]) extends ContentResolver {
73-
override def resolve(path: String): Option[InputStream] = {
74-
contents
75-
.toStream
76-
.flatMap((content) => {
77-
content.resolve(path)
78-
})
79-
.headOption
80-
}
73+
def resolve(path: NameIdentifier): Option[InputStream]
8174
}
8275

8376

@@ -93,7 +86,8 @@ object ContentResolver {
9386

9487
class DirectoryContentResolver(directory: File) extends ContentResolver {
9588

96-
override def resolve(path: String): Option[InputStream] = {
89+
override def resolve(ni: NameIdentifier): Option[InputStream] = {
90+
val path = NameIdentifierHelper.toWeaveFilePath(ni, File.separator) //Use unix based system
9791
val file = new File(directory, path)
9892
if (file.isFile) {
9993
Some(new FileInputStream(file))
@@ -107,12 +101,15 @@ class JarContentResolver(jarFile: => File) extends ContentResolver {
107101

108102
lazy val zipFile = new ZipFile(jarFile)
109103

110-
override def resolve(path: String): Option[InputStream] = {
111-
val zipEntry = if (path.startsWith("/")) {
112-
path.substring(1)
113-
} else {
114-
path
115-
}
104+
override def resolve(ni: NameIdentifier): Option[InputStream] = {
105+
val path = NameIdentifierHelper.toWeaveFilePath(ni, "/") //Use unix based system
106+
107+
val zipEntry: String =
108+
if (path.startsWith("/")) {
109+
path.substring(1)
110+
} else {
111+
path
112+
}
116113
val pathEntry = zipFile.getEntry(zipEntry)
117114
if (pathEntry != null) {
118115
Some(zipFile.getInputStream(pathEntry))

0 commit comments

Comments
 (0)