79b07c1453edc38dd3c1b3f5f50e87ab46a15eb2
[idea/community.git] / python / educational / interactive-learning / src / com / jetbrains / edu / learning / actions / StudyRunAction.java
1 package com.jetbrains.edu.learning.actions;
2
3 import com.intellij.execution.ExecutionException;
4 import com.intellij.execution.RunContentExecutor;
5 import com.intellij.execution.configurations.GeneralCommandLine;
6 import com.intellij.execution.process.OSProcessHandler;
7 import com.intellij.execution.process.ProcessHandler;
8 import com.intellij.icons.AllIcons;
9 import com.intellij.openapi.actionSystem.AnActionEvent;
10 import com.intellij.openapi.diagnostic.Logger;
11 import com.intellij.openapi.editor.Editor;
12 import com.intellij.openapi.fileEditor.FileDocumentManager;
13 import com.intellij.openapi.project.DumbAwareAction;
14 import com.intellij.openapi.project.Project;
15 import com.intellij.openapi.projectRoots.Sdk;
16 import com.intellij.openapi.util.Disposer;
17 import com.intellij.openapi.vfs.VirtualFile;
18 import com.jetbrains.edu.EduUtils;
19 import com.jetbrains.edu.courseFormat.Task;
20 import com.jetbrains.edu.courseFormat.TaskFile;
21 import com.jetbrains.edu.learning.StudyTaskManager;
22 import com.jetbrains.edu.learning.StudyUtils;
23 import com.jetbrains.edu.learning.courseFormat.UserTest;
24 import com.jetbrains.edu.learning.editor.StudyEditor;
25 import org.jetbrains.annotations.NotNull;
26
27 import java.util.List;
28
29 public class StudyRunAction extends DumbAwareAction {
30   private static final Logger LOG = Logger.getInstance(StudyRunAction.class.getName());
31   public static final String ACTION_ID = "StudyRunAction";
32   private ProcessHandler myHandler;
33
34   public StudyRunAction() {
35     super("Run File With Tests", "Run your code with tests", AllIcons.General.Run);
36   }
37
38   public void run(@NotNull final Project project) {
39     if (myHandler != null && !myHandler.isProcessTerminated()) return;
40     Editor selectedEditor = StudyUtils.getSelectedEditor(project);
41     FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
42     assert selectedEditor != null;
43     VirtualFile openedFile = fileDocumentManager.getFile(selectedEditor.getDocument());
44
45     if (openedFile != null && openedFile.getCanonicalPath() != null) {
46       String filePath = openedFile.getCanonicalPath();
47       executeFile(project, openedFile, filePath);
48     }
49   }
50
51   private void executeFile(@NotNull final Project project,
52                            @NotNull final VirtualFile openedFile, @NotNull final String filePath) {
53     GeneralCommandLine cmd = new GeneralCommandLine();
54     cmd.withWorkDirectory(openedFile.getParent().getCanonicalPath());
55
56     TaskFile selectedTaskFile = StudyUtils.getTaskFile(project, openedFile);
57     assert selectedTaskFile != null;
58     final Task currentTask = selectedTaskFile.getTask();
59     final Sdk sdk = StudyUtils.findSdk(currentTask, project);
60     if (sdk == null) {
61       StudyUtils.showNoSdkNotification(currentTask, project);
62       return;
63     }
64     String sdkHomePath = sdk.getHomePath();
65     if (sdkHomePath != null) {
66       cmd.setExePath(sdkHomePath);
67       Process process;
68       StudyUtils.setCommandLineParameters(cmd, project, filePath, sdkHomePath, currentTask);
69
70       try {
71         process = cmd.createProcess();
72       }
73       catch (ExecutionException e) {
74         LOG.error(e);
75         return;
76       }
77       myHandler = new OSProcessHandler(process);
78       final RunContentExecutor executor = StudyUtils.getExecutor(project, currentTask, myHandler);
79       if (executor != null) {
80         Disposer.register(project, executor);
81         executor.run();
82       }
83     }
84   }
85
86   public void actionPerformed(@NotNull AnActionEvent e) {
87     final Project project = e.getProject();
88     if (project != null) {
89       run(project);
90     }
91   }
92
93   @Override
94   public void update(AnActionEvent e) {
95     EduUtils.enableAction(e, false);
96
97     final Project project = e.getProject();
98     if (project != null) {
99       StudyEditor studyEditor = StudyUtils.getSelectedStudyEditor(project);
100       if (studyEditor != null) {
101         final List<UserTest> userTests = StudyTaskManager.getInstance(project).getUserTests(studyEditor.getTaskFile().getTask());
102         if (!userTests.isEmpty()) {
103           EduUtils.enableAction(e, true);
104         }
105       }
106     }
107   }
108 }