This is an automated email from the ASF dual-hosted git repository.

sergeykamov pushed a commit to branch NLPCRAFT-153
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git

commit 2f7cb15eb51bda3e9c84265f39cef1dfbd62e871
Author: Sergey Kamov <[email protected]>
AuthorDate: Tue Nov 3 09:53:47 2020 +0300

    WIP.
---
 .../nlpcraft/model/tools/cmdline/NCCli.scala       | 167 ++++++++++++++++-----
 1 file changed, 128 insertions(+), 39 deletions(-)

diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCli.scala 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCli.scala
index a176dc4..c3bae70 100644
--- 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCli.scala
+++ 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCli.scala
@@ -27,12 +27,9 @@ import java.util
 import java.util.Date
 import java.util.regex.Pattern
 
-import com.fasterxml.jackson.annotation.JsonInclude.Include
-import com.fasterxml.jackson.databind.ObjectMapper
-import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
-import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature
 import javax.net.ssl.SSLException
 import org.apache.commons.io.input.{ReversedLinesFileReader, Tailer, 
TailerListenerAdapter}
+import org.apache.commons.io.{FileUtils, IOUtils}
 import org.apache.commons.lang3.SystemUtils
 import org.apache.commons.lang3.time.DurationFormatUtils
 import org.apache.http.HttpResponse
@@ -47,12 +44,11 @@ import org.apache.nlpcraft.common.ansi.NCAnsi._
 import org.apache.nlpcraft.common.ansi.{NCAnsi, NCAnsiProgressBar, 
NCAnsiSpinner}
 import org.apache.nlpcraft.common.ascii.NCAsciiTable
 import org.apache.nlpcraft.common.version.NCVersion
-import org.apache.nlpcraft.model.NCModelAdapter
 import org.jline.builtins.Commands
+import org.jline.reader._
 import org.jline.reader.impl.DefaultParser
 import org.jline.reader.impl.DefaultParser.Bracket
 import org.jline.reader.impl.history.DefaultHistory
-import org.jline.reader._
 import org.jline.terminal.{Terminal, TerminalBuilder}
 import org.jline.utils.AttributedString
 import org.jline.utils.InfoCmp.Capability
