Search in sources :

Example 1 with CourseInfo

use of com.jetbrains.edu.learning.courseFormat.CourseInfo in project intellij-community by JetBrains.

the class CCGetCourseFromStepic method createCourse.

private static void createCourse(Project project, String courseId) {
    final VirtualFile baseDir = project.getBaseDir();
    final CourseInfo info = CCStepicConnector.getCourseInfo(courseId);
    if (info == null)
        return;
    final Course course = EduStepicConnector.getCourse(project, info);
    if (course != null) {
        flushCourse(course);
        final File courseDirectory = StudyUtils.getCourseDirectory(course);
        ApplicationManager.getApplication().invokeAndWait(() -> ApplicationManager.getApplication().runWriteAction(() -> {
            final VirtualFile[] children = baseDir.getChildren();
            for (VirtualFile child : children) {
                StudyUtils.deleteFile(child);
            }
            StudyGenerator.createCourse(course, baseDir, courseDirectory, project);
        }));
        StudyTaskManager.getInstance(project).setCourse(course);
        File courseDir = new File(OUR_COURSES_DIR, course.getName() + "-" + project.getName());
        course.setCourseDirectory(courseDir.getPath());
        course.setCourseMode(CCUtils.COURSE_MODE);
        project.getBaseDir().refresh(false, true);
        int index = 1;
        int taskIndex = 1;
        for (Lesson lesson : course.getLessons()) {
            final VirtualFile lessonDir = project.getBaseDir().findChild(EduNames.LESSON + String.valueOf(index));
            lesson.setIndex(index);
            if (lessonDir == null)
                continue;
            for (Task task : lesson.getTaskList()) {
                final VirtualFile taskDir = lessonDir.findChild(EduNames.TASK + String.valueOf(taskIndex));
                task.setIndex(taskIndex);
                task.setLesson(lesson);
                if (taskDir == null)
                    continue;
                for (final Map.Entry<String, TaskFile> entry : task.getTaskFiles().entrySet()) {
                    ApplicationManager.getApplication().invokeAndWait(() -> ApplicationManager.getApplication().runWriteAction(() -> createAnswerFile(project, taskDir, entry)));
                }
                taskIndex += 1;
            }
            index += 1;
            taskIndex = 1;
        }
        course.initCourse(true);
        ApplicationManager.getApplication().invokeAndWait(() -> StudyUtils.registerStudyToolWindow(course, project));
    }
    VirtualFileManager.getInstance().refreshWithoutFileWatcher(true);
    ProjectView.getInstance(project).refresh();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) TaskFile(com.jetbrains.edu.learning.courseFormat.TaskFile) Task(com.jetbrains.edu.learning.courseFormat.Task) Course(com.jetbrains.edu.learning.courseFormat.Course) StudyProjectGenerator.flushCourse(com.jetbrains.edu.learning.courseGeneration.StudyProjectGenerator.flushCourse) CourseInfo(com.jetbrains.edu.learning.courseFormat.CourseInfo) VirtualFile(com.intellij.openapi.vfs.VirtualFile) TaskFile(com.jetbrains.edu.learning.courseFormat.TaskFile) File(java.io.File) CCFromCourseArchive.createAnswerFile(com.jetbrains.edu.coursecreator.actions.CCFromCourseArchive.createAnswerFile) Lesson(com.jetbrains.edu.learning.courseFormat.Lesson) Map(java.util.Map)

Example 2 with CourseInfo

use of com.jetbrains.edu.learning.courseFormat.CourseInfo in project intellij-community by JetBrains.

the class StudyProjectGenerator method addCourse.

/**
   * Adds course to courses specified in params
   *
   * @param courses
   * @param courseDir must be directory containing course file
   * @return added course name or null if course is invalid
   */
