use of com.jetbrains.edu.learning.courseFormat.TaskFile in project intellij-community by JetBrains.
the class StudyProjectGenerator method flushTask.
public static void flushTask(@NotNull final Task task, @NotNull final File taskDirectory) {
FileUtil.createDirectory(taskDirectory);
for (Map.Entry<String, TaskFile> taskFileEntry : task.taskFiles.entrySet()) {
final String name = FileUtil.toSystemDependentName(taskFileEntry.getKey());
final TaskFile taskFile = taskFileEntry.getValue();
final File file = new File(taskDirectory, name);
FileUtil.createIfDoesntExist(file);
try {
if (EduUtils.isImage(taskFile.name)) {
FileUtil.writeToFile(file, Base64.decodeBase64(taskFile.text));
} else {
FileUtil.writeToFile(file, taskFile.text);
}
} catch (IOException e) {
LOG.error(e);
}
}
createFiles(taskDirectory, task.getTestsText());
if (task.getTaskTexts().isEmpty()) {
createTaskHtml(task, taskDirectory);
return;
}
task.setText(null);
createFiles(taskDirectory, task.getTaskTexts());
}
use of com.jetbrains.edu.learning.courseFormat.TaskFile in project intellij-community by JetBrains.
the class StudyHighlightErrorFilter method shouldHighlightErrorElement.
@Override
public boolean shouldHighlightErrorElement(@NotNull PsiErrorElement element) {
PsiFile file = element.getContainingFile();
if (file == null) {
return true;
}
VirtualFile virtualFile = file.getVirtualFile();
if (virtualFile == null) {
return true;
}
TaskFile taskFile = StudyUtils.getTaskFile(element.getProject(), virtualFile);
return taskFile == null || taskFile.isHighlightErrors();
}
use of com.jetbrains.edu.learning.courseFormat.TaskFile in project intellij-community by JetBrains.
the class StudySmartChecker method smartCheck.
public static void smartCheck(@NotNull final AnswerPlaceholder placeholder, @NotNull final Project project, @NotNull final VirtualFile answerFile, @NotNull final TaskFile answerTaskFile, @NotNull final TaskFile usersTaskFile, @NotNull final StudyTestRunner testRunner, @NotNull final VirtualFile virtualFile, @NotNull final Document usersDocument) {
VirtualFile fileWindows = null;
File resourceFile = null;
VirtualFile windowCopy = null;
try {
final int index = placeholder.getIndex();
String windowCopyName = answerFile.getNameWithoutExtension() + index + EduNames.WINDOW_POSTFIX + answerFile.getExtension();
windowCopy = answerFile.copy(project, answerFile.getParent(), windowCopyName);
final FileDocumentManager documentManager = FileDocumentManager.getInstance();
final Document windowDocument = documentManager.getDocument(windowCopy);
if (windowDocument != null) {
resourceFile = StudyUtils.copyResourceFile(virtualFile.getName(), windowCopy.getName(), project, usersTaskFile.getTask());
TaskFile windowTaskFile = answerTaskFile.getTask().copy().getTaskFile(StudyUtils.pathRelativeToTask(virtualFile));
if (windowTaskFile == null) {
return;
}
EduDocumentListener listener = new EduDocumentListener(windowTaskFile);
windowDocument.addDocumentListener(listener);
int start = placeholder.getOffset();
int end = start + placeholder.getRealLength();
final AnswerPlaceholder userAnswerPlaceholder = usersTaskFile.getAnswerPlaceholders().get(placeholder.getIndex());
int userStart = userAnswerPlaceholder.getOffset();
int userEnd = userStart + userAnswerPlaceholder.getRealLength();
String text = usersDocument.getText(new TextRange(userStart, userEnd));
windowDocument.replaceString(start, end, text);
ApplicationManager.getApplication().runWriteAction(() -> documentManager.saveDocument(windowDocument));
fileWindows = EduUtils.flushWindows(windowTaskFile, windowCopy);
Process smartTestProcess = testRunner.createCheckProcess(project, windowCopy.getPath());
final CapturingProcessHandler handler = new CapturingProcessHandler(smartTestProcess, null, windowCopy.getPath());
final ProcessOutput output = handler.runProcess();
final Course course = StudyTaskManager.getInstance(project).getCourse();
if (course != null) {
boolean res = StudyTestsOutputParser.getTestsOutput(output, course.isAdaptive()).isSuccess();
StudyTaskManager.getInstance(project).setStatus(userAnswerPlaceholder, res ? StudyStatus.Solved : StudyStatus.Failed);
}
}
} catch (ExecutionException | IOException e) {
LOG.error(e);
} finally {
StudyUtils.deleteFile(windowCopy);
StudyUtils.deleteFile(fileWindows);
if (resourceFile != null && resourceFile.exists() && !resourceFile.delete()) {
LOG.error("failed to delete", resourceFile.getPath());
}
}
}
use of com.jetbrains.edu.learning.courseFormat.TaskFile in project intellij-community by JetBrains.
the class PyStudyInspectionExtension method ignoreUnresolvedReference.
@Override
public boolean ignoreUnresolvedReference(@NotNull PyElement element, @NotNull PsiReference reference) {
final PsiFile file = element.getContainingFile();
final Project project = file.getProject();
if (StudyTaskManager.getInstance(project).getCourse() == null) {
return false;
}
TaskFile taskFile = StudyUtils.getTaskFile(project, file.getVirtualFile());
if (taskFile == null || taskFile.isUserCreated() || taskFile.isHighlightErrors()) {
return false;
}
if (PsiTreeUtil.getParentOfType(element, PyImportStatementBase.class) != null) {
return false;
}
return true;
}
use of com.jetbrains.edu.learning.courseFormat.TaskFile in project intellij-community by JetBrains.
the class CCProjectComponent method getAllAnswerTaskFiles.
private static List<VirtualFile> getAllAnswerTaskFiles(@NotNull Course course, @NotNull Project project) {
List<VirtualFile> result = new ArrayList<>();
for (Lesson lesson : course.getLessons()) {
for (Task task : lesson.getTaskList()) {
for (Map.Entry<String, TaskFile> entry : task.getTaskFiles().entrySet()) {
String name = entry.getKey();
String answerName = FileUtil.getNameWithoutExtension(name) + CCUtils.ANSWER_EXTENSION_DOTTED + FileUtilRt.getExtension(name);
String taskPath = FileUtil.join(project.getBasePath(), EduNames.LESSON + lesson.getIndex(), EduNames.TASK + task.getIndex());
VirtualFile taskFile = LocalFileSystem.getInstance().findFileByPath(FileUtil.join(taskPath, answerName));
if (taskFile == null) {
taskFile = LocalFileSystem.getInstance().findFileByPath(FileUtil.join(taskPath, EduNames.SRC, answerName));
}
if (taskFile != null) {
result.add(taskFile);
}
}
}
}
return result;
}
Aggregations