diff --git a/README.md b/README.md index 1c227e9..72f0a50 100644 --- a/README.md +++ b/README.md @@ -86,10 +86,11 @@ dw --help ```bash usage: dw [--eval] [-f ] [--help] [-i ] - [-o ] [-p ] [--privileges - ] [--untrusted-code] [-v] [--version] [--add-wizard - ] [--list-spells] [--local-spell ] - [--new-spell ] [-s ] [--update-grimoires] + [--migrate ] [-o ] [-p ] [--privileges ] [--silent] + [--untrusted-code] [-v] [--version] [--add-wizard ] + [--list-spells] [--local-spell ] [--new-spell + ] [-s ] [--update-grimoires] ......................................................................... @@ -100,20 +101,29 @@ usage: dw [--eval] [-f ] [--help] [-i ] .%%%%%...%%..%%....%%....%%..%%...%%.%%...%%%%%%..%%..%%....%%....%%%%%%. ......................................................................... - --eval Evaluates the script instead of - writing it. - -f,--file Specifies output file for the - transformation if not standard - output will be used. + --eval Executes the script but it + doesn't use the writer. This is + useful when launching a + webserver. + -f,--file Specifies the DataWeave file + path to execute. --help Shows the help. -i,--input Declares a new input. + --migrate Migrates a DW1 file to DW2 and + outputs the result. -o,--output Specifies output file for the transformation if not standard output will be used. - -p,--parameter Parameter to be passed. + -p,--parameter Parameter to be passed. All + input parameters are accessible + through the variable `params` + of type object. --privileges A comma separated set of the privileges for the script execution. + --silent Executes the script in silent + mode, where all info messages + is not going to be shown. --untrusted-code Run the script as untrusted, which means that the script has no privileges. @@ -145,6 +155,7 @@ filter (item) -> item.age > 17" Documentation reference: https://docs.mulesoft.com/dataweave/latest/ + ``` ### DataWeave CLI Environment Variables diff --git a/native-cli-integration-tests/src/test/resources/dw1/SimpleFile.dw1 b/native-cli-integration-tests/src/test/resources/dw1/SimpleFile.dw1 new file mode 100644 index 0000000..557e160 --- /dev/null +++ b/native-cli-integration-tests/src/test/resources/dw1/SimpleFile.dw1 @@ -0,0 +1,8 @@ +%dw 1.0 +%var arr = ["foo"] +--- +{ + a: [0, 1, 2] contains [1, 2], + b: sum [1..1000], + c: sizeOf ("123") +} \ No newline at end of file diff --git a/native-cli-integration-tests/src/test/scala/org/mule/weave/native/NativeCliTest.scala b/native-cli-integration-tests/src/test/scala/org/mule/weave/native/NativeCliTest.scala index 1fc9bdf..efc8f18 100644 --- a/native-cli-integration-tests/src/test/scala/org/mule/weave/native/NativeCliTest.scala +++ b/native-cli-integration-tests/src/test/scala/org/mule/weave/native/NativeCliTest.scala @@ -5,6 +5,7 @@ import org.scalatest.FreeSpec import org.scalatest.Matchers import java.io.File +import java.net.URL class NativeCliTest extends FreeSpec with Matchers @@ -22,6 +23,23 @@ class NativeCliTest extends FreeSpec DEFAULT_DW_CLI_HOME.mkdirs() } + "it should execute simple migration correctly" in { + val stream: URL = getClass.getClassLoader.getResource("dw1/SimpleFile.dw1") + val file = new File(stream.toURI) + val (_, output) = NativeCliITTestRunner(Array("--migrate", file.getAbsolutePath)).execute() + output.trim shouldBe + """ + |%dw 2.0 + |var arr = ["foo"] + |--- + |{ + | a: [0, 1, 2] contains [1, 2], + | b: sum(1 to 1000), + | c: sizeOf(("123")) + |} + |""".stripMargin.trim + } + "it should execute simple case correctly" in { val (_, output) = NativeCliITTestRunner("1 to 10").execute() output shouldBe "[\n 1,\n 2,\n 3,\n 4,\n 5,\n 6,\n 7,\n 8,\n 9,\n 10\n]" diff --git a/native-cli/build.gradle b/native-cli/build.gradle index 8e7e894..7493111 100644 --- a/native-cli/build.gradle +++ b/native-cli/build.gradle @@ -27,6 +27,7 @@ dependencies { implementation group: 'org.mule.weave', name: 'jsonschema-module', version: weaveVersion implementation group: 'org.mule.weave', name: 'http-module', version: ioVersion implementation group: 'org.mule.weave', name: 'process-module', version: ioVersion + implementation group: 'org.mule.weave', name: 'migrant', version: '2.5.0-SNAPSHOT' implementation(group: 'org.mule.weave', name: 'http-netty-module', version: ioVersion) { exclude group: 'org.slf4j' } diff --git a/native-cli/src/main/scala/org/mule/weave/dwnative/cli/CLIArgumentsParser.scala b/native-cli/src/main/scala/org/mule/weave/dwnative/cli/CLIArgumentsParser.scala index b5b8cdd..422b599 100644 --- a/native-cli/src/main/scala/org/mule/weave/dwnative/cli/CLIArgumentsParser.scala +++ b/native-cli/src/main/scala/org/mule/weave/dwnative/cli/CLIArgumentsParser.scala @@ -7,6 +7,7 @@ import org.mule.weave.dwnative.cli.commands.CloneWizardConfig import org.mule.weave.dwnative.cli.commands.CreateSpellCommand import org.mule.weave.dwnative.cli.commands.HelpCommand import org.mule.weave.dwnative.cli.commands.ListSpellsCommand +import org.mule.weave.dwnative.cli.commands.MigrateDW1Command import org.mule.weave.dwnative.cli.commands.RunWeaveCommand import org.mule.weave.dwnative.cli.commands.UpdateAllGrimoires import org.mule.weave.dwnative.cli.commands.UpdateGrimoireCommand @@ -100,6 +101,15 @@ class CLIArgumentsParser(console: Console) { return Right("Missing ") } } + + if (commandLine.hasOption(Options.MIGRATE)) { + val oldFilePath = commandLine.getOptionValue(Options.MIGRATE) + if (oldFilePath != null && !oldFilePath.isBlank) { + return Left(new MigrateDW1Command(oldFilePath, console)) + } else { + return Right("Missing ") + } + } if (commandLine.hasOption(Options.LIST_SPELLS)) { return Left(new ListSpellsCommand(console)) diff --git a/native-cli/src/main/scala/org/mule/weave/dwnative/cli/Options.scala b/native-cli/src/main/scala/org/mule/weave/dwnative/cli/Options.scala index 2a69430..407f494 100644 --- a/native-cli/src/main/scala/org/mule/weave/dwnative/cli/Options.scala +++ b/native-cli/src/main/scala/org/mule/weave/dwnative/cli/Options.scala @@ -12,6 +12,7 @@ object Options { val LIST_SPELLS = "list-spells" val LOCAL_SPELL = "local-spell" val NEW_SPELL = "new-spell" + val MIGRATE = "migrate" val OUTPUT = "output" val PRIVILEGES = "privileges" val PARAMETER = "parameter" @@ -32,7 +33,7 @@ object Options { .hasArgs() .numberOfArgs(2) .argName("param-name param-value") - .desc("Parameter to be passed.") + .desc("Parameter to be passed. All input parameters are accessible through the variable `params` of type object.") .build()) options.addOption(Option.builder("i") @@ -67,6 +68,15 @@ object Options { options.addOption(null, UNTRUSTED_CODE, false, "Run the script as untrusted, which means that the script has no privileges.") + + options.addOption(Option.builder() + .longOpt(MIGRATE) + .hasArg(true) + .argName("dw1-file-path") + .desc(s"Migrates a DW1 file to DW2 and outputs the result.") + .build() + ) + options.addOption(Option.builder() .longOpt(PRIVILEGES) .hasArg() diff --git a/native-cli/src/main/scala/org/mule/weave/dwnative/cli/commands/MigrateDW1Command.scala b/native-cli/src/main/scala/org/mule/weave/dwnative/cli/commands/MigrateDW1Command.scala new file mode 100644 index 0000000..77973a0 --- /dev/null +++ b/native-cli/src/main/scala/org/mule/weave/dwnative/cli/commands/MigrateDW1Command.scala @@ -0,0 +1,32 @@ +package org.mule.weave.dwnative.cli.commands + +import org.mule.weave.dwnative.cli.Console +import org.mule.weave.v2.V2LangMigrant + +import java.io.File +import java.io.FileWriter +import java.nio.charset.StandardCharsets +import scala.io.Source + +class MigrateDW1Command(val oldScriptPath: String, console: Console) extends WeaveCommand { + + def exec(): Int = { + var statusCode = ExitCodes.SUCCESS + val oldScriptFile = new File(oldScriptPath) + if (!oldScriptFile.exists()) { + console.error(s"Unable to find dw1 script to migrate") + statusCode = ExitCodes.FAILURE + } else { + val source = Source.fromFile(oldScriptFile, StandardCharsets.UTF_8.name()) + try { + val str = V2LangMigrant.migrateToV2(source.mkString) + console.out.write(str.getBytes(StandardCharsets.UTF_8)) + } finally { + source.close() + } + } + statusCode + } + + +}