Seedking пре 1 година
родитељ
комит
9ac908a215

+ 1 - 0
change-notes.html

@@ -2,6 +2,7 @@
 <ul>
 <ul>
     <li>[FIX]: Update version</li>
     <li>[FIX]: Update version</li>
     <li>[FIX]: Upgrade ui api version</li>
     <li>[FIX]: Upgrade ui api version</li>
+    <li>[FIX]: Refactoring ioRunv()</li>
 </ul>
 </ul>
 <strong>1.3.2</strong>
 <strong>1.3.2</strong>
 <ul>
 <ul>

+ 2 - 1
src/main/kotlin/io/xmake/project/XMakeDirectoryProjectGenerator.kt

@@ -14,6 +14,7 @@ import io.xmake.icons.XMakeIcons
 import io.xmake.utils.SystemUtils
 import io.xmake.utils.SystemUtils
 import java.io.File
 import java.io.File
 import javax.swing.Icon
 import javax.swing.Icon
+import io.xmake.utils.runVOutAll
 
 
 
 
 class XMakeDirectoryProjectGenerator : DirectoryProjectGeneratorBase<XMakeConfigData>(),
 class XMakeDirectoryProjectGenerator : DirectoryProjectGeneratorBase<XMakeConfigData>(),
@@ -33,7 +34,7 @@ class XMakeDirectoryProjectGenerator : DirectoryProjectGeneratorBase<XMakeConfig
          * @note we muse use ioRunv instead of Runv to read all output, otherwise it will wait forever on windows
          * @note we muse use ioRunv instead of Runv to read all output, otherwise it will wait forever on windows
          */
          */
         val tmpdir = "$contentEntryPath.dir"
         val tmpdir = "$contentEntryPath.dir"
