Skip to content

Commit 0c3ff4f

Browse files
author
Vitaliy
authored
Merge pull request #81 from vasilii-b/magento-version-util
Show the current project's Magento version in settings window
2 parents 2ec77d5 + a2f41f7 commit 0c3ff4f

File tree

7 files changed

+104
-18
lines changed

7 files changed

+104
-18
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.actions.generation.util;
7+
8+
import com.intellij.ide.DataManager;
9+
import com.intellij.json.psi.JsonFile;
10+
import com.intellij.json.psi.JsonObject;
11+
import com.intellij.openapi.actionSystem.DataConstants;
12+
import com.intellij.openapi.actionSystem.DataContext;
13+
import com.intellij.openapi.project.Project;
14+
import com.intellij.openapi.vfs.LocalFileSystem;
15+
import com.intellij.openapi.vfs.VirtualFile;
16+
import com.intellij.psi.PsiFile;
17+
import com.intellij.psi.PsiManager;
18+
import com.intellij.psi.util.PsiTreeUtil;
19+
import com.magento.idea.magento2plugin.magento.files.ComposerJson;
20+
import com.magento.idea.magento2plugin.php.module.ComposerPackageModel;
21+
import com.magento.idea.magento2plugin.php.module.ComposerPackageModelImpl;
22+
23+
import java.io.File;
24+
25+
public class MagentoVersion {
26+
private static MagentoVersion INSTANCE = null;
27+
private String version = "unknown";
28+
private Project project;
29+
30+
public static MagentoVersion getInstance() {
31+
if (null == INSTANCE) {
32+
INSTANCE = new MagentoVersion();
33+
}
34+
return INSTANCE;
35+
}
36+
37+
public String get() {
38+
DataContext dataContext = DataManager.getInstance().getDataContext();
39+
this.project = (Project) dataContext.getData(DataConstants.PROJECT);
40+
41+
VirtualFile file = LocalFileSystem.getInstance().findFileByPath(this.getFilePath());
42+
43+
if (file == null) {
44+
return version;
45+
}
46+
47+
PsiManager psiManager = PsiManager.getInstance(this.project);
48+
PsiFile composerFile = psiManager.findFile(file);
49+
50+
if (composerFile instanceof JsonFile) {
51+
JsonFile composerJsonFile = (JsonFile) composerFile;
52+
JsonObject jsonObject = PsiTreeUtil.getChildOfType(composerJsonFile, JsonObject.class);
53+
54+
if (jsonObject == null) {
55+
return version;
56+
}
57+
58+
ComposerPackageModel composerObject = new ComposerPackageModelImpl(jsonObject);
59+
60+
if (composerObject.getType() != null) {
61+
version = composerObject.getVersion();
62+
}
63+
}
64+
65+
return version;
66+
}
67+
68+
private String getFilePath() {
69+
return this.project.getBasePath() + File.separator + ComposerJson.FILE_NAME;
70+
}
71+
}

