1
1
package cc.unitmesh.devti.gui.snippet
2
2
3
+ import cc.unitmesh.devti.AutoDevBundle
3
4
import cc.unitmesh.devti.AutoDevNotifications
4
5
import cc.unitmesh.devti.provider.RunService
5
6
import com.intellij.ide.scratch.ScratchRootType
6
7
import com.intellij.openapi.actionSystem.ActionUpdateThread
7
8
import com.intellij.openapi.actionSystem.AnActionEvent
8
9
import com.intellij.openapi.actionSystem.PlatformDataKeys
10
+ import com.intellij.openapi.application.runWriteAction
9
11
import com.intellij.openapi.fileEditor.FileDocumentManager
10
12
import com.intellij.openapi.project.DumbAwareAction
13
+ import com.intellij.openapi.project.Project
14
+ import com.intellij.openapi.project.guessProjectDir
15
+ import com.intellij.openapi.util.NlsSafe
16
+ import com.intellij.openapi.vfs.VirtualFile
11
17
import com.intellij.psi.PsiManager
12
18
import java.io.File
19
+ import java.io.IOException
13
20
14
- class AutoDevRunAction : DumbAwareAction (" Run this file" ) {
21
+
22
+ class AutoDevRunAction : DumbAwareAction (AutoDevBundle .message("autodev.run.action")) {
15
23
override fun getActionUpdateThread (): ActionUpdateThread = ActionUpdateThread .EDT
16
24
17
25
override fun update (e : AnActionEvent ) {
@@ -34,11 +42,15 @@ class AutoDevRunAction : DumbAwareAction("Run this file") {
34
42
35
43
val document = editor.document
36
44
val file = FileDocumentManager .getInstance().getFile(document) ? : return
37
- val psiFile = PsiManager .getInstance(project).findFile(file)
45
+ var originPsiFile = PsiManager .getInstance(project).findFile(file)
38
46
? : return
39
47
40
- val scratchFile = ScratchRootType .getInstance()
41
- .createScratchFile(project, file.name, psiFile.language, document.text)
48
+ var scratchFile: VirtualFile ? = ScratchRootType .getInstance()
49
+ .createScratchFile(project, file.name, originPsiFile.language, document.text)
50
+
51
+ if (scratchFile?.extension == " Dockerfile" ) {
52
+ scratchFile = createDockerFile(project, document.text) ? : scratchFile
53
+ }
42
54
43
55
if (scratchFile == null ) {
44
56
AutoDevNotifications .warn(project, " Cannot create scratch file" )
@@ -49,6 +61,9 @@ class AutoDevRunAction : DumbAwareAction("Run this file") {
49
61
File (scratchFile.path).setExecutable(true )
50
62
}
51
63
64
+ var psiFile = PsiManager .getInstance(project).findFile(scratchFile)
65
+ ? : return
66
+
52
67
try {
53
68
RunService .provider(project, file)
54
69
?.runFile(project, scratchFile, psiFile, isFromToolAction = true )
@@ -58,4 +73,26 @@ class AutoDevRunAction : DumbAwareAction("Run this file") {
58
73
AutoDevNotifications .notify(project, " Run Failed: ${e.message} " )
59
74
}
60
75
}
76
+
77
+ private fun createDockerFile (project : Project , text : @NlsSafe String ): VirtualFile ? {
78
+ val projectDir = project.guessProjectDir()
79
+ if (projectDir == null ) {
80
+ return null
81
+ }
82
+
83
+ return runWriteAction {
84
+ try {
85
+ // 在项目根目录创建名为 dockerfile 的文件
86
+ var dockerfile = projectDir.findChild(" Dockerfile" )
87
+ if (dockerfile == null ) {
88
+ dockerfile = projectDir.createChildData(null , " Dockerfile" )
89
+ }
90
+
91
+ dockerfile.setBinaryContent(text.toByteArray())
92
+ return @runWriteAction dockerfile
93
+ } catch (e: IOException ) {
94
+ return @runWriteAction null
95
+ }
96
+ }
97
+ }
61
98
}
0 commit comments