Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding migration from dw1 to dw2 support inside the CLI #62

Merged
merged 2 commits into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,11 @@ dw --help

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


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

--eval Evaluates the script instead of
writing it.
-f,--file <file-path> 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 <file-path> Specifies the DataWeave file
path to execute.
--help Shows the help.
-i,--input <input-name input-path> Declares a new input.
--migrate <dw1-file-path> Migrates a DW1 file to DW2 and
outputs the result.
-o,--output <output-path> Specifies output file for the
transformation if not standard
output will be used.
-p,--parameter <param-name param-value> Parameter to be passed.
-p,--parameter <param-name param-value> Parameter to be passed. All
input parameters are accessible
through the variable `params`
of type object.
--privileges <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.
Expand Down Expand Up @@ -145,6 +155,7 @@ filter (item) -> item.age > 17"
Documentation reference:

https://docs.mulesoft.com/dataweave/latest/

```

### DataWeave CLI Environment Variables
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
%dw 1.0
%var arr = ["foo"]
---
{
a: [0, 1, 2] contains [1, 2],
b: sum [1..1000],
c: sizeOf ("123")
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]"
Expand Down
1 change: 1 addition & 0 deletions native-cli/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -100,6 +101,15 @@ class CLIArgumentsParser(console: Console) {
return Right("Missing <spell-name>")
}
}

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 <dw1-file-path>")
}
}

if (commandLine.hasOption(Options.LIST_SPELLS)) {
return Left(new ListSpellsCommand(console))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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")
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}


}