bb2264dbc18d042bd0069140f62582253b25d620
[idea/community.git] / python / educational-python / course-creator-python / src / com / jetbrains / edu / coursecreator / run / PyCCCommandLineState.java
1 package com.jetbrains.edu.coursecreator.run;
2
3 import com.intellij.execution.ExecutionException;
4 import com.intellij.execution.ExecutionResult;
5 import com.intellij.execution.Executor;
6 import com.intellij.execution.configurations.GeneralCommandLine;
7 import com.intellij.execution.configurations.ParamsGroup;
8 import com.intellij.execution.process.ProcessAdapter;
9 import com.intellij.execution.process.ProcessEvent;
10 import com.intellij.execution.process.ProcessHandler;
11 import com.intellij.execution.runners.ExecutionEnvironment;
12 import com.intellij.openapi.application.ApplicationManager;
13 import com.intellij.openapi.fileEditor.impl.text.TextEditorProvider;
14 import com.intellij.openapi.project.Project;
15 import com.intellij.openapi.util.io.FileUtil;
16 import com.intellij.openapi.vfs.LocalFileSystem;
17 import com.intellij.openapi.vfs.VirtualFile;
18 import com.jetbrains.edu.coursecreator.CCUtils;
19 import com.jetbrains.edu.learning.StudyTaskManager;
20 import com.jetbrains.edu.learning.StudyUtils;
21 import com.jetbrains.edu.learning.checker.StudyCheckUtils;
22 import com.jetbrains.edu.learning.core.EduNames;
23 import com.jetbrains.edu.learning.core.EduUtils;
24 import com.jetbrains.edu.learning.courseFormat.Course;
25 import com.jetbrains.edu.learning.courseFormat.Task;
26 import com.jetbrains.edu.learning.courseFormat.TaskFile;
27 import com.jetbrains.python.run.CommandLinePatcher;
28 import com.jetbrains.python.run.PythonCommandLineState;
29 import org.jetbrains.annotations.Nullable;
30
31 import java.io.File;
32 import java.util.Map;
33
34 public class PyCCCommandLineState extends PythonCommandLineState {
35   private final PyCCRunTestConfiguration myRunConfiguration;
36   private final VirtualFile myTaskDir;
37   private final Task myTask;
38
39   public PyCCCommandLineState(PyCCRunTestConfiguration runConfiguration,
40                               ExecutionEnvironment env) {
41     super(runConfiguration, env);
42     myRunConfiguration = runConfiguration;
43
44     VirtualFile testsFile = LocalFileSystem.getInstance().findFileByPath(myRunConfiguration.getPathToTest());
45     assert testsFile != null;
46     myTaskDir = StudyUtils.getTaskDir(testsFile);
47     assert myTaskDir != null;
48     myTask = StudyUtils.getTask(myRunConfiguration.getProject(), myTaskDir);
49     assert myTask != null;
50   }
51
52   @Override
53   protected void buildCommandLineParameters(GeneralCommandLine commandLine) {
54     ParamsGroup group = commandLine.getParametersList().getParamsGroup(GROUP_SCRIPT);
55     assert group != null;
56
57     Project project = myRunConfiguration.getProject();
58     Course course = StudyTaskManager.getInstance(project).getCourse();
59     assert course != null;
60
61     group.addParameter(myRunConfiguration.getPathToTest());
62     group.addParameter(new File(course.getCourseDirectory()).getPath());
63
64     String path = getFirstTaskFilePath();
65     if (path != null) {
66       group.addParameter(path);
67     }
68   }
69
70   @Nullable
71   private String getFirstTaskFilePath() {
72     for (Map.Entry<String, TaskFile> entry : myTask.getTaskFiles().entrySet()) {
73       String path = getTaskFilePath(entry.getKey());
74       if (!entry.getValue().getAnswerPlaceholders().isEmpty()) {
75         return path;
76       }
77       VirtualFile virtualFile = LocalFileSystem.getInstance().findFileByPath(path);
78       if (virtualFile == null) {
79         continue;
80       }
81       if (TextEditorProvider.isTextFile(virtualFile)) {
82         return path;
83       }
84     }
85     return null;
86   }
87
88
89   private String getTaskFilePath(String name) {
90     String taskDirPath = FileUtil.toSystemDependentName(myTaskDir.getPath());
91     return myTaskDir.findChild(EduNames.SRC) != null ?
92            FileUtil.join(taskDirPath, EduNames.SRC, name) :
93            FileUtil.join(taskDirPath, name);
94   }
95
96   @Override
97   public ExecutionResult execute(Executor executor, CommandLinePatcher... patchers) throws ExecutionException {
98     CCUtils.createResources(myRunConfiguration.getProject(), myTask, myTaskDir);
99     ApplicationManager.getApplication().runWriteAction(() -> StudyCheckUtils.flushWindows(myTask, myTaskDir));
100
101     return super.execute(executor, patchers);
102   }
103
104   @Override
105   protected ProcessHandler doCreateProcess(GeneralCommandLine commandLine) throws ExecutionException {
106     ProcessHandler handler = super.doCreateProcess(commandLine);
107     handler.addProcessListener(new ProcessAdapter() {
108       @Override
109       public void processTerminated(ProcessEvent event) {
110         ApplicationManager.getApplication().invokeLater(() -> EduUtils.deleteWindowDescriptions(myTask, myTaskDir));
111       }
112     });
113     return handler;
114   }
115 }