private static void appendPackagesFolders(
      final @NotNull Collection<String> excludedPackagesUrls,
      final @Nullable VirtualFile folder,
      final @NotNull ProjectFileIndex fileIndex) {
    if (folder == null) return;

    VfsUtilCore.visitChildrenRecursively(
        folder,
        new VirtualFileVisitor() {
          @NotNull
          public Result visitFileEx(@NotNull final VirtualFile file) {
            if (!fileIndex.isInContent(file)) {
              return SKIP_CHILDREN;
            }

            if (file.isDirectory()) {
              if ("packages".equals(file.getName())) {
                return SKIP_CHILDREN;
              } else {
                excludedPackagesUrls.add(file.getUrl() + "/packages");
              }
            }

            return CONTINUE;
          }
        });
  }
 private String dubImportPath(String rootPath) {
   String pathUrl = VirtualFileManager.constructUrl(LocalFileSystem.PROTOCOL, rootPath);
   VirtualFile file = VirtualFileManager.getInstance().findFileByUrl(pathUrl);
   if (file == null) {
     return null;
   }
   final List<String> sourcesDir = new ArrayList<>();
   VfsUtilCore.visitChildrenRecursively(
       file,
       new VirtualFileVisitor() {
         @Override
         public boolean visitFile(@NotNull VirtualFile file) {
           if (file.isDirectory()) {
             if (file.getName().equals("source")) {
               sourcesDir.add("source");
             }
             if (file.getName().equals("src")) {
               sourcesDir.add("src");
             }
           }
           return true;
         }
       });
   return sourcesDir.isEmpty() ? null : rootPath + File.separator + sourcesDir.get(0);
 }
