synchronize courses after update for custom projects
[idea/community.git] / python / edu / learn-python / src / com / jetbrains / python / edu / StudyInitialConfigurator.java
1 package com.jetbrains.python.edu;
2
3 import com.intellij.codeInsight.CodeInsightSettings;
4 import com.intellij.ide.RecentProjectsManager;
5 import com.intellij.ide.ui.UISettings;
6 import com.intellij.ide.util.PropertiesComponent;
7 import com.intellij.openapi.application.PathManager;
8 import com.intellij.openapi.diagnostic.Logger;
9 import com.intellij.openapi.fileTypes.FileTypeManager;
10 import com.intellij.openapi.project.ex.ProjectManagerEx;
11 import com.intellij.openapi.util.io.FileUtil;
12 import com.intellij.platform.templates.github.ZipUtil;
13 import com.intellij.util.PathUtil;
14 import com.intellij.util.messages.MessageBus;
15 import org.jetbrains.annotations.NonNls;
16
17 import java.io.File;
18 import java.io.IOException;
19
20 @SuppressWarnings({"UtilityClassWithoutPrivateConstructor", "UtilityClassWithPublicConstructor"})
21 public class StudyInitialConfigurator {
22   private static final Logger LOG = Logger.getInstance(StudyInitialConfigurator.class.getName());
23   @NonNls private static final String CONFIGURED_V1 = "StudyPyCharm.InitialConfiguration";
24   @NonNls private static final String CONFIGURED_V11 = "StudyPyCharm.InitialConfiguration1.1";
25
26   /**
27    * @noinspection UnusedParameters
28    */
29   public StudyInitialConfigurator(MessageBus bus,
30                                   UISettings uiSettings,
31                                   CodeInsightSettings codeInsightSettings,
32                                   final PropertiesComponent propertiesComponent,
33                                   FileTypeManager fileTypeManager,
34                                   final ProjectManagerEx projectManager,
35                                   RecentProjectsManager recentProjectsManager) {
36     final File file = new File(getCoursesRoot(), "introduction_course.zip");
37     if (!propertiesComponent.getBoolean(CONFIGURED_V1, false)) {
38       final File newCourses = new File(PathManager.getConfigPath(), "courses");
39       try {
40         FileUtil.createDirectory(newCourses);
41         copyCourse(file, newCourses);
42         propertiesComponent.setValue(CONFIGURED_V1, "true");
43
44       }
45       catch (IOException e) {
46         LOG.warn("Couldn't copy bundled courses " + e);
47       }
48     }
49     if (!propertiesComponent.getBoolean(CONFIGURED_V11, false)) {
50       final File newCourses = new File(PathManager.getConfigPath(), "courses");
51       if (newCourses.exists()) {
52         try {
53           copyCourse(file, newCourses);
54           propertiesComponent.setValue(CONFIGURED_V11, "true");
55         }
56         catch (IOException e) {
57           LOG.warn("Couldn't copy bundled courses " + e);
58         }
59       }
60     }
61   }
62
63   private static void copyCourse(File bundledCourse, File userCourseDir) throws IOException {
64     String fileName = bundledCourse.getName();
65     String unzippedName = fileName.substring(0, fileName.indexOf("."));
66     File courseDir = new File(userCourseDir, unzippedName);
67     ZipUtil.unzip(null, courseDir, bundledCourse, null, null, true);
68   }
69
70   public static File getCoursesRoot() {
71     @NonNls String jarPath = PathUtil.getJarPathForClass(StudyInitialConfigurator.class);
72     if (jarPath.endsWith(".jar")) {
73       final File jarFile = new File(jarPath);
74
75
76       File pluginBaseDir = jarFile.getParentFile();
77       return new File(pluginBaseDir, "courses");
78     }
79
80     return new File(jarPath , "courses");
81   }
82
83 }