0bde8098b28ff8d094dfc937d92cfe5029e70db0
[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.project.Project;
14 import com.intellij.openapi.util.io.FileUtil;
15 import com.intellij.openapi.vfs.LocalFileSystem;
16 import com.intellij.openapi.vfs.VirtualFile;
17 import com.jetbrains.edu.coursecreator.CCUtils;
18 import com.jetbrains.edu.learning.StudyTaskManager;
19 import com.jetbrains.edu.learning.StudyUtils;
20 import com.jetbrains.edu.learning.checker.StudyCheckUtils;
21 import com.jetbrains.edu.learning.core.EduNames;
22 import com.jetbrains.edu.learning.core.EduUtils;
23 import com.jetbrains.edu.learning.courseFormat.Course;
24 import com.jetbrains.edu.learning.courseFormat.Task;
25 import com.jetbrains.python.run.CommandLinePatcher;
26 import com.jetbrains.python.run.PythonCommandLineState;
27 import org.jetbrains.annotations.NotNull;
28
29 public class PyCCCommandLineState extends PythonCommandLineState {
30   private final PyCCRunTestConfiguration myRunConfiguration;
31   private final VirtualFile myTaskDir;
32   private final Task myTask;
33
34   public PyCCCommandLineState(PyCCRunTestConfiguration runConfiguration,
35                               ExecutionEnvironment env) {
36     super(runConfiguration, env);
37     myRunConfiguration = runConfiguration;
38
39     VirtualFile testsFile = LocalFileSystem.getInstance().findFileByPath(myRunConfiguration.getPathToTest());
40     assert testsFile != null;
41     myTaskDir = StudyUtils.getTaskDir(testsFile);
42     assert myTaskDir != null;
43     myTask = StudyUtils.getTask(myRunConfiguration.getProject(), myTaskDir);
44     assert myTask != null;
45   }
46
47   @Override
48   protected void buildCommandLineParameters(GeneralCommandLine commandLine) {
49     ParamsGroup group = commandLine.getParametersList().getParamsGroup(GROUP_SCRIPT);
50     assert group != null;
51
52     Project project = myRunConfiguration.getProject();
53     Course course = StudyTaskManager.getInstance(project).getCourse();
54     assert course != null;
55
56     group.addParameter(myRunConfiguration.getPathToTest());
57     group.addParameter(course.getCourseDirectory());
58
59     group.addParameter(getFirstTaskFilePath());
60   }
61
62   @NotNull
63   private String getFirstTaskFilePath() {
64     String firstTaskFileName = StudyUtils.getFirst(myTask.getTaskFiles().keySet());
65     return myTaskDir.findChild(EduNames.SRC) != null ?
66            FileUtil.join(myTaskDir.getPath(), EduNames.SRC, firstTaskFileName) :
67            FileUtil.join(myTaskDir.getPath(), firstTaskFileName);
68   }
69
70   @Override
71   public ExecutionResult execute(Executor executor, CommandLinePatcher... patchers) throws ExecutionException {
72     CCUtils.createResources(myRunConfiguration.getProject(), myTask, myTaskDir);
73     ApplicationManager.getApplication().runWriteAction(() -> StudyCheckUtils.flushWindows(myTask, myTaskDir));
74
75     return super.execute(executor, patchers);
76   }
77
78   @Override
79   protected ProcessHandler doCreateProcess(GeneralCommandLine commandLine) throws ExecutionException {
80     ProcessHandler handler = super.doCreateProcess(commandLine);
81     handler.addProcessListener(new ProcessAdapter() {
82       @Override
83       public void processTerminated(ProcessEvent event) {
84         ApplicationManager.getApplication().invokeLater(() -> EduUtils.deleteWindowDescriptions(myTask, myTaskDir));
85       }
86     });
87     return handler;
88   }
89 }