-        SystemUtils.ioRunv(
+        runVOutAll(
             listOf(
             listOf(
                 SystemUtils.xmakeProgram,
                 SystemUtils.xmakeProgram,
                 "create",
                 "create",

+ 2 - 1
src/main/kotlin/io/xmake/project/XMakeModuleBuilder.kt

@@ -10,6 +10,7 @@ import com.intellij.openapi.util.Disposer
 import com.intellij.openapi.util.io.FileUtil
 import com.intellij.openapi.util.io.FileUtil
 import com.intellij.openapi.vfs.LocalFileSystem
 import com.intellij.openapi.vfs.LocalFileSystem
 import io.xmake.utils.SystemUtils
 import io.xmake.utils.SystemUtils
+import io.xmake.utils.runVOutAll
 import java.io.File
 import java.io.File
 
 
 
 
@@ -37,7 +38,7 @@ class XMakeModuleBuilder : ModuleBuilder() {
          * @note we muse use ioRunv instead of Runv to read all output, otherwise it will wait forever on windows
          * @note we muse use ioRunv instead of Runv to read all output, otherwise it will wait forever on windows
          */
          */
         val tmpdir = "$contentEntryPath.dir"
         val tmpdir = "$contentEntryPath.dir"
-        SystemUtils.ioRunv(
+        runVOutAll(
             listOf(
             listOf(
                 SystemUtils.xmakeProgram,
                 SystemUtils.xmakeProgram,
                 "create",
                 "create",

+ 3 - 2
src/main/kotlin/io/xmake/shared/XMakeConfiguration.kt

@@ -8,6 +8,7 @@ import com.intellij.openapi.components.ProjectComponent
 import com.intellij.openapi.diagnostic.Logger
 import com.intellij.openapi.diagnostic.Logger
 import com.intellij.openapi.project.Project
 import com.intellij.openapi.project.Project
 import io.xmake.utils.SystemUtils
 import io.xmake.utils.SystemUtils
+import io.xmake.utils.runVOutAll
 
 
 @State(name = "XMakeProjectSettings")
 @State(name = "XMakeProjectSettings")
 class XMakeConfiguration(// the project
 class XMakeConfiguration(// the project
@@ -132,7 +133,7 @@ class XMakeConfiguration(// the project
 
 
             // make targets
             // make targets
             var targets = arrayOf("default", "all")
             var targets = arrayOf("default", "all")
-            val results = SystemUtils.ioRunv(
+            val results = runVOutAll(
                 listOf(
                 listOf(
                     SystemUtils.xmakeProgram,
                     SystemUtils.xmakeProgram,
                     "l",
                     "l",
@@ -140,7 +141,7 @@ class XMakeConfiguration(// the project
                     "import(\"core.project.config\"); import(\"core.project.project\"); config.load(); for name, _ in pairs((project.targets())) do print(name) end"
                     "import(\"core.project.config\"); import(\"core.project.project\"); config.load(); for name, _ in pairs((project.targets())) do print(name) end"
                 ), data.workingDirectory
                 ), data.workingDirectory
             )
             )
-            results.split("\n").forEach {
+            results.forEach {
                 if (it.trim() != "") {
                 if (it.trim() != "") {
                     targets += it
                     targets += it
                 }
                 }

+ 57 - 0
src/main/kotlin/io/xmake/utils/Command.kt

@@ -0,0 +1,57 @@
+package io.xmake.utils
+
+import com.intellij.execution.configurations.GeneralCommandLine
+import com.intellij.execution.util.ExecUtil
+import io.xmake.utils.interact.kSystemEnv
+import io.xmake.utils.interact.kLineSeparator
+/**
+ * [runVOutAll]
+ *
+ * @param argv the command arguments
+ * @param workDir the working directory
+ * @return a List<String>, the all output of the command
+ */
+fun runVOutAll(argv: List<String>, workDir: String? = null): List<String> {
+    val ret: List<String>
+    val commandLine: GeneralCommandLine = GeneralCommandLine(argv)
+        .withWorkDirectory(workDir)
+        .withCharset(Charsets.UTF_8)
+        .withEnvironment(kSystemEnv)
+        .withParentEnvironmentType(GeneralCommandLine.ParentEnvironmentType.CONSOLE)
+    commandLine.withEnvironment("COLORTERM", "nocolor")
+    val commandOutput = ExecUtil.execAndGetOutput(commandLine).stdout
+    ret = commandOutput.split(kLineSeparator)
+    return ret
+}
+
+
+/**
+ * [runVOutLine]
+ *
+ * @param argv the command arguments
+ * @param minLine return begin line
+ * @param maxLine return end line
+ * @param workDir the working directory
+ * @return a string containing the lines of output from the command,
+ * starting from the line number specified by `minLine` and ending at the line number specified by `maxLine`.
+ * If the command produces fewer lines than `minLine`, the return will be an empty string.
+ * If the command produces lines but fewer than `maxLine`, all lines from `minLine` to the end of output will be returned.
+ * Lines are returned as a single string, with each line separated by the system's line separator.
+ */
+
+inline fun runVOutLine(argv: List<String>, minLine: Int, maxLine: Int = minLine, workDir: String? = null): String {
+    TODO()
+}
+
+/**
+ * [vRunV]
+ *
+ * output on console
+ * @param console the console
+ * @param argv the command arguments
+ * @param workDir the working directory
+ * @return void
+ */
+inline fun vRunV(console: String/*TODO()*/, argv: List<String>, workDir: String? = null) {
+    TODO()
+}

+ 2 - 80
src/main/kotlin/io/xmake/utils/SystemUtils.kt

@@ -45,7 +45,7 @@ object SystemUtils {
             )
             )
             for (program in programs) {
             for (program in programs) {
                 if (program == "xmake" || File(program).exists()) {
                 if (program == "xmake" || File(program).exists()) {
-                    val result = ioRunv(listOf(program, "--version"))
+                    val result = runVOutAll(listOf(program, "--version"))
                     if (result.isNotEmpty()) {
                     if (result.isNotEmpty()) {
                         _xmakeProgram = program
                         _xmakeProgram = program
                         break
                         break
@@ -63,7 +63,7 @@ object SystemUtils {
     var xmakeVersion: String
     var xmakeVersion: String
         get() {
         get() {
             if (_xmakeVersion == "") {
             if (_xmakeVersion == "") {
-                val result = ioRunv(listOf(xmakeProgram, "--version")).split(',')
+                val result = runVOutAll(listOf(xmakeProgram, "--version"))[0].split(',')
                 if (result.isNotEmpty()) {
                 if (result.isNotEmpty()) {
                     _xmakeVersion = result[0]
                     _xmakeVersion = result[0]
                 }
                 }
@@ -81,84 +81,6 @@ object SystemUtils {
         else -> "linux"
         else -> "linux"
     }
     }
 
 
-    // run command with arguments
-    fun Runv(argv: List<String>, workingDirectory: String? = null): Int {
-
-        var code = -1
-        try {
-
-            // init process builder
-            val processBuilder = ProcessBuilder(argv)
-
-            // init working directory
-            if (workingDirectory !== null) {
-                processBuilder.directory(File(workingDirectory))
-            }
-
-            // run process
-            val process = processBuilder.start()
-
-            // wait for process
-            code = process.waitFor()
-
-        } catch (e: IOException) {
-            e.printStackTrace()
-        } finally {
-        }
-        return code
-    }
-
-    // run command with arguments and return output
-    fun ioRunv(argv: List<String>, workingDirectory: String? = null): String {
-
-        var result = ""
-        var bufferReader: BufferedReader? = null
-        try {
-
-            // init process builder
-            val processBuilder = ProcessBuilder(argv)
-
-            // init working directory
-            if (workingDirectory !== null) {
-                processBuilder.directory(File(workingDirectory))
-            }
-
-            // disable color for xmake
-            processBuilder.environment().put("COLORTERM", "nocolor")
-
-            // run process
-            val process = processBuilder.start()
-
-            // get input buffer reader
-            bufferReader = BufferedReader(InputStreamReader(process.getInputStream()))
-
-            // get io output
-            var line: String? = bufferReader.readLine()
-            while (line != null) {
-                result += line + "\n"
-                line = bufferReader.readLine()
-            }
-
-            // wait for process
-            if (process.waitFor() != 0)
-                result = ""
-
-        } catch (e: IOException) {
-            e.printStackTrace()
-        } finally {
-
-            if (bufferReader != null) {
-                try {
-                    bufferReader.close()
-                } catch (e: Exception) {
-                    e.printStackTrace()
-                }
-
-            }
-        }
-        return result
-    }
-
     // parse problems for the given line
     // parse problems for the given line
     private fun parseProblem(info: String): XMakeProblem? {
     private fun parseProblem(info: String): XMakeProblem? {
 
 

+ 8 - 0
src/main/kotlin/io/xmake/utils/interact/LocalData.kt

@@ -0,0 +1,8 @@
+package io.xmake.utils.interact
+
+
+val kSystemEnv: MutableMap<String, String>?
+    get() = System.getenv()
+
+val kLineSeparator: String
+    get() = System.lineSeparator()