private boolean needTransformCopying(CompileScope compileScope) { final CompilerConfiguration configuration = CompilerConfiguration.getInstance(myProject); final ProjectFileIndex index = ProjectRootManager.getInstance(myProject).getFileIndex(); for (VirtualFile file : FilenameIndex.getVirtualFilesByName( myProject, AST_TRANSFORM_FILE_NAME, GlobalSearchScope.projectScope(myProject))) { if (compileScope.belongs(file.getUrl()) && index.isInSource(file) && !configuration.isResourceFile(file)) { return true; } } return false; }
@NotNull public static VirtualFile getTargetDirectoryFor( @NotNull Project project, @NotNull VirtualFile sourceFile, @Nullable String targetFile, @Nullable String targetPackage, boolean returnRoot) { boolean hasPackage = StringUtil.isNotEmpty(targetPackage); ProjectRootManager rootManager = ProjectRootManager.getInstance(project); ProjectFileIndex fileIndex = ProjectFileIndex.SERVICE.getInstance(project); Collection<VirtualFile> files = targetFile == null ? Collections.<VirtualFile>emptyList() : FilenameIndex.getVirtualFilesByName( project, targetFile, ProjectScope.getAllScope(project)); VirtualFile existingFile = null; for (VirtualFile file : files) { String existingFilePackage = fileIndex.getPackageNameByDirectory(file.getParent()); if (!hasPackage || existingFilePackage == null || targetPackage.equals(existingFilePackage)) { existingFile = file; break; } } VirtualFile existingFileRoot = existingFile == null ? null : fileIndex.isInSourceContent(existingFile) ? fileIndex.getSourceRootForFile(existingFile) : fileIndex.isInContent(existingFile) ? fileIndex.getContentRootForFile(existingFile) : null; boolean preferGenRoot = sourceFile.getFileType() == BnfFileType.INSTANCE || sourceFile.getFileType() == JFlexFileType.INSTANCE; boolean preferSourceRoot = hasPackage && !preferGenRoot; VirtualFile[] sourceRoots = rootManager.getContentSourceRoots(); VirtualFile[] contentRoots = rootManager.getContentRoots(); final VirtualFile virtualRoot = existingFileRoot != null ? existingFileRoot : preferSourceRoot && fileIndex.isInSource(sourceFile) ? fileIndex.getSourceRootForFile(sourceFile) : fileIndex.isInContent(sourceFile) ? fileIndex.getContentRootForFile(sourceFile) : getFirstElement( preferSourceRoot && sourceRoots.length > 0 ? sourceRoots : contentRoots); if (virtualRoot == null) { fail(project, sourceFile, "Unable to guess target source root"); throw new ProcessCanceledException(); } try { String genDirName = Options.GEN_DIR.get(); boolean newGenRoot = !fileIndex.isInSourceContent(virtualRoot); final String relativePath = (hasPackage && newGenRoot ? genDirName + "/" + targetPackage : hasPackage ? targetPackage : newGenRoot ? genDirName : "") .replace('.', '/'); if (relativePath.isEmpty()) { return virtualRoot; } else { VirtualFile result = new WriteAction<VirtualFile>() { @Override protected void run(@NotNull Result<VirtualFile> result) throws Throwable { result.setResult(VfsUtil.createDirectoryIfMissing(virtualRoot, relativePath)); } }.execute().throwException().getResultObject(); VfsUtil.markDirtyAndRefresh(false, true, true, result); return returnRoot && newGenRoot ? ObjectUtils.assertNotNull(virtualRoot.findChild(genDirName)) : returnRoot ? virtualRoot : result; } } catch (ProcessCanceledException ex) { throw ex; } catch (Exception ex) { fail(project, sourceFile, ex.getMessage()); throw new ProcessCanceledException(); } }