Search in sources :

Example 1 with FileDeletedEvent

use of org.jetbrains.jps.incremental.messages.FileDeletedEvent in project intellij-community by JetBrains.

the class BuildOperations method cleanOutputsCorrespondingToChangedFiles.

public static <R extends BuildRootDescriptor, T extends BuildTarget<R>> Map<T, Set<File>> cleanOutputsCorrespondingToChangedFiles(final CompileContext context, DirtyFilesHolder<R, T> dirtyFilesHolder) throws ProjectBuildException {
    final BuildDataManager dataManager = context.getProjectDescriptor().dataManager;
    try {
        final Map<T, Set<File>> cleanedSources = new HashMap<>();
        final THashSet<File> dirsToDelete = new THashSet<>(FileUtil.FILE_HASHING_STRATEGY);
        final Collection<String> deletedPaths = new ArrayList<>();
        dirtyFilesHolder.processDirtyFiles(new FileProcessor<R, T>() {

            // cache the mapping locally
            private final Map<T, SourceToOutputMapping> mappingsCache = new HashMap<>();

            private final TObjectIntHashMap<T> idsCache = new TObjectIntHashMap<>();

            @Override
            public boolean apply(T target, File file, R sourceRoot) throws IOException {
                SourceToOutputMapping srcToOut = mappingsCache.get(target);
                if (srcToOut == null) {
                    srcToOut = dataManager.getSourceToOutputMap(target);
                    mappingsCache.put(target, srcToOut);
                }
                final int targetId;
                if (!idsCache.containsKey(target)) {
                    targetId = dataManager.getTargetsState().getBuildTargetId(target);
                    idsCache.put(target, targetId);
                } else {
                    targetId = idsCache.get(target);
                }
                final String srcPath = file.getPath();
                final Collection<String> outputs = srcToOut.getOutputs(srcPath);
                if (outputs != null) {
                    final boolean shouldPruneOutputDirs = target instanceof ModuleBasedTarget;
                    final List<String> deletedForThisSource = new ArrayList<>(outputs.size());
                    for (String output : outputs) {
                        deleteRecursively(output, deletedForThisSource, shouldPruneOutputDirs ? dirsToDelete : null);
                    }
                    deletedPaths.addAll(deletedForThisSource);
                    dataManager.getOutputToTargetRegistry().removeMapping(deletedForThisSource, targetId);
                    Set<File> cleaned = cleanedSources.get(target);
                    if (cleaned == null) {
                        cleaned = new THashSet<>(FileUtil.FILE_HASHING_STRATEGY);
                        cleanedSources.put(target, cleaned);
                    }
                    cleaned.add(file);
                }
                return true;
            }
        });
        if (JavaBuilderUtil.isCompileJavaIncrementally(context)) {
            final ProjectBuilderLogger logger = context.getLoggingManager().getProjectBuilderLogger();
            if (logger.isEnabled()) {
                logger.logDeletedFiles(deletedPaths);
            }
        }
        if (!deletedPaths.isEmpty()) {
            context.processMessage(new FileDeletedEvent(deletedPaths));
        }
        // attempting to delete potentially empty directories
        FSOperations.pruneEmptyDirs(context, dirsToDelete);
        return cleanedSources;
    } catch (Exception e) {
        throw new ProjectBuildException(e);
    }
}
Also used : SourceToOutputMapping(org.jetbrains.jps.builders.storage.SourceToOutputMapping) THashSet(gnu.trove.THashSet) TObjectIntHashMap(gnu.trove.TObjectIntHashMap) ProjectBuilderLogger(org.jetbrains.jps.builders.logging.ProjectBuilderLogger) TObjectIntHashMap(gnu.trove.TObjectIntHashMap) BuildDataManager(org.jetbrains.jps.incremental.storage.BuildDataManager) IOException(java.io.IOException) THashSet(gnu.trove.THashSet) IOException(java.io.IOException) FileDeletedEvent(org.jetbrains.jps.incremental.messages.FileDeletedEvent) File(java.io.File)

Aggregations

THashSet (gnu.trove.THashSet)1 TObjectIntHashMap (gnu.trove.TObjectIntHashMap)1 File (java.io.File)1 IOException (java.io.IOException)1 ProjectBuilderLogger (org.jetbrains.jps.builders.logging.ProjectBuilderLogger)1 SourceToOutputMapping (org.jetbrains.jps.builders.storage.SourceToOutputMapping)1 FileDeletedEvent (org.jetbrains.jps.incremental.messages.FileDeletedEvent)1 BuildDataManager (org.jetbrains.jps.incremental.storage.BuildDataManager)1