forked from magento/magento2-phpstorm-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModuleScanner.java
202 lines (177 loc) · 6.4 KB
/
ModuleScanner.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
package com.magento.idea.magento2uct.execution.scanner;
import com.intellij.json.psi.JsonFile;
import com.intellij.json.psi.JsonObject;
import com.intellij.json.psi.JsonProperty;
import com.intellij.json.psi.JsonValue;
import com.intellij.openapi.util.Pair;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiFile;
import com.intellij.psi.xml.XmlFile;
import com.intellij.psi.xml.XmlTag;
import com.magento.idea.magento2plugin.magento.files.ComposerJson;
import com.magento.idea.magento2plugin.magento.files.ModuleXml;
import com.magento.idea.magento2plugin.magento.packages.Package;
import com.magento.idea.magento2uct.execution.scanner.data.ComponentData;
import com.magento.idea.magento2uct.execution.scanner.filter.ModuleScannerFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import org.jetbrains.annotations.NotNull;
public final class ModuleScanner implements Iterable<ComponentData> {
private static final String FRAMEWORK_LIBRARY_NAME = "magento/framework";
private final PsiDirectory rootDirectory;
private final List<ComponentData> componentDataList;
private final List<ModuleScannerFilter> filters;
/**
* Magento 2 module components scanner constructor.
*
* @param rootDirectory PsiDirectory
* @param filters ModuleScannerFilter[]
*/
public ModuleScanner(
final @NotNull PsiDirectory rootDirectory,
final @NotNull ModuleScannerFilter... filters
) {
this.rootDirectory = rootDirectory;
this.filters = Arrays.asList(filters);
componentDataList = new ArrayList<>();
}
@Override
public @NotNull Iterator<ComponentData> iterator() {
return run().iterator();
}
/**
* Get found modules qty.
*
* @return int
*/
public int getModuleCount() {
return componentDataList.size();
}
/**
* Run scanner.
*
* @return List[ComponentData]
*/
private List<ComponentData> run() {
componentDataList.clear();
findModuleComponent(rootDirectory);
return componentDataList;
}
/**
* Look up magento 2 module components.
*
* @param directory PsiDirectory
*/
@SuppressWarnings({
"PMD.NPathComplexity",
"PMD.CyclomaticComplexity",
"PMD.CognitiveComplexity",
"PMD.AvoidDeeplyNestedIfStmts"
})
private void findModuleComponent(final @NotNull PsiDirectory directory) {
String name = null;
String composerBasedName = null;
String composerType;
String type = "magento2-module";
for (final PsiDirectory subDirectory : directory.getSubdirectories()) {
if (Package.moduleBaseAreaDir.equals(subDirectory.getName())) {
for (final PsiFile file : subDirectory.getFiles()) {
if (file instanceof XmlFile && ModuleXml.FILE_NAME.equals(file.getName())) {
final XmlTag rootTag = ((XmlFile) file).getRootTag();
if (rootTag != null && rootTag.getSubTags().length > 0) {
final XmlTag moduleTag = rootTag.getSubTags()[0];
name = moduleTag.getAttributeValue("name");
}
break;
}
}
break;
}
}
for (final PsiFile file : directory.getFiles()) {
if (file instanceof JsonFile && ComposerJson.FILE_NAME.equals(file.getName())) {
final Pair<String, String> meta = scanModuleComposerMeta((JsonFile) file);
composerBasedName = meta.getFirst();
composerType = meta.getSecond();
if (composerBasedName == null || composerType == null) {
return;
}
if (name == null && FRAMEWORK_LIBRARY_NAME.equals(composerBasedName)) {
name = meta.getFirst();
type = composerType;
}
if (!type.equals(composerType) && !"project".equals(composerType)) {
return;
}
break;
}
}
if (name != null) {
final ComponentData component = new ComponentData(
name,
composerBasedName,
type,
directory
);
boolean isExcluded = false;
for (final ModuleScannerFilter filter : filters) {
if (filter.isExcluded(component)) {
isExcluded = true;
break;
}
}
if (!isExcluded) {
componentDataList.add(component);
}
return;
}
for (final PsiDirectory subDirectory : directory.getSubdirectories()) {
findModuleComponent(subDirectory);
}
}
/**
* Scan module metadata from the composer.json file.
*
* @param composerFile JsonFile
*
* @return Pair[String, String]
*/
@SuppressWarnings({
"PMD.NPathComplexity",
"PMD.CyclomaticComplexity",
"PMD.CognitiveComplexity"
})
private Pair<String, String> scanModuleComposerMeta(final JsonFile composerFile) {
final JsonValue topNode = composerFile.getTopLevelValue();
String name = null;
String type = null;
if (topNode instanceof JsonObject) {
for (final JsonProperty property : ((JsonObject) topNode).getPropertyList()) {
switch (property.getName()) {
case "name":
if (property.getValue() != null) {
name = property.getValue().getText().replace("\"", "");
}
break;
case "type":
if (property.getValue() != null) {
type = property.getValue().getText().replace("\"", "");
}
break;
default:
break;
}
if (name != null && type != null) {
break;
}
}
}
return new Pair<>(name, type);
}
}