|
| 1 | +/* |
| 2 | + * Copyright (c) 2017 Patrick Scheibe |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal |
| 6 | + * in the Software without restriction, including without limitation the rights |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + * copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | + * THE SOFTWARE. |
| 21 | + * |
| 22 | + */ |
| 23 | + |
| 24 | +package de.halirutan.mathematica.library; |
| 25 | + |
| 26 | +import com.intellij.openapi.progress.ProgressIndicator; |
| 27 | +import com.intellij.openapi.roots.OrderRootType; |
| 28 | +import com.intellij.openapi.roots.libraries.ui.RootDetector; |
| 29 | +import com.intellij.openapi.vfs.JarFileSystem; |
| 30 | +import com.intellij.openapi.vfs.VfsUtil; |
| 31 | +import com.intellij.openapi.vfs.VirtualFile; |
| 32 | +import com.intellij.util.CommonProcessors; |
| 33 | +import org.jetbrains.annotations.NotNull; |
| 34 | + |
| 35 | +import java.util.ArrayList; |
| 36 | +import java.util.Collection; |
| 37 | +import java.util.Collections; |
| 38 | +import java.util.List; |
| 39 | + |
| 40 | +/** |
| 41 | + * Provides functionality to detect the root directories of valid Mathematica source libraries |
| 42 | + * |
| 43 | + * @author patrick (25.11.17). |
| 44 | + */ |
| 45 | +public class MathematicaLibraryRootDetector extends RootDetector { |
| 46 | + MathematicaLibraryRootDetector(OrderRootType rootType, boolean jarDirectory, String presentableRootTypeName) { |
| 47 | + super(rootType, jarDirectory, presentableRootTypeName); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Provides a way to select directories, starting from some root, that are likely to be the parent-directory of |
| 52 | + * package sources. We test for each directory if either there is a PacletInfo.m in it or if it contains a package |
| 53 | + * file that has the same name as the directory itself. |
| 54 | + * <p> |
| 55 | + * If the user selects a directory that contains several packages, we collect all valid package dirs. |
| 56 | + * |
| 57 | + * @param dir Starting directory for the package search. In the best case, it is the root of the package itself |
| 58 | + * |
| 59 | + * @return A list of found packages. |
| 60 | + */ |
| 61 | + private static Collection<VirtualFile> collectPackageDirs(final VirtualFile dir) { |
| 62 | + if (!dir.isDirectory()) { |
| 63 | + return Collections.emptyList(); |
| 64 | + } |
| 65 | + |
| 66 | + CommonProcessors.CollectProcessor<VirtualFile> paclets = new CommonProcessors.CollectProcessor<VirtualFile>() { |
| 67 | + @Override |
| 68 | + public boolean accept(VirtualFile virtualFile) { |
| 69 | + if (virtualFile.isDirectory()) { |
| 70 | + final String dirName = virtualFile.getName(); |
| 71 | + final VirtualFile[] children = virtualFile.getChildren(); |
| 72 | + for (VirtualFile child : children) { |
| 73 | + if (!child.isDirectory() && ("PacletInfo.m".equals(child.getName()) || |
| 74 | + (dirName.equals(child.getNameWithoutExtension()) && "m".equals(child.getExtension())))) { |
| 75 | + return true; |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + return false; |
| 80 | + } |
| 81 | + }; |
| 82 | + VfsUtil.processFilesRecursively(dir, paclets); |
| 83 | + return paclets.getResults(); |
| 84 | + } |
| 85 | + |
| 86 | + @NotNull |
| 87 | + @Override |
| 88 | + public Collection<VirtualFile> detectRoots(@NotNull VirtualFile rootCandidate, @NotNull ProgressIndicator progressIndicator) { |
| 89 | + final List<VirtualFile> result = new ArrayList<>(); |
| 90 | + final Collection<VirtualFile> packageDirectories = collectPackageDirs(rootCandidate); |
| 91 | + if (rootCandidate.getFileSystem() instanceof JarFileSystem || packageDirectories.isEmpty()) { |
| 92 | + return result; |
| 93 | + } |
| 94 | + result.addAll(packageDirectories); |
| 95 | + return result; |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments