Unified fonts in add answer placeholder panel
[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 import java.io.File;
30
31 public class PyCCCommandLineState extends PythonCommandLineState {
32   private final PyCCRunTestConfiguration myRunConfiguration;
33   private final VirtualFile myTaskDir;
34   private final Task myTask;
35
36   public PyCCCommandLineState(PyCCRunTestConfiguration runConfiguration,
37                               ExecutionEnvironment env) {
38     super(runConfiguration, env);
39     myRunConfiguration = runConfiguration;
40
41     VirtualFile testsFile = LocalFileSystem.getInstance().findFileByPath(myRunConfiguration.getPathToTest());
42     assert testsFile != null;
43     myTaskDir = StudyUtils.getTaskDir(testsFile);
44     assert myTaskDir != null;
45     myTask = StudyUtils.getTask(myRunConfiguration.getProject(), myTaskDir);
46     assert myTask != null;
47   }
48
49   @Override
50   protected void buildCommandLineParameters(GeneralCommandLine commandLine) {
51     ParamsGroup group = commandLine.getParametersList().getParamsGroup(GROUP_SCRIPT);
52     assert group != null;
53
54     Project project = myRunConfiguration.getProject();
55     Course course = StudyTaskManager.getInstance(project).getCourse();
56     assert course != null;
57
58     group.addParameter(myRunConfiguration.getPathToTest());
59     group.addParameter(new File(course.getCourseDirectory()).getPath());
60
61     group.addParameter(getFirstTaskFilePath());
62   }
63
64   @NotNull
65   private String getFirstTaskFilePath() {
66     String firstTaskFileName = StudyUtils.getFirst(myTask.getTaskFiles().keySet());
67     String taskDirPath = FileUtil.toSystemDependentName(myTaskDir.getPath());
68     return myTaskDir.findChild(EduNames.SRC) != null ?
69            FileUtil.join(taskDirPath, EduNames.SRC, firstTaskFileName) :
70            FileUtil.join(taskDirPath, firstTaskFileName);
71   }
72
73   @Override
74   public ExecutionResult execute(Executor executor, CommandLinePatcher... patchers) throws ExecutionException {
75     CCUtils.createResources(myRunConfiguration.getProject(), myTask, myTaskDir);
76     ApplicationManager.getApplication().runWriteAction(() -> StudyCheckUtils.flushWindows(myTask, myTaskDir));
77
78     return super.execute(executor, patchers);
79   }
80
81   @Override
82   protected ProcessHandler doCreateProcess(GeneralCommandLine commandLine) throws ExecutionException {
83     ProcessHandler handler = super.doCreateProcess(commandLine);
84     handler.addProcessListener(new ProcessAdapter() {
85       @Override
86       public void processTerminated(ProcessEvent event) {
87         ApplicationManager.getApplication().invokeLater(() -> EduUtils.deleteWindowDescriptions(myTask, myTaskDir));
88       }
89     });
90     return handler;
91   }
92 }