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

Fixes for overrite for js and css files in theme #1046

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions resources/fileTemplates/internal/Require Config JS File.js.ft
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var config = {
}
20 changes: 20 additions & 0 deletions resources/fileTemplates/internal/Require Config JS File.js.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<html lang="en">
<body>
<font face="verdana" size="-1">
<p>
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.
</p>
<p>
Read more about requirejs-config.js in the
<a href="https://devdocs.magento.com/guides/v2.4/javascript-dev-guide/javascript/requirejs.html">DevDocs</a>.
</p>
</font>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<String> themeNames = new ModuleIndex(project).getEditableThemeNames();

for (final String themeName : themeNames) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -45,16 +47,12 @@ protected PsiDirectory getTargetDirectory(
final PsiDirectory directory,
final List<String> 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))
);
}

/**
Expand Down Expand Up @@ -97,11 +95,42 @@ protected List<String> 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<String> getLibPathComponets(final PsiFile file) {
final List<String> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -40,24 +41,29 @@ public void execute(final PsiFile baseFile, final String themeName) {

final GetMagentoModuleUtil.MagentoModuleData moduleData =
GetMagentoModuleUtil.getByContext(baseFile.getContainingDirectory(), project);
List<String> pathComponents;

if (moduleData == null) {
return;
}

List<String> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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) {}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading