Skip to content

Commit f0aeba6

Browse files
Merge pull request #928 from Iamwade/913-eliminate-parse-usage
913: Fixed parse usage
2 parents 4f2a8b7 + 3d50b18 commit f0aeba6

File tree

3 files changed

+29
-20
lines changed

3 files changed

+29
-20
lines changed

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

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,26 @@
55

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

8-
import com.google.gson.JsonElement;
9-
import com.google.gson.JsonParser;
8+
import com.intellij.json.psi.JsonFile;
109
import com.intellij.openapi.project.Project;
1110
import com.intellij.openapi.util.Pair;
1211
import com.intellij.openapi.vfs.VirtualFile;
1312
import com.intellij.psi.PsiDirectory;
1413
import com.intellij.psi.PsiFile;
14+
import com.intellij.psi.PsiManager;
1515
import com.magento.idea.magento2plugin.actions.generation.data.ModuleComposerJsonData;
1616
import com.magento.idea.magento2plugin.actions.generation.generator.data.ModuleDirectoriesData;
1717
import com.magento.idea.magento2plugin.actions.generation.generator.util.DirectoryGenerator;
1818
import com.magento.idea.magento2plugin.actions.generation.generator.util.FileFromTemplateGenerator;
1919
import com.magento.idea.magento2plugin.indexes.ModuleIndex;
2020
import com.magento.idea.magento2plugin.magento.files.ComposerJson;
2121
import com.magento.idea.magento2plugin.util.CamelCaseToHyphen;
22-
import java.io.FileNotFoundException;
23-
import java.io.FileReader;
2422
import java.util.List;
2523
import java.util.Properties;
2624
import org.jetbrains.annotations.NotNull;
25+
import org.json.simple.JSONObject;
26+
import org.json.simple.parser.JSONParser;
27+
import org.json.simple.parser.ParseException;
2728

2829
public class ModuleComposerJsonGenerator extends FileGenerator {
2930

@@ -170,30 +171,38 @@ private Pair<String, String> getDependencyData(
170171
final PsiFile virtualFile = moduleDir.findFile(ComposerJson.FILE_NAME);
171172

172173
if (virtualFile != null) { //NOPMD
173-
final VirtualFile composerJsonFile = virtualFile.getVirtualFile();
174-
if (composerJsonFile.exists()) {
175-
final JsonElement jsonElement =
176-
new JsonParser().parse(
177-
new FileReader(composerJsonFile.getPath())//NOPMD
178-
);
179-
final JsonElement versionJsonElement =
180-
jsonElement.getAsJsonObject().get("version");
181-
final JsonElement nameJsonElement = jsonElement.getAsJsonObject().get("name");
174+
final VirtualFile composerJsonVirtualFile = virtualFile.getVirtualFile();
175+
176+
if (composerJsonVirtualFile.exists()) {
177+
final PsiFile composerJsonFile = PsiManager.getInstance(project)
178+
.findFile(composerJsonVirtualFile);
179+
if (!(composerJsonFile instanceof JsonFile)) {
180+
return Pair.create("", "");
181+
}
182+
final JSONParser parser = new JSONParser();
183+
final Object obj = parser.parse(
184+
composerJsonFile.getText()
185+
);
186+
final JSONObject jsonObject = (JSONObject) obj;
187+
final String versionJsonElement = jsonObject.get("version").toString();
188+
final String nameJsonElement = jsonObject.get("name").toString();
189+
182190
if (versionJsonElement != null) {
183-
version = versionJsonElement.getAsString();
191+
version = versionJsonElement;
184192
final int minorVersionSeparator = version.lastIndexOf('.');
185193
version = new StringBuilder(version)
186194
.replace(minorVersionSeparator + 1, version.length(),"*")
187195
.toString();
188196
}
197+
189198
if (nameJsonElement != null) {
190-
moduleName = nameJsonElement.getAsString();
199+
moduleName = nameJsonElement;
191200
}
192201
}
193202
} else {
194203
return Pair.create("", "");
195204
}
196-
} catch (FileNotFoundException e) { //NOPMD
205+
} catch (ParseException exception) { //NOPMD
197206
// It's fine
198207
}
199208

testData/actions/generation/generator/ModuleComposerJsonGenerator/generateFileInRoot/composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"type": "magento2-module",
66
"require": {
77
"magento/framework": "*",
8-
"foo/bar": "*",
9-
"magento/backend": "*"
8+
"foo/bar": "1.0.*",
9+
"magento/module-backend": "103.0.*"
1010
},
1111
"license": [
1212
"Test License 1",

testData/actions/generation/generator/ModuleComposerJsonGenerator/generateModuleFile/composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"type": "magento2-module",
66
"require": {
77
"magento/framework": "*",
8-
"foo/bar": "*",
9-
"magento/backend": "*"
8+
"foo/bar": "1.0.*",
9+
"magento/module-backend": "103.0.*"
1010
},
1111
"license": [
1212
"Test License 1",

0 commit comments

Comments
 (0)