Ver código fonte

ioRunv() -> ioRunvInPool() and complete TODO()

Seedking 1 ano atrás
pai
commit
e1e3535130

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

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

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

@@ -10,7 +10,7 @@ import com.intellij.openapi.util.Disposer
 import com.intellij.openapi.util.io.FileUtil
 import com.intellij.openapi.vfs.LocalFileSystem
 import io.xmake.utils.SystemUtils
-import io.xmake.utils.ioRunv
+import io.xmake.utils.ioRunvInPool
 import java.io.File
 
 
@@ -38,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
          */
         val tmpdir = "$contentEntryPath.dir"
-        ioRunv(
+        ioRunvInPool(
             listOf(
                 SystemUtils.xmakeProgram,
                 "create",

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

@@ -8,7 +8,7 @@ import com.intellij.openapi.components.ProjectComponent
 import com.intellij.openapi.diagnostic.Logger
 import com.intellij.openapi.project.Project
 import io.xmake.utils.SystemUtils
-import io.xmake.utils.ioRunv
+import io.xmake.utils.ioRunvInPool
 
 @State(name = "XMakeProjectSettings")
 class XMakeConfiguration(// the project
@@ -136,7 +136,7 @@ class XMakeConfiguration(// the project
 
             // make targets
             var targets = arrayOf("default", "all")
-            val results = ioRunv(
+            val results = ioRunvInPool(
                 listOf(
                     SystemUtils.xmakeProgram,
                     "l",

+ 58 - 34
src/main/kotlin/io/xmake/utils/Command.kt

@@ -3,64 +3,88 @@ package io.xmake.utils
 import com.intellij.execution.configurations.GeneralCommandLine
 import com.intellij.execution.process.ProcessNotCreatedException
 import com.intellij.execution.util.ExecUtil
-import com.jetbrains.rd.util.Callable
-import io.xmake.utils.interact.kSystemEnv
-import io.xmake.utils.interact.kLineSeparator
-import java.io.FileNotFoundException
-import java.io.IOException
+import com.intellij.openapi.application.ApplicationManager
+import com.jetbrains.rd.util.Runnable
+import io.xmake.utils.interact.kSysEnv
+import io.xmake.utils.interact.kLineSep
+import java.util.concurrent.Callable
 import java.util.concurrent.Executors
 import java.util.concurrent.Future
 
 /**
  * [ioRunv]
  *
+ * don't use it easily, it will block the main thread
+ *
  * @param argv the command arguments
  * @param workDir the working directory
  * @return a List<String>, the all output of the command
+ *
+ * error: return empty list -> emptyList()
  */
 fun ioRunv(argv: List<String>, workDir: String? = null): List<String> {
-    val call = Callable {
-        val ret: List<String> = listOf("")
-        try {
-            val commandLine: GeneralCommandLine = GeneralCommandLine(argv)
-                .withWorkDirectory(workDir)
-                .withCharset(Charsets.UTF_8)
-                .withEnvironment(kSystemEnv)
-                .withParentEnvironmentType(GeneralCommandLine.ParentEnvironmentType.CONSOLE)
-            commandLine.withEnvironment("COLORTERM", "nocolor")
-            val output = ExecUtil.execAndGetOutput(commandLine)
-            output.stdout.split(kLineSeparator)
-        } catch (e: Exception) {
-            ret
-        }
+    val commandLine: GeneralCommandLine = GeneralCommandLine(argv)
+        .withWorkDirectory(workDir)
+        .withCharset(Charsets.UTF_8)
+        .withEnvironment(kSysEnv)
+        .withParentEnvironmentType(GeneralCommandLine.ParentEnvironmentType.CONSOLE)
+    commandLine.withEnvironment("COLORTERM", "nocolor")
+    try {
+        val output = ExecUtil.execAndGetOutput(commandLine)
+        return output.stdout.split(kLineSep)
+    } catch (e: ProcessNotCreatedException) {
+        return emptyList()
     }
-
-    val executor = Executors.newSingleThreadExecutor()
-    val commandOutput: Future<List<String>> = executor.submit(call)
-    val result = commandOutput.get()
-    executor.shutdown()
-    return result
 }
 
+fun ioRunvInPool(argv: List<String>, workDir: String? = null): List<String> {
+    val callable: Callable<List<String>> = Callable {
+        return@Callable ioRunv(argv, workDir)
+    }
+    return ApplicationManager.getApplication().executeOnPooledThread(callable).get()
+}
 
 /**
- * [ioRunvLine]
+ * [ioRunvBackground]
  *
+ * processing in background
  * @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.
+ * @return Unit (void)
+ *
+ * error: pop up prompt in the lower right corner if the operation fails
  */
+fun ioRunvBackground(argv: List<String>, workDir: String? = null): Unit {
+    TODO()
+}
 
-fun ioRunvLine(argv: List<String>, minLine: Int, maxLine: Int = minLine, workDir: String? = null): String {
+/**
+ * [ioRunvNonBlock]
+ *
+ * will not block the main thread, will poll for return
+ *
+ * @param argv the command arguments
+ * @param workDir the working directory
+ * @return a List<String>, the all output of the command
+ *
+ * error: return empty list -> emptyList()
+ */
+fun ioRunvNonBlock(argv: List<String>, workDir: String? = null): List<String>{
     TODO()
 }
 
+fun ioRunvSingle(argv: List<String>, workDir: String? = null): List<String> {
+    val call = Callable {
+        return@Callable ioRunv(argv, workDir)
+    }
+
+    val executor = Executors.newSingleThreadExecutor()
+    val commandOutput: Future<List<String>> = executor.submit(call)
+    val result = commandOutput.get()
+    executor.shutdown()
+    return result
+}
+
 /**
  * [vRunv]
  *

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

@@ -43,7 +43,7 @@ object SystemUtils {
             )
             for (program in programs) {
                 if (program == "xmake" || File(program).exists()) {
-                    val result = ioRunv(listOf(program, "--version"))
+                    val result = ioRunvInPool(listOf(program, "--version"))
                     if (result.isNotEmpty()) {
                         _xmakeProgram = program
                         break

+ 113 - 13
src/main/kotlin/io/xmake/utils/interact/XMakeData.kt

@@ -1,30 +1,130 @@
 package io.xmake.utils.interact
 
-import io.xmake.utils.SystemUtils
-import io.xmake.utils.ioRunv
+import io.xmake.utils.ioRunvInPool
 
+/*
+ * ioTemp: return of ioRunv()
+ */
+
+/**
+ * [kXMakeFind]
+ *
+ * @return a boolean, true if xmake is found
+ */
 val kXMakeFind:Boolean
-    get() = TODO()
+    get() = ioRunvInPool(listOf("xmake", "--version")).isNotEmpty()
 
+/**
+ * [kXMakeVersion]
+ *
+ * @return string of xmake version
+ *
+ * like: "2.8.6 HEAD.211710b67"
+ *
+ * error: return empty string -> ""
+ */
 val kXMakeVersion:String
     get() {
-        val ioTemp: String = ioRunv(listOf(SystemUtils.xmakeProgram, "--version"))[0]
+        val ioTemp: List<String> = ioRunvInPool(listOf("xmake", "--version"))
         if (ioTemp.isNotEmpty()) {
-            val verTemp = ioTemp.split(' ')[1]
-            return verTemp.substring(1, verTemp.length - 1)
-
+            val verTemp = ioTemp[0].split(' ')[1]
+            return verTemp.substring(1, verTemp.length - 1).replace('+', ' ')
         }
-        return ioTemp
+        return ""
     }
 
 val kXMakeInstallDir:String
     get() = TODO()
 
-val kArchList:List<String>
-    get() = TODO()
-
+/**
+ * [kPlatList]
+ *
+ * @return a list of plat string
+ *
+ * error: return empty list -> emptyList()
+ */
 val kPlatList:List<String>
-    get() = TODO()
+    get() {
+        //TODO("可能使用xmake完整路径作为输入")
+        val ioTemp: List<String> = ioRunvInPool(listOf("xmake", "f", "config"))
+        if(ioTemp.isEmpty()) return emptyList()
+
+        val ret: List<String>
+        val regex: Regex = Regex("""-\s(\w+)""")
+        val searchBegin: String = "-p PLAT"
+        val searchEnd: String = "-a ARCH"
+        var indexBegin: Int = 0
+        var indexEnd: Int = 0
+
+        for(index in ioTemp.indices) {
+            if(!ioTemp[index].contains(searchBegin)) {
+                continue
+            } else {
+                indexBegin = index
+                break
+            }
+        }
+
+        for(index in indexBegin until ioTemp.size) {
+            if(!ioTemp[index].contains(searchEnd)) {
+                continue
+            } else {
+                indexEnd = index
+                break
+            }
+        }
 
+        val subListTemp = ioTemp.subList(indexBegin + 1, indexEnd)
+        ret = subListTemp.map {
+            regex.find(it)?.groups?.get(1)?.value ?: ""
+        }
+        return ret
+    }
+
+/**
+ * [kPlatArchMap]
+ *
+ * @return a map of plat and arch list
+ *
+ * error: return empty map -> emptyMap()
+ */
 val kPlatArchMap:Map<String, List<String>>
-    get() = TODO()
+    get() {
+        //TODO("可能使用xmake完整路径作为输入")
+        val ioTemp: List<String> = ioRunvInPool(listOf("xmake", "f", "config"))
+        if(ioTemp.isEmpty()) return emptyMap()
+
+        val ret: Map<String, List<String>>
+        val regex: Regex = Regex("""-\s(\w+): (.+)""")
+        val searchBegin: String = "-a ARCH"
+        val searchEnd: String = "-m MODE"
+        var indexBegin: Int = 0
+        var indexEnd: Int = 0
+
+        for(index in ioTemp.indices) {
+            if(!ioTemp[index].contains(searchBegin)) {
+                continue
+            } else {
+                indexBegin = index
+                break
+            }
+        }
+
+        for(index in indexBegin until ioTemp.size) {
+            if(!ioTemp[index].contains(searchEnd)) {
+                continue
+            } else {
+                indexEnd = index
+                break
+            }
+        }
+
+        val subListTemp: List<String> = ioTemp.subList(indexBegin + 1, indexEnd)
+        ret = subListTemp.associate {
+            val matchResult = regex.find(it)
+            val plat = matchResult?.groups?.get(1)?.value ?: ""
+            val arch = matchResult?.groups?.get(2)?.value ?: ""
+            plat to arch.split(' ')
+        }
+        return ret
+    }