EDU-704 Unable to preview course from course creator with russian name on Windows
[idea/community.git] / python / educational-python / course-creator-python / src / com / jetbrains / edu / coursecreator / CreateFromArchiveProjectStep.java
1 package com.jetbrains.edu.coursecreator;
2
3 import com.intellij.openapi.module.Module;
4 import com.intellij.openapi.project.Project;
5 import com.intellij.openapi.util.io.FileUtil;
6 import com.intellij.openapi.vfs.VirtualFile;
7 import com.intellij.platform.DirectoryProjectGenerator;
8 import com.jetbrains.edu.coursecreator.actions.CCCreateCourseArchive;
9 import com.jetbrains.edu.learning.PyStudyDirectoryProjectGenerator;
10 import com.jetbrains.edu.learning.StudyTaskManager;
11 import com.jetbrains.edu.learning.courseFormat.Course;
12 import com.jetbrains.edu.learning.courseGeneration.StudyProjectGenerator;
13 import com.jetbrains.edu.learning.stepic.CourseInfo;
14 import com.jetbrains.python.newProject.actions.PyCharmNewProjectStep;
15 import org.jetbrains.annotations.NotNull;
16
17 class CreateFromArchiveProjectStep extends PyCharmNewProjectStep {
18
19   public CreateFromArchiveProjectStep(Project project, Module module) {
20     super(new MyCustomization(project, module));
21   }
22
23   protected static class MyCustomization extends PyCharmNewProjectStep.Customization {
24
25     private final Project myProject;
26     private final Module myModule;
27     private PyStudyDirectoryProjectGenerator myGenerator = new PyStudyDirectoryProjectGenerator();
28
29     public MyCustomization(Project project,
30                            Module module) {
31
32       myProject = project;
33       myModule = module;
34     }
35
36     @NotNull
37     @Override
38     protected DirectoryProjectGenerator[] getProjectGenerators() {
39       return new DirectoryProjectGenerator[] {};
40     }
41
42
43     @NotNull
44     @Override
45     protected DirectoryProjectGenerator createEmptyProjectGenerator() {
46       Course course = StudyTaskManager.getInstance(myProject).getCourse();
47       if (course != null) {
48         VirtualFile folder = CCUtils.getGeneratedFilesFolder(myProject, myModule);
49         String zipName = FileUtil.sanitizeFileName(course.getName());
50         String locationDir = folder.getPath();
51         CCCreateCourseArchive.createCourseArchive(myProject, myModule, zipName, locationDir, false);
52         String path = FileUtil.join(FileUtil.toSystemDependentName(folder.getPath()), zipName + ".zip");
53         StudyProjectGenerator generator = myGenerator.getGenerator();
54         CourseInfo info = generator.addLocalCourse(path);
55         assert info != null;
56         generator.setSelectedCourse(info);
57       }
58       return myGenerator;
59     }
60   }
61 }