Create course from course creator plugin (EDU-349)
authorLiana Bakradze <liana.bakradze@jetbrains.com>
Tue, 14 Jun 2016 15:28:03 +0000 (18:28 +0300)
committerLiana Bakradze <liana.bakradze@jetbrains.com>
Tue, 14 Jun 2016 15:28:03 +0000 (18:28 +0300)
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCCreateCourseArchive.java
python/educational-python/course-creator-python/course-creator-python.iml
python/educational-python/course-creator-python/resources/META-INF/plugin.xml
python/educational-python/course-creator-python/src/com/jetbrains/edu/coursecreator/CreateFromArchiveProjectStep.java [new file with mode: 0644]
python/educational-python/course-creator-python/src/com/jetbrains/edu/coursecreator/PyCCCreateProjectFromArchive.java [new file with mode: 0644]
python/ide/src/com/jetbrains/python/newProject/actions/PyCharmNewProjectStep.java

index cbf71f341b249bf72ebf4ec638392298b4ec07af..05e9e87c60f8961c9f50fa74ea69386aa5a1dcae 100644 (file)
@@ -68,20 +68,19 @@ public class CCCreateCourseArchive extends DumbAwareAction {
     if (project == null || module == null) {
       return;
     }
-    createCourseArchive(project, module);
-  }
-
-  private void createCourseArchive(final Project project, Module module) {
-    final Course course = StudyTaskManager.getInstance(project).getCourse();
-    if (course == null) return;
     CreateCourseArchiveDialog dlg = new CreateCourseArchiveDialog(project, this);
     dlg.show();
     if (dlg.getExitCode() != DialogWrapper.OK_EXIT_CODE) {
       return;
     }
-    final VirtualFile baseDir = project.getBaseDir();
+    createCourseArchive(project, module, myZipName, myLocationDir, true);
+  }
 
