|
5 | 5 |
|
6 | 6 | package com.magento.idea.magento2plugin.actions.generation.generator;
|
7 | 7 |
|
8 |
| -import com.google.gson.JsonElement; |
9 |
| -import com.google.gson.JsonParser; |
| 8 | +import com.intellij.json.psi.JsonFile; |
10 | 9 | import com.intellij.openapi.project.Project;
|
11 | 10 | import com.intellij.openapi.util.Pair;
|
12 | 11 | import com.intellij.openapi.vfs.VirtualFile;
|
13 | 12 | import com.intellij.psi.PsiDirectory;
|
14 | 13 | import com.intellij.psi.PsiFile;
|
| 14 | +import com.intellij.psi.PsiManager; |
15 | 15 | import com.magento.idea.magento2plugin.actions.generation.data.ModuleComposerJsonData;
|
16 | 16 | import com.magento.idea.magento2plugin.actions.generation.generator.data.ModuleDirectoriesData;
|
17 | 17 | import com.magento.idea.magento2plugin.actions.generation.generator.util.DirectoryGenerator;
|
18 | 18 | import com.magento.idea.magento2plugin.actions.generation.generator.util.FileFromTemplateGenerator;
|
19 | 19 | import com.magento.idea.magento2plugin.indexes.ModuleIndex;
|
20 | 20 | import com.magento.idea.magento2plugin.magento.files.ComposerJson;
|
21 | 21 | import com.magento.idea.magento2plugin.util.CamelCaseToHyphen;
|
22 |
| -import java.io.FileNotFoundException; |
23 |
| -import java.io.FileReader; |
24 | 22 | import java.util.List;
|
25 | 23 | import java.util.Properties;
|
26 | 24 | 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; |
27 | 28 |
|
28 | 29 | public class ModuleComposerJsonGenerator extends FileGenerator {
|
29 | 30 |
|
@@ -170,30 +171,38 @@ private Pair<String, String> getDependencyData(
|
170 | 171 | final PsiFile virtualFile = moduleDir.findFile(ComposerJson.FILE_NAME);
|
171 | 172 |
|
172 | 173 | 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 | + |
182 | 190 | if (versionJsonElement != null) {
|
183 |
| - version = versionJsonElement.getAsString(); |
| 191 | + version = versionJsonElement; |
184 | 192 | final int minorVersionSeparator = version.lastIndexOf('.');
|
185 | 193 | version = new StringBuilder(version)
|
186 | 194 | .replace(minorVersionSeparator + 1, version.length(),"*")
|
187 | 195 | .toString();
|
188 | 196 | }
|
| 197 | + |
189 | 198 | if (nameJsonElement != null) {
|
190 |
| - moduleName = nameJsonElement.getAsString(); |
| 199 | + moduleName = nameJsonElement; |
191 | 200 | }
|
192 | 201 | }
|
193 | 202 | } else {
|
194 | 203 | return Pair.create("", "");
|
195 | 204 | }
|
196 |
| - } catch (FileNotFoundException e) { //NOPMD |
| 205 | + } catch (ParseException exception) { //NOPMD |
197 | 206 | // It's fine
|
198 | 207 | }
|
199 | 208 |
|
|
0 commit comments