@Nullable
private static CourseInfo addCourse(List<CourseInfo> courses, File courseDir) {
    if (courseDir.isDirectory()) {
        File[] courseFiles = courseDir.listFiles((dir, name) -> name.equals(EduNames.COURSE_META_FILE));
        if (courseFiles == null || courseFiles.length != 1) {
            LOG.info("User tried to add course with more than one or without course files");
            return null;
        }
        File courseFile = courseFiles[0];
        CourseInfo courseInfo = getCourseInfo(courseFile);
        if (courseInfo != null) {
            courses.add(0, courseInfo);
        }
        return courseInfo;
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) TaskFile(com.jetbrains.edu.learning.courseFormat.TaskFile) PsiFile(com.intellij.psi.PsiFile) NewVirtualFile(com.intellij.openapi.vfs.newvfs.NewVirtualFile) CourseInfo(com.jetbrains.edu.learning.courseFormat.CourseInfo) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with CourseInfo

use of com.jetbrains.edu.learning.courseFormat.CourseInfo in project intellij-community by JetBrains.

the class StudyProjectGenerator method getCourseInfo.

/**
   * Parses course json meta file and finds course name
   *
   * @return information about course or null if course file is invalid
   */
@Nullable
private static CourseInfo getCourseInfo(File courseFile) {
    if (courseFile.isDirectory()) {
        File[] courseFiles = courseFile.listFiles((dir, name) -> name.equals(EduNames.COURSE_META_FILE));
        if (courseFiles == null || courseFiles.length != 1) {
            LOG.info("More than one or without course files");
            return null;
        }
        courseFile = courseFiles[0];
    }
    CourseInfo courseInfo = null;
    BufferedReader reader = null;
    try {
        if (courseFile.getName().equals(EduNames.COURSE_META_FILE)) {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(courseFile), "UTF-8"));
            JsonReader r = new JsonReader(reader);
            JsonParser parser = new JsonParser();
            JsonElement el = parser.parse(r);
            String courseName = el.getAsJsonObject().get(COURSE_NAME_ATTRIBUTE).getAsString();
            String courseDescription = el.getAsJsonObject().get(COURSE_DESCRIPTION).getAsString();
            JsonArray courseAuthors = el.getAsJsonObject().get(AUTHOR_ATTRIBUTE).getAsJsonArray();
            String language = el.getAsJsonObject().get(LANGUAGE_ATTRIBUTE).getAsString();
            courseInfo = new CourseInfo();
            courseInfo.setName(courseName);
            courseInfo.setDescription(courseDescription);
            courseInfo.setType("pycharm " + language);
            final ArrayList<StepicUser> authors = new ArrayList<>();
            for (JsonElement author : courseAuthors) {
                final JsonObject authorAsJsonObject = author.getAsJsonObject();
                final StepicUser stepicUser = new StepicUser();
                stepicUser.setFirstName(authorAsJsonObject.get("first_name").getAsString());
                stepicUser.setLastName(authorAsJsonObject.get("last_name").getAsString());
                authors.add(stepicUser);
            }
            courseInfo.setAuthors(authors);
        }
    } catch (Exception e) {
    //error will be shown in UI
    } finally {
        StudyUtils.closeSilently(reader);
    }
    return courseInfo;
}
Also used : CourseInfo(com.jetbrains.edu.learning.courseFormat.CourseInfo) JsonReader(com.google.gson.stream.JsonReader) StepicUser(com.jetbrains.edu.learning.stepic.StepicUser) VirtualFile(com.intellij.openapi.vfs.VirtualFile) TaskFile(com.jetbrains.edu.learning.courseFormat.TaskFile) PsiFile(com.intellij.psi.PsiFile) NewVirtualFile(com.intellij.openapi.vfs.newvfs.NewVirtualFile) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with CourseInfo

use of com.jetbrains.edu.learning.courseFormat.CourseInfo in project intellij-community by JetBrains.

the class StudyProjectGenerator method flushCache.

