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.VirtualFile;
11 import com.intellij.platform.templates.github.GeneratorException;
12 import com.intellij.platform.templates.github.GithubTagInfo;
13 import com.intellij.platform.templates.github.ZipUtil;
14 import com.intellij.util.PlatformUtils;
15 import org.jetbrains.annotations.Nls;
16 import org.jetbrains.annotations.NotNull;
17 import org.jetbrains.annotations.Nullable;
20 import java.io.UnsupportedEncodingException;
21 import java.net.URLEncoder;
24 * @author Sergey Simonchik
26 public abstract class AbstractGithubTagDownloadedProjectGenerator extends WebProjectTemplate<GithubTagInfo> {
28 private static final Logger LOG = Logger.getInstance(AbstractGithubTagDownloadedProjectGenerator.class);
33 public final String getName() {
34 return getDisplayName();
38 protected abstract String getDisplayName();
41 protected abstract String getGithubUserName();
44 protected abstract String getGithubRepositoryName();
48 public abstract String getDescription();
50 private String getTitle() {
51 return getDisplayName();
56 public String getHelpId() {
57 return "create.from.template." + getGithubUserName() + "." + getGithubRepositoryName();
61 public void generateProject(@NotNull final Project project, @NotNull final VirtualFile baseDir,
62 @NotNull GithubTagInfo tag, @NotNull Module module) {
64 unpackToDir(project, new File(baseDir.getPath()), tag);
66 catch (GeneratorException e) {
67 showErrorMessage(project, e.getMessage());
69 ApplicationManager.getApplication().runWriteAction(new Runnable() {
72 baseDir.refresh(true, true);
79 public GithubProjectGeneratorPeer createPeer() {
80 return new GithubProjectGeneratorPeer(this);
84 public boolean isPrimaryGenerator() {
85 return PlatformUtils.isWebStorm();
88 private void unpackToDir(@Nullable Project project,
89 @NotNull File extractToDir,
90 @NotNull GithubTagInfo tag) throws GeneratorException {
91 File zipArchiveFile = getCacheFile(tag);
92 String primaryUrl = getPrimaryZipArchiveUrlForDownload(tag);
93 boolean downloaded = false;
94 if (primaryUrl != null) {
96 downloadAndUnzip(project, primaryUrl, zipArchiveFile, extractToDir, false);
98 } catch (GeneratorException e) {
99 LOG.info("Can't download " + primaryUrl, e);
100 FileUtil.delete(zipArchiveFile);
104 if (ApplicationManager.getApplication().isUnitTestMode()) {
105 throw new GeneratorException("Download " + tag.getZipballUrl() + " is skipped in unit test mode");
107 downloadAndUnzip(project, tag.getZipballUrl(), zipArchiveFile, extractToDir, true);
111 private void downloadAndUnzip(@Nullable Project project,
113 @NotNull File zipArchiveFile,
114 @NotNull File extractToDir,
115 boolean retryOnError) throws GeneratorException {
116 GithubDownloadUtil.downloadContentToFileWithProgressSynchronously(
122 getGithubRepositoryName(),
125 LOG.info("Content of " + url + " has been successfully downloaded to " + zipArchiveFile.getAbsolutePath()
126 + ", size " + zipArchiveFile.length() + " bytes");
127 ZipUtil.unzipWithProgressSynchronously(project, getTitle(), zipArchiveFile, extractToDir, true);
131 public abstract String getPrimaryZipArchiveUrlForDownload(@NotNull GithubTagInfo tag);
134 private File getCacheFile(@NotNull GithubTagInfo tag) {
135 String fileName = tag.getName() + ".zip";
137 fileName = URLEncoder.encode(fileName, "UTF-8");
138 } catch (UnsupportedEncodingException e) {
139 LOG.warn("Can't urlEncode", e);
141 return GithubDownloadUtil.findCacheFile(getGithubUserName(), getGithubRepositoryName(), fileName);
144 private void showErrorMessage(@NotNull Project project, @NotNull String message) {
145 String fullMessage = "Error creating " + getDisplayName() + " project. " + message;
146 String title = "Create " + getDisplayName() + " Project";
147 Messages.showErrorDialog(project, fullMessage, title);