1 package com.intellij.lang.javascript.boilerplate;
3 import com.intellij.ide.util.projectWizard.WebProjectTemplate;
4 import com.intellij.openapi.application.ApplicationManager;
5 import com.intellij.openapi.diagnostic.Logger;
6 import com.intellij.openapi.module.Module;
7 import com.intellij.openapi.project.Project;
8 import com.intellij.openapi.ui.Messages;
9 import com.intellij.openapi.util.io.FileUtil;
10 import com.intellij.openapi.vfs.VfsUtilCore;
11 import com.intellij.openapi.vfs.VirtualFile;
12 import com.intellij.platform.templates.github.GeneratorException;
13 import com.intellij.platform.templates.github.GithubTagInfo;
14 import com.intellij.platform.templates.github.ZipUtil;
15 import com.intellij.util.PlatformUtils;
16 import org.jetbrains.annotations.Nls;
17 import org.jetbrains.annotations.NotNull;
18 import org.jetbrains.annotations.Nullable;
21 import java.io.UnsupportedEncodingException;
22 import java.net.URLEncoder;
25 * @author Sergey Simonchik
27 public abstract class AbstractGithubTagDownloadedProjectGenerator extends WebProjectTemplate<GithubTagInfo> {
29 private static final Logger LOG = Logger.getInstance(AbstractGithubTagDownloadedProjectGenerator.class);
34 public final String getName() {
35 return getDisplayName();
39 protected abstract String getDisplayName();
42 protected abstract String getGithubUserName();
45 protected abstract String getGithubRepositoryName();
49 public abstract String getDescription();
51 private String getTitle() {
52 return getDisplayName();
57 public String getHelpId() {
58 return "create.from.template." + getGithubUserName() + "." + getGithubRepositoryName();
62 public void generateProject(@NotNull final Project project, @NotNull final VirtualFile baseDir,
63 @NotNull GithubTagInfo tag, @NotNull Module module) {
65 unpackToDir(project, VfsUtilCore.virtualToIoFile(baseDir), tag);
67 catch (GeneratorException e) {
68 showErrorMessage(project, e.getMessage());
70 ApplicationManager.getApplication().runWriteAction(new Runnable() {
73 baseDir.refresh(true, true);
80 public GithubProjectGeneratorPeer createPeer() {
81 return new GithubProjectGeneratorPeer(this);
85 public boolean isPrimaryGenerator() {
86 return PlatformUtils.isWebStorm();
89 private void unpackToDir(@Nullable Project project,
90 @NotNull File extractToDir,
91 @NotNull GithubTagInfo tag) throws GeneratorException {
92 File zipArchiveFile = getCacheFile(tag);
93 String primaryUrl = getPrimaryZipArchiveUrlForDownload(tag);
94 boolean downloaded = false;
95 if (primaryUrl != null) {
97 downloadAndUnzip(project, primaryUrl, zipArchiveFile, extractToDir, false);
99 } catch (GeneratorException e) {
100 LOG.info("Can't download " + primaryUrl, e);
101 FileUtil.delete(zipArchiveFile);
105 if (ApplicationManager.getApplication().isUnitTestMode()) {
106 throw new GeneratorException("Download " + tag.getZipballUrl() + " is skipped in unit test mode");
108 downloadAndUnzip(project, tag.getZipballUrl(), zipArchiveFile, extractToDir, true);
112 private void downloadAndUnzip(@Nullable Project project,
114 @NotNull File zipArchiveFile,
115 @NotNull File extractToDir,
116 boolean retryOnError) throws GeneratorException {
117 GithubDownloadUtil.downloadContentToFileWithProgressSynchronously(
123 getGithubRepositoryName(),
126 LOG.info("Content of " + url + " has been successfully downloaded to " + zipArchiveFile.getAbsolutePath()
127 + ", size " + zipArchiveFile.length() + " bytes");
128 ZipUtil.unzipWithProgressSynchronously(project, getTitle(), zipArchiveFile, extractToDir, true);
132 public abstract String getPrimaryZipArchiveUrlForDownload(@NotNull GithubTagInfo tag);
135 private File getCacheFile(@NotNull GithubTagInfo tag) {
136 String fileName = tag.getName() + ".zip";
138 fileName = URLEncoder.encode(fileName, "UTF-8");
139 } catch (UnsupportedEncodingException e) {
140 LOG.warn("Can't urlEncode", e);
142 return GithubDownloadUtil.findCacheFile(getGithubUserName(), getGithubRepositoryName(), fileName);
145 private void showErrorMessage(@NotNull Project project, @NotNull String message) {
146 String fullMessage = "Error creating " + getDisplayName() + " project. " + message;
147 String title = "Create " + getDisplayName() + " Project";
148 Messages.showErrorDialog(project, fullMessage, title);