Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

760: fixed throwable when io exception is thrown during plugin generation #860

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions resources/magento2/validation.properties
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ validator.command.isNotValid=The {0} field does not contain a valid Magento 2 CL
validator.module.noSuchModule=No such module {0}
validator.file.alreadyExists={0} already exists
validator.file.cantBeCreated={0} can't be created
validator.file.cantBeCreatedWithException=The ''{0}'' cannot be created. Original message was: ''{1}''
validator.file.isNotWritable=%s file is binary or has no document associations
validator.file.noDocumentAssociations={0} file is binary or has no document associations
validator.class.alreadyDeclared={0} already declared in the target module
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package com.magento.idea.magento2plugin.actions.generation.generator;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.project.Project;
Expand Down Expand Up @@ -88,7 +89,7 @@ public PluginClassGenerator(
*
* @return PsiFile
*/
@SuppressWarnings("PMD.CognitiveComplexity")
@SuppressWarnings({"PMD.CognitiveComplexity", "PMD.ExcessiveMethodLength"})
@Override
public PsiFile generate(final String actionName) {
final PsiFile[] pluginFile = {null};
Expand All @@ -100,15 +101,27 @@ public PsiFile generate(final String actionName) {
pluginClass = createPluginClass(actionName);
}
if (pluginClass == null) {
final String errorMessage = validatorBundle.message(
"validator.file.cantBeCreated",
"Plugin Class"
);
JOptionPane.showMessageDialog(
null,
errorMessage,
errorTitle,
JOptionPane.ERROR_MESSAGE
String errorMessage;

if (fileFromTemplateGenerator.getLastExceptionMessage() == null) {
errorMessage = validatorBundle.message(
"validator.file.cantBeCreated",
"Plugin Class"
);
} else {
errorMessage = validatorBundle.message(
"validator.file.cantBeCreatedWithException",
"Plugin Class",
fileFromTemplateGenerator.getLastExceptionMessage()
);
}
ApplicationManager.getApplication().invokeLater(
() -> JOptionPane.showMessageDialog(
null,
errorMessage,
errorTitle,
JOptionPane.ERROR_MESSAGE
)
);

return;
Expand All @@ -132,11 +145,13 @@ public PsiFile generate(final String actionName) {
"validator.file.alreadyExists",
"Plugin Class"
);
JOptionPane.showMessageDialog(
null,
errorMessage,
errorTitle,
JOptionPane.ERROR_MESSAGE
ApplicationManager.getApplication().invokeLater(
() -> JOptionPane.showMessageDialog(
null,
errorMessage,
errorTitle,
JOptionPane.ERROR_MESSAGE
)
);

return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiDirectory;
Expand All @@ -31,9 +30,11 @@
import org.jetbrains.annotations.Nullable;

public class FileFromTemplateGenerator {
private final Project project;

public FileFromTemplateGenerator(final Project project) {
private final @NotNull Project project;
private @Nullable String exceptionMessage;

public FileFromTemplateGenerator(final @NotNull Project project) {
this.project = project;
}

Expand All @@ -44,27 +45,30 @@ public FileFromTemplateGenerator(final Project project) {
* @param attributes Properties
* @param baseDir PsiDirectory
* @param actionName String
*
* @return PsiFile
*/
@Nullable
public PsiFile generate(
public @Nullable PsiFile generate(
final @NotNull ModuleFileInterface moduleFile,
final @NotNull Properties attributes,
final @NotNull PsiDirectory baseDir,
final @NotNull String actionName
) {
final Ref<PsiFile> fileRef = new Ref<>(null);
final Ref<String> exceptionRef = new Ref<>(null);
exceptionMessage = null;//NOPMD
final String filePath = baseDir.getText().concat("/").concat(moduleFile.getFileName());

CommandProcessor.getInstance().executeCommand(project, () -> {
final Runnable run = () -> {
try {
PsiFile file = createFile(moduleFile, filePath, baseDir, attributes);
final PsiFile file = createFile(moduleFile, filePath, baseDir, attributes);

if (file != null) {
fileRef.set(file);
}
} catch (IncorrectOperationException | IOException var9) {
exceptionRef.set(var9.getMessage());
} catch (IncorrectOperationException | IOException exception) {
exceptionRef.set(exception.getMessage());
}
};
ApplicationManager.getApplication().runWriteAction(run);
Expand All @@ -73,11 +77,20 @@ public PsiFile generate(
if (exceptionRef.isNull()) {
return fileRef.get();
}
exceptionMessage = exceptionRef.get();

Messages.showErrorDialog(exceptionRef.get(), actionName);
return null;
}

/**
* Get last thrown exception message if exists.
*
* @return String
*/
public @Nullable String getLastExceptionMessage() {
return exceptionMessage;
}

@Nullable
private PsiFile createFile(
final @NotNull ModuleFileInterface moduleFile,
Expand All @@ -89,13 +102,19 @@ private PsiFile createFile(
final String fileName = path.get(path.size() - 1);
final PsiFile fileTemplate = createFileFromTemplate(
getTemplateManager(),
baseDir, moduleFile.getTemplate(), attributes, fileName, moduleFile.getLanguage());
baseDir,
moduleFile.getTemplate(),
attributes,
fileName,
moduleFile.getLanguage()
);

if (fileTemplate == null) {
throw new IncorrectOperationException("Template not found!");
} else {
PsiElement file;

file = baseDir.add(fileTemplate);

if (file instanceof PsiFile) {
return (PsiFile)file;
} else {
Expand Down Expand Up @@ -125,6 +144,7 @@ public PsiFile createFileFromTemplate(
final @NotNull Language language
) throws IOException {
FileTemplate fileTemplate;

try {
fileTemplate = templateManager.getInternalTemplate(templateName);
} catch (IllegalStateException e) {
Expand All @@ -140,6 +160,7 @@ public PsiFile createFileFromTemplate(
true,
false
);

if (fileTemplate.isReformatCode()) {
CodeStyleManager.getInstance(project).reformat(file);
}
Expand Down