Skip to content

Commit cea30bd

Browse files
authored
Adding migration from dw1 to dw2 support inside the CLI (#62)
* Adding migration from dw1 to dw2 support inside the CLI * Fixing build
1 parent fe86ecf commit cea30bd

7 files changed

Lines changed: 101 additions & 11 deletions

File tree

README.md

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,11 @@ dw --help
8686

8787
```bash
8888
usage: dw [--eval] [-f <file-path>] [--help] [-i <input-name input-path>]
89-
[-o <output-path>] [-p <param-name param-value>] [--privileges
90-
<privileges>] [--untrusted-code] [-v] [--version] [--add-wizard
91-
<wizard-name>] [--list-spells] [--local-spell <spell-folder>]
92-
[--new-spell <spell-name>] [-s <spell-name>] [--update-grimoires]
89+
[--migrate <dw1-file-path>] [-o <output-path>] [-p <param-name
90+
param-value>] [--privileges <privileges>] [--silent]
91+
[--untrusted-code] [-v] [--version] [--add-wizard <wizard-name>]
92+
[--list-spells] [--local-spell <spell-folder>] [--new-spell
93+
<spell-name>] [-s <spell-name>] [--update-grimoires]
9394

9495

9596
.........................................................................
@@ -100,20 +101,29 @@ usage: dw [--eval] [-f <file-path>] [--help] [-i <input-name input-path>]
100101
.%%%%%...%%..%%....%%....%%..%%...%%.%%...%%%%%%..%%..%%....%%....%%%%%%.
101102
.........................................................................
102103

103-
--eval Evaluates the script instead of
104-
writing it.
105-
-f,--file <file-path> Specifies output file for the
106-
transformation if not standard
107-
output will be used.
104+
--eval Executes the script but it
105+
doesn't use the writer. This is
106+
useful when launching a
107+
webserver.
108+
-f,--file <file-path> Specifies the DataWeave file
109+
path to execute.
108110
--help Shows the help.
109111
-i,--input <input-name input-path> Declares a new input.
112+
--migrate <dw1-file-path> Migrates a DW1 file to DW2 and
113+
outputs the result.
110114
-o,--output <output-path> Specifies output file for the
111115
transformation if not standard
112116
output will be used.
113-
-p,--parameter <param-name param-value> Parameter to be passed.
117+
-p,--parameter <param-name param-value> Parameter to be passed. All
118+
input parameters are accessible
119+
through the variable `params`
120+
of type object.
114121
--privileges <privileges> A comma separated set of the
115122
privileges for the script
116123
execution.
124+
--silent Executes the script in silent
125+
mode, where all info messages
126+
is not going to be shown.
117127
--untrusted-code Run the script as untrusted,
118128
which means that the script has
119129
no privileges.
@@ -145,6 +155,7 @@ filter (item) -> item.age > 17"
145155
Documentation reference:
146156
147157
https://docs.mulesoft.com/dataweave/latest/
158+
148159
```
149160
150161
### DataWeave CLI Environment Variables
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
%dw 1.0
2+
%var arr = ["foo"]
3+
---
4+
{
5+
a: [0, 1, 2] contains [1, 2],
6+
b: sum [1..1000],
7+
c: sizeOf ("123")
8+
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import org.scalatest.FreeSpec
55
import org.scalatest.Matchers
66

77
import java.io.File
8+
import java.net.URL
89

910
class NativeCliTest extends FreeSpec
1011
with Matchers
@@ -22,6 +23,23 @@ class NativeCliTest extends FreeSpec
2223
DEFAULT_DW_CLI_HOME.mkdirs()
2324
}
2425

26+
"it should execute simple migration correctly" in {
27+
val stream: URL = getClass.getClassLoader.getResource("dw1/SimpleFile.dw1")
28+
val file = new File(stream.toURI)
29+
val (_, output) = NativeCliITTestRunner(Array("--migrate", file.getAbsolutePath)).execute()
30+
output.trim shouldBe
31+
"""
32+
|%dw 2.0
33+
|var arr = ["foo"]
34+
|---
35+
|{
36+
| a: [0, 1, 2] contains [1, 2],
37+
| b: sum(1 to 1000),
38+
| c: sizeOf(("123"))
39+
|}
40+
|""".stripMargin.trim
41+
}
42+
2543
"it should execute simple case correctly" in {
2644
val (_, output) = NativeCliITTestRunner("1 to 10").execute()
2745
output shouldBe "[\n 1,\n 2,\n 3,\n 4,\n 5,\n 6,\n 7,\n 8,\n 9,\n 10\n]"

native-cli/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ dependencies {
2727
implementation group: 'org.mule.weave', name: 'jsonschema-module', version: weaveVersion
2828
implementation group: 'org.mule.weave', name: 'http-module', version: ioVersion
2929
implementation group: 'org.mule.weave', name: 'process-module', version: ioVersion
30+
implementation group: 'org.mule.weave', name: 'migrant', version: '2.5.0-SNAPSHOT'
3031
implementation(group: 'org.mule.weave', name: 'http-netty-module', version: ioVersion) {
3132
exclude group: 'org.slf4j'
3233
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import org.mule.weave.dwnative.cli.commands.CloneWizardConfig
77
import org.mule.weave.dwnative.cli.commands.CreateSpellCommand
88
import org.mule.weave.dwnative.cli.commands.HelpCommand
99
import org.mule.weave.dwnative.cli.commands.ListSpellsCommand
10+
import org.mule.weave.dwnative.cli.commands.MigrateDW1Command
1011
import org.mule.weave.dwnative.cli.commands.RunWeaveCommand
1112
import org.mule.weave.dwnative.cli.commands.UpdateAllGrimoires
1213
import org.mule.weave.dwnative.cli.commands.UpdateGrimoireCommand
@@ -100,6 +101,15 @@ class CLIArgumentsParser(console: Console) {
100101
return Right("Missing <spell-name>")
101102
}
102103
}
104+
105+
if (commandLine.hasOption(Options.MIGRATE)) {
106+
val oldFilePath = commandLine.getOptionValue(Options.MIGRATE)
107+
if (oldFilePath != null && !oldFilePath.isBlank) {
108+
return Left(new MigrateDW1Command(oldFilePath, console))
109+
} else {
110+
return Right("Missing <dw1-file-path>")
111+
}
112+
}
103113

104114
if (commandLine.hasOption(Options.LIST_SPELLS)) {
105115
return Left(new ListSpellsCommand(console))

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ object Options {
1212
val LIST_SPELLS = "list-spells"
1313
val LOCAL_SPELL = "local-spell"
1414
val NEW_SPELL = "new-spell"
15+
val MIGRATE = "migrate"
1516
val OUTPUT = "output"
1617
val PRIVILEGES = "privileges"
1718
val PARAMETER = "parameter"
@@ -32,7 +33,7 @@ object Options {
3233
.hasArgs()
3334
.numberOfArgs(2)
3435
.argName("param-name param-value")
35-
.desc("Parameter to be passed.")
36+
.desc("Parameter to be passed. All input parameters are accessible through the variable `params` of type object.")
3637
.build())
3738

3839
options.addOption(Option.builder("i")
@@ -67,6 +68,15 @@ object Options {
6768

6869
options.addOption(null, UNTRUSTED_CODE, false, "Run the script as untrusted, which means that the script has no privileges.")
6970

71+
72+
options.addOption(Option.builder()
73+
.longOpt(MIGRATE)
74+
.hasArg(true)
75+
.argName("dw1-file-path")
76+
.desc(s"Migrates a DW1 file to DW2 and outputs the result.")
77+
.build()
78+
)
79+
7080
options.addOption(Option.builder()
7181
.longOpt(PRIVILEGES)
7282
.hasArg()
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.mule.weave.dwnative.cli.commands
2+
3+
import org.mule.weave.dwnative.cli.Console
4+
import org.mule.weave.v2.V2LangMigrant
5+
6+
import java.io.File
7+
import java.io.FileWriter
8+
import java.nio.charset.StandardCharsets
9+
import scala.io.Source
10+
11+
class MigrateDW1Command(val oldScriptPath: String, console: Console) extends WeaveCommand {
12+
13+
def exec(): Int = {
14+
var statusCode = ExitCodes.SUCCESS
15+
val oldScriptFile = new File(oldScriptPath)
16+
if (!oldScriptFile.exists()) {
17+
console.error(s"Unable to find dw1 script to migrate")
18+
statusCode = ExitCodes.FAILURE
19+
} else {
20+
val source = Source.fromFile(oldScriptFile, StandardCharsets.UTF_8.name())
21+
try {
22+
val str = V2LangMigrant.migrateToV2(source.mkString)
23+
console.out.write(str.getBytes(StandardCharsets.UTF_8))
24+
} finally {
25+
source.close()
26+
}
27+
}
28+
statusCode
29+
}
30+
31+
32+
}

0 commit comments

Comments
 (0)