-    VirtualFile archiveFolder = CCUtils.generateFolder(project, module, this, myZipName);
+  public static void createCourseArchive(final Project project, Module module, String zipName, String locationDir, boolean showMessage) {
+    final Course course = StudyTaskManager.getInstance(project).getCourse();
+    if (course == null) return;
+    final VirtualFile baseDir = project.getBaseDir();
+    VirtualFile archiveFolder = CCUtils.generateFolder(project, module, null, zipName);
     if (archiveFolder == null) {
       return;
     }
@@ -112,7 +111,7 @@ public class CCCreateCourseArchive extends DumbAwareAction {
         generateJson(project, archiveFolder);
         resetTaskFiles(savedTaskFiles);
         VirtualFileManager.getInstance().refreshWithoutFileWatcher(false);
-        packCourse(archiveFolder);
+        packCourse(archiveFolder, locationDir, zipName, showMessage);
         synchronize(project);
       }
 
@@ -173,16 +172,18 @@ public class CCCreateCourseArchive extends DumbAwareAction {
     ProjectView.getInstance(project).refresh();
   }
 
-  private void packCourse(@NotNull final VirtualFile baseDir) {
+  private static void packCourse(@NotNull final VirtualFile baseDir, String locationDir, String zipName, boolean showMessage) {
     try {
-      final File zipFile = new File(myLocationDir, myZipName + ".zip");
+      final File zipFile = new File(locationDir, zipName + ".zip");
       ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));
       VirtualFile[] courseFiles = baseDir.getChildren();
       for (VirtualFile file : courseFiles) {
         ZipUtil.addFileOrDirRecursively(zos, null, new File(file.getPath()), file.getName(), null, null);
       }
       zos.close();
-      Messages.showInfoMessage("Course archive was saved to " + zipFile.getPath(), "Course Archive Was Created Successfully");
+      if (showMessage) {
+        Messages.showInfoMessage("Course archive was saved to " + zipFile.getPath(), "Course Archive Was Created Successfully");
+      }
     }
     catch (IOException e1) {
       LOG.error(e1);
index cf8d512811173bde0b0dc78428441411f4acc3a4..52b4e677d8d37f1b84bafa34399c62460553571a 100644 (file)
@@ -14,5 +14,7 @@
     <orderEntry type="module" module-name="educational-python" />
     <orderEntry type="module" module-name="python-community" />
     <orderEntry type="module" module-name="student" />
+    <orderEntry type="module" module-name="python-ide-community" />
+    <orderEntry type="module" module-name="student-python" />
   </component>
 </module>
\ No newline at end of file
index db71e42c97a73e9b7a610bd4955c7a1ef4de4faa..e8425b2205ff696ae1b3069f895c485967131102 100644 (file)
     <pyReferenceResolveProvider implementation="com.jetbrains.edu.coursecreator.PyCCReferenceResolveProvider"/>
   </extensions>
 
+  <actions>
+    <action class="com.jetbrains.edu.coursecreator.PyCCCreateProjectFromArchive" id="PyCC.NewEdu" text="Create New Educational Project">
+      <add-to-group group-id="CCFileGroup" anchor="last"/>
+      <add-to-group group-id="CCProjectViewGroup" anchor="last"/>
+    </action>
+  </actions>
+
 </idea-plugin>
\ No newline at end of file
diff --git a/python/educational-python/course-creator-python/src/com/jetbrains/edu/coursecreator/CreateFromArchiveProjectStep.java b/python/educational-python/course-creator-python/src/com/jetbrains/edu/coursecreator/CreateFromArchiveProjectStep.java
new file mode 100644 (file)
index 0000000..033b83e
--- /dev/null
@@ -0,0 +1,61 @@
+package com.jetbrains.edu.coursecreator;
+
+import com.intellij.openapi.module.Module;
+import com.intellij.openapi.project.Project;
+import com.intellij.openapi.util.io.FileUtil;
+import com.intellij.openapi.vfs.VirtualFile;
+import com.intellij.platform.DirectoryProjectGenerator;
+import com.jetbrains.edu.coursecreator.actions.CCCreateCourseArchive;
+import com.jetbrains.edu.learning.PyStudyDirectoryProjectGenerator;
+import com.jetbrains.edu.learning.StudyTaskManager;
+import com.jetbrains.edu.learning.courseFormat.Course;
+import com.jetbrains.edu.learning.courseGeneration.StudyProjectGenerator;
+import com.jetbrains.edu.learning.stepic.CourseInfo;
+import com.jetbrains.python.newProject.actions.PyCharmNewProjectStep;
+import org.jetbrains.annotations.NotNull;
+
+class CreateFromArchiveProjectStep extends PyCharmNewProjectStep {
+
+  public CreateFromArchiveProjectStep(Project project, Module module) {
+    super(new MyCustomization(project, module));
+  }
+
+  protected static class MyCustomization extends PyCharmNewProjectStep.Customization {
+
+    private final Project myProject;
+    private final Module myModule;
+    private PyStudyDirectoryProjectGenerator myGenerator = new PyStudyDirectoryProjectGenerator();
+
+    public MyCustomization(Project project,
+                           Module module) {
+
+      myProject = project;
+      myModule = module;
+    }
+
+    @NotNull
+    @Override
+    protected DirectoryProjectGenerator[] getProjectGenerators() {
+      return new DirectoryProjectGenerator[] {};
+    }
+
+
+    @NotNull
+    @Override
+    protected DirectoryProjectGenerator createEmptyProjectGenerator() {
+      Course course = StudyTaskManager.getInstance(myProject).getCourse();
+      if (course != null) {
+        VirtualFile folder = CCUtils.getGeneratedFilesFolder(myProject, myModule);
+        String zipName = FileUtil.sanitizeFileName(course.getName());
+        String locationDir = folder.getPath();
+        CCCreateCourseArchive.createCourseArchive(myProject, myModule, zipName, locationDir, false);
+        String path = FileUtil.join(folder.getPath(), zipName + ".zip");
+        StudyProjectGenerator generator = myGenerator.getGenerator();
+        CourseInfo info = generator.addLocalCourse(path);
+        assert info != null;
+        generator.setSelectedCourse(info);
+      }
+      return myGenerator;
+    }
+  }
+}
diff --git a/python/educational-python/course-creator-python/src/com/jetbrains/edu/coursecreator/PyCCCreateProjectFromArchive.java b/python/educational-python/course-creator-python/src/com/jetbrains/edu/coursecreator/PyCCCreateProjectFromArchive.java
new file mode 100644 (file)
index 0000000..752f87a
--- /dev/null
@@ -0,0 +1,33 @@
+package com.jetbrains.edu.coursecreator;
+
+import com.intellij.ide.util.projectWizard.AbstractNewProjectDialog;
+import com.intellij.openapi.actionSystem.*;
+import com.intellij.openapi.module.Module;
+import com.intellij.openapi.project.DumbAwareAction;
+import com.intellij.openapi.project.Project;
+
+public class PyCCCreateProjectFromArchive extends DumbAwareAction {
+  @Override
+  public void actionPerformed(AnActionEvent e) {
+    final Project project = e.getData(CommonDataKeys.PROJECT);
+    final Module module = e.getData(LangDataKeys.MODULE);
+    if (project == null || module == null) {
+      return;
+    }
+
+    AbstractNewProjectDialog dialog = new AbstractNewProjectDialog() {
+      @Override
+      protected DefaultActionGroup createRootStep() {
+        return new CreateFromArchiveProjectStep(project, module);
+      }
+    };
+    dialog.show();
+  }
+
+  @Override
+  public void update(AnActionEvent e) {
+    Presentation presentation = e.getPresentation();
+    Project project = e.getProject();
+    presentation.setEnabledAndVisible(project != null && CCUtils.isCourseCreator(project));
+  }
+}
index a248fb2eefee31bc1d12ef187689217226014ce8..cebf8c054ebedb559f6f2756a1b433ed71da0a33 100644 (file)
@@ -31,7 +31,6 @@ import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 
 import java.util.Arrays;
-import java.util.Comparator;
 import java.util.List;
 
 public class PyCharmNewProjectStep extends AbstractNewProjectStep {
@@ -40,7 +39,11 @@ public class PyCharmNewProjectStep extends AbstractNewProjectStep {
     super(new Customization());
   }
 
-  private static class Customization extends AbstractNewProjectStep.Customization {
+  public PyCharmNewProjectStep(@NotNull AbstractNewProjectStep.Customization customization) {
+    super(customization);
+  }
+
+  protected static class Customization extends AbstractNewProjectStep.Customization {
     private final List<DirectoryProjectGenerator> pluginSpecificGenerators = Lists.newArrayList();
 
     @NotNull