1
1
package cc.unitmesh.devti.actions
2
2
3
3
import cc.unitmesh.devti.AutoDevBundle
4
+ import cc.unitmesh.devti.actions.context.DevOpsContext
4
5
import cc.unitmesh.devti.custom.tasks.FileGenerateTask
5
6
import cc.unitmesh.devti.provider.BuildSystemProvider
6
7
import cc.unitmesh.devti.template.GENIUS_CICD
7
8
import cc.unitmesh.devti.template.TemplateRender
8
- import cc.unitmesh.devti.actions.context.DevOpsContext
9
9
import com.intellij.openapi.actionSystem.AnAction
10
10
import com.intellij.openapi.actionSystem.AnActionEvent
11
+ import com.intellij.openapi.diagnostic.logger
11
12
import com.intellij.openapi.progress.ProgressManager
12
13
import com.intellij.openapi.progress.Task
13
14
import com.intellij.openapi.progress.impl.BackgroundableProcessIndicator
@@ -18,20 +19,31 @@ class GenerateGitHubActionsAction : AnAction(AutoDevBundle.message("action.new.g
18
19
override fun actionPerformed (e : AnActionEvent ) {
19
20
val project = e.project ? : return
20
21
21
- // first, we need to guess language
22
- val githubActions = BuildSystemProvider .guess(project);
23
- val templateRender = TemplateRender (GENIUS_CICD )
24
- templateRender.context = DevOpsContext .from(githubActions)
25
- val template = templateRender.getTemplate(" generate-github-action.vm" )
22
+ try {
23
+ // 改进变量命名
24
+ val buildSystem = BuildSystemProvider .guess(project)
25
+ val templateRender = TemplateRender (GENIUS_CICD )
26
+ templateRender.context = DevOpsContext .from(buildSystem)
27
+ val template = templateRender.getTemplate(" generate-github-action.vm" )
28
+
29
+ // 安全的路径处理
30
+ val projectDir = project.guessProjectDir()?.toNioPath()
31
+ ? : throw IllegalStateException (" Cannot determine project directory" )
32
+
33
+ val workflowDir = projectDir.resolve(" .github" ).resolve(" workflows" )
34
+ workflowDir.createDirectories()
26
35
27
- project.guessProjectDir() !! .toNioPath().resolve( " .github " ).resolve( " workflows " )
28
- .createDirectories( )
36
+ val msgs = templateRender.buildMsgs(template )
37
+ val task : Task . Backgroundable = FileGenerateTask (project, msgs, " ci.yml " )
29
38
30
- val msgs = templateRender.buildMsgs(template)
39
+ ProgressManager .getInstance()
40
+ .runProcessWithProgressAsynchronously(task, BackgroundableProcessIndicator (task))
31
41
32
- val task: Task .Backgroundable = FileGenerateTask (project, msgs, " ci.yml" )
33
- ProgressManager .getInstance()
34
- .runProcessWithProgressAsynchronously(task, BackgroundableProcessIndicator (task))
42
+ } catch (e: Exception ) {
43
+ // 添加错误处理,可以显示错误通知给用户
44
+ logger<GenerateGitHubActionsAction >().error(" Failed to generate GitHub Actions workflow" , e)
45
+ // 可以添加用户通知
46
+ }
35
47
}
36
48
}
37
49
0 commit comments