-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
11 changed files
with
172 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
native-cli/src/main/java/org/mule/weave/cli/pico/PicoValidateScript.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package org.mule.weave.cli.pico; | ||
|
||
import org.mule.weave.dwnative.cli.Console; | ||
import org.mule.weave.dwnative.cli.DefaultConsole$; | ||
import org.mule.weave.dwnative.cli.commands.VerifyWeaveCommand; | ||
import org.mule.weave.dwnative.cli.commands.WeaveModule; | ||
import org.mule.weave.dwnative.cli.commands.WeaveVerifyConfig; | ||
import org.mule.weave.v2.io.FileHelper; | ||
import org.mule.weave.v2.parser.ast.variables.NameIdentifier; | ||
import org.mule.weave.v2.utils.DataWeaveVersion; | ||
import picocli.CommandLine; | ||
import scala.Option; | ||
|
||
import java.io.File; | ||
import java.util.concurrent.Callable; | ||
|
||
import static org.mule.weave.cli.pico.AbstractPicoExecCommand.calculateRuntimeVersion; | ||
import static org.mule.weave.cli.pico.AbstractPicoExecCommand.fileToString; | ||
|
||
@CommandLine.Command( | ||
name = "validate", | ||
description = "Validate if a script is valid or not." | ||
) | ||
public class PicoValidateScript implements Callable<Integer> { | ||
|
||
Console console; | ||
|
||
@CommandLine.Option(names = {"--language-level"}, description = {"The version of DW to be supported."}) | ||
protected String languageLevel = null; | ||
|
||
@CommandLine.Parameters( | ||
index = "0", | ||
arity = "0..1", | ||
description = "The DW script to be used", | ||
paramLabel = "SCRIPT" | ||
) | ||
String script = null; | ||
|
||
@CommandLine.Option(names = {"--file", "-f"}, description = "The Path to the dw file to run.") | ||
File dwFile = null; | ||
|
||
@CommandLine.Option(names = {"--input", "-i"}, description = "The name of an in implicit input.") | ||
String[] inputs = new String[0]; | ||
|
||
@CommandLine.Spec | ||
CommandLine.Model.CommandSpec spec = null; | ||
|
||
|
||
public PicoValidateScript() { | ||
this.console = DefaultConsole$.MODULE$; | ||
} | ||
|
||
public PicoValidateScript(Console console) { | ||
this.console = console; | ||
} | ||
|
||
@Override | ||
public Integer call() { | ||
if ((script == null && dwFile == null) || (script != null && dwFile != null)) { | ||
String msg = "The script and file parameters are mutually exclusive, but one is required."; | ||
throw new CommandLine.ParameterException(spec.commandLine(), msg); | ||
} | ||
Option<DataWeaveVersion> dataWeaveVersionOption = calculateRuntimeVersion(languageLevel, spec); | ||
|
||
WeaveVerifyConfig config = new WeaveVerifyConfig( | ||
|
||
((nr) -> { | ||
if (script != null) { | ||
return new WeaveModule(script, NameIdentifier.ANONYMOUS_NAME().name()); | ||
} else if (dwFile != null) { | ||
return new WeaveModule(fileToString(dwFile), FileHelper.baseName(dwFile)); | ||
} else { | ||
throw new RuntimeException("Missing dw script or main file"); | ||
} | ||
}), | ||
dataWeaveVersionOption, | ||
inputs | ||
|
||
); | ||
final VerifyWeaveCommand command = new VerifyWeaveCommand(config, console); | ||
return command.exec(); | ||
} | ||
|
||
|
||
} |
21 changes: 21 additions & 0 deletions
21
native-cli/src/main/java/org/mule/weave/cli/pico/PicoWizard.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.mule.weave.cli.pico; | ||
|
||
import picocli.CommandLine; | ||
|
||
@CommandLine.Command( | ||
name = "wizard", | ||
description = "Wizard actions.", | ||
subcommands = { | ||
PicoAddWizard.class | ||
} | ||
) | ||
public class PicoWizard implements Runnable { | ||
|
||
@CommandLine.Spec | ||
CommandLine.Model.CommandSpec spec; | ||
|
||
@Override | ||
public void run() { | ||
spec.commandLine().usage(System.out); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
native-cli/src/main/scala/org/mule/weave/dwnative/cli/commands/VerifyWeaveCommand.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package org.mule.weave.dwnative.cli.commands | ||
|
||
import org.mule.weave.dwnative.NativeRuntime | ||
import org.mule.weave.dwnative.cli.Console | ||
import org.mule.weave.dwnative.utils.DataWeaveUtils | ||
import org.mule.weave.v2.parser.ast.variables.NameIdentifier | ||
import org.mule.weave.v2.parser.phase.CompilationException | ||
import org.mule.weave.v2.runtime.ScriptingBindings | ||
import org.mule.weave.v2.utils.DataWeaveVersion | ||
|
||
class VerifyWeaveCommand(val config: WeaveVerifyConfig, console: Console) extends WeaveCommand { | ||
val weaveUtils = new DataWeaveUtils(console) | ||
|
||
def exec(): Int = { | ||
var exitCode: Int = ExitCodes.SUCCESS | ||
val nativeRuntime: NativeRuntime = new NativeRuntime(weaveUtils.getLibPathHome(), Array(), console, config.maybeLanguageLevel) | ||
val weaveModule = config.scriptToRun(nativeRuntime) | ||
try { | ||
console.info(s"Compiling `${weaveModule.nameIdentifier}`...") | ||
val bindings = ScriptingBindings() | ||
config.inputs.foreach((input) => { | ||
bindings.addBinding(input, "") | ||
}) | ||
nativeRuntime.compileScript(weaveModule.content, bindings, NameIdentifier(weaveModule.nameIdentifier), "") | ||
console.info(s"No errors found.") | ||
} catch { | ||
case ce: CompilationException => { | ||
val ee = ce.getErrorMessages() | ||
ee.foreach((em) => { | ||
console.error(em) | ||
}) | ||
console.info(s"${ee.length} errors found") | ||
exitCode = ExitCodes.FAILURE | ||
} | ||
} | ||
exitCode | ||
} | ||
} | ||
|
||
case class WeaveVerifyConfig( | ||
scriptToRun: NativeRuntime => WeaveModule, | ||
maybeLanguageLevel: Option[DataWeaveVersion], | ||
inputs: Array[String] | ||
) | ||
|