Search in sources :

Example 1 with TYPO3ExtensionDefinition

use of com.cedricziel.idea.typo3.domain.TYPO3ExtensionDefinition in project idea-php-typo3-plugin by cedricziel.

the class ExtensionDefinitionFactory method fromComposerManifest.

/**
 * Reads a composer manifest and compiles it into a {@link TYPO3ExtensionDefinition}, if possible.
 *
 * @param composerManifest The manifest virtual file system entry to parse
 * @return The derived extension definition.
 * @throws IOException thrown if reading the manifest fails.
 */
private static TYPO3ExtensionDefinition fromComposerManifest(VirtualFile composerManifest) throws IOException {
    TYPO3ExtensionDefinition extensionDefinition = new TYPO3ExtensionDefinition();
    extensionDefinition.setRootDirectory(composerManifest.getParent());
    JsonElement jsonElement = ComposerConfigUtils.parseJson(composerManifest);
    if (jsonElement.isJsonObject()) {
        JsonObject jsonObject = jsonElement.getAsJsonObject();
        String packageName = "";
        String composerPackageType = "";
        String extensionKey = "";
        JsonElement nameElement = jsonObject.get("name");
        if (nameElement == null) {
            return null;
        }
        packageName = nameElement.getAsString();
        JsonElement typeElement = jsonObject.get("type");
        if (typeElement.isJsonPrimitive()) {
            composerPackageType = typeElement.getAsString();
            if (!isValidPackageType(composerPackageType)) {
                return null;
            }
        }
        extensionDefinition.setPackageName(packageName);
        /*
              Here we either extract the extension key from the composer manifest or
              guess the extension key from the package name.
             */
        if (jsonObject.has("extra") && !extractExtensionKeyFromComposerExtras(jsonObject.getAsJsonObject()).isEmpty()) {
            extensionKey = extractExtensionKeyFromComposerExtras(jsonObject.getAsJsonObject());
        } else {
            extensionKey = extensionKeyFromPackageName(packageName);
            if (composerPackageType.equals(COMPOSER_TYPE_CMS_FRAMEWORK)) {
                if ("cms_".equals(extensionKey.substring(0, 4))) {
                    extensionKey = extensionKey.substring(4);
                }
            }
        }
        extensionDefinition.setExtensionKey(extensionKey);
    }
    return extensionDefinition;
}
Also used : JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) TYPO3ExtensionDefinition(com.cedricziel.idea.typo3.domain.TYPO3ExtensionDefinition)

Example 2 with TYPO3ExtensionDefinition

use of com.cedricziel.idea.typo3.domain.TYPO3ExtensionDefinition in project idea-php-typo3-plugin by cedricziel.

the class ExtensionDefinitionFactory method fromDirectory.

public static TYPO3ExtensionDefinition fromDirectory(VirtualDirectoryImpl virtualDirectory) {
    TYPO3ExtensionDefinition extensionDefinition = null;
    // try finding composer manifest
    VirtualFileSystemEntry composerManifest = virtualDirectory.findChild("composer.json");
    if (composerManifest != null) {
        try {
            extensionDefinition = fromComposerManifest(composerManifest);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return extensionDefinition != null ? extensionDefinition : null;
}
Also used : VirtualFileSystemEntry(com.intellij.openapi.vfs.newvfs.impl.VirtualFileSystemEntry) IOException(java.io.IOException) TYPO3ExtensionDefinition(com.cedricziel.idea.typo3.domain.TYPO3ExtensionDefinition)

Example 3 with TYPO3ExtensionDefinition

use of com.cedricziel.idea.typo3.domain.TYPO3ExtensionDefinition in project idea-php-typo3-plugin by cedricziel.

the class TYPO3ExtensionUtil method findContainingExtension.

/**
 * Traverses the given directories and returns the first valid
 * extension definition that's applicable.
 *
 * @param directories List of directories to analyze
 */
public static TYPO3ExtensionDefinition findContainingExtension(PsiDirectory[] directories) {
    for (PsiDirectory directory : directories) {
        VirtualDirectoryImpl virtualFile = (VirtualDirectoryImpl) directory.getVirtualFile();
        while (!isExtensionRootDirectory(virtualFile)) {
            if (virtualFile.getParent() == null) {
                return null;
            }
            virtualFile = virtualFile.getParent();
        }
        TYPO3ExtensionDefinition extensionDefinition = ExtensionDefinitionFactory.fromDirectory(virtualFile);
        if (extensionDefinition != null) {
            return extensionDefinition;
        }
    }
    return null;
}
Also used : VirtualDirectoryImpl(com.intellij.openapi.vfs.newvfs.impl.VirtualDirectoryImpl) PsiDirectory(com.intellij.psi.PsiDirectory) TYPO3ExtensionDefinition(com.cedricziel.idea.typo3.domain.TYPO3ExtensionDefinition)

Example 4 with TYPO3ExtensionDefinition

use of com.cedricziel.idea.typo3.domain.TYPO3ExtensionDefinition in project idea-php-typo3-plugin by cedricziel.

the class GenerateFscElementAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent anActionEvent) {
    Project project = anActionEvent.getProject();
    if (project == null) {
        return;
    }
    ToolWindow toolWindow = anActionEvent.getData(PlatformDataKeys.TOOL_WINDOW);
    if (toolWindow == null) {
        return;
    }
    PsiDirectory[] psiDirectories = ActionUtil.findDirectoryFromActionEvent(anActionEvent);
    if (psiDirectories.length == 0) {
        return;
    }
    TYPO3ExtensionDefinition extensionDefinition = TYPO3ExtensionUtil.findContainingExtension(psiDirectories);
    if (extensionDefinition == null) {
        Messages.showErrorDialog("Could not extract extension from working directory. Does your extension contain a composer manifest?", "Error While Trying to Find Extension");
        return;
    }
    GenerateFscElementForm.create(toolWindow.getComponent(), project, extensionDefinition);
}
Also used : Project(com.intellij.openapi.project.Project) ToolWindow(com.intellij.openapi.wm.ToolWindow) PsiDirectory(com.intellij.psi.PsiDirectory) TYPO3ExtensionDefinition(com.cedricziel.idea.typo3.domain.TYPO3ExtensionDefinition)

Aggregations

TYPO3ExtensionDefinition (com.cedricziel.idea.typo3.domain.TYPO3ExtensionDefinition)4 PsiDirectory (com.intellij.psi.PsiDirectory)2 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 Project (com.intellij.openapi.project.Project)1 VirtualDirectoryImpl (com.intellij.openapi.vfs.newvfs.impl.VirtualDirectoryImpl)1 VirtualFileSystemEntry (com.intellij.openapi.vfs.newvfs.impl.VirtualFileSystemEntry)1 ToolWindow (com.intellij.openapi.wm.ToolWindow)1 IOException (java.io.IOException)1