src/com/magento/idea/magento2plugin/inspections/xml/CacheableFalseInDefaultLayoutInspection.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, fina
2424
@Override
2525
public void visitXmlAttribute(XmlAttribute attribute) {
2626
String fileName = holder.getFile().getName();
27-
if (!fileName.equals(LayoutXml.DefaultFileName)) return;
27+
if (!fileName.equals(LayoutXml.DEFAULT_FILENAME)) return;
2828
final String text = attribute.getValue();
2929
final String attributeName = attribute.getName();
30-
if (!attributeName.equals(LayoutXml.CacheableAttributeName)) return;
31-
if (!attribute.getParent().getName().equals(LayoutXml.BlockAttributeTagName)
32-
&& !attribute.getParent().getName().equals(LayoutXml.ReferenceBlockAttributeTagName)) return;
30+
if (!attributeName.equals(LayoutXml.CACHEABLE_ATTRIBUTE_NAME)) return;
31+
if (!attribute.getParent().getName().equals(LayoutXml.BLOCK_ATTRIBUTE_TAG_NAME)
32+
&& !attribute.getParent().getName().equals(LayoutXml.REFERENCE_BLOCK_ATTRIBUTE_TAG_NAME)) return;
3333
if (text == null) return;
34-
if (text.equals(LayoutXml.CacheableAttributeFalseValue)) {
34+
if (text.equals(LayoutXml.CACHEABLE_ATTRIBUTE_VALUE_FALSE)) {
3535
holder.registerProblem(attribute, CacheDisableProblemDescription,
3636
ProblemHighlightType.WARNING,
3737
new XmlRemoveCacheableAttributeQuickFix());

src/com/magento/idea/magento2plugin/magento/files/LayoutXml.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
package com.magento.idea.magento2plugin.magento.files;
66

77
public class LayoutXml {
8-
public static String DefaultFileName = "default.xml";
9-
public static String CacheableAttributeName = "cacheable";
10-
public static String CacheableAttributeFalseValue = "false";
11-
public static String BlockAttributeTagName = "block";
12-
public static String ReferenceBlockAttributeTagName = "referenceBlock";
8+
public static String DEFAULT_FILENAME = "default.xml";
9+
public static String CACHEABLE_ATTRIBUTE_NAME = "cacheable";
10+
public static String CACHEABLE_ATTRIBUTE_VALUE_FALSE = "false";
11+
public static String BLOCK_ATTRIBUTE_TAG_NAME = "block";
12+
public static String REFERENCE_BLOCK_ATTRIBUTE_TAG_NAME = "referenceBlock";
1313
public static String XML_ATTRIBUTE_TEMPLATE = "template";
1414
}

src/com/magento/idea/magento2plugin/magento/packages/Package.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
*/
55
package com.magento.idea.magento2plugin.magento.packages;
66

7-
import java.util.ArrayList;
8-
97
public class Package {
108
public static String PACKAGES_ROOT = "app/code";
119
public static String VENDOR = "vendor";

src/com/magento/idea/magento2plugin/project/SettingsForm.form

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
<border type="none"/>
1616
<children>
1717
<grid id="61372" layout-manager="FormLayout">
18+
<rowspec value="center:max(d;4px):noGrow"/>
19+
<rowspec value="top:3dlu:noGrow"/>
1820
<rowspec value="center:max(d;4px):noGrow"/>
1921
<rowspec value="top:3dlu:noGrow"/>
2022
<rowspec value="center:max(d;4px):noGrow"/>
@@ -46,6 +48,15 @@
4648
<text value="Regenerate urn map"/>
4749
</properties>
4850
</component>
51+
<component id="c2894" class="javax.swing.JLabel" binding="magentoVersion">
52+
<constraints>
53+
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
54+
<forms/>
55+
</constraints>
56+
<properties>
57+
<text value=""/>
58+
</properties>
59+
</component>
4960
</children>
5061
</grid>
5162
<component id="2ef99" class="javax.swing.JCheckBox" binding="pluginEnabled">

src/com/magento/idea/magento2plugin/project/SettingsForm.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@
1515
import com.intellij.psi.PsiFile;
1616
import com.intellij.psi.PsiManager;
1717
import com.intellij.psi.search.FilenameIndex;
18+
import com.magento.idea.magento2plugin.actions.generation.util.MagentoVersion;
1819
import com.magento.idea.magento2plugin.indexes.IndexManager;
19-
import com.magento.idea.magento2plugin.php.module.ComposerPackageModel;
20-
import com.magento.idea.magento2plugin.php.module.MagentoComponent;
21-
import com.magento.idea.magento2plugin.php.module.MagentoComponentManager;
22-
import com.magento.idea.magento2plugin.php.module.MagentoModule;
20+
import com.magento.idea.magento2plugin.php.module.*;
2321
import org.jetbrains.annotations.Nls;
2422
import org.jetbrains.annotations.NotNull;
2523
import org.jetbrains.annotations.Nullable;
@@ -42,6 +40,8 @@ public class SettingsForm implements Configurable {
4240
private JButton buttonReindex;
4341
private JPanel panel1;
4442
private JButton regenerateUrnMapButton;
43+
private JLabel magentoVersion;
44+
private MagentoVersion magentoVersionModel = MagentoVersion.getInstance();
4545

4646
public SettingsForm(@NotNull final Project project) {
4747
this.project = project;
@@ -78,6 +78,12 @@ public void mouseClicked(MouseEvent e) {
7878
regenerateUrnMapButton.addMouseListener(
7979
new RegenerateUrnMapListener(project)
8080
);
81+
82+
String version = magentoVersionModel.get();
83+
if (version != null) {
84+
magentoVersion.setText("Magento version: " . concat(version));
85+
}
86+
8187
return (JComponent) panel1;
8288
}
8389

tests/com/magento/idea/magento2plugin/inspections/xml/CacheableFalseInDefaultLayoutInspectionTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ protected boolean isWriteActionRequired() {
2929
}
3030

3131
public void testWithCacheableFalseBlock() throws Exception {
32-
myFixture.configureByFile(getTestName(true) + "/" + LayoutXml.DefaultFileName);
32+
myFixture.configureByFile(getTestName(true) + "/" + LayoutXml.DEFAULT_FILENAME);
3333
myFixture.testHighlighting(true, false, false);
3434
}
3535

3636
public void testWithoutCacheableFalseBlock() throws Exception {
37-
myFixture.configureByFile(getTestName(true) + "/" + LayoutXml.DefaultFileName);
37+
myFixture.configureByFile(getTestName(true) + "/" + LayoutXml.DEFAULT_FILENAME);
3838
myFixture.testHighlighting(true, true, true);
3939
}
4040

0 commit comments

Comments
 (0)