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