Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

913: Fixed parse usage #928

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,26 @@

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

import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.intellij.json.psi.JsonFile;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import com.magento.idea.magento2plugin.actions.generation.data.ModuleComposerJsonData;
import com.magento.idea.magento2plugin.actions.generation.generator.data.ModuleDirectoriesData;
import com.magento.idea.magento2plugin.actions.generation.generator.util.DirectoryGenerator;
import com.magento.idea.magento2plugin.actions.generation.generator.util.FileFromTemplateGenerator;
import com.magento.idea.magento2plugin.indexes.ModuleIndex;
import com.magento.idea.magento2plugin.magento.files.ComposerJson;
import com.magento.idea.magento2plugin.util.CamelCaseToHyphen;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.List;
import java.util.Properties;
import org.jetbrains.annotations.NotNull;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class ModuleComposerJsonGenerator extends FileGenerator {

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

if (virtualFile != null) { //NOPMD
final VirtualFile composerJsonFile = virtualFile.getVirtualFile();
if (composerJsonFile.exists()) {
final JsonElement jsonElement =
new JsonParser().parse(
new FileReader(composerJsonFile.getPath())//NOPMD
);
final JsonElement versionJsonElement =
jsonElement.getAsJsonObject().get("version");
final JsonElement nameJsonElement = jsonElement.getAsJsonObject().get("name");
final VirtualFile composerJsonVirtualFile = virtualFile.getVirtualFile();

if (composerJsonVirtualFile.exists()) {
final PsiFile composerJsonFile = PsiManager.getInstance(project)
.findFile(composerJsonVirtualFile);
if (!(composerJsonFile instanceof JsonFile)) {
return Pair.create("", "");
}
final JSONParser parser = new JSONParser();
final Object obj = parser.parse(
composerJsonFile.getText()
);
final JSONObject jsonObject = (JSONObject) obj;
final String versionJsonElement = jsonObject.get("version").toString();
final String nameJsonElement = jsonObject.get("name").toString();

if (versionJsonElement != null) {
version = versionJsonElement.getAsString();
version = versionJsonElement;
final int minorVersionSeparator = version.lastIndexOf('.');
version = new StringBuilder(version)
.replace(minorVersionSeparator + 1, version.length(),"*")
.toString();
}

if (nameJsonElement != null) {
moduleName = nameJsonElement.getAsString();
moduleName = nameJsonElement;
}
}
} else {
return Pair.create("", "");
}
} catch (FileNotFoundException e) { //NOPMD
} catch (ParseException exception) { //NOPMD
// It's fine
}

Expand Down