Skip to content
This repository was archived by the owner on May 31, 2025. It is now read-only.

Commit 28520c5

Browse files
authored
Adding a way to validate if a script is valid or not (#79)
* Adding a way to validate if a script is valid or not * Update readme
1 parent de6a1cd commit 28520c5

File tree

11 files changed

+172
-16
lines changed

11 files changed

+172
-16
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ dw help
8585
```
8686

8787
```bash
88-
____ __ ____ __ _ _ ____ __ _ _ ____
88+
____ __ ____ __ _ _ ____ __ _ _ ____
8989
( \ / _\(_ _)/ _\ / )( \( __) / _\ / )( \( __)
9090
) D (/ \ )( / \\ /\ / ) _) / \\ \/ / ) _)
9191
(____/\_/\_/(__)\_/\_/(_/\_)(____)\_/\_/ \__/ (____)
@@ -94,7 +94,9 @@ Usage: <main class> [-hV] [COMMAND]
9494
-V, --version Print version information and exit.
9595
Commands:
9696
run Runs provided DW script.
97-
add-wizard Adds a new Wizard to your network of trusted wizards.
97+
wizard Wizard actions.
98+
add Adds a new Wizard to your network of trusted wizards.
99+
validate Validate if a script is valid or not.
98100
migrate Translates a DW1 script into a DW2 script.
99101
spell Runs the specified Spell.
100102
create Creates a new spell with the given name.

native-cli/src/main/java/org/mule/weave/cli/DWCLI.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
import org.mule.weave.cli.pico.PicoRunSpell;
1010
import org.mule.weave.cli.pico.PicoMigrate;
1111
import org.mule.weave.cli.pico.PicoUpdateSpells;
12+
import org.mule.weave.cli.pico.PicoValidateScript;
1213
import org.mule.weave.cli.pico.PicoVersionProvider;
14+
import org.mule.weave.cli.pico.PicoWizard;
1315
import org.mule.weave.dwnative.cli.Console;
1416
import org.mule.weave.dwnative.cli.DefaultConsole$;
1517
import picocli.CommandLine;
@@ -44,7 +46,8 @@ public void run(String[] args, Console console) {
4446
mixinStandardHelpOptions = true,
4547
subcommands = {
4648
PicoRunScript.class,
47-
PicoAddWizard.class,
49+
PicoWizard.class,
50+
PicoValidateScript.class,
4851
PicoMigrate.class,
4952
PicoRunSpell.class,
5053
CommandLine.HelpCommand.class,

native-cli/src/main/java/org/mule/weave/cli/pico/AbstractPicoExecCommand.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public static <A, B> Map<A, B> toScalaMap(java.util.Map<A, B> m) {
5151
);
5252
}
5353

54-
public String fileToString(File f) {
54+
public static String fileToString(File f) {
5555
try {
5656
return Files.readString(f.toPath(), StandardCharsets.UTF_8);
5757
} catch (IOException e) {
@@ -73,19 +73,19 @@ public Integer call() {
7373
return doCall();
7474
}
7575

76-
protected Option<DataWeaveVersion> calculateRuntimeVersion() {
76+
public static Option<DataWeaveVersion> calculateRuntimeVersion(String languageLevel1, CommandLine.Model.CommandSpec spec1) {
7777
Option<DataWeaveVersion> dataWeaveVersionOption;
7878
try {
79-
dataWeaveVersionOption = Option.apply(languageLevel).map((s) -> DataWeaveVersion.apply(s));
79+
dataWeaveVersionOption = Option.apply(languageLevel1).map((s) -> DataWeaveVersion.apply(s));
8080
if (dataWeaveVersionOption.isDefined()) {
8181
DataWeaveVersion dataWeaveVersion = dataWeaveVersionOption.get();
8282
DataWeaveVersion currentVersion = DataWeaveVersion.apply();
8383
if (dataWeaveVersion.$greater(currentVersion)) {
84-
throw new CommandLine.ParameterException(spec.commandLine(), "Invalid language level, cannot be higher than " + currentVersion.toString());
84+
throw new CommandLine.ParameterException(spec1.commandLine(), "Invalid language level, cannot be higher than " + currentVersion.toString());
8585
}
8686
}
8787
} catch (NumberFormatException e) {
88-
throw new CommandLine.ParameterException(spec.commandLine(), "Invalid language-level option value : `" + languageLevel + "`.");
88+
throw new CommandLine.ParameterException(spec1.commandLine(), "Invalid language-level option value : `" + languageLevel1 + "`.");
8989
}
9090
return dataWeaveVersionOption;
9191
}

native-cli/src/main/java/org/mule/weave/cli/pico/PicoAddWizard.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88

99
import java.util.concurrent.Callable;
1010

11+
12+
1113
@CommandLine.Command(
12-
name = "add-wizard",
14+
name = "add",
1315
description =
1416
"Adds a new Wizard to your network of trusted wizards."
1517

native-cli/src/main/java/org/mule/weave/cli/pico/PicoRepl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ protected Integer doCall() {
3131
Optional.ofNullable(inputs).map((s) -> toScalaMap(s)).orElse(Map$.MODULE$.<String, File>empty()),
3232
Optional.ofNullable(literalInput).map((s) -> toScalaMap(s)).orElse(Map$.MODULE$.<String, String>empty()),
3333
Option.apply(privileges).map((s) -> JavaConverters.asScalaBuffer(s).toSeq()),
34-
calculateRuntimeVersion()
34+
calculateRuntimeVersion(languageLevel, spec)
3535
);
3636
ReplCommand replCommand = new ReplCommand(replConfiguration, console);
3737
return replCommand.exec();

native-cli/src/main/java/org/mule/weave/cli/pico/PicoRunScript.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@
1919

2020
@CommandLine.Command(
2121
name = "run",
22-
description =
23-
"Runs provided DW script."
24-
22+
description = "Runs provided DW script."
2523
)
2624
public class PicoRunScript extends AbstractPicoRunCommand {
2725

@@ -55,7 +53,7 @@ protected Integer doCall() {
5553
throw new CommandLine.ParameterException(spec.commandLine(), msg);
5654
}
5755

58-
Option<DataWeaveVersion> dataWeaveVersionOption = calculateRuntimeVersion();
56+
Option<DataWeaveVersion> dataWeaveVersionOption = calculateRuntimeVersion(languageLevel, spec);
5957
final WeaveRunnerConfig config = new WeaveRunnerConfig(
6058
new String[0],
6159
eval,

native-cli/src/main/java/org/mule/weave/cli/pico/PicoRunSpell.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ protected Integer doCall() {
127127
final SpellDependencyManager manager = new SpellDependencyManager(spellFolder, console);
128128
final Function1<NativeRuntime, DependencyResolutionResult[]> resolver = (nr) -> manager.resolveDependencies(nr);
129129

130-
Option<DataWeaveVersion> dataWeaveVersionOption = calculateRuntimeVersion();
130+
Option<DataWeaveVersion> dataWeaveVersionOption = calculateRuntimeVersion(languageLevel, spec);
131131

132132
final WeaveRunnerConfig config = WeaveRunnerConfig.apply(
133133
new String[]{srcFolder.getAbsolutePath()},
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package org.mule.weave.cli.pico;
2+
3+
import org.mule.weave.dwnative.cli.Console;
4+
import org.mule.weave.dwnative.cli.DefaultConsole$;
5+
import org.mule.weave.dwnative.cli.commands.VerifyWeaveCommand;
6+
import org.mule.weave.dwnative.cli.commands.WeaveModule;
7+
import org.mule.weave.dwnative.cli.commands.WeaveVerifyConfig;
8+
import org.mule.weave.v2.io.FileHelper;
9+
import org.mule.weave.v2.parser.ast.variables.NameIdentifier;
10+
import org.mule.weave.v2.utils.DataWeaveVersion;
11+
import picocli.CommandLine;
12+
import scala.Option;
13+
14+
import java.io.File;
15+
import java.util.concurrent.Callable;
16+
17+
import static org.mule.weave.cli.pico.AbstractPicoExecCommand.calculateRuntimeVersion;
18+
import static org.mule.weave.cli.pico.AbstractPicoExecCommand.fileToString;
19+
20+
@CommandLine.Command(
21+
name = "validate",
22+
description = "Validate if a script is valid or not."
23+
)
24+
public class PicoValidateScript implements Callable<Integer> {
25+
26+
Console console;
27+
28+
@CommandLine.Option(names = {"--language-level"}, description = {"The version of DW to be supported."})
29+
protected String languageLevel = null;
30+
31+
@CommandLine.Parameters(
32+
index = "0",
33+
arity = "0..1",
34+
description = "The DW script to be used",
35+
paramLabel = "SCRIPT"
36+
)
37+
String script = null;
38+
39+
@CommandLine.Option(names = {"--file", "-f"}, description = "The Path to the dw file to run.")
40+
File dwFile = null;
41+
42+
@CommandLine.Option(names = {"--input", "-i"}, description = "The name of an in implicit input.")
43+
String[] inputs = new String[0];
44+
45+
@CommandLine.Spec
46+
CommandLine.Model.CommandSpec spec = null;
47+
48+
49+
public PicoValidateScript() {
50+
this.console = DefaultConsole$.MODULE$;
51+
}
52+
53+
public PicoValidateScript(Console console) {
54+
this.console = console;
55+
}
56+
57+
@Override
58+
public Integer call() {
59+
if ((script == null && dwFile == null) || (script != null && dwFile != null)) {
60+
String msg = "The script and file parameters are mutually exclusive, but one is required.";
61+
throw new CommandLine.ParameterException(spec.commandLine(), msg);
62+
}
63+
Option<DataWeaveVersion> dataWeaveVersionOption = calculateRuntimeVersion(languageLevel, spec);
64+
65+
WeaveVerifyConfig config = new WeaveVerifyConfig(
66+
67+
((nr) -> {
68+
if (script != null) {
69+
return new WeaveModule(script, NameIdentifier.ANONYMOUS_NAME().name());
70+
} else if (dwFile != null) {
71+
return new WeaveModule(fileToString(dwFile), FileHelper.baseName(dwFile));
72+
} else {
73+
throw new RuntimeException("Missing dw script or main file");
74+
}
75+
}),
76+
dataWeaveVersionOption,
77+
inputs
78+
79+
);
80+
final VerifyWeaveCommand command = new VerifyWeaveCommand(config, console);
81+
return command.exec();
82+
}
83+
84+
85+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.mule.weave.cli.pico;
2+
3+
import picocli.CommandLine;
4+
5+
@CommandLine.Command(
6+
name = "wizard",
7+
description = "Wizard actions.",
8+
subcommands = {
9+
PicoAddWizard.class
10+
}
11+
)
12+
public class PicoWizard implements Runnable {
13+
14+
@CommandLine.Spec
15+
CommandLine.Model.CommandSpec spec;
16+
17+
@Override
18+
public void run() {
19+
spec.commandLine().usage(System.out);
20+
}
21+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class NativeRuntime(libDir: File, path: Array[File], console: Console, maybeLang
101101
}
102102
}
103103

104-
private def compileScript(script: String, inputs: ScriptingBindings, nameIdentifier: NameIdentifier, defaultOutputMimeType: String) = {
104+
def compileScript(script: String, inputs: ScriptingBindings, nameIdentifier: NameIdentifier, defaultOutputMimeType: String): DataWeaveScript = {
105105
var config = weaveScriptingEngine.newConfig()
106106
.withScript(script)
107107
.withInputs(inputs.entries().map(wi => new InputType(wi, None)).toArray)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.mule.weave.dwnative.cli.commands
2+
3+
import org.mule.weave.dwnative.NativeRuntime
4+
import org.mule.weave.dwnative.cli.Console
5+
import org.mule.weave.dwnative.utils.DataWeaveUtils
6+
import org.mule.weave.v2.parser.ast.variables.NameIdentifier
7+
import org.mule.weave.v2.parser.phase.CompilationException
8+
import org.mule.weave.v2.runtime.ScriptingBindings
9+
import org.mule.weave.v2.utils.DataWeaveVersion
10+
11+
class VerifyWeaveCommand(val config: WeaveVerifyConfig, console: Console) extends WeaveCommand {
12+
val weaveUtils = new DataWeaveUtils(console)
13+
14+
def exec(): Int = {
15+
var exitCode: Int = ExitCodes.SUCCESS
16+
val nativeRuntime: NativeRuntime = new NativeRuntime(weaveUtils.getLibPathHome(), Array(), console, config.maybeLanguageLevel)
17+
val weaveModule = config.scriptToRun(nativeRuntime)
18+
try {
19+
console.info(s"Compiling `${weaveModule.nameIdentifier}`...")
20+
val bindings = ScriptingBindings()
21+
config.inputs.foreach((input) => {
22+
bindings.addBinding(input, "")
23+
})
24+
nativeRuntime.compileScript(weaveModule.content, bindings, NameIdentifier(weaveModule.nameIdentifier), "")
25+
console.info(s"No errors found.")
26+
} catch {
27+
case ce: CompilationException => {
28+
val ee = ce.getErrorMessages()
29+
ee.foreach((em) => {
30+
console.error(em)
31+
})
32+
console.info(s"${ee.length} errors found")
33+
exitCode = ExitCodes.FAILURE
34+
}
35+
}
36+
exitCode
37+
}
38+
}
39+
40+
case class WeaveVerifyConfig(
41+
scriptToRun: NativeRuntime => WeaveModule,
42+
maybeLanguageLevel: Option[DataWeaveVersion],
43+
inputs: Array[String]
44+
)
45+

0 commit comments

Comments
 (0)