Beispiel #3
0
  /**
   * Collects all files which are located in the passed directory.
   *
   * @throws IllegalArgumentException if <code>dir</code> isn't a directory.
   */
  public static void collectFiles(
      final VirtualFile dir,
      final List<VirtualFile> files,
      final boolean recursive,
      final boolean addDirectories) {
    if (!dir.isDirectory()) {
      throw new IllegalArgumentException(
          VcsBundle.message("exception.text.file.should.be.directory", dir.getPresentableUrl()));
    }

    final FileTypeManager fileTypeManager = FileTypeManager.getInstance();
    VfsUtilCore.visitChildrenRecursively(
        dir,
        new VirtualFileVisitor() {
          @Override
          public boolean visitFile(@NotNull VirtualFile file) {
            if (file.isDirectory()) {
              if (addDirectories) {
                files.add(file);
              }
              if (!recursive && !Comparing.equal(file, dir)) {
                return false;
              }
            } else if (fileTypeManager == null || file.getFileType() != FileTypes.UNKNOWN) {
              files.add(file);
            }
            return true;
          }
        });
  }
 public static void refreshRecursively(@NotNull VirtualFile file) {
   VfsUtilCore.visitChildrenRecursively(
       file,
       new VirtualFileVisitor() {
         @Override
         public boolean visitFile(@NotNull VirtualFile file) {
           file.getChildren();
           return true;
         }
       });
   file.refresh(false, true);
 }
 private VirtualFile refresh(File file) {
   VirtualFile vFile = myFileSystem.refreshAndFindFileByIoFile(file);
   assertNotNull(file.toString(), vFile);
   VfsUtilCore.visitChildrenRecursively(
       vFile,
       new VirtualFileVisitor() {
         @Override
         public boolean visitFile(@NotNull VirtualFile file) {
           file.getChildren();
           return true;
         }
       });
   return vFile;
 }
 public static void collectJarFiles(
     final VirtualFile dir, final List<VirtualFile> container, final boolean recursively) {
   VfsUtilCore.visitChildrenRecursively(
       dir,
       new VirtualFileVisitor(SKIP_ROOT, (recursively ? null : ONE_LEVEL_DEEP)) {
         @Override
         public boolean visitFile(@NotNull VirtualFile file) {
           final VirtualFile jarRoot = StandardFileSystems.getJarRootForLocalFile(file);
           if (jarRoot != null) {
             container.add(jarRoot);
             return false;
           }
           return true;
         }
       });
 }
  private void countAndMarkUnresolved(
      @NotNull VirtualFile file, @NotNull final Set<VirtualFile> result, final boolean inDbOnly) {
    if (file.isDirectory()) {
      VfsUtilCore.visitChildrenRecursively(
          file,
          new VirtualFileVisitor() {
            @Override
            public boolean visitFile(@NotNull VirtualFile file) {
              return doCountAndMarkUnresolved(file, result);
            }

            @Nullable
            @Override
            public Iterable<VirtualFile> getChildrenIterable(@NotNull VirtualFile file) {
              return inDbOnly ? ((NewVirtualFile) file).iterInDbChildren() : null;
            }
          });
    } else {
      doCountAndMarkUnresolved(file, result);
    }
  }
  public static boolean iterateRecursively(
      @NotNull final VirtualFile root,
      @NotNull final VirtualFileFilter filter,
      @NotNull final ContentIterator iterator) {

    try {
      VfsUtilCore.visitChildrenRecursively(
          root,
          new VirtualFileVisitor() {
            @Override
            public boolean visitFile(@NotNull VirtualFile file) {
              if (!file.isValid() || !filter.accept(file)) return false;

              if (!iterator.processFile(file)) throw new StopItException();
              return true;
            }
          });
      return true;
    } catch (StopItException e) {
      return false;
    }
  }
  public void testVisitRecursively() throws Exception {
    final DirectoryData data = new DirectoryData(myProject.getBaseDir());
    try {
      data.clear();
      data.create();

      final File subDir = new File(data.getBase().getPath(), "DL0N1");
      final VirtualFile vSubDir = LocalFileSystem.getInstance().findFileByIoFile(subDir);
      assertNotNull(vSubDir);

      VfsUtilCore.visitChildrenRecursively(
          data.getBase(),
          new VirtualFileVisitor() {
            @Override
            public boolean visitFile(@NotNull VirtualFile file) {
              assertTrue(!VfsUtilCore.isAncestor(vSubDir, file, true));
              return !vSubDir.equals(file);
            }
          });
    } finally {
      data.clear();
    }
  }
  @NotNull
  private List<VirtualFile> findRebarConfigs(
      @NotNull final VirtualFile root, @NotNull final ProgressIndicator indicator) {
    root.refresh(false, true);
    final List<VirtualFile> foundRebarConfigs = new ArrayList<VirtualFile>();
    VfsUtilCore.visitChildrenRecursively(
        root,
        new VirtualFileVisitor() {
          @Override
          public boolean visitFile(@NotNull VirtualFile file) {
            indicator.checkCanceled();
            if (file.isDirectory()) {
              if (isExamplesDirectory(file) || isRelDirectory(root.getPath(), file.getPath()))
                return false;
              indicator.setText2(file.getPath());
            } else if (file.getName().equalsIgnoreCase("rebar.config")) {
              foundRebarConfigs.add(file);
            }
            return true;
          }
        });

    return foundRebarConfigs;
  }
  private void buildOutputItemsList(
      final String outputDir,
      final Module module,
      VirtualFile from,
      final FileTypeManager typeManager,
      final VirtualFile sourceRoot,
      final String packagePrefix,
      final List<File> filesToRefresh,
      final Map<String, Collection<TranslatingCompiler.OutputItem>> results)
      throws CacheCorruptedException {
    final Ref<CacheCorruptedException> exRef = new Ref<CacheCorruptedException>(null);
    final ModuleFileIndex fileIndex = ModuleRootManager.getInstance(module).getFileIndex();
    final GlobalSearchScope srcRootScope =
        GlobalSearchScope.moduleScope(module)
            .intersectWith(GlobalSearchScopes.directoryScope(myProject, sourceRoot, true));

    final Collection<FileType> registeredInputTypes =
        CompilerManager.getInstance(myProject).getRegisteredInputTypes(myTranslatingCompiler);

    final ContentIterator contentIterator =
        new ContentIterator() {
          public boolean processFile(final VirtualFile child) {
            try {
              if (child.isValid()) {
                if (!child.isDirectory() && registeredInputTypes.contains(child.getFileType())) {
                  updateOutputItemsList(
                      outputDir,
                      child,
                      sourceRoot,
                      packagePrefix,
                      filesToRefresh,
                      results,
                      srcRootScope);
                }
              }
              return true;
            } catch (CacheCorruptedException e) {
              exRef.set(e);
              return false;
            }
          }
        };
    if (fileIndex.isInContent(from)) {
      // use file index for iteration to handle 'inner modules' and excludes properly
      fileIndex.iterateContentUnderDirectory(from, contentIterator);
    } else {
      // seems to be a root for generated sources
      VfsUtilCore.visitChildrenRecursively(
          from,
          new VirtualFileVisitor() {
            @Override
            public boolean visitFile(@NotNull VirtualFile file) {
              if (!file.isDirectory()) {
                contentIterator.processFile(file);
              }
              return true;
            }
          });
    }
    final CacheCorruptedException exc = exRef.get();
    if (exc != null) {
      throw exc;
    }
  }