@@ -1136,8 +1132,57 @@ object NCCli extends App {
         ),
         // TODO:
         Command(
+            name = "project",
+            group = "4. Tools",
+            synopsis = s"Generates project stub.",
+            desc = Some(
+                "Generates project stub for given parameters."
+            ),
+            body = cmdProject,
+            params = Seq(
+                Parameter(
+                    id = "outputDir",
+                    names = Seq("--outputDir", "-d"),
+                    value = Some("path"),
+                    optional = false,
+                    desc = "Output dir."
+                ),
+                Parameter(
+                    id = "baseName",
+                    names = Seq("--baseName", "-n"),
+                    value = Some("name"),
+                    optional = false,
+                    desc = "Base name."
+                ),
+                Parameter(
+                    id = "lang",
+                    names = Seq("--lang", "-l"),
+                    value = Some("name"),
+                    optional = false,
+                    desc = s"Language name. Supported: ${y("'java'")}, 
${y("'scala'")}, ${y("'groovy'")}, ${y("'kotlin'")}."
+                ),
+                Parameter(
+                    id = "buildTool",
+                    names = Seq("--buildTool", "-t"),
+                    value = Some("name"),
+                    optional = false,
+                    desc = s"Build tool name. Supported: ${y("'mvn'")}, 
${y("'grape'")}, ${y("'gradle'")}, ${y("'sbt'")}."
+                )
+            ),
+            examples = Seq(
+                Example(
+                    usage = Seq("> project -d=. -n=myproject -l=java -t=mvn"),
+                    desc = s"Generates project with given parameters"
+                ),
+                Example(
+                    usage = Seq("> project --outputDir=. --baseName=myproject 
--lang=scala --buildTool=sbt"),
+                    desc = s"Generates project with given parameters"
+                )
+            )
+        ),
+        Command(
             name = "model",
-            group = "3. REPL Commands",
+            group = "4. Tools",
             synopsis = s"Generates model file stub.",
             desc = Some(
                 "Generates model file stub as json or yaml file."
@@ -1146,17 +1191,17 @@ object NCCli extends App {
             params = Seq(
                 Parameter(
                     id = "outputDir",
-                    names = Seq("--output-dir", "-d"),
+                    names = Seq("--outputDir", "-d"),
                     value = Some("path"),
                     optional = false,
                     desc = "Output dir."
                 ),
                 Parameter(
                     id = "fileName",
-                    names = Seq("--file-name", "-f"),
-                    value = Some("path"),
+                    names = Seq("--fileName", "-f"),
+                    value = Some("name"),
                     optional = false,
-                    desc = "File name."
+                    desc = s"File name. Supported extensions: ${y("'json'")}, 
${y("'js'")}, ${y("'yaml'")}, ${y("'yml'")}."
                 )
             ),
             examples = Seq(
@@ -1684,7 +1729,8 @@ object NCCli extends App {
 
                     // Reset REPL state right away.
                     state = ReplState()
-                } else
+                }
+                else
                     error(s"Failed to stop the local REST server (pid 
${c(pid)}).")
 
             case None ⇒ throw NoLocalServer()
@@ -1695,18 +1741,16 @@ object NCCli extends App {
      * @param args Arguments, if any, for this command.
      * @param repl Whether or not executing from REPL.
      */
-    private def cmdNoAnsi(cmd: Command, args: Seq[Argument], repl: Boolean): 
Unit = {
+    private def cmdNoAnsi(cmd: Command, args: Seq[Argument], repl: Boolean): 
Unit =
         NCAnsi.setEnabled(false)
-    }
 
     /**
      * @param cmd Command descriptor.
      * @param args Arguments, if any, for this command.
      * @param repl Whether or not executing from REPL.
      */
-    private def cmdAnsi(cmd: Command, args: Seq[Argument], repl: Boolean): 
Unit = {
+    private def cmdAnsi(cmd: Command, args: Seq[Argument], repl: Boolean): 
Unit =
         NCAnsi.setEnabled(true)
-    }
 
     /**
      * @param cmd Command descriptor.
@@ -2219,6 +2263,12 @@ object NCCli extends App {
 
     /**
       *
+      * @param dir
+      */
+    private def isValidDir(dir: File): Boolean = if (dir.exists()) 
dir.isDirectory else dir.mkdirs()
+
+    /**
+      *
       * @param cmd Command descriptor.
       * @param args Arguments, if any, for this command.
       * @param repl Whether or not executing from REPL.
@@ -2235,48 +2285,77 @@ object NCCli extends App {
         if (idx <= 0)
             throw InvalidParameter(cmd, "fileName")
 
-        val isJson =
+        val modelFile =
             fileName.substring(idx + 1).toLowerCase match {
-                case "js" | "json" ⇒ true
-                case "yml" | "yaml" ⇒ false
+                case "js" | "json" ⇒ "cli/model/lightswitch_model.json"
+                case "yml" | "yaml" ⇒ "cli/model/lightswitch_model.yaml"
 
                 case _ ⇒ throw InvalidParameter(cmd, "fileName")
             }
 
-        val dir = new File(outputDir)
-
-        val okDir = if (dir.exists()) dir.isDirectory else dir.mkdirs()
+        val outDir = new File(outputDir)
 
-        if (!okDir)
+        if (!isValidDir(outDir))
             logln(s"${r("FAIL")} ${y(s"Invalid folder: $outputDir")}")
         else {
-            val mdl = new NCModelAdapter ("id", "Generated model", "1.0.0" ) {}
-            val file = new File(outputDir, fileName)
-
-            val mapper =
-                if (isJson)
-                    new ObjectMapper()
-                else
-                    new ObjectMapper(new 
YAMLFactory().disable(Feature.WRITE_DOC_START_MARKER))
-
-            mapper.setSerializationInclusion(Include.NON_NULL)
+            val outFile = new File(outDir, fileName)
 
             try {
-                managed(new FileOutputStream(file)) acquireAndGet { stream ⇒
-                    mapper.writerWithDefaultPrettyPrinter().writeValue(stream, 
mdl)
-
-                    stream.flush()
+                managed(new FileOutputStream(outFile)) acquireAndGet { out ⇒
+                    managed(U.getStream(modelFile)) acquireAndGet { in ⇒
+                        IOUtils.copy(in, out)
+                    }
                 }
 
-                logln(s"\nFile ${c(file.getAbsolutePath)} created.")
+                logln(s"\nFile ${c(outFile.getAbsolutePath)} created.")
             }
             catch {
-                case e: IOException ⇒ logln(s"${r("FAIL")} processing 
${c(file.getAbsolutePath)}: ${y(e.getLocalizedMessage)}")
+                case e: IOException ⇒ logln(s"${r("FAIL")} processing 
${c(outFile.getAbsolutePath)}: ${y(e.getLocalizedMessage)}")
             }
         }
     }
 
     /**
+      *
+      * @param cmd Command descriptor.
+      * @param args Arguments, if any, for this command.
+      * @param repl Whether or not executing from REPL.
+      */
+    private def cmdProject(cmd: Command, args: Seq[Argument], repl: Boolean): 
Unit = {
+        def get(name: String): String =
+            args.find(_.parameter.id == name).flatMap(_.value).getOrElse(throw 
MissingParameter(cmd, name))
+
+        val outputDir = get("outputDir")
+        val baseName = get("baseName")
+        val lang = get("lang").toLowerCase
+        val buildTool = get("buildTool").toLowerCase
+
+        def check(name: String, value: String, supported: String*): Unit =
+           if (!supported.contains(value))
+               throw InvalidParameter(cmd, name)
+
+        check("lang", lang, "java", "scala", "groovy", "kotlin")
+        check("buildTool", buildTool, "mvn", "grape", "gradle", "sbt")
+
+        val outputDirFull = new File(outputDir, baseName)
+
+        if (!isValidDir(outputDirFull))
+            logln(s"${r("FAIL")} ${y(s"Invalid folder: $outputDir")}")
+        else {
+//            FileUtils.copyDirectory(
+//                U.getFile(s"cli/projects/$lang-$buildTool"),
+//                outputDirFull
+//            )
+//            FileUtils.copyFile(
+//                U.getFile(s"cli/model/lightswitch_model.json"),
+//                new File(outputDirFull, 
"src/main/resources/lightswitch_model.json")
+//            )
+
+            logln(s"\nProject ${c(outputDirFull.getAbsolutePath)} created.")
+        }
+    }
+
+    /**
      *
      * @param cmd
      * @param path
@@ -2997,3 +3076,13 @@ object NCCli extends App {
     // Boot up.
     boot(args)
 }
+
+object x extends App {
+    x11()
+
+    def x11(): Unit = {
+        val x = U.getFilesResources("cli/projects/java-mvn")
+
+        println(x)
+    }
+}

Reply via email to