From d0f07c3e0bfe7c60ad23111d03640d88d726506b Mon Sep 17 00:00:00 2001 From: Mariano De Achaval Date: Wed, 24 Nov 2021 18:50:27 -0300 Subject: [PATCH] Fix list-spells and add more tests --- native-cli/build.gradle | 29 +++---- .../org/mule/weave/dwnative/cli/Console.scala | 8 +- .../weave/dwnative/cli/DataWeaveCLI.scala | 16 +++- .../cli/commands/ListSpellsCommand.scala | 49 ++++++----- .../dwnative/cli/utils/SpellsUtils.scala | 11 ++- .../weave/dwnative/cli/DataWeaveCLITest.scala | 7 ++ .../mule/weave/dwnative/cli/TestConsole.scala | 26 +++++- native-common/build.gradle | 84 ------------------- 8 files changed, 102 insertions(+), 128 deletions(-) delete mode 100644 native-common/build.gradle diff --git a/native-cli/build.gradle b/native-cli/build.gradle index bc03b9b..736dc1e 100644 --- a/native-cli/build.gradle +++ b/native-cli/build.gradle @@ -23,7 +23,7 @@ sourceSets { mainClassName = 'org.mule.weave.dwnative.cli.DataWeaveCLI' -dependencies{ +dependencies { compile group: 'org.mule.weave', name: 'runtime', version: weaveVersion compile group: 'org.graalvm.sdk', name: 'graal-sdk', version: '1.0.0-rc12' compile group: 'org.mule.weave', name: 'core-modules', version: weaveVersion @@ -41,8 +41,8 @@ dependencies{ ext { aws = [ - accessKey: System.getenv("AWS_ACCESS_KEY_ID"), - secretKey: System.getenv("AWS_SECRET_KEY") + accessKey: System.getenv("AWS_ACCESS_KEY_ID"), + secretKey: System.getenv("AWS_SECRET_KEY") ] } @@ -58,17 +58,12 @@ task genVersions() { parentFile.mkdirs() } final PrintWriter outputPrinter = new PrintWriter(new FileWriter(componentVersion)) - def properties = new Properties() - def gradleProperties = new File(project.projectDir, "../gradle.properties") - println(gradleProperties) - properties.load(new FileInputStream(gradleProperties)) + outputPrinter.println("package org.mule.weave.v2.version") outputPrinter.println() outputPrinter.println("object ComponentVersion {") - def entrySet = properties.entrySet() - for (Map.Entry entry : entrySet) { - outputPrinter.println("\tvar " + entry.getKey() + " = \"" + entry.getValue() + "\"") - } + outputPrinter.println("\tval weaveVersion = \"" + weaveVersion + "\"") + outputPrinter.println("\tval nativeVersion = \"" + nativeVersion + "\"") outputPrinter.println("}") outputPrinter.close() } @@ -91,7 +86,7 @@ import org.apache.tools.ant.taskdefs.condition.Os task distro(dependsOn: nativeImage, type: Zip) { classifier = "native-distro-" + getOsName() into("libs") { - from { configurations.runtime.filter {it.getName().contains("ndjson-module")} } + from { configurations.runtime.filter { it.getName().contains("ndjson-module") } } } into("bin") { from 'build/graal/' @@ -122,9 +117,9 @@ graal { option "--allow-incomplete-classpath" option "-H:EnableURLProtocols=http,https" option "--initialize-at-run-time=" + - "io.netty,"+ - "org.asynchttpclient,"+ - "org.mule.weave.v2.module.http.netty.HttpAsyncClientService,"+ + "io.netty," + + "org.asynchttpclient," + + "org.mule.weave.v2.module.http.netty.HttpAsyncClientService," + "scala.util.Random," + "org.mule.weave.v2.sdk.SPIBasedModuleLoaderProvider\$" option "--initialize-at-build-time=" + @@ -138,7 +133,7 @@ graal { "org.mule.weave.v2.ts.," + "org.mule.weave.v2.scope.," + "org.mule.weave.v2.scope.," + - "org.mule.weave.v2.grammar.,"+ + "org.mule.weave.v2.grammar.," + "org.mule.weave.v2.sdk.," + "org.mule.weave.v2.utils.," + "org.mule.weave.v2.versioncheck.," + @@ -147,7 +142,7 @@ graal { "org.parboiled2.," + "shapeless.syntax." - //For Runtime execution + //For Runtime execution // "org.mule.weave.v2.interpreted.,"+ // "org.mule.weave.v2.model.values.," // "org.mule.weave.v2.model.types.," diff --git a/native-cli/src/main/scala/org/mule/weave/dwnative/cli/Console.scala b/native-cli/src/main/scala/org/mule/weave/dwnative/cli/Console.scala index 01f6cda..5f56afb 100644 --- a/native-cli/src/main/scala/org/mule/weave/dwnative/cli/Console.scala +++ b/native-cli/src/main/scala/org/mule/weave/dwnative/cli/Console.scala @@ -35,6 +35,8 @@ trait Console { def error(message: String): Unit + def fatal(message: String): Unit + def warn(message: String): Unit def clear(): Unit @@ -49,7 +51,11 @@ object DefaultConsole extends Console { } override def error(message: String): Unit = { - System.err.println(AnsiColor.red(message)) + System.err.println(AnsiColor.red("[ERROR] " + message)) + } + + override def fatal(message: String): Unit = { + System.err.println(AnsiColor.red("[FATAL] " + message)) } override def clear(): Unit = { diff --git a/native-cli/src/main/scala/org/mule/weave/dwnative/cli/DataWeaveCLI.scala b/native-cli/src/main/scala/org/mule/weave/dwnative/cli/DataWeaveCLI.scala index 35c75b0..931fb21 100644 --- a/native-cli/src/main/scala/org/mule/weave/dwnative/cli/DataWeaveCLI.scala +++ b/native-cli/src/main/scala/org/mule/weave/dwnative/cli/DataWeaveCLI.scala @@ -2,6 +2,9 @@ package org.mule.weave.dwnative.cli import org.mule.weave.dwnative.cli.commands.UsageCommand +import java.io.PrintWriter +import java.io.StringWriter + object DataWeaveCLI extends App { { @@ -23,7 +26,18 @@ class DataWeaveCLIRunner { -1 } case Left(weaveCommand) => { - weaveCommand.exec() + try { + weaveCommand.exec() + } catch { + case exception: Exception => { + val exceptionString = new StringWriter() + exception.printStackTrace(new PrintWriter(exceptionString)) + console.error("Unexpected exception happened while executing command. " + + "Please report this as an issue in https://github.com/mulesoft-labs/data-weave-cli/issues with all the details to reproduce.\n" + + s"Stacktrace is: ${exceptionString}") + -1 + } + } } case _ => { 0 diff --git a/native-cli/src/main/scala/org/mule/weave/dwnative/cli/commands/ListSpellsCommand.scala b/native-cli/src/main/scala/org/mule/weave/dwnative/cli/commands/ListSpellsCommand.scala index a39c3e7..024dc1c 100644 --- a/native-cli/src/main/scala/org/mule/weave/dwnative/cli/commands/ListSpellsCommand.scala +++ b/native-cli/src/main/scala/org/mule/weave/dwnative/cli/commands/ListSpellsCommand.scala @@ -5,6 +5,7 @@ import org.mule.weave.dwnative.cli.utils.SpellsUtils import org.mule.weave.dwnative.cli.utils.SpellsUtils._ import java.io.File +import java.io.FileFilter import scala.io.Source class ListSpellsCommand(console: Console) extends WeaveCommand { @@ -21,30 +22,40 @@ class ListSpellsCommand(console: Console) extends WeaveCommand { val builder = new StringBuilder() builder.append("Spells:\n") val grimoires: File = utils.grimoiresFolders() - val grimoiresDirs: Array[File] = grimoires.listFiles() + var grimoiresDirs: Array[File] = listGrimoires(grimoires) + if (grimoiresDirs == null || !new File(grimoires, DATA_WEAVE_GRIMOIRE_FOLDER).exists()) { + new AddWizardCommand(CloneWizardConfig(null), console).exec() + grimoiresDirs = listGrimoires(grimoires) + } if (grimoiresDirs != null) { - grimoiresDirs.foreach((g) => { - val name = if (g.getName.equals(DATA_WEAVE_GRIMOIRE_FOLDER)) "" else utils.wizardName(g.getName) + "/" - val spells = g.listFiles() - if (spells != null) { - spells.foreach((s) => { - if (s.isDirectory && !s.isHidden) { - builder.append(s" - ${name}${s.getName}:") - val readme = new File(s, "Readme.md") - if (readme.exists()) { - val source = Source.fromFile(readme, "UTF-8") - builder.append("\n ") - builder.append(source.mkString.replaceAllLiterally("\n", "\n ").slice(0, 450)) + grimoiresDirs + .foreach((file) => { + val name = if (file.getName.equals(DATA_WEAVE_GRIMOIRE_FOLDER)) "" else utils.wizardName(file.getName) + "/" + val spells = file.listFiles() + if (spells != null) { + spells.foreach((s) => { + if (s.isDirectory && !s.isHidden) { + builder.append(s" - ${name}${s.getName}:") + val readme = new File(s, "Readme.md") + if (readme.exists()) { + val source = Source.fromFile(readme, "UTF-8") + builder.append("\n ") + builder.append(source.mkString.replaceAllLiterally("\n", "\n ").slice(0, 450)) + builder.append("\n") + source.close() + } builder.append("\n") - source.close() } - builder.append("\n") - } - }) - } - }) + }) + } + }) } builder.toString() } + private def listGrimoires(grimoires: File) = { + grimoires.listFiles(new FileFilter { + override def accept(pathname: File): Boolean = pathname.isDirectory && !pathname.isHidden + }) + } } diff --git a/native-cli/src/main/scala/org/mule/weave/dwnative/cli/utils/SpellsUtils.scala b/native-cli/src/main/scala/org/mule/weave/dwnative/cli/utils/SpellsUtils.scala index 1aa69db..88d7894 100644 --- a/native-cli/src/main/scala/org/mule/weave/dwnative/cli/utils/SpellsUtils.scala +++ b/native-cli/src/main/scala/org/mule/weave/dwnative/cli/utils/SpellsUtils.scala @@ -23,8 +23,15 @@ class SpellsUtils(console: Console) { def wizardName(grimoire: String): String = { if (grimoire == null) "DW" - else - grimoire.substring(0, grimoire.length - s"-${DATA_WEAVE_GRIMOIRE_FOLDER}".length) + else { + val length = grimoire.length - s"-${DATA_WEAVE_GRIMOIRE_FOLDER}".length + if (length <= 0) { + console.error("Invalid grimoire name: `" + grimoire + "`") + grimoire + } else { + grimoire.substring(0, length) + } + } } def hoursSinceLastUpdate(): Int = { diff --git a/native-cli/src/test/scala/org/mule/weave/dwnative/cli/DataWeaveCLITest.scala b/native-cli/src/test/scala/org/mule/weave/dwnative/cli/DataWeaveCLITest.scala index 7e1cf47..a1325c0 100644 --- a/native-cli/src/test/scala/org/mule/weave/dwnative/cli/DataWeaveCLITest.scala +++ b/native-cli/src/test/scala/org/mule/weave/dwnative/cli/DataWeaveCLITest.scala @@ -20,6 +20,13 @@ class DataWeaveCLITest extends FreeSpec with Matchers { "should take into account the env variable for default output" in { + val console = new TestConsole(System.in, System.out, Map()) + new DataWeaveCLIRunner().run(Array("--list-spells"), console) + + console.fatalMessages.isEmpty shouldBe (true) + } + + "should work when listing all the spells" in { val stream = new ByteArrayOutputStream() new DataWeaveCLIRunner().run(Array("root: 'Mariano'"), new TestConsole(System.in, stream, Map(DataWeaveUtils.DW_DEFAULT_OUTPUT_MIMETYPE_VAR -> "application/xml"))) val source = Source.fromBytes(stream.toByteArray, "UTF-8") diff --git a/native-cli/src/test/scala/org/mule/weave/dwnative/cli/TestConsole.scala b/native-cli/src/test/scala/org/mule/weave/dwnative/cli/TestConsole.scala index b1559d9..7bbfeeb 100644 --- a/native-cli/src/test/scala/org/mule/weave/dwnative/cli/TestConsole.scala +++ b/native-cli/src/test/scala/org/mule/weave/dwnative/cli/TestConsole.scala @@ -1,14 +1,32 @@ package org.mule.weave.dwnative.cli + import java.io.InputStream import java.io.OutputStream +import scala.collection.mutable.ArrayBuffer + +class TestConsole(val in: InputStream, val out: OutputStream, val envVars: Map[String, String] = Map()) extends Console { + + val infoMessages = new ArrayBuffer[String]() + val errorMessages = new ArrayBuffer[String]() + val fatalMessages = new ArrayBuffer[String]() + val warnMessages = new ArrayBuffer[String]() + -class TestConsole(val in: InputStream,val out: OutputStream, val envVars: Map[String,String] = Map()) extends Console { + override def info(message: String): Unit = { + infoMessages +=(message) + } - override def info(message: String): Unit = DefaultConsole.info(message) + override def error(message: String): Unit = { + errorMessages +=(message) + } - override def error(message: String): Unit = DefaultConsole.error(message) + override def fatal(message: String): Unit = { + fatalMessages +=(message) + } - override def warn(message: String): Unit = DefaultConsole.warn(message) + override def warn(message: String): Unit = { + warnMessages +=(message) + } override def clear(): Unit = DefaultConsole.clear() diff --git a/native-common/build.gradle b/native-common/build.gradle deleted file mode 100644 index 9045542..0000000 --- a/native-common/build.gradle +++ /dev/null @@ -1,84 +0,0 @@ -buildscript { - repositories { - maven { - url "https://plugins.gradle.org/m2/" - } - } - dependencies { - classpath group: 'commons-io', name: 'commons-io', version: '2.5' - } -} - - -plugins { - id 'com.github.johnrengelman.shadow' version '5.2.0' - id 'java' -} - -sourceSets { - main { - scala { - srcDirs = ['src/main/scala', 'build/genresource'] - } - } -} - -// Merging Service Files -shadowJar { - mergeServiceFiles() -} - -dependencies { - - compile group: 'org.mule.weave', name: 'runtime', version: weaveVersion - compile group: 'org.graalvm.sdk', name: 'graal-sdk', version: '1.0.0-rc12' - compile group: 'org.mule.weave', name: 'core-modules', version: weaveVersion - compile group: 'org.mule.weave', name: 'yaml-module', version: weaveVersion - compile group: 'org.mule.weave', name: 'dependency-manager', version: weaveVersion - compile(group: 'org.mule.weave', name: 'raml-module', version: "1.0.0-SNAPSHOT"){ - exclude group: 'org.slf4j' - exclude group: 'org.topbraid.shacl' - } - compile group: 'org.mule.weave', name: 'http-module', version: "1.0.0-SNAPSHOT" - compile group: 'org.mule.weave', name: 'http-netty-module', version: "1.0.0-SNAPSHOT" - -} - -def genDirectory = new File("$project.buildDir/genresource") - - -task genVersions() { - println("Generating Versions File.") - def componentVersion = new File(genDirectory, "org/mule/weave/v2/version/ComponentVersion.scala") - def parentFile = componentVersion.getParentFile() - if (!parentFile.exists()) { - parentFile.mkdirs() - } - final PrintWriter outputPrinter = new PrintWriter(new FileWriter(componentVersion)) - def properties = new Properties() - def gradleProperties = new File(project.projectDir, "../gradle.properties") - println(gradleProperties) - properties.load(new FileInputStream(gradleProperties)) - outputPrinter.println("package org.mule.weave.v2.version") - outputPrinter.println() - outputPrinter.println("object ComponentVersion {") - def entrySet = properties.entrySet() - for (Map.Entry entry : entrySet) { - outputPrinter.println("\tvar " + entry.getKey() + " = \"" + entry.getValue() + "\"") - } - outputPrinter.println("}") - outputPrinter.close() -} - - -defaultTasks += genVersions - -compileScala { - dependsOn genVersions -} - - - - - -