Skip to content

Commit

Permalink
W-11932674: add language level option (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
andres-rad authored Nov 29, 2022
1 parent cea30bd commit 702e490
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -46,23 +38,32 @@ 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
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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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) {
Expand All @@ -300,7 +307,7 @@ class CLIArgumentsParser(console: Console) {
Right(s"Missing <script-content> or -f <file-path> 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))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 {
Expand All @@ -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) => {
Expand Down Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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" ))
Expand Down

0 comments on commit 702e490

Please sign in to comment.