Skip to content

Commit

Permalink
feat: fix npe and update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
reko committed Oct 11, 2022
1 parent 1fd3bc6 commit 6495724
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
.qodana
build
/gradle.local.properties
/certs
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,31 @@ idea奎托斯插件
.kratos配置项:

| key | value | desc |
|--------------| ---- | ---- |
|--------------| ---- | -- |
| wireLocation | string | wire.go生成目录 |
| layoutRepository | string | kratos cli -r参数 |

example:
```properties
wireLocation=cmd/post
layoutRepository=https://gitee.com/go-kratos/kratos-layout.git
```

## 2. pb.go生成

## clients
* 在pb文件添加注释//kratos:clients
* 点击一键生成则会运行对应的kratos proto client

## pb
* 在pb文件添加注释//kratos:pb
* (optional) 添加注释depends如:
> //depends:./third_party
* 点击一键生成则会生成对应的pb.go
## 3. biz/service/data模板

TODO

## 4. 一键生成wire/provider set/pb.go
## 4. 生成wire/provider set/pb.go

点击run旁边的go logo会运行生成provider set/wire.go/kartos proto clients/pb.go

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ import com.intellij.psi.PsiManager
import com.intellij.psi.search.FileTypeIndex
import com.intellij.psi.search.GlobalSearchScope
import java.nio.charset.Charset

private var LOG:Logger? = null
private fun getLogger(): Logger {
if(LOG == null){
LOG = Logger.getInstance("PbHelper")
}
return LOG!!
}
private fun findDependency(file: PsiFile): List<String> {
if (file !is PbFile) {
return arrayListOf<String>()
Expand All @@ -40,14 +46,17 @@ fun genPbTask(file: PsiFile): KratosTask? {
val otherPaths = findDependency(file)
val cmds = arrayListOf("protoc")
cmds.addAll(otherPaths)
cmds.add("--go_out=paths=source_relative:./$parentPath")
cmds.add("./$parentPath/*.proto")
cmds.add("--proto_path=${project.basePath}/$parentPath")
cmds.add("--go_out=paths=source_relative:${project.basePath}/$parentPath")
cmds.add("${project.basePath}/$parentPath/${file.name}")
val cmd = GeneralCommandLine(cmds)
.withCharset(Charset.forName("UTF-8"))
.withWorkDirectory(project.basePath)
return KratosTask(
{
ExecUtil.execAndGetOutput(cmd)
getLogger().debug("will run pb command:${cmd.commandLineString}")
val output = ExecUtil.execAndGetOutput(cmd)
getLogger().debug("pb command code:${output.exitCode} output:${output.stdout} err:${output.stderr}")
},
"Generate Client Task"
)
Expand All @@ -62,7 +71,9 @@ fun genClientTask(file: PsiFile): KratosTask? {
.withWorkDirectory(project.basePath)
return KratosTask(
{
ExecUtil.execAndGetOutput(cmd)
getLogger().debug("will run client command:${cmd.commandLineString}")
val output = ExecUtil.execAndGetOutput(cmd)
getLogger().debug("client command code:${output.exitCode} output:${output.stdout} err:${output.stderr}")
},
"Generate Client Task"
)
Expand All @@ -77,12 +88,13 @@ fun genAllPb(p: Project): List<KratosTask> {
val clientPbComment =
file.children.find { v -> v is PsiComment && v.text.contains(KratosPbClientAction.TOKEN) }
if (clientPbComment != null) {
tasks.add(genPbTask(file) ?: continue)

getLogger().debug("find client comment:${file.virtualFile.path}")
tasks.add(genClientTask(file) ?: continue)
}
val pbComment = file.children.find { v -> v is PsiComment && v.text.contains(KratosPbAction.TOKEN) }
if (pbComment != null) {
tasks.add(genClientTask(file) ?: continue)
getLogger().debug("find pb comment:${file.virtualFile.path}")
tasks.add(genPbTask(file) ?: continue)
}
}
return tasks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ import com.intellij.psi.search.GlobalSearchScope
import java.nio.charset.Charset

private const val TOKEN = "wired"
private val LOG = Logger.getInstance("WireHelper")

private var LOG:Logger? = null
private fun getLogger(): Logger {
if(LOG == null){
LOG = Logger.getInstance("WireHelper")
}
return LOG!!
}

private fun scanForNotProvideTypes(providerSets: List<ProviderSet>): HashSet<ProviderType> {
val arguments = arrayListOf<ProviderType>()
Expand Down Expand Up @@ -165,7 +172,7 @@ private fun genWire(dir: PsiDirectory, config: KratosConfig): List<KratosTask>?
.createFileFromText(wireFileName, GoFileType.INSTANCE, plainWire)
GoFormatterUtil.reformat(wireFile)
val documentManager = PsiDocumentManager.getInstance(project)
val doc = documentManager.getDocument(wireFile)!!

val exe = GoSdkUtil.findExecutableInGoPath("wire", dir.project, null) ?: return null
val cmd = GeneralCommandLine(exe.path, targetDir.virtualFile.canonicalPath)
.withCharset(Charset.forName("UTF-8"))
Expand All @@ -180,17 +187,18 @@ private fun genWire(dir: PsiDirectory, config: KratosConfig): List<KratosTask>?
}, "Generate Provider Set${providerSet.fileName}", true)
})
tasks.add(KratosTask({
targetDir.files.find { v -> v.name == wireFileName }?.delete()
val file = targetDir.add(wireFile).containingFile
val doc = documentManager.getDocument(file)!!
doc.insertString(0, comment)
documentManager.commitDocument(doc)
targetDir.files.find { v -> v.name == wireFileName }?.delete()
targetDir.add(wireFile)

}, "Generate Wire File", true))
tasks.add(KratosTask({
LOG.debug("will run:${cmd.commandLineString}")
getLogger().info("will run:${cmd.commandLineString}")
val result = ExecUtil.execAndGetOutput(cmd)
LOG.debug("wire command result:")
LOG.debug(result.stderr)
getLogger().info("wire command result:")
getLogger().info(result.stderr)
}, "Wire Command"))
return tasks
}

0 comments on commit 6495724

Please sign in to comment.