Skip to content

Commit 45937f6

Browse files
committed
refactor(devti): improve file handling and code structure
- Rename and restructure Dockerfile creation logic - Enhance JSON file processing in devcontainer setup - Add support for making shell scripts executable - Simplify and clarify code in AutoDevContainer and AutoDevRunAction classes
1 parent 79fe5f0 commit 45937f6

File tree

2 files changed

+40
-48
lines changed

2 files changed

+40
-48
lines changed

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

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,20 @@ class AutoDevRunAction : AnAction(AutoDevBundle.message("autodev.run.action")) {
3232
val document = editor.document
3333
val file = FileDocumentManager.getInstance().getFile(document)
3434

35-
if (file != null) {
36-
val lightFile = file as? LightVirtualFile
37-
if (lightFile?.language == JsonLanguage.INSTANCE) {
38-
val virtualFile = AutoDevContainer.updateForDevContainer(project, file, document.text)
39-
?: lightFile
40-
e.presentation.isEnabled = RunService.provider(project, virtualFile) != null
41-
return
42-
}
35+
if (file == null) {
36+
e.presentation.isEnabled = false
37+
return
38+
}
4339

44-
e.presentation.isEnabled = RunService.provider(project, file) != null
40+
val lightFile = file as? LightVirtualFile
41+
if (lightFile?.language == JsonLanguage.INSTANCE) {
42+
val virtualFile = AutoDevContainer.updateForDevContainer(project, file, document.text)
43+
?: lightFile
44+
e.presentation.isEnabled = RunService.provider(project, virtualFile) != null
4545
return
4646
}
4747

48-
e.presentation.isEnabled = false
48+
e.presentation.isEnabled = RunService.provider(project, file) != null
4949
}
5050

5151
override fun actionPerformed(e: AnActionEvent) {
@@ -60,23 +60,28 @@ class AutoDevRunAction : AnAction(AutoDevBundle.message("autodev.run.action")) {
6060
var scratchFile: VirtualFile? = ScratchRootType.getInstance()
6161
.createScratchFile(project, file.name, originPsiFile.language, document.text)
6262

63-
if (scratchFile?.extension == "Dockerfile") {
64-
scratchFile = createDockerFile(project, document.text) ?: scratchFile
65-
} else if (scratchFile?.extension?.lowercase() == "json") {
66-
scratchFile = AutoDevContainer.updateForDevContainer(project, file as LightVirtualFile, document.text) ?: scratchFile
63+
val extension = scratchFile?.extension
64+
when {
65+
extension == "Dockerfile" -> {
66+
scratchFile = createDockerFile(project, document.text) ?: scratchFile
67+
}
68+
69+
extension?.lowercase() == "json" -> {
70+
scratchFile = AutoDevContainer.updateForDevContainer(project, file as LightVirtualFile, document.text)
71+
?: scratchFile
72+
}
73+
74+
scratchFile?.extension == "sh" -> {
75+
File(scratchFile.path).setExecutable(true)
76+
}
6777
}
6878

6979
if (scratchFile == null) {
7080
AutoDevNotifications.warn(project, "Cannot create scratch file")
7181
return
7282
}
7383

74-
if (scratchFile.extension == "sh") {
75-
File(scratchFile.path).setExecutable(true)
76-
}
77-
78-
var psiFile = PsiManager.getInstance(project).findFile(scratchFile)
79-
?: return
84+
var psiFile = PsiManager.getInstance(project).findFile(scratchFile) ?: return
8085

8186
try {
8287
RunService.provider(project, scratchFile)
@@ -88,25 +93,18 @@ class AutoDevRunAction : AnAction(AutoDevBundle.message("autodev.run.action")) {
8893
}
8994
}
9095

91-
private fun createDockerFile(project: Project, text: @NlsSafe String): VirtualFile? {
92-
val projectDir = project.guessProjectDir()
93-
if (projectDir == null) {
94-
return null
95-
}
96-
97-
return runWriteAction {
98-
try {
99-
// 在项目根目录创建名为 dockerfile 的文件
100-
var dockerfile = projectDir.findChild("Dockerfile")
101-
if (dockerfile == null) {
102-
dockerfile = projectDir.createChildData(null, "Dockerfile")
103-
}
104-
105-
dockerfile.setBinaryContent(text.toByteArray())
106-
return@runWriteAction dockerfile
107-
} catch (e: IOException) {
108-
return@runWriteAction null
96+
private fun createDockerFile(project: Project, text: String): VirtualFile? = runWriteAction {
97+
val projectDir = project.guessProjectDir()!!
98+
try {
99+
var dockerfile = projectDir.findChild("Dockerfile")
100+
if (dockerfile == null) {
101+
dockerfile = projectDir.createChildData(null, "Dockerfile")
109102
}
103+
104+
dockerfile.setBinaryContent(text.toByteArray())
105+
return@runWriteAction dockerfile
106+
} catch (e: IOException) {
107+
return@runWriteAction null
110108
}
111109
}
112110
}

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,12 @@ object AutoDevContainer {
1111
private val DEV_CONTAINER_PROPS =
1212
setOf("image", "dockerFile", "containerEnv", "remoteUser", "customizations", "features")
1313

14-
fun updateForDevContainer(
15-
project: Project,
16-
lightVirtualFile: LightVirtualFile,
17-
content: String
18-
): LightVirtualFile? {
19-
val fileName = lightVirtualFile.name.lowercase()
14+
fun updateForDevContainer(project: Project, vfile: LightVirtualFile, content: String): LightVirtualFile? {
15+
val fileName = vfile.name.lowercase()
2016
if ((!content.startsWith("{") && !content.endsWith("}"))) return null
2117

2218
if (fileName == "devcontainer.json" || fileName.contains("devcontainer")) {
23-
return lightVirtualFile
19+
return vfile
2420
}
2521

2622
val objectMapper = ObjectMapper().registerModule(KotlinModule.Builder().build())
@@ -52,8 +48,6 @@ object AutoDevContainer {
5248
}
5349

5450
if (!isDevContainer) return null
55-
val newFile = LightVirtualFile("devcontainer.json", JsonLanguage.INSTANCE, content)
56-
57-
return newFile
51+
return LightVirtualFile("devcontainer.json", JsonLanguage.INSTANCE, content)
5852
}
5953
}

0 commit comments

Comments
 (0)