diff --git a/resources/fileTemplates/internal/Require Config JS File.js.ft b/resources/fileTemplates/internal/Require Config JS File.js.ft new file mode 100644 index 000000000..2238246bd --- /dev/null +++ b/resources/fileTemplates/internal/Require Config JS File.js.ft @@ -0,0 +1,2 @@ +var config = { +} diff --git a/resources/fileTemplates/internal/Require Config JS File.js.html b/resources/fileTemplates/internal/Require Config JS File.js.html new file mode 100644 index 000000000..f958bd890 --- /dev/null +++ b/resources/fileTemplates/internal/Require Config JS File.js.html @@ -0,0 +1,20 @@ + + + + +

+ All configuration is done in the requirejs-config.js file. It has a single root object config which contains + the configuration options described below. All the configuration settings are optional and are used only when required. +

+

+ Read more about requirejs-config.js in the + DevDocs. +

+
+ + \ No newline at end of file diff --git a/src/com/magento/idea/magento2plugin/actions/generation/OverrideTemplateInThemeAction.java b/src/com/magento/idea/magento2plugin/actions/generation/OverrideTemplateInThemeAction.java index 325ac90ca..302015cc1 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/OverrideTemplateInThemeAction.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/OverrideTemplateInThemeAction.java @@ -10,16 +10,19 @@ import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiFile; import com.magento.idea.magento2plugin.MagentoIcons; -import com.magento.idea.magento2plugin.actions.CopyMagentoPath; import com.magento.idea.magento2plugin.actions.generation.dialog.OverrideTemplateInThemeDialog; +import com.magento.idea.magento2plugin.magento.packages.OverridableFileType; +import com.magento.idea.magento2plugin.magento.packages.Package; +import java.util.Arrays; import org.jetbrains.annotations.NotNull; public class OverrideTemplateInThemeAction extends OverrideFileInThemeAction { public static final String ACTION_NAME = "Override this file in a project theme"; - public static final String ACTION_TEMPLATE_DESCRIPTION = "Override template in project theme"; - public static final String ACTION_STYLES_DESCRIPTION = "Override styles in project theme"; - public static final String LESS_FILE_EXTENSION = "less"; + public static final String ACTION_TEMPLATE_DESCRIPTION = + "Override template file in project theme"; + public static final String ACTION_STYLES_DESCRIPTION = "Override styles file in project theme"; + public static final String ACTION_JS_DESCRIPTION = "Override javascript file in project theme"; public OverrideTemplateInThemeAction() { super(ACTION_NAME, ACTION_TEMPLATE_DESCRIPTION, MagentoIcons.MODULE); @@ -47,11 +50,18 @@ protected boolean isOverrideAllowed( } final String fileExtension = virtualFile.getExtension(); - if (!CopyMagentoPath.PHTML_EXTENSION.equals(fileExtension) - && !LESS_FILE_EXTENSION.equals(fileExtension)) { + if (!OverridableFileType.getOverwritableFileExtensions().contains(fileExtension)) { return false; } - return isFileInModuleOrTheme(file, project); + if (OverridableFileType.isFileJS(fileExtension) + && !virtualFile.getCanonicalPath().contains(Package.libWebRoot) + && !Arrays.asList(virtualFile.getCanonicalPath() + .split(Package.V_FILE_SEPARATOR)).contains("web")) { + return false; + } + + return OverridableFileType.isFileJS(fileExtension) + || isFileInModuleOrTheme(file, project); } } diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/OverrideTemplateInThemeDialog.java b/src/com/magento/idea/magento2plugin/actions/generation/dialog/OverrideTemplateInThemeDialog.java index 31920f0e4..a0dde4de8 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/dialog/OverrideTemplateInThemeDialog.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/OverrideTemplateInThemeDialog.java @@ -8,7 +8,6 @@ import com.intellij.openapi.project.Project; import com.intellij.psi.PsiDirectory; import com.intellij.psi.PsiFile; -import com.magento.idea.magento2plugin.actions.CopyMagentoPath; import com.magento.idea.magento2plugin.actions.generation.OverrideTemplateInThemeAction; import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.FieldValidation; import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.RuleRegistry; @@ -17,6 +16,7 @@ import com.magento.idea.magento2plugin.indexes.ModuleIndex; import com.magento.idea.magento2plugin.magento.packages.Areas; import com.magento.idea.magento2plugin.magento.packages.ComponentType; +import com.magento.idea.magento2plugin.magento.packages.OverridableFileType; import com.magento.idea.magento2plugin.magento.packages.Package; import com.magento.idea.magento2plugin.util.magento.GetMagentoModuleUtil; import java.awt.event.ActionEvent; @@ -65,9 +65,12 @@ public OverrideTemplateInThemeDialog( setContentPane(contentPane); setModal(true); - if (CopyMagentoPath.PHTML_EXTENSION.equals(psiFile.getVirtualFile().getExtension())) { + final String fileType = psiFile.getVirtualFile().getExtension(); + if (OverridableFileType.isFilePhtml(fileType)) { setTitle(OverrideTemplateInThemeAction.ACTION_TEMPLATE_DESCRIPTION); - } else { + } else if (OverridableFileType.isFileJS(fileType)) { + setTitle(OverrideTemplateInThemeAction.ACTION_JS_DESCRIPTION); + } else if (OverridableFileType.isFileStyle(fileType)) { setTitle(OverrideTemplateInThemeAction.ACTION_STYLES_DESCRIPTION); } getRootPane().setDefaultButton(buttonOK); @@ -121,27 +124,37 @@ private String getTheme() { return this.theme.getSelectedItem().toString(); } + @SuppressWarnings("PMD.CognitiveComplexity") private void fillThemeOptions() { final GetMagentoModuleUtil.MagentoModuleData moduleData = GetMagentoModuleUtil.getByContext(psiFile.getContainingDirectory(), project); - - if (moduleData == null) { - return; - } String area = ""; // NOPMD - if (moduleData.getType().equals(ComponentType.module)) { - final PsiDirectory viewDir = moduleData.getViewDir(); - - if (viewDir == null) { + if (moduleData == null) { + if (psiFile.getVirtualFile().getExtension() + .equals(OverridableFileType.JS.getType())) { + area = "base"; + } else { return; } - final String filePath = psiFile.getVirtualFile().getPath(); - final String relativePath = filePath.replace(viewDir.getVirtualFile().getPath(), ""); - area = relativePath.split(Package.V_FILE_SEPARATOR)[1]; } else { - area = moduleData.getName().split(Package.V_FILE_SEPARATOR)[0]; + if (moduleData.getType().equals(ComponentType.module)) { + final PsiDirectory viewDir = moduleData.getViewDir(); + + if (viewDir == null) { + return; + } + final String filePath = psiFile.getVirtualFile().getPath(); + final String relativePath = filePath.replace( + viewDir.getVirtualFile().getPath(), + "" + ); + area = relativePath.split(Package.V_FILE_SEPARATOR)[1]; + } else { + area = moduleData.getName().split(Package.V_FILE_SEPARATOR)[0]; + } } + final List themeNames = new ModuleIndex(project).getEditableThemeNames(); for (final String themeName : themeNames) { diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/OverrideInThemeGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/OverrideInThemeGenerator.java index b13e93030..4e5512b04 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/OverrideInThemeGenerator.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/OverrideInThemeGenerator.java @@ -11,11 +11,13 @@ import com.magento.idea.magento2plugin.actions.generation.generator.util.DirectoryGenerator; import com.magento.idea.magento2plugin.bundles.ValidatorBundle; import com.magento.idea.magento2plugin.magento.packages.Areas; +import com.magento.idea.magento2plugin.magento.packages.File; import com.magento.idea.magento2plugin.util.RegExUtil; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.regex.Pattern; +import java.util.stream.Collectors; @SuppressWarnings("PMD.AbstractClassWithoutAbstractMethod") public abstract class OverrideInThemeGenerator { @@ -45,16 +47,12 @@ protected PsiDirectory getTargetDirectory( final PsiDirectory directory, final List pathComponents ) { - PsiDirectory result = directory; - PsiDirectory tempDirectory = directory; final DirectoryGenerator generator = DirectoryGenerator.getInstance(); - for (final String directoryName : pathComponents) { - result = generator.findOrCreateSubdirectory(tempDirectory, directoryName); - tempDirectory = result; - } - - return result; + return generator.findOrCreateSubdirectories( + directory, + pathComponents.stream().collect(Collectors.joining(File.separator)) + ); } /** @@ -97,11 +95,42 @@ protected List getThemePathComponents(final PsiFile file) { final Pattern pattern = Pattern.compile(RegExUtil.Magento.MODULE_NAME); PsiDirectory parent = file.getParent(); - do { + boolean isLib = true; + for (final String filePart : parent.toString().split("/")) { + if (Pattern.matches(String.valueOf(pattern), filePart)) { + isLib = false; + } + } + + if (isLib) { + pathComponents.addAll(getLibPathComponets(file)); + } else { + do { + pathComponents.add(parent.getName()); + parent = parent.getParent(); + } while (!pattern.matcher(parent.getName()).find()); + pathComponents.add(parent.getName()); + Collections.reverse(pathComponents); + } + + return pathComponents; + } + + /** + * Get file path to lib. + * + * @param file PsiFile + * @return List + */ + protected List getLibPathComponets(final PsiFile file) { + final List pathComponents = new ArrayList<>(); + PsiDirectory parent = file.getParent(); + + while (!"web".equals(parent.getName())) { pathComponents.add(parent.getName()); parent = parent.getParent(); - } while (!pattern.matcher(parent.getName()).find()); - pathComponents.add(parent.getName()); + } + pathComponents.add("web"); Collections.reverse(pathComponents); return pathComponents; diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/OverrideTemplateInThemeGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/OverrideTemplateInThemeGenerator.java index f2e76d0ca..6520bc601 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/OverrideTemplateInThemeGenerator.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/OverrideTemplateInThemeGenerator.java @@ -15,6 +15,7 @@ import com.maddyhome.idea.copyright.actions.UpdateCopyrightProcessor; import com.magento.idea.magento2plugin.indexes.ModuleIndex; import com.magento.idea.magento2plugin.magento.packages.ComponentType; +import com.magento.idea.magento2plugin.magento.packages.OverridableFileType; import com.magento.idea.magento2plugin.util.magento.GetComponentNameByDirectoryUtil; import com.magento.idea.magento2plugin.util.magento.GetMagentoModuleUtil; import java.util.List; @@ -40,24 +41,29 @@ public void execute(final PsiFile baseFile, final String themeName) { final GetMagentoModuleUtil.MagentoModuleData moduleData = GetMagentoModuleUtil.getByContext(baseFile.getContainingDirectory(), project); + List pathComponents; if (moduleData == null) { - return; - } - - List pathComponents; - if (moduleData.getType().equals(ComponentType.module)) { - pathComponents = getModulePathComponents( - baseFile, - GetComponentNameByDirectoryUtil.execute( - baseFile.getContainingDirectory(), - project - ) - ); - } else if (moduleData.getType().equals(ComponentType.theme)) { - pathComponents = getThemePathComponents(baseFile); + if (baseFile.getVirtualFile().getExtension().equals(OverridableFileType.JS.getType())) { + pathComponents = getLibPathComponets(baseFile); + } else { + return; + } } else { - return; + + if (moduleData.getType().equals(ComponentType.module)) { + pathComponents = getModulePathComponents( + baseFile, + GetComponentNameByDirectoryUtil.execute( + baseFile.getContainingDirectory(), + project + ) + ); + } else if (moduleData.getType().equals(ComponentType.theme)) { + pathComponents = getThemePathComponents(baseFile); + } else { + return; + } } final ModuleIndex moduleIndex = new ModuleIndex(project); diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/js/RequireJsConfigGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/js/RequireJsConfigGenerator.java new file mode 100644 index 000000000..7df2d3c08 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/js/RequireJsConfigGenerator.java @@ -0,0 +1,51 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.actions.generation.generator.js; + +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiDirectory; +import com.intellij.psi.PsiFile; +import com.magento.idea.magento2plugin.actions.generation.generator.FileGenerator; +import com.magento.idea.magento2plugin.actions.generation.generator.util.FileFromTemplateGenerator; +import com.magento.idea.magento2plugin.magento.files.RequireConfigJsFile; +import java.util.Properties; +import org.jetbrains.annotations.NotNull; + + +public class RequireJsConfigGenerator extends FileGenerator { + + private final PsiDirectory directory; + + public RequireJsConfigGenerator( + final @NotNull Project project, + final @NotNull PsiDirectory directory + ) { + super(project); + this.directory = directory; + } + + @Override + public PsiFile generate(final @NotNull String actionName) { + final RequireConfigJsFile file = new RequireConfigJsFile(); + PsiFile configFile = directory.findFile(file.getFileName()); + + if (configFile == null) { + final FileFromTemplateGenerator generator = new FileFromTemplateGenerator(project); + configFile = generator.generate( + file, + new Properties(), + directory, + actionName + ); + } + + return configFile; + } + + @SuppressWarnings("PMD.UncommentedEmptyMethodBody") + @Override + protected void fillAttributes(final Properties attributes) {} +} diff --git a/src/com/magento/idea/magento2plugin/magento/files/RequireConfigJsFile.java b/src/com/magento/idea/magento2plugin/magento/files/RequireConfigJsFile.java new file mode 100644 index 000000000..86659aa05 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/magento/files/RequireConfigJsFile.java @@ -0,0 +1,33 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.magento.files; + +import com.intellij.lang.Language; +import com.intellij.lang.javascript.JavascriptLanguage; + +public class RequireConfigJsFile implements ModuleFileInterface { + + public static final String TEMPLATE = "Require Config JS File"; + public static final String FILE_NAME = "requirejs-config.js"; + + public static final String MAP_PROPERTY = "map"; + public static final String ASTERISK_PROPERTY = "*"; + + @Override + public String getFileName() { + return FILE_NAME; + } + + @Override + public String getTemplate() { + return TEMPLATE; + } + + @Override + public Language getLanguage() { + return JavascriptLanguage.INSTANCE; + } +} diff --git a/src/com/magento/idea/magento2plugin/magento/packages/OverridableFileType.java b/src/com/magento/idea/magento2plugin/magento/packages/OverridableFileType.java new file mode 100644 index 000000000..41347d2a9 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/magento/packages/OverridableFileType.java @@ -0,0 +1,102 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.magento.packages; + +import java.util.ArrayList; +import java.util.List; + +public enum OverridableFileType { + + CSS("css"), + LESS("less"), + JS("js"), + PHTML("phtml"); + + private final String filetype; + + /** + * Overridable File Type constructor. + * + * @param filetype String + */ + OverridableFileType(final String filetype) { + this.filetype = filetype; + } + + /** + * Get overridable file type. + * + * @return String + */ + public String getType() { + return filetype; + } + + /** + * Checks if file extension is js. + * + * @param filetype String + * @return boolean + */ + public static Boolean isFileJS(final String filetype) { + return filetype.equals(OverridableFileType.JS.getType()); + } + + /** + * Checks is file extension is phtml. + * + * @param fileType String + * @return Boolean + */ + public static Boolean isFilePhtml(final String fileType) { + return fileType.equals(OverridableFileType.PHTML.getType()); + } + + /** + * Checks is file extension is less or css. + * @param fileType String + * @return Boolean + */ + public static Boolean isFileStyle(final String fileType) { + Boolean result = false; + final List fileStyleTypes = getStyleFileTypes(); + for (final String styleType : fileStyleTypes) { + if (styleType.equals(fileType)) { + result = true; + } + } + + return result; + } + + /** + * Get overridable file extensions. + * @return List + */ + public static List getOverwritableFileExtensions() { + final List fileTypes = new ArrayList<>(); + + fileTypes.add(OverridableFileType.CSS.getType()); + fileTypes.add(OverridableFileType.LESS.getType()); + fileTypes.add(OverridableFileType.PHTML.getType()); + fileTypes.add(OverridableFileType.JS.getType()); + + return fileTypes; + } + + /** + * Returns styles file extensions. + * @return List + */ + public static List getStyleFileTypes() { + final List styleFileTypes = new ArrayList<>(); + + styleFileTypes.add(OverridableFileType.CSS.getType()); + styleFileTypes.add(OverridableFileType.LESS.getType()); + + return styleFileTypes; + } +}