Skip to content

Commit 3c0560c

Browse files
committed
feat(run-action): add Dockerfile support and i18n for run action #308
- Added support for creating Dockerfiles when running scratch files. - Integrated i18n for the "Run this file" action text.
1 parent 79c9641 commit 3c0560c

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

core/src/main/kotlin/cc/unitmesh/devti/gui/snippet/AutoDevRunAction.kt

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
package cc.unitmesh.devti.gui.snippet
22

3+
import cc.unitmesh.devti.AutoDevBundle
34
import cc.unitmesh.devti.AutoDevNotifications
45
import cc.unitmesh.devti.provider.RunService
56
import com.intellij.ide.scratch.ScratchRootType
67
import com.intellij.openapi.actionSystem.ActionUpdateThread
78
import com.intellij.openapi.actionSystem.AnActionEvent
89
import com.intellij.openapi.actionSystem.PlatformDataKeys
10+
import com.intellij.openapi.application.runWriteAction
911
import com.intellij.openapi.fileEditor.FileDocumentManager
1012
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
1117
import com.intellij.psi.PsiManager
1218
import java.io.File
19+
import java.io.IOException
1320

14-
class AutoDevRunAction : DumbAwareAction("Run this file") {
21+
22+
class AutoDevRunAction : DumbAwareAction(AutoDevBundle.message("autodev.run.action")) {
1523
override fun getActionUpdateThread(): ActionUpdateThread = ActionUpdateThread.EDT
1624

1725
override fun update(e: AnActionEvent) {
@@ -34,11 +42,15 @@ class AutoDevRunAction : DumbAwareAction("Run this file") {
3442

3543
val document = editor.document
3644
val file = FileDocumentManager.getInstance().getFile(document) ?: return
37-
val psiFile = PsiManager.getInstance(project).findFile(file)
45+
var originPsiFile = PsiManager.getInstance(project).findFile(file)
3846
?: return
3947

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+
}
4254

4355
if (scratchFile == null) {
4456
AutoDevNotifications.warn(project, "Cannot create scratch file")
@@ -49,6 +61,9 @@ class AutoDevRunAction : DumbAwareAction("Run this file") {
4961
File(scratchFile.path).setExecutable(true)
5062
}
5163

64+
var psiFile = PsiManager.getInstance(project).findFile(scratchFile)
65+
?: return
66+
5267
try {
5368
RunService.provider(project, file)
5469
?.runFile(project, scratchFile, psiFile, isFromToolAction = true)
@@ -58,4 +73,26 @@ class AutoDevRunAction : DumbAwareAction("Run this file") {
5873
AutoDevNotifications.notify(project, "Run Failed: ${e.message}")
5974
}
6075
}
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+
}
6198
}

core/src/main/resources/messages/AutoDevBundle_en.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,4 @@ prompts.autodev.bridge=Bridge
224224
autodev.custom.llms.placeholder=Custom different LLM
225225
settings.autodev.coder.customLlms=Config LLMs: Plan/ACT/Completion/Embedding/FastApply
226226
sketch.compile.devins=Collect context (aka. transpile AutoDev DevIns)
227+
autodev.run.action=Run this file

core/src/main/resources/messages/AutoDevBundle_zh.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,4 @@ autodev.custom.llms.placeholder=Custom diffrent LLM
224224
settings.autodev.coder.customLlms=自定义不同模型: Plan/ACT/Completion/Embedding/FastApply
225225
#sketch.compile.devins="Collect context (Transpile AutoDev DevIns)"
226226
sketch.compile.devins=收集上下文中(即转译 AutoDev DevIns 指令)
227+
autodev.run.action=Run this file

0 commit comments

Comments
 (0)