|
| 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.generator.util; |
| 7 | + |
| 8 | +import com.intellij.openapi.project.Project; |
| 9 | +import com.intellij.psi.PsiDirectory; |
| 10 | +import com.intellij.psi.PsiFile; |
| 11 | +import com.magento.idea.magento2plugin.indexes.ModuleIndex; |
| 12 | +import com.magento.idea.magento2plugin.magento.files.GraphQLSchema; |
| 13 | +import com.magento.idea.magento2plugin.magento.files.ModuleFileInterface; |
| 14 | +import com.magento.idea.magento2plugin.magento.packages.Areas; |
| 15 | +import com.magento.idea.magento2plugin.magento.packages.Package; |
| 16 | +import com.magento.idea.magento2plugin.util.magento.FileBasedIndexUtil; |
| 17 | +import java.util.Properties; |
| 18 | + |
| 19 | +public final class FindOrCreateGraphQLSchema { |
| 20 | + |
| 21 | + private final Project project; |
| 22 | + |
| 23 | + public FindOrCreateGraphQLSchema(final Project project) { |
| 24 | + this.project = project; |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Finds or creates graphQL schema file. |
| 29 | + * |
| 30 | + * @param actionName String |
| 31 | + * @param moduleName String |
| 32 | + * |
| 33 | + * @return PsiFile |
| 34 | + */ |
| 35 | + public PsiFile execute(final String actionName, final String moduleName) { |
| 36 | + PsiDirectory parentDirectory = new ModuleIndex(project) |
| 37 | + .getModuleDirectoryByModuleName(moduleName); |
| 38 | + |
| 39 | + if (parentDirectory == null) { |
| 40 | + return null; |
| 41 | + } |
| 42 | + final DirectoryGenerator directoryGenerator = DirectoryGenerator.getInstance(); |
| 43 | + final FileFromTemplateGenerator fileFromTemplateGenerator = |
| 44 | + new FileFromTemplateGenerator(project); |
| 45 | + parentDirectory = directoryGenerator |
| 46 | + .findOrCreateSubdirectory(parentDirectory, Package.moduleBaseAreaDir); |
| 47 | + final GraphQLSchema graphQlSchemaFile = GraphQLSchema.getInstance(); |
| 48 | + PsiFile graphQlSchema = FileBasedIndexUtil.findModuleConfigFile( |
| 49 | + graphQlSchemaFile.getFileName(), |
| 50 | + Areas.getAreaByString(Areas.base.name()), |
| 51 | + moduleName, |
| 52 | + project |
| 53 | + ); |
| 54 | + |
| 55 | + if (graphQlSchema == null) { |
| 56 | + graphQlSchema = fileFromTemplateGenerator.generate( |
| 57 | + (ModuleFileInterface) graphQlSchema, |
| 58 | + new Properties(), |
| 59 | + parentDirectory, |
| 60 | + actionName |
| 61 | + ); |
| 62 | + } |
| 63 | + return graphQlSchema; |
| 64 | + } |
| 65 | +} |
0 commit comments