Browse Source

add two new action

littleheuer 4 years ago
parent
commit
4e1d8504b6

+ 2 - 1
.gitignore

@@ -5,6 +5,7 @@ node_modules
 *.swp
 *.swo
 *.jar
-build
+build/
+deps/
 .gradle
 .idea

+ 63 - 35
build.gradle.kts

@@ -8,48 +8,76 @@ plugins {
     id("org.jetbrains.kotlin.jvm") version "1.5.10"
 }
 
-apply{
-    plugin("org.jetbrains.intellij")
-    plugin("idea")
-}
+allprojects {
+    apply {
+        plugin("idea")
+        plugin("kotlin")
+        plugin("org.jetbrains.intellij")
+    }
 
-group = properties("pluginGroup")
-version = properties("pluginVersion")
+    repositories {
+        mavenCentral()
+        maven("https://oss.sonatype.org/content/repositories/snapshots/")
+        maven("https://cache-redirector.jetbrains.com/intellij-dependencies")
+    }
 
-repositories {
-    mavenCentral()
-    maven("https://oss.sonatype.org/content/repositories/snapshots/")
-    maven("https://cache-redirector.jetbrains.com/intellij-dependencies")
-}
+    configurations {
+        all {
+            // Allows using project dependencies instead of IDE dependencies during compilation running
+            resolutionStrategy.sortArtifacts(ResolutionStrategy.SortOrder.DEPENDENCY_FIRST)
+        }
+    }
 
-dependencies {
-    implementation("org.jetbrains.kotlin:kotlin-stdlib:1.5.21")
-    testImplementation("org.junit.jupiter:junit-jupiter-api:5.7.2")
-    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.7.2")
-}
+    // See https://github.com/JetBrains/gradle-intellij-plugin/
+    intellij {
+        pluginName.set(properties("pluginName"))
+        version.set(properties("baseVersion"))
+        //type.set(properties("platformType"))
+        downloadSources.set(properties("platformDownloadSources").toBoolean())
+        ideaDependencyCachePath.set(dependencyCachePath)
+        updateSinceUntilBuild.set(true)
+        plugins.set(properties("platformPlugins").split(',').map(String::trim).filter(String::isNotEmpty))
+    }
 
-// See https://github.com/JetBrains/gradle-intellij-plugin/
-intellij {
-    pluginName.set(properties("pluginName"))
-    version.set(properties("platformVersion"))
-    type.set(properties("platformType"))
-    downloadSources.set(properties("platformDownloadSources").toBoolean())
-    updateSinceUntilBuild.set(true)
-    plugins.set(properties("platformPlugins").split(',').map(String::trim).filter(String::isNotEmpty))
-}
+    tasks {
+        test {
+            useJUnitPlatform()
+        }
+        getByName("buildSearchableOptions").enabled = false
+        patchPluginXml {
+            version.set(properties("pluginVersion"))
+            sinceBuild.set(properties("pluginSinceBuild"))
+            untilBuild.set(properties("pluginUntilBuild"))
+        }
 
-tasks {
-    withType<Test> {
-        useJUnitPlatform()
+        runPluginVerifier {
+            ideVersions.set(properties("pluginVerifierIdeVersions").split(',').map(String::trim).filter(String::isNotEmpty))
+        }
     }
-    getByName("buildSearchableOptions").enabled = false
-    patchPluginXml {
-        version.set(properties("pluginVersion"))
-        sinceBuild.set(properties("pluginSinceBuild"))
-        untilBuild.set(properties("pluginUntilBuild"))
+
+    dependencies {
+        implementation("org.jetbrains.kotlin:kotlin-stdlib:1.5.21")
+        testImplementation("org.junit.jupiter:junit-jupiter-api:5.7.2")
+        testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.7.2")
     }
+}
 
-    runPluginVerifier {
-        ideVersions.set(properties("pluginVerifierIdeVersions").split(',').map(String::trim).filter(String::isNotEmpty))
+val Project.dependencyCachePath
+    get(): String {
+        val cachePath = file("${rootProject.projectDir}/deps")
+        if (!cachePath.exists()) {
+            cachePath.mkdirs()
+        }
+        return cachePath.absolutePath
+    }
+
+
+project(":clion") {
+    intellij {
+        version.set(properties("clionVersion"))
+        plugins.set(properties("clionPlugins").split(',').map(String::trim).filter(String::isNotEmpty))
+    }
+    dependencies {
+        implementation(project(":"))
     }
 }

+ 11 - 0
clion/src/main/resources/META-INF/clion-only.xml

@@ -0,0 +1,11 @@
+<idea-plugin xmlns:xi="http://www.w3.org/2001/XInclude" allow-bundled-update="true">
+    <depends>com.intellij.cidr.base</depends>
+    <depends>com.intellij.clion</depends>
+    <depends>com.intellij.modules.cidr.debugger</depends>
+
+    <!--<extensions defaultExtensionNs="cidr.debugger">
+        <languageSupport language="lua" implementationClass="io.xmake.debugger.CppDebuggerLanguageSupport"/>
+        <lineBreakpointFileTypesProvider implementation="io.xmake.debugger.CppLineBreakpointFileTypesProvider"/>
+    </extensions>-->
+
+</idea-plugin>

+ 4 - 4
gradle.properties

@@ -7,17 +7,17 @@ pluginUntilBuild=212.*
 # Plugin Verifier integration -> https://github.com/JetBrains/gradle-intellij-plugin#plugin-verifier-dsl
 # See https://jb.gg/intellij-platform-builds-list for available build versions
 pluginVerifierIdeVersions=2020.1.1, 2021.1, 2021.2
-platformType=IC
-platformVersion=2021.2
-platformDownloadSources=false
+baseVersion=IC-2021.2
+clionVersion=CL-2021.2.1
+platformDownloadSources=true
 
 # Plugin Dependencies -> https://plugins.jetbrains.com/docs/intellij/plugin-dependencies.html
 # Example: platformPlugins = com.intellij.java, com.jetbrains.php:203.4449.22
 platformPlugins=com.intellij.java, org.jetbrains.kotlin
+clionPlugins=com.intellij.cidr.base, com.intellij.clion
 # Opt-out flag for bundling Kotlin standard library.
 # See https://kotlinlang.org/docs/reference/using-gradle.html#dependency-on-the-standard-library for details.
 kotlin.stdlib.default.dependency=false
 
 
 
-

+ 1 - 0
settings.gradle.kts

@@ -8,3 +8,4 @@ pluginManagement {
     }
 }
 rootProject.name = "xmake-idea"
+include("clion")

+ 33 - 0
src/main/kotlin/io/xmake/actions/UpdateCmakeListsAction.kt

@@ -0,0 +1,33 @@
+package io.xmake.actions
+
+import com.intellij.execution.process.ProcessAdapter
+import com.intellij.execution.process.ProcessEvent
+import com.intellij.openapi.actionSystem.AnAction
+import com.intellij.openapi.actionSystem.AnActionEvent
+import com.intellij.openapi.vfs.LocalFileSystem
+import io.xmake.project.xmakeConsoleView
+import io.xmake.shared.xmakeConfiguration
+import io.xmake.utils.SystemUtils
+
+class UpdateCmakeListsAction : AnAction() {
+    override fun actionPerformed(e: AnActionEvent) {
+        // the project
+        val project = e.project ?: return
+
+        // clear console first
+        project.xmakeConsoleView.clear()
+
+        // configure and build it
+        val xmakeConfiguration = project.xmakeConfiguration
+        if (xmakeConfiguration.changed) {
+            SystemUtils.runvInConsole(project, xmakeConfiguration.configurationCommandLine).addProcessListener(object: ProcessAdapter() {
+                override fun processTerminated(e: ProcessEvent) {
+                    SystemUtils.runvInConsole(project, xmakeConfiguration.updateCmakeListsCommandLine, false, true, true)
+                }
+            })
+            xmakeConfiguration.changed = false
+        } else {
+            SystemUtils.runvInConsole(project, xmakeConfiguration.updateCmakeListsCommandLine, true, true, true)
+        }
+    }
+}

+ 32 - 0
src/main/kotlin/io/xmake/actions/UpdateCompileCommandsAction.kt

@@ -0,0 +1,32 @@
+package io.xmake.actions
+
+import com.intellij.execution.process.ProcessAdapter
+import com.intellij.execution.process.ProcessEvent
+import com.intellij.openapi.actionSystem.AnAction
+import com.intellij.openapi.actionSystem.AnActionEvent
+import io.xmake.project.xmakeConsoleView
+import io.xmake.shared.xmakeConfiguration
+import io.xmake.utils.SystemUtils
+
+class UpdateCompileCommandsAction : AnAction() {
+    override fun actionPerformed(e: AnActionEvent) {
+        // the project
+        val project = e.project ?: return
+
+        // clear console first
+        project.xmakeConsoleView.clear()
+
+        // configure and build it
+        val xmakeConfiguration = project.xmakeConfiguration
+        if (xmakeConfiguration.changed) {
+            SystemUtils.runvInConsole(project, xmakeConfiguration.configurationCommandLine).addProcessListener(object: ProcessAdapter() {
+                override fun processTerminated(e: ProcessEvent) {
+                    SystemUtils.runvInConsole(project, xmakeConfiguration.updateCompileCommansLine, false, true, true)
+                }
+            })
+            xmakeConfiguration.changed = false
+        } else {
+            SystemUtils.runvInConsole(project, xmakeConfiguration.updateCompileCommansLine, true, true, true)
+        }
+    }
+}

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

@@ -93,7 +93,8 @@ class XMakeConfiguration(// the project
         get() {
 
             // make parameters
-            val parameters = mutableListOf("f", "-p", data.currentPlatform, "-a", data.currentArchitecture, "-m", data.currentMode)
+            val parameters =
+                mutableListOf("f", "-p", data.currentPlatform, "-a", data.currentArchitecture, "-m", data.currentMode)
             if (data.currentPlatform == "android" && data.androidNDKDirectory != "") {
                 parameters.add("--ndk=\"${data.androidNDKDirectory}\"")
             }
@@ -123,13 +124,26 @@ class XMakeConfiguration(// the project
             return makeCommandLine(parameters)
         }
 
+    val updateCmakeListsCommandLine: GeneralCommandLine
+        get() = makeCommandLine(mutableListOf("project", "-k", "cmake", "-y"))
+
+    val updateCompileCommansLine: GeneralCommandLine
+        get() = makeCommandLine(mutableListOf("project", "-k", "compile_commands"))
+
     // the targets
     val targets: Array<String>
         get() {
 
             // make targets
             var targets = arrayOf("default", "all")
-            val results = SystemUtils.ioRunv(listOf(SystemUtils.xmakeProgram, "l", "-c", "import(\"core.project.config\"); import(\"core.project.project\"); config.load(); for name, _ in pairs((project.targets())) do print(name) end"), data.workingDirectory)
+            val results = SystemUtils.ioRunv(
+                listOf(
+                    SystemUtils.xmakeProgram,
+                    "l",
+                    "-c",
+                    "import(\"core.project.config\"); import(\"core.project.project\"); config.load(); for name, _ in pairs((project.targets())) do print(name) end"
+                ), data.workingDirectory
+            )
             results.split("\n").forEach {
                 if (it.trim() != "") {
                     targets += it
@@ -147,14 +161,14 @@ class XMakeConfiguration(// the project
         get() = _data
         set(value) {
             val newState = State(
-                    currentPlatform = value.currentPlatform,
-                    currentArchitecture = value.currentArchitecture,
-                    currentMode = value.currentMode,
-                    workingDirectory = value.workingDirectory,
-                    androidNDKDirectory = value.androidNDKDirectory,
-                    buildOutputDirectory = value.buildOutputDirectory,
-                    verboseOutput = value.verboseOutput,
-                    additionalConfiguration = value.additionalConfiguration
+                currentPlatform = value.currentPlatform,
+                currentArchitecture = value.currentArchitecture,
+                currentMode = value.currentMode,
+                workingDirectory = value.workingDirectory,
+                androidNDKDirectory = value.androidNDKDirectory,
+                buildOutputDirectory = value.buildOutputDirectory,
+                verboseOutput = value.verboseOutput,
+                additionalConfiguration = value.additionalConfiguration
             )
             if (_data != newState) {
                 _data = newState
@@ -163,18 +177,21 @@ class XMakeConfiguration(// the project
         }
 
     // make command line
-    fun makeCommandLine(parameters: List<String>, environmentVariables: EnvironmentVariablesData = EnvironmentVariablesData.DEFAULT): GeneralCommandLine {
+    fun makeCommandLine(
+        parameters: List<String>,
+        environmentVariables: EnvironmentVariablesData = EnvironmentVariablesData.DEFAULT
+    ): GeneralCommandLine {
 
         // make command
         return GeneralCommandLine(SystemUtils.xmakeProgram)
-                .withParameters(parameters)
-                .withCharset(Charsets.UTF_8)
-                .withWorkDirectory(data.workingDirectory)
-                .withEnvironment(environmentVariables.envs)
-                .withRedirectErrorStream(true)
+            .withParameters(parameters)
+            .withCharset(Charsets.UTF_8)
+            .withWorkDirectory(data.workingDirectory)
+            .withEnvironment(environmentVariables.envs)
+            .withRedirectErrorStream(true)
     }
 
-    data class State (
+    data class State(
         var currentPlatform: String = SystemUtils.platform(),
         var currentArchitecture: String = "",
         var currentMode: String = "release",
@@ -244,5 +261,5 @@ class XMakeConfiguration(// the project
 
 val Project.xmakeConfiguration: XMakeConfiguration
     get() = this.getComponent(XMakeConfiguration::class.java)
-            ?: error("Failed to get XMakeConfiguration for $this")
+        ?: error("Failed to get XMakeConfiguration for $this")
 

+ 7 - 1
src/main/kotlin/io/xmake/shared/XMakeProblem.kt

@@ -1,4 +1,10 @@
 package io.xmake.shared
 
-class XMakeProblem (val file: String? = null, val line: String? = "0", val column: String? = "0", val kind: String? = "error", val message: String? = "") {
+class XMakeProblem(
+    val file: String? = null,
+    val line: String? = "0",
+    val column: String? = "0",
+    val kind: String? = "error",
+    val message: String? = ""
+) {
 }

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

@@ -6,8 +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.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
@@ -17,17 +18,19 @@ import java.io.BufferedReader
 import java.io.IOException
 import java.io.InputStreamReader
 import java.io.File
+import java.nio.file.Path
+import java.nio.file.Paths
 import java.util.regex.Pattern
 
 
 object SystemUtils {
 
     // the log
-    private val Log = Logger.getInstance(SystemUtils::class.java.getName())
+    private val LOG = logger<SystemUtils>()
 
     // the xmake program
-    private var _xmakeProgram:String = ""
-    var xmakeProgram:String
+    private var _xmakeProgram: String = ""
+    var xmakeProgram: String
         get() {
 
             // cached? return it directly
@@ -42,7 +45,12 @@ object SystemUtils {
             }
 
             // attempt to get xmake program
-            val programs = arrayOf("xmake", (System.getenv("HOME") ?: "") + "/.local/bin/xmake", "/usr/local/bin/xmake", "/usr/bin/xmake")
+            val programs = arrayOf(
+                "xmake",
+                (System.getenv("HOME") ?: "") + "/.local/bin/xmake",
+                "/usr/local/bin/xmake",
+                "/usr/bin/xmake"
+            )
             for (program in programs) {
                 if (program == "xmake" || File(program).exists()) {
 
@@ -61,6 +69,7 @@ object SystemUtils {
                         }
 
                     } catch (e: IOException) {
+                        LOG.error(e)
                         e.printStackTrace()
                     }
                 }
@@ -69,11 +78,13 @@ object SystemUtils {
             // ok?
             return _xmakeProgram
         }
-        set(value) { _xmakeProgram = value }
+        set(value) {
+            _xmakeProgram = value
+        }
 
     // the xmake version
-    private var _xmakeVersion:String = ""
-    var xmakeVersion:String
+    private var _xmakeVersion: String = ""
+    var xmakeVersion: String
         get() {
             if (_xmakeVersion == "") {
                 val result = ioRunv(listOf(xmakeProgram, "--version")).split(',')
@@ -83,7 +94,9 @@ object SystemUtils {
             }
             return _xmakeVersion
         }
-        set(value) { _xmakeVersion = value }
+        set(value) {
+            _xmakeVersion = value
+        }
 
     // get platform
     fun platform(): String = when {
@@ -211,7 +224,13 @@ object SystemUtils {
     }
 
     // run process in console
-    fun runvInConsole(project: Project, commandLine: GeneralCommandLine, showConsole: Boolean = true, showProblem: Boolean = false, showExitCode: Boolean = false): ProcessHandler {
+    fun runvInConsole(
+        project: Project,
+        commandLine: GeneralCommandLine,
+        showConsole: Boolean = true,
+        showProblem: Boolean = false,
+        showExitCode: Boolean = false
+    ): ProcessHandler {
 
         // create handler
         val handler = ConsoleProcessHandler(project.xmakeConsoleView, commandLine, showExitCode)
@@ -252,3 +271,5 @@ object SystemUtils {
         return handler
     }
 }
+
+val VirtualFile.pathAsPath: Path get() = Paths.get(path)

+ 0 - 4
src/main/resources/META-INF/clion-only.xml

@@ -1,4 +0,0 @@
-<idea-plugin xmlns:xi="http://www.w3.org/2001/XInclude" allow-bundled-update="true">
-    <depends>com.intellij.cidr.base</depends>
-    <depends>com.intellij.clion</depends>
-</idea-plugin>

+ 10 - 1
src/main/resources/META-INF/plugin.xml

@@ -91,7 +91,9 @@
     <!--    <depends>com.intellij.modules.java</depends>-->
     <depends>com.intellij.modules.lang</depends>
     <depends>com.intellij.modules.platform</depends>
-    <depends optional="true" config-file="clion-only.xml">com.intellij.modules.clion</depends>
+    <depends optional="true" config-file="../../../../clion/src/main/resources/META-INF/clion-only.xml">
+        com.intellij.modules.clion
+    </depends>
 
     <extensions defaultExtensionNs="com.intellij">
 
@@ -143,6 +145,13 @@
             <separator/>
             <action id="XMake.QuickStart" class="io.xmake.actions.QuickStartAction" text="Quick Start"
                     icon="/icons/quickstart.png" description="Quick start a new project."/>
+            <action id="XMake.UpdateCmakeLists" class="io.xmake.actions.UpdateCmakeListsAction" text="Update CmakeLists"
+                    description="Create or update the CmakeLists.txt.">
+                <keyboard-shortcut keymap="$default" first-keystroke="ctrl U" second-keystroke="ctrl alt U"/>
+            </action>
+            <action id="XMake.UpdateCompileCommands" class="io.xmake.actions.UpdateCompileCommandsAction"
+                    text="Update Compile Commands" description="Create or update  compile_commands.js.">
+            </action>
         </group>
     </actions>