1
1
package com .magento .idea .magento2plugin .actions .generation .generator ;
2
2
3
3
import com .intellij .openapi .project .Project ;
4
+ import com .intellij .openapi .ui .popup .JBPopupFactory ;
4
5
import com .intellij .psi .PsiDirectory ;
5
6
import com .intellij .psi .PsiFile ;
6
7
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 ;
7
16
8
17
public class OverrideInThemeGenerator {
9
18
@@ -25,12 +34,85 @@ public OverrideInThemeGenerator(Project project) {
25
34
public void execute (PsiFile baseFile , String themeName ) {
26
35
ModuleIndex moduleIndex = ModuleIndex .getInstance (project );
27
36
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
+ }
28
58
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
32
59
directory .copyFileFrom (baseFile .getName (), baseFile );
33
60
PsiFile newFile = directory .findFile (baseFile .getName ());
34
61
newFile .navigate (true );
35
62
}
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
+ }
36
118
}
0 commit comments