Browse Source

improve xmake create

ruki 3 years ago
parent
commit
2f7402d946

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

@@ -32,18 +32,24 @@ class XMakeDirectoryProjectGenerator : DirectoryProjectGeneratorBase<XMakeConfig
         /* create empty project
          * @note we muse use ioRunv instead of Runv to read all output, otherwise it will wait forever on windows
          */
+        val tmpdir = "$contentEntryPath.dir"
         SystemUtils.ioRunv(
             listOf(
                 SystemUtils.xmakeProgram,
                 "create",
                 "-P",
-                contentEntryPath,
+                tmpdir,
                 "-l",
-                data.languagesModel,
+                data?.languagesModel.toString(),
                 "-t",
-                data.kindsModel
+                data?.kindsModel.toString()
             )
         )
+        val tmpFile = File(tmpdir)
+        if (tmpFile.exists()) {
+            tmpFile.copyRecursively(File(contentEntryPath), true)
+            tmpFile.deleteRecursively()
+        }
     }
 
     override fun createStep(

+ 8 - 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.vfs.LocalFileSystem
 import io.xmake.utils.SystemUtils
+import java.io.File
 
 
 class XMakeModuleBuilder : ModuleBuilder() {
@@ -35,18 +36,24 @@ class XMakeModuleBuilder : ModuleBuilder() {
         /* create empty project
          * @note we muse use ioRunv instead of Runv to read all output, otherwise it will wait forever on windows
          */
+        val tmpdir = "$contentEntryPath.dir"
         SystemUtils.ioRunv(
             listOf(
                 SystemUtils.xmakeProgram,
                 "create",
                 "-P",
-                contentEntryPath,
+                tmpdir,
                 "-l",
                 configurationData?.languagesModel.toString(),
                 "-t",
                 configurationData?.kindsModel.toString()
             )
         )
+        val tmpFile = File(tmpdir)
+        if (tmpFile.exists()) {
+            tmpFile.copyRecursively(File(contentEntryPath), true)
+            tmpFile.deleteRecursively()
+        }
     }
 
     override fun getModuleType(): ModuleType<*> {

+ 60 - 56
src/main/kotlin/io/xmake/project/XMakeProjectConfigurable.kt

@@ -17,6 +17,7 @@ import com.intellij.ui.layout.Row
 import com.intellij.ui.layout.panel
 import io.xmake.shared.XMakeConfiguration
 import io.xmake.shared.xmakeConfiguration
+import io.xmake.shared.xmakeConfigurationOrNull
 import java.awt.Dimension
 import javax.swing.JComponent
 import javax.swing.JPanel
@@ -28,15 +29,14 @@ import java.awt.event.KeyEvent
 import java.awt.event.KeyListener
 import java.awt.event.ItemEvent
 import java.awt.event.ItemListener
+import java.io.IOException
 
 class XMakeProjectConfigurable(
         project: Project
 ) : Configurable, Configurable.NoScroll {
 
-    // the xmake configuration
-    val xmakeConfiguration = project.xmakeConfiguration
-
     // the platforms ui
+    private val project = project
     private val platformsModel = DefaultComboBoxModel<String>()
     private val platformsComboBox = ComboBox<String>(platformsModel)
 
@@ -188,72 +188,78 @@ class XMakeProjectConfigurable(
     }
 
     override fun reset() {
+        val xmakeConfiguration = project.xmakeConfigurationOrNull
+        if (xmakeConfiguration != null) {
+            // reset platforms
+            platformsModel.removeAllElements()
+            for (platform in xmakeConfiguration.platforms) {
+                platformsModel.addElement(platform)
+            }
+            platformsModel.selectedItem = xmakeConfiguration.data.currentPlatform
 
-        // reset platforms
-        platformsModel.removeAllElements()
-        for (platform in xmakeConfiguration.platforms) {
-            platformsModel.addElement(platform)
-        }
-        platformsModel.selectedItem = xmakeConfiguration.data.currentPlatform
-
-        // reset architectures
-        architecturesModel.removeAllElements()
-        for (architecture in xmakeConfiguration.architectures) {
-            architecturesModel.addElement(architecture)
-        }
-        architecturesModel.selectedItem = xmakeConfiguration.data.currentArchitecture
+            // reset architectures
+            architecturesModel.removeAllElements()
+            for (architecture in xmakeConfiguration.architectures) {
+                architecturesModel.addElement(architecture)
+            }
+            architecturesModel.selectedItem = xmakeConfiguration.data.currentArchitecture
 
-        // reset modes
-        modesModel.removeAllElements()
-        for (mode in xmakeConfiguration.modes) {
-            modesModel.addElement(mode)
-        }
-        modesModel.selectedItem = xmakeConfiguration.data.currentMode
+            // reset modes
+            modesModel.removeAllElements()
+            for (mode in xmakeConfiguration.modes) {
+                modesModel.addElement(mode)
+            }
+            modesModel.selectedItem = xmakeConfiguration.data.currentMode
 
-        // reset additional configuration
-        additionalConfiguration.text = xmakeConfiguration.data.additionalConfiguration
+            // reset additional configuration
+            additionalConfiguration.text = xmakeConfiguration.data.additionalConfiguration
 
-        // reset working directory
-        workingDirectory.component.text = xmakeConfiguration.data.workingDirectory
+            // reset working directory
+            workingDirectory.component.text = xmakeConfiguration.data.workingDirectory
 
-        // reset build output directory
-        buildOutputDirectory.component.text = xmakeConfiguration.data.buildOutputDirectory
+            // reset build output directory
+            buildOutputDirectory.component.text = xmakeConfiguration.data.buildOutputDirectory
 
-        // reset android ndk directory
-        androidNDKDirectory.component.text = xmakeConfiguration.data.androidNDKDirectory
+            // reset android ndk directory
+            androidNDKDirectory.component.text = xmakeConfiguration.data.androidNDKDirectory
 
-        // reset verbose output
-        verboseOutput.isSelected = xmakeConfiguration.data.verboseOutput
+            // reset verbose output
+            verboseOutput.isSelected = xmakeConfiguration.data.verboseOutput
 
-        // reset configuration command text
-        configurationCommandText.text = previewConfigurationCommand
+            // reset configuration command text
+            configurationCommandText.text = previewConfigurationCommand
+        }
     }
 
     @Throws(ConfigurationException::class)
     override fun apply() {
-
-        xmakeConfiguration.data.currentPlatform         = platformsModel.selectedItem.toString()
-        xmakeConfiguration.data.currentArchitecture     = architecturesModel.selectedItem.toString()
-        xmakeConfiguration.data.currentMode             = modesModel.selectedItem.toString()
-        xmakeConfiguration.data.additionalConfiguration = additionalConfiguration.text
-        xmakeConfiguration.data.workingDirectory        = workingDirectory.component.text
-        xmakeConfiguration.data.buildOutputDirectory    = buildOutputDirectory.component.text
-        xmakeConfiguration.data.androidNDKDirectory     = androidNDKDirectory.component.text
-        xmakeConfiguration.data.verboseOutput           = verboseOutput.isSelected
+        val xmakeConfiguration = project.xmakeConfigurationOrNull
+        if (xmakeConfiguration != null) {
+            xmakeConfiguration.data.currentPlatform = platformsModel.selectedItem.toString()
+            xmakeConfiguration.data.currentArchitecture = architecturesModel.selectedItem.toString()
+            xmakeConfiguration.data.currentMode = modesModel.selectedItem.toString()
+            xmakeConfiguration.data.additionalConfiguration = additionalConfiguration.text
+            xmakeConfiguration.data.workingDirectory = workingDirectory.component.text
+            xmakeConfiguration.data.buildOutputDirectory = buildOutputDirectory.component.text
+            xmakeConfiguration.data.androidNDKDirectory = androidNDKDirectory.component.text
+            xmakeConfiguration.data.verboseOutput = verboseOutput.isSelected
+        }
     }
 
     override fun isModified(): Boolean {
-
-        if (xmakeConfiguration.data.currentPlatform != platformsModel.selectedItem.toString() ||
-            xmakeConfiguration.data.currentArchitecture != architecturesModel.selectedItem.toString() ||
-            xmakeConfiguration.data.currentMode != modesModel.selectedItem.toString() ||
-            xmakeConfiguration.data.additionalConfiguration != additionalConfiguration.text ||
-            xmakeConfiguration.data.workingDirectory != workingDirectory.component.text ||
-            xmakeConfiguration.data.buildOutputDirectory != buildOutputDirectory.component.text ||
-            xmakeConfiguration.data.androidNDKDirectory != androidNDKDirectory.component.text ||
-            xmakeConfiguration.data.verboseOutput != verboseOutput.isSelected) {
-            xmakeConfiguration.changed = true
-            return true
+        val xmakeConfiguration = project.xmakeConfigurationOrNull
+        if (xmakeConfiguration != null) {
+            if (xmakeConfiguration.data.currentPlatform != platformsModel.selectedItem.toString() ||
+                xmakeConfiguration.data.currentArchitecture != architecturesModel.selectedItem.toString() ||
+                xmakeConfiguration.data.currentMode != modesModel.selectedItem.toString() ||
+                xmakeConfiguration.data.additionalConfiguration != additionalConfiguration.text ||
+                xmakeConfiguration.data.workingDirectory != workingDirectory.component.text ||
+                xmakeConfiguration.data.buildOutputDirectory != buildOutputDirectory.component.text ||
+                xmakeConfiguration.data.androidNDKDirectory != androidNDKDirectory.component.text ||
+                xmakeConfiguration.data.verboseOutput != verboseOutput.isSelected) {
+                xmakeConfiguration.changed = true
+                return true
+            }
         }
         return false
     }
@@ -298,8 +304,6 @@ class XMakeProjectConfigurable(
         }
 
     companion object {
-
-        // get log
         private val Log = Logger.getInstance(XMakeProjectConfigurable::class.java.getName())
     }
 }

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

@@ -204,7 +204,7 @@ class XMakeConfiguration(// the project
     )
 
     // ensure state
-    fun ensureState() {
+    private fun ensureState() {
         if (data.workingDirectory == "") {
             data.workingDirectory = project.basePath.toString()
         }
@@ -263,3 +263,6 @@ val Project.xmakeConfiguration: XMakeConfiguration
     get() = this.getComponent(XMakeConfiguration::class.java)
         ?: error("Failed to get XMakeConfiguration for $this")
 
+val Project.xmakeConfigurationOrNull: XMakeConfiguration?
+    get() = this.getComponent(XMakeConfiguration::class.java) ?: null
+

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

@@ -6,13 +6,9 @@ import com.intellij.execution.process.ProcessEvent
 import com.intellij.execution.process.ProcessHandler
 import com.intellij.openapi.application.ApplicationManager
 import com.intellij.openapi.util.SystemInfo
-import com.intellij.openapi.diagnostic.logger
 import com.intellij.openapi.project.Project
 import com.intellij.openapi.vfs.VirtualFile
-import io.xmake.project.xmakeConsoleView
-import io.xmake.project.xmakeOutputPanel
-import io.xmake.project.xmakeProblemList
-import io.xmake.project.xmakeToolWindow
+import io.xmake.project.*
 import io.xmake.shared.XMakeProblem
 import java.io.BufferedReader
 import java.io.IOException
@@ -22,12 +18,8 @@ import java.nio.file.Path
 import java.nio.file.Paths
 import java.util.regex.Pattern
 
-
 object SystemUtils {
 
-    // the log
-    private val LOG = logger<SystemUtils>()
-
     // the xmake program
     private var _xmakeProgram: String = ""
     var xmakeProgram: String
@@ -53,29 +45,13 @@ object SystemUtils {
             )
             for (program in programs) {
                 if (program == "xmake" || File(program).exists()) {
-
-                    try {
-
-                        // init process builder
-                        val processBuilder = ProcessBuilder(listOf(program, "--version"))
-
-                        // run process
-                        val process = processBuilder.start()
-
-                        // wait for process
-                        if (process.waitFor() == 0) {
-                            _xmakeProgram = program
-                            break
-                        }
-
-                    } catch (e: IOException) {
-                        LOG.error(e)
-                        e.printStackTrace()
+                    val result = ioRunv(listOf(program, "--version"))
+                    if (result.isNotEmpty()) {
+                        _xmakeProgram = program
+                        break
                     }
                 }
             }
-
-            // ok?
             return _xmakeProgram
         }
         set(value) {
@@ -88,7 +64,7 @@ object SystemUtils {
         get() {
             if (_xmakeVersion == "") {
                 val result = ioRunv(listOf(xmakeProgram, "--version")).split(',')
-                if (result.size > 0) {
+                if (result.isNotEmpty()) {
                     _xmakeVersion = result[0]
                 }
             }
@@ -129,8 +105,6 @@ object SystemUtils {
             e.printStackTrace()
         } finally {
         }
-
-        // ok?
         return code
     }
 
@@ -182,8 +156,6 @@ object SystemUtils {
 
             }
         }
-
-        // ok?
         return result
     }
 
@@ -272,4 +244,4 @@ object SystemUtils {
     }
 }
 
-val VirtualFile.pathAsPath: Path get() = Paths.get(path)
+val VirtualFile.pathAsPath: Path get() = Paths.get(path)