diff --git a/native-cli/src/main/scala/org/mule/weave/dwnative/NativeRuntime.scala b/native-cli/src/main/scala/org/mule/weave/dwnative/NativeRuntime.scala index 8a73633..1a11960 100644 --- a/native-cli/src/main/scala/org/mule/weave/dwnative/NativeRuntime.scala +++ b/native-cli/src/main/scala/org/mule/weave/dwnative/NativeRuntime.scala @@ -12,15 +12,7 @@ import org.mule.weave.v2.io.service.CustomWorkingDirectoryService import org.mule.weave.v2.io.service.WorkingDirectoryService import org.mule.weave.v2.model.EvaluationContext import org.mule.weave.v2.model.ServiceManager -import org.mule.weave.v2.model.service.CharsetProviderService -import org.mule.weave.v2.model.service.DefaultSecurityManagerService -import org.mule.weave.v2.model.service.LoggingService -import org.mule.weave.v2.model.service.ProtocolUrlSourceProviderResolverService -import org.mule.weave.v2.model.service.ReadFunctionProtocolHandler -import org.mule.weave.v2.model.service.SecurityManagerService -import org.mule.weave.v2.model.service.UrlProtocolHandler -import org.mule.weave.v2.model.service.UrlSourceProviderResolverService -import org.mule.weave.v2.model.service.WeaveRuntimePrivilege +import org.mule.weave.v2.model.service.{CharsetProviderService, DefaultLanguageLevelService, DefaultSecurityManagerService, LanguageLevelService, LoggingService, ProtocolUrlSourceProviderResolverService, ReadFunctionProtocolHandler, SecurityManagerService, UrlProtocolHandler, UrlSourceProviderResolverService, WeaveLanguageLevelService, WeaveRuntimePrivilege} import org.mule.weave.v2.model.values.BinaryValue import org.mule.weave.v2.module.reader.AutoPersistedOutputStream import org.mule.weave.v2.module.reader.SourceProvider @@ -46,6 +38,7 @@ import org.mule.weave.v2.sdk.NameIdentifierHelper import org.mule.weave.v2.sdk.SPIBasedModuleLoaderProvider import org.mule.weave.v2.sdk.TwoLevelWeaveResourceResolver import org.mule.weave.v2.sdk.WeaveResourceResolver +import org.mule.weave.v2.utils.DataWeaveVersion import java.io.File import java.io.OutputStream @@ -53,16 +46,24 @@ import java.io.PrintWriter import java.io.StringWriter import java.nio.charset.Charset import java.nio.charset.StandardCharsets +import java.util.Properties -class NativeRuntime(libDir: File, path: Array[File], console: Console) { +class NativeRuntime(libDir: File, path: Array[File], console: Console, maybeLanguageLevel: Option[DataWeaveVersion] = None) { private val dataWeaveUtils = new DataWeaveUtils(console) private val pathBasedResourceResolver: PathBasedResourceResolver = PathBasedResourceResolver(path ++ Option(libDir.listFiles()).getOrElse(new Array[File](0))) + private val languageLevelService: LanguageLevelService = { + maybeLanguageLevel match { + case Some(version) => WeaveLanguageLevelService(version) + case None => DefaultLanguageLevelService + } + } + private val weaveScriptingEngine: DataWeaveScriptingEngine = { setupEnv() - DataWeaveScriptingEngine(new NativeModuleComponentFactory(() => pathBasedResourceResolver, systemFirst = true), ParserConfiguration()) + new DataWeaveScriptingEngine(new NativeModuleComponentFactory(() => pathBasedResourceResolver, systemFirst = true), ParserConfiguration(), new Properties(), languageLevelService = languageLevelService) } if (console.isDebugEnabled()) { 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 422b599..3e3e3e0 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 @@ -23,6 +23,7 @@ import org.mule.weave.v2.io.FileHelper import org.mule.weave.v2.parser.ast.variables.NameIdentifier import org.mule.weave.v2.runtime.utils.AnsiColor.red import org.mule.weave.v2.sdk.NameIdentifierHelper +import org.mule.weave.v2.utils.DataWeaveVersion import java.io.File import scala.collection.mutable @@ -40,6 +41,7 @@ class CLIArgumentsParser(console: Console) { var output: Option[String] = None var eval: Boolean = false var maybePrivileges: Option[Seq[String]] = None + var maybeLanguageLevel: Option[DataWeaveVersion] = None var dependencyResolver: Option[(NativeRuntime) => Array[DependencyResolutionResult]] = None @@ -278,6 +280,11 @@ class CLIArgumentsParser(console: Console) { val privileges = commandLine.getOptionValue(Options.PRIVILEGES) maybePrivileges = Some(privileges.split(",")) } + + if (commandLine.hasOption(Options.LANGUAGE_LEVEL)) { + val languageLevelStr = commandLine.getOptionValue(Options.LANGUAGE_LEVEL) + maybeLanguageLevel = Some(DataWeaveVersion(languageLevelStr)) + } val commandLineArgs = commandLine.getArgs if (commandLineArgs != null) { @@ -300,7 +307,7 @@ class CLIArgumentsParser(console: Console) { Right(s"Missing or -f or --spell ") } else { - val config: WeaveRunnerConfig = WeaveRunnerConfig(paths, eval, scriptToRun.get, dependencyResolver ,params.toMap, inputs.toMap, output, maybePrivileges) + val config: WeaveRunnerConfig = WeaveRunnerConfig(paths, eval, scriptToRun.get, dependencyResolver ,params.toMap, inputs.toMap, output, maybePrivileges, maybeLanguageLevel) Left(new RunWeaveCommand(config, 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 407f494..fff9911 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 @@ -23,6 +23,7 @@ object Options { val VERBOSE = "verbose" val VERSION = "version" val EXPERIMENTAL_TAG = "[Experimental]" + val LANGUAGE_LEVEL = "language-level" val OPTIONS: CliOptions = { val options = new CliOptions() @@ -123,7 +124,15 @@ object Options { ) options.addOption(null, UPDATE_GRIMOIRES, false, s"$EXPERIMENTAL_TAG Update all wizard grimoires.") - + + options.addOption(Option.builder() + .longOpt(LANGUAGE_LEVEL) + .hasArg(true) + .argName("language-level") + .desc(s"$LANGUAGE_LEVEL sets the language level under which the script runs") + .build() + ) + options } } diff --git a/native-cli/src/main/scala/org/mule/weave/dwnative/cli/commands/RunWeaveCommand.scala b/native-cli/src/main/scala/org/mule/weave/dwnative/cli/commands/RunWeaveCommand.scala index 0f477ea..92fdfaf 100644 --- a/native-cli/src/main/scala/org/mule/weave/dwnative/cli/commands/RunWeaveCommand.scala +++ b/native-cli/src/main/scala/org/mule/weave/dwnative/cli/commands/RunWeaveCommand.scala @@ -16,6 +16,7 @@ import org.mule.weave.v2.model.values.StringValue import org.mule.weave.v2.module.DataFormatManager import org.mule.weave.v2.runtime.ExecuteResult import org.mule.weave.v2.runtime.ScriptingBindings +import org.mule.weave.v2.utils.DataWeaveVersion import sun.misc.Signal import sun.misc.SignalHandler @@ -24,7 +25,6 @@ import java.io.FileOutputStream import java.io.OutputStream import java.io.PrintWriter import java.io.StringWriter -import scala.collection.immutable import scala.util.Try class RunWeaveCommand(val config: WeaveRunnerConfig, console: Console) extends WeaveCommand { @@ -38,7 +38,7 @@ class RunWeaveCommand(val config: WeaveRunnerConfig, console: Console) extends W def exec(): Int = { var exitCode = ExitCodes.SUCCESS val path: Array[File] = config.path.map(new File(_)) - val nativeRuntime: NativeRuntime = new NativeRuntime(weaveUtils.getLibPathHome(), path, console) + val nativeRuntime: NativeRuntime = new NativeRuntime(weaveUtils.getLibPathHome(), path, console, config.maybeLanguageLevel) config.dependencyResolver.foreach((dep) => { val results = dep(nativeRuntime) results.foreach((dm) => { @@ -170,7 +170,8 @@ case class WeaveRunnerConfig(path: Array[String], params: Map[String, String], inputs: Map[String, File], outputPath: Option[String], - maybePrivileges: Option[Seq[String]]) + maybePrivileges: Option[Seq[String]], + maybeLanguageLevel: Option[DataWeaveVersion]) case class WeaveModule(content: String, nameIdentifier: String) \ No newline at end of file diff --git a/native-cli/src/test/scala/org/mule/weave/dwnative/cli/ArgumentsParserTest.scala b/native-cli/src/test/scala/org/mule/weave/dwnative/cli/ArgumentsParserTest.scala index 5273e87..209fefb 100644 --- a/native-cli/src/test/scala/org/mule/weave/dwnative/cli/ArgumentsParserTest.scala +++ b/native-cli/src/test/scala/org/mule/weave/dwnative/cli/ArgumentsParserTest.scala @@ -96,7 +96,15 @@ class ArgumentsParserTest extends FreeSpec with Matchers { val runWeaveCommand = commandToRun.asInstanceOf[CreateSpellCommand] assert(runWeaveCommand.spellName == "Test") } - + + "should parse language-level correctly" in { + val parser = new CLIArgumentsParser(new TestConsole()) + val value = parser.parse(Array("'Test'", "--language-level", "2.4")) + assert(value.isLeft) + val commandToRun = value.left.get + assert(commandToRun.isInstanceOf[RunWeaveCommand]) + } + "should fail parsing unrecognized argument" in { val parser = new CLIArgumentsParser(new TestConsole()) val value = parser.parse(Array("-o", "/tmp/out.json", "--parameter", "p1", "p2", "p3", "1 to 10" ))