private static void doCollectResourceDirs( AndroidFacet facet, boolean collectResCacheDirs, List<String> result, CompileContext context) { final Module module = facet.getModule(); if (collectResCacheDirs) { final AndroidPlatform platform = facet.getConfiguration().getAndroidPlatform(); final int platformToolsRevision = platform != null ? platform.getSdk().getPlatformToolsRevision() : -1; if (platformToolsRevision < 0 || platformToolsRevision > 7) { // png cache is supported since platform-tools-r8 final String resCacheDirOsPath = findResourcesCacheDirectory(module, false, context); if (resCacheDirOsPath != null) { result.add(resCacheDirOsPath); } else { LOG.info("PNG cache not found for module " + module.getName()); } } } final VirtualFile resourcesDir = AndroidAptCompiler.getResourceDirForApkCompiler(module, facet); if (resourcesDir != null) { result.add(resourcesDir.getPath()); } }
public static void removeDuplicatingClasses( final Module module, @NotNull final String packageName, @NotNull String className, @Nullable File classFile, String sourceRootPath) { if (sourceRootPath == null) { return; } VirtualFile sourceRoot = LocalFileSystem.getInstance().findFileByPath(sourceRootPath); if (sourceRoot == null) { return; } final Project project = module.getProject(); final JavaPsiFacade facade = JavaPsiFacade.getInstance(project); final String interfaceQualifiedName = packageName + '.' + className; PsiClass[] classes = facade.findClasses(interfaceQualifiedName, GlobalSearchScope.moduleScope(module)); final ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(project).getFileIndex(); for (PsiClass c : classes) { PsiFile psiFile = c.getContainingFile(); if (className.equals(FileUtil.getNameWithoutExtension(psiFile.getName()))) { VirtualFile virtualFile = psiFile.getVirtualFile(); if (virtualFile != null && projectFileIndex.getSourceRootForFile(virtualFile) == sourceRoot) { final String path = virtualFile.getPath(); File f = new File(path); try { f = f.getCanonicalFile(); classFile = classFile != null ? classFile.getCanonicalFile() : null; if (f != null && !f.equals(classFile) && f.exists()) { if (f.delete()) { virtualFile.refresh(true, false); } else { ApplicationManager.getApplication() .invokeLater( new Runnable() { public void run() { Messages.showErrorDialog( project, "Can't delete file " + path, CommonBundle.getErrorTitle()); } }, project.getDisposed()); } } } catch (IOException e) { LOG.info(e); } } } } }
private static void unexcludeRootIfNeccessary( @NotNull VirtualFile root, @NotNull ModuleRootManager manager) { Set<VirtualFile> excludedRoots = new HashSet<VirtualFile>(Arrays.asList(manager.getExcludeRoots())); VirtualFile excludedRoot = root; while (excludedRoot != null && !excludedRoots.contains(excludedRoot)) { excludedRoot = excludedRoot.getParent(); } if (excludedRoot == null) { return; } Set<VirtualFile> rootsToExclude = new HashSet<VirtualFile>(); collectChildrenRecursively(excludedRoot, root, rootsToExclude); final ModifiableRootModel model = manager.getModifiableModel(); ContentEntry contentEntry = findContentEntryForRoot(model, excludedRoot); if (contentEntry != null) { ExcludeFolder excludedFolder = null; for (ExcludeFolder folder : contentEntry.getExcludeFolders()) { if (folder.getFile() == excludedRoot) { excludedFolder = folder; break; } } if (excludedFolder != null) { contentEntry.removeExcludeFolder(excludedFolder); } for (VirtualFile rootToExclude : rootsToExclude) { if (!excludedRoots.contains(rootToExclude)) { contentEntry.addExcludeFolder(rootToExclude); } } } ApplicationManager.getApplication() .runWriteAction( new Runnable() { @Override public void run() { model.commit(); } }); }
private static void collectChildrenRecursively( @NotNull VirtualFile root, @NotNull VirtualFile anchor, @NotNull Collection<VirtualFile> result) { if (root == anchor) { return; } VirtualFile parent = anchor.getParent(); if (parent == null) { return; } for (VirtualFile child : parent.getChildren()) { if (child != anchor) { result.add(child); } } if (parent != root) { collectChildrenRecursively(root, parent, result); } }
@Nullable public static VirtualFile getProguardConfigFile(@NotNull AndroidFacet facet) { final VirtualFile root = AndroidRootUtil.getMainContentRoot(facet); return root != null ? root.findChild(PROGUARD_CFG_FILE_NAME) : null; }
public static void collectAllResources( @NotNull final AndroidFacet facet, final Set<ResourceEntry> resourceSet) { final LocalResourceManager manager = facet.getLocalResourceManager(); for (final String resType : ResourceType.getNames()) { for (final ResourceElement element : manager.getValueResources(resType)) { ApplicationManager.getApplication() .runReadAction( new Runnable() { @Override public void run() { if (!element.isValid() || facet.getModule().isDisposed() || facet.getModule().getProject().isDisposed()) { return; } final String name = element.getName().getValue(); if (name != null) { resourceSet.add(new ResourceEntry(resType, name)); } } }); } } for (final Resources resources : manager.getResourceElements()) { ApplicationManager.getApplication() .runReadAction( new Runnable() { @Override public void run() { if (!resources.isValid() || facet.getModule().isDisposed() || facet.getModule().getProject().isDisposed()) { return; } for (final Attr attr : resources.getAttrs()) { final String name = attr.getName().getValue(); if (name != null) { resourceSet.add(new ResourceEntry(ResourceType.ATTR.getName(), name)); } } for (final DeclareStyleable styleable : resources.getDeclareStyleables()) { final String name = styleable.getName().getValue(); if (name != null) { resourceSet.add( new ResourceEntry(ResourceType.DECLARE_STYLEABLE.getName(), name)); } } } }); } ApplicationManager.getApplication() .runReadAction( new Runnable() { @Override public void run() { if (facet.getModule().isDisposed() || facet.getModule().getProject().isDisposed()) { return; } for (String id : manager.getIds()) { resourceSet.add(new ResourceEntry(ResourceType.ID.getName(), id)); } } }); final HashSet<VirtualFile> visited = new HashSet<VirtualFile>(); for (VirtualFile subdir : manager.getResourceSubdirs(null)) { final HashSet<VirtualFile> resourceFiles = new HashSet<VirtualFile>(); AndroidUtils.collectFiles(subdir, visited, resourceFiles); for (VirtualFile file : resourceFiles) { resourceSet.add(new ResourceEntry(subdir.getName(), file.getName())); } } }