diff --git a/build.gradle.kts b/build.gradle.kts index 3a99811..42c7847 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -7,36 +7,33 @@ plugins { // Java support id("java") // Kotlin support - id("org.jetbrains.kotlin.jvm") version "1.8.20" + id("org.jetbrains.kotlin.jvm") version "1.8.22" // Gradle IntelliJ Plugin - id("org.jetbrains.intellij") version "1.15.0" + id("org.jetbrains.intellij") version "1.14.1" // Gradle Changelog Plugin id("org.jetbrains.changelog") version "2.0.0" - // Gradle Qodana Plugin - id("org.jetbrains.qodana") version "0.1.13" } group = properties("pluginGroup") version = properties("pluginVersion") -val compileKotlin: KotlinCompile by tasks // Configure project's dependencies repositories { mavenCentral() maven(url="https://www.jetbrains.com/intellij-repository/releases") } -// Set the JVM language level used to compile sources and generate files - Java 11 is required since 2020.3 kotlin { jvmToolchain { languageVersion.set(JavaLanguageVersion.of(17)) } } - +val compileKotlin: KotlinCompile by tasks compileKotlin.kotlinOptions { jvmTarget = JavaVersion.VERSION_17.majorVersion } + // Configure Gradle IntelliJ Plugin - read more: https://plugins.jetbrains.com/docs/intellij/tools-gradle-intellij-plugin.html intellij { pluginName.set(properties("pluginName")) @@ -53,13 +50,6 @@ changelog { groups.set(emptyList()) } -// Configure Gradle Qodana Plugin - read more: https://github.com/JetBrains/gradle-qodana-plugin -qodana { - cachePath.set(projectDir.resolve(".qodana").canonicalPath) - reportPath.set(projectDir.resolve("build/reports/inspections").canonicalPath) - saveReport.set(true) - showReport.set(System.getenv("QODANA_SHOW_REPORT")?.toBoolean() ?: false) -} tasks { wrapper { diff --git a/gradle.properties b/gradle.properties index 9bc30aa..d5e609d 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,7 +3,7 @@ pluginGroup = com.github.suhli.ideagokratosplugin pluginName = go-kratos-plugin # SemVer format -> https://semver.org -pluginVersion = 0.0.16 +pluginVersion = 0.0.17 # Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html pluginSinceBuild = 221.* @@ -25,6 +25,6 @@ org.gradle.jvmargs=-XX\:MaxHeapSize\=4096m -Xmx8192m ## suppress inspection "UnusedProperty" #kotlin.stdlib.default.dependency = false #systemProp.http.proxyHost=127.0.0.1 -#systemProp.http.proxyPort=7890 +#systemProp.http.proxyPort=6152 #systemProp.https.proxyHost=127.0.0.1 -#systemProp.https.proxyPort=7890 \ No newline at end of file +#systemProp.https.proxyPort=6152 \ No newline at end of file diff --git a/src/main/kotlin/com/github/suhli/ideagokratosplugin/helper/DirHelper.kt b/src/main/kotlin/com/github/suhli/ideagokratosplugin/helper/DirHelper.kt index 8fb477b..054ebb4 100644 --- a/src/main/kotlin/com/github/suhli/ideagokratosplugin/helper/DirHelper.kt +++ b/src/main/kotlin/com/github/suhli/ideagokratosplugin/helper/DirHelper.kt @@ -1,5 +1,6 @@ package com.github.suhli.ideagokratosplugin.helper +import com.intellij.openapi.project.Project import com.intellij.openapi.vfs.VfsUtil import com.intellij.openapi.vfs.VirtualFileManager import com.intellij.openapi.vfs.findFileOrDirectory @@ -30,9 +31,25 @@ class DirHelper { return target } + fun relativeToRoot(project:Project,path:String):String?{ + val root = project.basePath ?: return null + var p = path.replace(root, "") + // / -> ./ + if (p.startsWith(File.separator)) { + p = ".$p" + } + // xxx -> ./xxx + else if(!p.startsWith(".")){ + p = ".${File.separator}$p" + } + return p + } fun relativeToRoot(file: PsiFileSystemItem): String? { val project = file.project val root = project.basePath ?: return null + if(!file.virtualFile.path.startsWith(root)){ + return null + } var path = file.virtualFile.path.replace(root, "") if (path.startsWith(File.separator)) { path = path.replaceFirst(File.separator, "") diff --git a/src/main/kotlin/com/github/suhli/ideagokratosplugin/helper/PbHelper.kt b/src/main/kotlin/com/github/suhli/ideagokratosplugin/helper/PbHelper.kt index 62942d9..ea65fd9 100644 --- a/src/main/kotlin/com/github/suhli/ideagokratosplugin/helper/PbHelper.kt +++ b/src/main/kotlin/com/github/suhli/ideagokratosplugin/helper/PbHelper.kt @@ -50,7 +50,8 @@ private fun setDependsOn(file: PsiFile) { val dependsComments = file.children.filter { v -> v is PsiComment && v.text.contains("dependsOn:") } val entries = settings.importPathEntries val entryPaths = - settings.importPathEntries.filter { v -> v.location.startsWith("file") }.map { v -> v.location.replace("file://","") }.toHashSet() + settings.importPathEntries.filter { v -> v.location.startsWith("file") } + .map { v -> v.location.replace("file://", "") }.toHashSet() val externalEntries = hashSetOf() val toRemove = hashSetOf() for (i in dependsComments) { @@ -70,8 +71,13 @@ private fun setDependsOn(file: PsiFile) { val toAddDir = DirHelper.join(pkgDir, path) val paths = pkgDir.split("@") if (paths[0].endsWith(pkg) && !entryPaths.contains(toAddDir)) { - val prev = entryPaths.find { v -> v.contains(pkg) && v.split("@").size == 2 && v.split("@")[1].replace(path,"") != paths[1]} - if(prev != null){ + val prev = entryPaths.find { v -> + v.contains(pkg) && v.split("@").size == 2 && v.split("@")[1].replace( + path, + "" + ) != paths[1] + } + if (prev != null) { toRemove.add(prev) } externalEntries.add(toAddDir) @@ -82,7 +88,7 @@ private fun setDependsOn(file: PsiFile) { for (i in externalEntries) { entries.add(PbProjectSettings.ImportPathEntry("file://$i", "")) } - settings.importPathEntries = entries.filter { v->!toRemove.contains(v.location.replace("file://","")) }.toList() + settings.importPathEntries = entries.filter { v -> !toRemove.contains(v.location.replace("file://", "")) }.toList() } private fun findDependency(file: PsiFile): HashSet { @@ -127,15 +133,16 @@ fun genPbTask(file: PsiFile): KratosTask? { return null } val project = file.project - val parentPath = DirHelper.split(DirHelper.relativeToRoot(file.parent ?: return null) ?: return null) + val path = file.parent?.virtualFile?.path ?: return null + val parentPathRelative = DirHelper.relativeToRoot(project, path) ?: return null val otherPaths = hashSetOf() otherPaths.addAll(findDependency(file)) val cmds = arrayListOf("protoc") cmds.addAll(otherPaths) - cmds.add("--proto_path=${DirHelper.join(project.basePath!!, *parentPath)}") - cmds.add("--go_out=paths=source_relative:${DirHelper.join(project.basePath!!, *parentPath)}") + cmds.add("--proto_path=$parentPathRelative") + cmds.add("--go_out=paths=source_relative:$parentPathRelative") cmds.addAll(additionalArgs(file)) - cmds.add(DirHelper.join(project.basePath!!, *parentPath, file.name)) + cmds.add(DirHelper.join(*DirHelper.split(parentPathRelative), file.name)) return KratosTask( { runAndLog(project, cmds) @@ -148,17 +155,18 @@ fun genPbTask(file: PsiFile): KratosTask? { fun genClientTask(file: PsiFile): KratosTask? { val project = file.project - val parentPath = DirHelper.split(DirHelper.relativeToRoot(file.parent ?: return null) ?: return null) + val path = file.parent?.virtualFile?.path ?: return null + val parentPathRelative = DirHelper.relativeToRoot(project, path) ?: return null val otherPaths = hashSetOf() otherPaths.addAll(findDependency(file)) val cmds = arrayListOf("protoc") cmds.addAll(otherPaths) - cmds.add("--proto_path=${DirHelper.join(project.basePath!!, *parentPath)}") - cmds.add("--go_out=paths=source_relative:${DirHelper.join(project.basePath!!, *parentPath)}") - cmds.add("--go-http_out=paths=source_relative:${DirHelper.join(project.basePath!!, *parentPath)}") - cmds.add("--go-grpc_out=paths=source_relative:${DirHelper.join(project.basePath!!, *parentPath)}") + cmds.add("--proto_path=$parentPathRelative") + cmds.add("--go_out=paths=source_relative:$parentPathRelative") + cmds.add("--go-http_out=paths=source_relative:$parentPathRelative") + cmds.add("--go-grpc_out=paths=source_relative:$parentPathRelative") cmds.addAll(additionalArgs(file)) - cmds.add(DirHelper.join(project.basePath!!, *parentPath, file.name)) + cmds.add(DirHelper.join(*DirHelper.split(parentPathRelative), file.name)) return KratosTask( { runAndLog(project, cmds)