Skip to content

Commit 93a14f1

Browse files
committed
Copy file to correct location in selected theme
1 parent 6e26d15 commit 93a14f1

File tree

2 files changed

+87
-5
lines changed

2 files changed

+87
-5
lines changed

src/com/magento/idea/magento2plugin/actions/generation/dialog/OverrideInThemeDialog.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
package com.magento.idea.magento2plugin.actions.generation.dialog;
66

77
import com.intellij.openapi.project.Project;
8+
import com.intellij.openapi.ui.popup.JBPopupFactory;
89
import com.intellij.psi.PsiFile;
9-
import com.jetbrains.php.lang.psi.elements.Method;
10-
import com.jetbrains.php.lang.psi.elements.PhpClass;
1110
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.OverrideInThemeDialogValidator;
1211
import com.magento.idea.magento2plugin.actions.generation.generator.OverrideInThemeGenerator;
1312
import com.magento.idea.magento2plugin.indexes.ModuleIndex;
@@ -68,6 +67,7 @@ public void actionPerformed(ActionEvent e) {
6867

6968
private void onOK() {
7069
if (!validator.validate(project)) {
70+
JBPopupFactory.getInstance().createMessage("Invalid theme selection.").showCenteredInCurrentWindow(project);
7171
return;
7272
}
7373

src/com/magento/idea/magento2plugin/actions/generation/generator/OverrideInThemeGenerator.java

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
package com.magento.idea.magento2plugin.actions.generation.generator;
22

33
import com.intellij.openapi.project.Project;
4+
import com.intellij.openapi.ui.popup.JBPopupFactory;
45
import com.intellij.psi.PsiDirectory;
56
import com.intellij.psi.PsiFile;
67
import com.magento.idea.magento2plugin.indexes.ModuleIndex;
8+
import com.magento.idea.magento2plugin.util.RegExUtil;
9+
import com.magento.idea.magento2plugin.util.magento.GetComponentNameByDirectory;
10+
11+
import java.util.ArrayList;
12+
import java.util.Collections;
13+
import java.util.List;
14+
import java.util.regex.Matcher;
15+
import java.util.regex.Pattern;
716

817
public class OverrideInThemeGenerator {
918

@@ -25,12 +34,85 @@ public OverrideInThemeGenerator(Project project) {
2534
public void execute(PsiFile baseFile, String themeName) {
2635
ModuleIndex moduleIndex = ModuleIndex.getInstance(project);
2736
PsiDirectory directory = moduleIndex.getModuleDirectoryByModuleName(themeName);
37+
GetComponentNameByDirectory getComponentNameByDirectory = GetComponentNameByDirectory.getInstance(project);
38+
String componentName = getComponentNameByDirectory.execute(baseFile.getContainingDirectory());
39+
40+
List<String> pathComponents;
41+
if (isModule(componentName)) {
42+
pathComponents = getModulePathComponents(baseFile, componentName);
43+
} else if (isTheme(componentName)) {
44+
pathComponents = getThemePathComponents(baseFile);
45+
} else {
46+
return;
47+
}
48+
49+
directory = getTargetDirectory(directory, pathComponents);
50+
51+
if (directory.findFile(baseFile.getName()) != null) {
52+
JBPopupFactory.getInstance()
53+
.createMessage("Override already exists in selected theme.")
54+
.showCenteredInCurrentWindow(project);
55+
directory.findFile(baseFile.getName()).navigate(true);
56+
return;
57+
}
2858

29-
//TODO: Copy file to correct path when source is module
30-
//TODO: Copy file to correct path when source is theme
31-
//TODO: Throw error if theme file already exists
3259
directory.copyFileFrom(baseFile.getName(), baseFile);
3360
PsiFile newFile = directory.findFile(baseFile.getName());
3461
newFile.navigate(true);
3562
}
63+
64+
private boolean isModule(String componentName) {
65+
Pattern modulePattern = Pattern.compile(RegExUtil.Magento.MODULE_NAME);
66+
Matcher moduleMatcher = modulePattern.matcher(componentName);
67+
68+
return moduleMatcher.find();
69+
}
70+
71+
private boolean isTheme(String componentName) {
72+
Pattern themePattern = Pattern.compile(RegExUtil.Magento.THEME_NAME);
73+
Matcher themeMatcher = themePattern.matcher(componentName);
74+
75+
return themeMatcher.find();
76+
}
77+
78+
private List<String> getModulePathComponents(PsiFile file, String componentName) {
79+
List<String> pathComponents = new ArrayList<>();
80+
PsiDirectory parent = file.getParent();
81+
while (!parent.getName().equals("frontend") && !parent.getName().equals("adminhtml")) {
82+
pathComponents.add(parent.getName());
83+
parent = parent.getParent();
84+
}
85+
pathComponents.add(componentName);
86+
Collections.reverse(pathComponents);
87+
88+
return pathComponents;
89+
}
90+
91+
private List<String> getThemePathComponents(PsiFile file) {
92+
List<String> pathComponents = new ArrayList<>();
93+
Pattern pattern = Pattern.compile(RegExUtil.Magento.MODULE_NAME);
94+
95+
PsiDirectory parent = file.getParent();
96+
do {
97+
pathComponents.add(parent.getName());
98+
parent = parent.getParent();
99+
} while (!pattern.matcher(parent.getName()).find());
100+
pathComponents.add(parent.getName());
101+
Collections.reverse(pathComponents);
102+
103+
return pathComponents;
104+
}
105+
106+
private PsiDirectory getTargetDirectory(PsiDirectory directory, List<String> pathComponents) {
107+
for (int i = 0; i < pathComponents.size(); i++) {
108+
String directoryName = pathComponents.get(i);
109+
if (directory.findSubdirectory(directoryName) != null) {
110+
directory = directory.findSubdirectory(directoryName);
111+
} else {
112+
directory = directory.createSubdirectory(directoryName);
113+
}
114+
}
115+
116+
return directory;
117+
}
36118
}

0 commit comments

Comments
 (0)