Skip to content

Commit

Permalink
fix: some case generate in nested dir
Browse files Browse the repository at this point in the history
  • Loading branch information
shuhangli committed Aug 22, 2023
1 parent 47408b9 commit 7ef1083
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 31 deletions.
18 changes: 4 additions & 14 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand All @@ -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
#systemProp.https.proxyPort=6152
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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, "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>()
val toRemove = hashSetOf<String>()
for (i in dependsComments) {
Expand All @@ -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)
Expand All @@ -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<String> {
Expand Down Expand Up @@ -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<String>()
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)
Expand All @@ -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<String>()
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)
Expand Down

0 comments on commit 7ef1083

Please sign in to comment.