Update existing course for PyCharm Edu 101
authorEkaterina Tuzova <Ekaterina.Tuzova@jetbrains.com>
Sat, 20 Dec 2014 11:02:28 +0000 (14:02 +0300)
committerEkaterina Tuzova <Ekaterina.Tuzova@jetbrains.com>
Sat, 20 Dec 2014 11:02:28 +0000 (14:02 +0300)
python/edu/learn-python/src/com/jetbrains/python/edu/StudyInitialConfigurator.java
python/edu/learn-python/src/com/jetbrains/python/edu/StudyNames.java [new file with mode: 0644]
python/edu/learn-python/src/com/jetbrains/python/edu/StudyTaskManager.java

index 4fde1ab720cc7ef39e92d894527186e78cd57c7e..aa2d2eb112171dc7c003685d8f6588929b6de1c0 100644 (file)
@@ -2,6 +2,7 @@ package com.jetbrains.python.edu;
 
 import com.intellij.codeInsight.CodeInsightSettings;
 import com.intellij.ide.RecentProjectsManager;
+import com.intellij.ide.impl.ProjectUtil;
 import com.intellij.ide.ui.UISettings;
 import com.intellij.ide.util.PropertiesComponent;
 import com.intellij.openapi.application.PathManager;
@@ -19,11 +20,11 @@ import java.io.IOException;
 
 @SuppressWarnings({"UtilityClassWithoutPrivateConstructor", "UtilityClassWithPublicConstructor"})
 public class StudyInitialConfigurator {
-  private static final Logger LOG = Logger.getInstance(StudyInitialConfigurator.class.getName()
-  );
+  private static final Logger LOG = Logger.getInstance(StudyInitialConfigurator.class.getName());
   @NonNls private static final String CONFIGURED_V1 = "StudyPyCharm.InitialConfiguration";
   @NonNls private static final String CONFIGURED_V11 = "StudyPyCharm.InitialConfiguration1.1";
 
+  public static boolean UPDATE_PROJECT = false;
 
   /**
    * @noinspection UnusedParameters
@@ -59,6 +60,11 @@ public class StudyInitialConfigurator {
           LOG.warn("Couldn't copy bundled courses " + e);
         }
       }
+      final File projectDir = new File(ProjectUtil.getBaseDir(), "PythonIntroduction");
+      if (projectDir.exists()) {
+        //noinspection AssignmentToStaticFieldFromInstanceMethod
+        UPDATE_PROJECT = true;
+      }
     }
   }
 
diff --git a/python/edu/learn-python/src/com/jetbrains/python/edu/StudyNames.java b/python/edu/learn-python/src/com/jetbrains/python/edu/StudyNames.java
new file mode 100644 (file)
index 0000000..3903d17
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ * Copyright 2000-2014 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.jetbrains.python.edu;
+
+import org.jetbrains.annotations.NonNls;
+
+@NonNls
+public class StudyNames {
+  public static final String INTRODUCTION_COURSE = "introduction_course";
+  public static final String TASK_HTML = "task.html";
+  public static final String TASK_TESTS = "tests.py";
+  public static final String COURSES = "courses";
+  public static final String LESSON = "lesson";
+
+  private StudyNames() {
+  }
+
+}
index 365aa25afd23bc7d0587b48a5f3736b01abd3873..8d46fe6f560c5e70c1fa8f3b276a39414e1043b4 100644 (file)
@@ -1,6 +1,7 @@
 package com.jetbrains.python.edu;
 
 import com.intellij.ide.BrowserUtil;
+import com.intellij.ide.impl.ProjectUtil;
 import com.intellij.ide.ui.UISettings;
 import com.intellij.ide.util.PropertiesComponent;
 import com.intellij.notification.Notification;
@@ -10,7 +11,9 @@ import com.intellij.notification.Notifications;
 import com.intellij.openapi.actionSystem.*;
 import com.intellij.openapi.actionSystem.ex.AnActionListener;
 import com.intellij.openapi.application.ApplicationManager;
+import com.intellij.openapi.application.PathManager;
 import com.intellij.openapi.components.*;
+import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.editor.EditorFactory;
 import com.intellij.openapi.fileEditor.FileEditor;
 import com.intellij.openapi.fileEditor.FileEditorManager;
@@ -20,9 +23,8 @@ import com.intellij.openapi.project.DumbAware;
 import com.intellij.openapi.project.DumbAwareRunnable;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.startup.StartupManager;
-import com.intellij.openapi.ui.popup.Balloon;
-import com.intellij.openapi.ui.popup.JBPopupAdapter;
-import com.intellij.openapi.ui.popup.LightweightWindowEvent;
+import com.intellij.openapi.ui.popup.*;
+import com.intellij.openapi.util.io.FileUtil;
 import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.openapi.vfs.VirtualFileAdapter;
 import com.intellij.openapi.vfs.VirtualFileEvent;
@@ -43,6 +45,8 @@ import org.jetbrains.annotations.Nullable;
 
 import javax.swing.*;
 import javax.swing.event.HyperlinkEvent;
+import java.io.File;
+import java.io.IOException;
 import java.lang.reflect.Method;
 import java.net.URL;
 import java.util.HashMap;
@@ -64,6 +68,7 @@ import java.util.Map;
     )}
 )
 public class StudyTaskManager implements ProjectComponent, PersistentStateComponent<Element>, DumbAware {
+  private static final Logger LOG = Logger.getInstance(StudyTaskManager.class.getName());
   public static final String COURSE_ELEMENT = "courseElement";
   private static Map<String, StudyTaskManager> myTaskManagers = new HashMap<String, StudyTaskManager>();
   private static Map<String, String> myDeletedShortcuts = new HashMap<String, String>();
@@ -109,6 +114,12 @@ public class StudyTaskManager implements ProjectComponent, PersistentStateCompon
 
   @Override
   public void projectOpened() {
+    final File pythonIntroduction = new File(ProjectUtil.getBaseDir(), "PythonIntroduction");
+    if (StudyInitialConfigurator.UPDATE_PROJECT && myProject.getBasePath().equals(pythonIntroduction.getAbsolutePath())) {
+      //noinspection AssignmentToStaticFieldFromInstanceMethod
+      StudyInitialConfigurator.UPDATE_PROJECT = false;
+      updateCourse();
+    }
     ApplicationManager.getApplication().invokeLater(new DumbAwareRunnable() {
       @Override
       public void run() {
@@ -152,8 +163,9 @@ public class StudyTaskManager implements ProjectComponent, PersistentStateCompon
               }
               catch (Exception e) {
                 final ToolWindow toolWindow = toolWindowManager.getToolWindow(toolWindowId);
-                if (toolWindow == null)
+                if (toolWindow == null) {
                   toolWindowManager.registerToolWindow(toolWindowId, true, ToolWindowAnchor.RIGHT, myProject, true);
+                }
               }
 
               final ToolWindow studyToolWindow = toolWindowManager.getToolWindow(toolWindowId);
@@ -232,6 +244,40 @@ public class StudyTaskManager implements ProjectComponent, PersistentStateCompon
     });
   }
 
+  private void updateCourse() {
+    final File userCourseDir = new File(PathManager.getConfigPath(), StudyNames.COURSES);
+    final File courseDir = new File(userCourseDir, StudyNames.INTRODUCTION_COURSE);
+    final File[] files = courseDir.listFiles();
+    if (files == null) return;
+    for (File lesson : files) {
+      if (lesson.getName().startsWith(StudyNames.LESSON)) {
+        final File[] tasks = lesson.listFiles();
+        if (tasks == null) continue;
+        for (File task : tasks) {
+          final File taskDescr = new File(task, StudyNames.TASK_HTML);
+          final File taskTests = new File(task, StudyNames.TASK_TESTS);
+          copyFile(lesson, task, taskDescr, StudyNames.TASK_HTML);
+          copyFile(lesson, task, taskTests, StudyNames.TASK_TESTS);
+        }
+      }
+    }
+
+    final Notification notification =
+      new Notification("Update.course", "Course update", "Current course is synchronized", NotificationType.INFORMATION);
+    notification.notify(myProject);
+  }
+
+  private void copyFile(@NotNull final File lesson, @NotNull final File task, @NotNull final File taskDescr,
+                        @NotNull final String fileName) {
+    if (taskDescr.exists()) {
+      try {
+        FileUtil.copy(taskDescr, new File(new File(new File(myProject.getBasePath(), lesson.getName()), task.getName()), fileName));
+      }
+      catch (IOException e) {
+        LOG.warn("Failed to copy " + lesson.getName() + " " + task.getName());
+      }
+    }
+  }
 
   private static void addShortcut(@NotNull final String shortcutString, @NotNull final String actionIdString, boolean isAdditional) {
     Keymap keymap = KeymapManager.getInstance().getActiveKeymap();