public static void flushCache(List<CourseInfo> courses, boolean preserveOld) {
    File cacheFile = new File(OUR_COURSES_DIR, CACHE_NAME);
    PrintWriter writer = null;
    try {
        if (!createCacheFile(cacheFile))
            return;
        Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
        final Set<CourseInfo> courseInfos = new HashSet<>();
        courseInfos.addAll(courses);
        if (preserveOld) {
            courseInfos.addAll(getCoursesFromCache());
        }
        writer = new PrintWriter(cacheFile, "UTF-8");
        try {
            for (CourseInfo courseInfo : courseInfos) {
                final String json = gson.toJson(courseInfo);
                writer.println(json);
            }
        } finally {
            StudyUtils.closeSilently(writer);
        }
    } catch (IOException e) {
        LOG.error(e);
    } finally {
        StudyUtils.closeSilently(writer);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) TaskFile(com.jetbrains.edu.learning.courseFormat.TaskFile) PsiFile(com.intellij.psi.PsiFile) NewVirtualFile(com.intellij.openapi.vfs.newvfs.NewVirtualFile) CourseInfo(com.jetbrains.edu.learning.courseFormat.CourseInfo)

Example 5 with CourseInfo

use of com.jetbrains.edu.learning.courseFormat.CourseInfo in project intellij-community by JetBrains.

the class StudyProjectGenerator method addLocalCourse.

/**
   * Adds course from zip archive to courses
   *
   * @return added course name or null if course is invalid
   */
@Nullable
public CourseInfo addLocalCourse(String zipFilePath) {
    File file = new File(zipFilePath);
    try {
        String fileName = file.getName();
        String unzippedName = fileName.substring(0, fileName.indexOf("."));
        File courseDir = new File(OUR_COURSES_DIR, unzippedName);
        ZipUtil.unzip(null, courseDir, file, null, null, true);
        CourseInfo courseName = addCourse(myCourses, courseDir);
        flushCache(myCourses);
        if (courseName != null && !courseName.getName().equals(unzippedName)) {
            //noinspection ResultOfMethodCallIgnored
            File dest = new File(OUR_COURSES_DIR, courseName.getName());
            if (dest.exists()) {
                FileUtil.delete(dest);
            }
            courseDir.renameTo(dest);
            //noinspection ResultOfMethodCallIgnored
            courseDir.delete();
        }
        return courseName;
    } catch (IOException e) {
        LOG.error("Failed to unzip course archive");
        LOG.error(e);
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) TaskFile(com.jetbrains.edu.learning.courseFormat.TaskFile) PsiFile(com.intellij.psi.PsiFile) NewVirtualFile(com.intellij.openapi.vfs.newvfs.NewVirtualFile) CourseInfo(com.jetbrains.edu.learning.courseFormat.CourseInfo) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

CourseInfo (com.jetbrains.edu.learning.courseFormat.CourseInfo)16 VirtualFile (com.intellij.openapi.vfs.VirtualFile)8 TaskFile (com.jetbrains.edu.learning.courseFormat.TaskFile)7 NewVirtualFile (com.intellij.openapi.vfs.newvfs.NewVirtualFile)5 PsiFile (com.intellij.psi.PsiFile)5 Course (com.jetbrains.edu.learning.courseFormat.Course)4 File (java.io.File)4 Project (com.intellij.openapi.project.Project)3 Lesson (com.jetbrains.edu.learning.courseFormat.Lesson)3 List (java.util.List)3 Nullable (org.jetbrains.annotations.Nullable)3 IdeView (com.intellij.ide.IdeView)2 Notification (com.intellij.notification.Notification)2 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)2 Task (com.intellij.openapi.progress.Task)2 AncestorListenerAdapter (com.intellij.ui.AncestorListenerAdapter)2 ArrayList (java.util.ArrayList)2 JsonReader (com.google.gson.stream.JsonReader)1 FacetValidatorsManager (com.intellij.facet.ui.FacetValidatorsManager)1 ValidationResult (com.intellij.facet.ui.ValidationResult)1