|
| 1 | +package fr.adrienbrault.idea.symfony2plugin.asset; |
| 2 | + |
| 3 | +import com.intellij.openapi.project.Project; |
| 4 | +import com.intellij.openapi.vfs.VfsUtil; |
| 5 | +import com.intellij.openapi.vfs.VirtualFile; |
| 6 | +import com.intellij.openapi.vfs.VirtualFileVisitor; |
| 7 | +import com.intellij.psi.PsiDirectory; |
| 8 | +import com.jetbrains.php.PhpIndex; |
| 9 | +import fr.adrienbrault.idea.symfony2plugin.Settings; |
| 10 | +import fr.adrienbrault.idea.symfony2plugin.util.SymfonyBundleUtil; |
| 11 | +import fr.adrienbrault.idea.symfony2plugin.util.dict.SymfonyBundle; |
| 12 | +import org.apache.commons.lang.StringUtils; |
| 13 | +import org.jetbrains.annotations.NotNull; |
| 14 | +import org.jetbrains.annotations.Nullable; |
| 15 | + |
| 16 | +import java.util.*; |
| 17 | +import java.util.regex.Matcher; |
| 18 | +import java.util.regex.Pattern; |
| 19 | + |
| 20 | +/** |
| 21 | + * @author Daniel Espendiller <daniel@espendiller.net> |
| 22 | + */ |
| 23 | +public class AssetDirectoryReader { |
| 24 | + |
| 25 | + public static AssetDirectoryReader INSTANCE = new AssetDirectoryReader(); |
| 26 | + |
| 27 | + final private boolean includeBundleDir; |
| 28 | + |
| 29 | + @NotNull |
| 30 | + final private Collection<String> filterExtension = new HashSet<>(); |
| 31 | + |
| 32 | + public AssetDirectoryReader() { |
| 33 | + includeBundleDir = false; |
| 34 | + } |
| 35 | + |
| 36 | + public AssetDirectoryReader(@NotNull String[] filterExtension, boolean includeBundleDir) { |
| 37 | + this.includeBundleDir = includeBundleDir; |
| 38 | + this.filterExtension.addAll(Arrays.asList(filterExtension)); |
| 39 | + } |
| 40 | + |
| 41 | + @Nullable |
| 42 | + private static VirtualFile getProjectAssetRoot(@NotNull Project project) { |
| 43 | + VirtualFile projectDirectory = project.getBaseDir(); |
| 44 | + String webDirectoryName = Settings.getInstance(project).directoryToWeb; |
| 45 | + return VfsUtil.findRelativeFile(projectDirectory, webDirectoryName.split("/")); |
| 46 | + } |
| 47 | + |
| 48 | + @NotNull |
| 49 | + public Collection<AssetFile> getAssetFiles(@NotNull Project project) { |
| 50 | + Collection<AssetFile> files = new ArrayList<>(); |
| 51 | + |
| 52 | + VirtualFile webDirectory = getProjectAssetRoot(project); |
| 53 | + if (null == webDirectory) { |
| 54 | + return files; |
| 55 | + } |
| 56 | + |
| 57 | + VfsUtil.visitChildrenRecursively(webDirectory, new VirtualFileVisitor() { |
| 58 | + @Override |
| 59 | + public boolean visitFile(@NotNull VirtualFile virtualFile) { |
| 60 | + if(isValidFile(virtualFile)) { |
| 61 | + files.add(new AssetFile(virtualFile, AssetEnum.Position.Web, webDirectory)); |
| 62 | + } |
| 63 | + return super.visitFile(virtualFile); |
| 64 | + } |
| 65 | + }); |
| 66 | + |
| 67 | + if(!this.includeBundleDir) { |
| 68 | + return files; |
| 69 | + } |
| 70 | + |
| 71 | + SymfonyBundleUtil symfonyBundleUtil = new SymfonyBundleUtil(PhpIndex.getInstance(project)); |
| 72 | + for(SymfonyBundle bundle : symfonyBundleUtil.getBundles()) { |
| 73 | + PsiDirectory bundleDirectory = bundle.getDirectory(); |
| 74 | + if(null == bundleDirectory) { |
| 75 | + continue; |
| 76 | + } |
| 77 | + |
| 78 | + VirtualFile bundleDirectoryVirtual = bundleDirectory.getVirtualFile(); |
| 79 | + VirtualFile resourceDirectory = VfsUtil.findRelativeFile(bundleDirectoryVirtual, "Resources"); |
| 80 | + |
| 81 | + if (null != resourceDirectory) { |
| 82 | + VfsUtil.visitChildrenRecursively(resourceDirectory, new VirtualFileVisitor() { |
| 83 | + @Override |
| 84 | + public boolean visitFile(@NotNull VirtualFile virtualFile) { |
| 85 | + if(isValidFile(virtualFile)) { |
| 86 | + files.add(new AssetFile(virtualFile, AssetEnum.Position.Bundle, bundleDirectoryVirtual, '@' + bundle.getName() + "/")); |
| 87 | + } |
| 88 | + return super.visitFile(virtualFile); |
| 89 | + } |
| 90 | + }); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return files; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * '@SampleBundle/Resources/public/js/*' |
| 99 | + * 'assets/js/*' |
| 100 | + * 'assets/js/*.js' |
| 101 | + */ |
| 102 | + @NotNull |
| 103 | + public Collection<VirtualFile> resolveAssetFile(@NotNull Project project, @NotNull String filename) { |
| 104 | + String assetName = StringUtils.stripStart(filename.replace("\\", "/").replaceAll("/+", "/"), "/"); |
| 105 | + |
| 106 | + // '@SampleBundle/Resources/public/js/foo,js' |
| 107 | + // TODO: '@SampleBundle/Resources/public/js/*' |
| 108 | + // TODO: '@SampleBundle/Resources/public/js/*.js' |
| 109 | + if(filename.startsWith("@")) { |
| 110 | + Collection<VirtualFile> files = new ArrayList<>(); |
| 111 | + |
| 112 | + int i = filename.indexOf("/"); |
| 113 | + if(i > 0) { |
| 114 | + String relativeFilename = filename.substring(1, i); |
| 115 | + SymfonyBundle bundle = new SymfonyBundleUtil(PhpIndex.getInstance(project)).getBundle(relativeFilename); |
| 116 | + if(bundle != null) { |
| 117 | + String assetPath = filename.substring(i + 1); |
| 118 | + |
| 119 | + Matcher matcher = Pattern.compile("^(.*[/\\\\])\\*([.\\w+]*)$").matcher(assetPath); |
| 120 | + if (!matcher.find()) { |
| 121 | + VirtualFile relative = bundle.getRelative(assetPath); |
| 122 | + if(relative != null) { |
| 123 | + files.add(relative); |
| 124 | + } |
| 125 | + } else { |
| 126 | + // "/*" |
| 127 | + // "/*.js" |
| 128 | + PsiDirectory directory = bundle.getDirectory(); |
| 129 | + if(directory != null) { |
| 130 | + files.addAll(collectWildcardDirectories(matcher, directory.getVirtualFile())); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + return files; |
| 137 | + } |
| 138 | + |
| 139 | + Collection<VirtualFile> files = new ArrayList<>(); |
| 140 | + |
| 141 | + VirtualFile webDirectory = getProjectAssetRoot(project); |
| 142 | + if (null == webDirectory) { |
| 143 | + return files; |
| 144 | + } |
| 145 | + |
| 146 | + Matcher matcher = Pattern.compile("^(.*[/\\\\])\\*([.\\w+]*)$").matcher(assetName); |
| 147 | + if (!matcher.find()) { |
| 148 | + VirtualFile assetFile = VfsUtil.findRelativeFile(webDirectory, assetName.split("/")); |
| 149 | + if(assetFile != null) { |
| 150 | + files.add(assetFile); |
| 151 | + } |
| 152 | + } else { |
| 153 | + // "/*" |
| 154 | + // "/*.js" |
| 155 | + files.addAll(collectWildcardDirectories(matcher, webDirectory)); |
| 156 | + } |
| 157 | + |
| 158 | + return files; |
| 159 | + } |
| 160 | + |
| 161 | + private Collection<VirtualFile> collectWildcardDirectories(@NotNull Matcher matcher, @NotNull VirtualFile directory) { |
| 162 | + Collection<VirtualFile> files = new HashSet<>(); |
| 163 | + |
| 164 | + String pathName = matcher.group(1); |
| 165 | + String fileExtension = matcher.group(2).length() > 0 ? matcher.group(2) : null; |
| 166 | + |
| 167 | + pathName = StringUtils.stripEnd(pathName, "/"); |
| 168 | + |
| 169 | + if(fileExtension == null) { |
| 170 | + // @TODO: filter files |
| 171 | + // 'assets/js/*' |
| 172 | + VirtualFile assetFile = VfsUtil.findRelativeFile(directory, pathName.split("/")); |
| 173 | + if(assetFile != null) { |
| 174 | + files.add(assetFile); |
| 175 | + } |
| 176 | + } else { |
| 177 | + // @TODO: filter files |
| 178 | + // 'assets/js/*.js' |
| 179 | + VirtualFile assetFile = VfsUtil.findRelativeFile(directory, pathName.split("/")); |
| 180 | + if(assetFile != null) { |
| 181 | + files.add(assetFile); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + return files; |
| 186 | + } |
| 187 | + |
| 188 | + private boolean isValidFile(@NotNull VirtualFile virtualFile) { |
| 189 | + if (virtualFile.isDirectory()) { |
| 190 | + return false; |
| 191 | + } |
| 192 | + |
| 193 | + if(filterExtension.size() == 0) { |
| 194 | + return true; |
| 195 | + } |
| 196 | + |
| 197 | + String extension = virtualFile.getExtension(); |
| 198 | + return extension != null && this.filterExtension.contains(extension); |
| 199 | + } |
| 200 | +} |
0 commit comments