Search in sources :

Example 1 with GenericCompilerCache

use of com.intellij.compiler.impl.generic.GenericCompilerCache in project intellij-community by JetBrains.

the class GenericCompilerRunner method invokeCompiler.

private <T extends BuildTarget, Item extends CompileItem<Key, SourceState, OutputState>, Key, SourceState, OutputState> boolean invokeCompiler(GenericCompiler<Key, SourceState, OutputState> compiler, final GenericCompilerInstance<T, Item, Key, SourceState, OutputState> instance) throws IOException, ExitException {
    final GenericCompilerCache<Key, SourceState, OutputState> cache = CompilerCacheManager.getInstance(myProject).getGenericCompilerCache(compiler);
    GenericCompilerPersistentData data = new GenericCompilerPersistentData(getGenericCompilerCacheDir(myProject, compiler), compiler.getVersion());
    if (data.isVersionChanged()) {
        LOG.info("Clearing cache for " + compiler.getDescription());
        cache.wipe();
        data.save();
    }
    final Set<String> targetsToRemove = new HashSet<>(data.getAllTargets());
    new ReadAction() {

        protected void run(@NotNull final Result result) {
            for (T target : instance.getAllTargets()) {
                targetsToRemove.remove(target.getId());
            }
        }
    }.execute();
    if (!myOnlyCheckStatus) {
        for (final String target : targetsToRemove) {
            final int id = data.removeId(target);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Removing obsolete target '" + target + "' (id=" + id + ")");
            }
            final List<Key> keys = new ArrayList<>();
            CompilerUtil.runInContext(myContext, "Processing obsolete targets...", () -> {
                cache.processSources(id, new CommonProcessors.CollectProcessor<>(keys));
                List<GenericCompilerCacheState<Key, SourceState, OutputState>> obsoleteSources = new ArrayList<>();
                for (Key key : keys) {
                    final GenericCompilerCache.PersistentStateData<SourceState, OutputState> state = cache.getState(id, key);
                    obsoleteSources.add(new GenericCompilerCacheState<>(key, state.mySourceState, state.myOutputState));
                }
                instance.processObsoleteTarget(target, obsoleteSources);
            });
            checkForErrorsOrCanceled();
            for (Key key : keys) {
                cache.remove(id, key);
            }
        }
    }
    final List<T> selectedTargets = new ReadAction<List<T>>() {

        protected void run(@NotNull final Result<List<T>> result) {
            result.setResult(instance.getSelectedTargets());
        }
    }.execute().getResultObject();
    boolean didSomething = false;
    for (T target : selectedTargets) {
        int id = data.getId(target.getId());
        didSomething |= processTarget(target, id, compiler, instance, cache);
    }
    data.save();
    return didSomething;
}
Also used : GenericCompilerCache(com.intellij.compiler.impl.generic.GenericCompilerCache) NotNull(org.jetbrains.annotations.NotNull) GenericCompilerPersistentData(com.intellij.compiler.impl.generic.GenericCompilerPersistentData) Result(com.intellij.openapi.application.Result) RunResult(com.intellij.openapi.application.RunResult) ReadAction(com.intellij.openapi.application.ReadAction) CommonProcessors(com.intellij.util.CommonProcessors) THashSet(gnu.trove.THashSet)

Example 2 with GenericCompilerCache

use of com.intellij.compiler.impl.generic.GenericCompilerCache in project intellij-community by JetBrains.

the class GenericCompilerRunner method processTarget.

private <T extends BuildTarget, Item extends CompileItem<Key, SourceState, OutputState>, Key, SourceState, OutputState> boolean processTarget(T target, final int targetId, final GenericCompiler<Key, SourceState, OutputState> compiler, final GenericCompilerInstance<T, Item, Key, SourceState, OutputState> instance, final GenericCompilerCache<Key, SourceState, OutputState> cache) throws IOException, ExitException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Processing target '" + target + "' (id=" + targetId + ") by " + compiler);
    }
    final List<Item> items = instance.getItems(target);
    checkForErrorsOrCanceled();
    final List<GenericCompilerProcessingItem<Item, SourceState, OutputState>> toProcess = new ArrayList<>();
    final THashSet<Key> keySet = new THashSet<>(new SourceItemHashingStrategy<>(compiler));
    final Ref<IOException> exception = Ref.create(null);
    final Map<Item, SourceState> sourceStates = new HashMap<>();
    DumbService.getInstance(myProject).runReadActionInSmartMode(() -> {
        try {
            for (Item item : items) {
                final Key key = item.getKey();
                keySet.add(key);
                if (item.isExcluded())
                    continue;
                final GenericCompilerCache.PersistentStateData<SourceState, OutputState> data = cache.getState(targetId, key);
                SourceState sourceState = data != null ? data.mySourceState : null;
                final OutputState outputState = data != null ? data.myOutputState : null;
                if (myForceCompile || sourceState == null || !item.isSourceUpToDate(sourceState) || outputState == null || !item.isOutputUpToDate(outputState)) {
                    sourceStates.put(item, item.computeSourceState());
                    toProcess.add(new GenericCompilerProcessingItem<>(item, sourceState, outputState));
                }
            }
        } catch (IOException e) {
            exception.set(e);
        }
    });
    if (!exception.isNull()) {
        throw exception.get();
    }
    final List<Key> toRemove = new ArrayList<>();
    cache.processSources(targetId, key -> {
        if (!keySet.contains(key)) {
            toRemove.add(key);
        }
        return true;
    });
    if (LOG.isDebugEnabled()) {
        LOG.debug(toProcess.size() + " items will be processed, " + toRemove.size() + " items will be removed");
        for (int i = 0; i < getItemsCountToShowInLog(toProcess.size()); i++) {
            LOG.debug("to process:" + toProcess.get(i).getItem().getKey());
        }
        for (int i = 0; i < getItemsCountToShowInLog(toRemove.size()); i++) {
            LOG.debug("to delete:" + toRemove.get(i));
        }
    }
    if (toProcess.isEmpty() && toRemove.isEmpty()) {
        return false;
    }
    if (myOnlyCheckStatus) {
        throw new ExitException(ExitStatus.CANCELLED);
    }
    List<GenericCompilerCacheState<Key, SourceState, OutputState>> obsoleteItems = new ArrayList<>();
    for (Key key : toRemove) {
        final GenericCompilerCache.PersistentStateData<SourceState, OutputState> data = cache.getState(targetId, key);
        obsoleteItems.add(new GenericCompilerCacheState<>(key, data.mySourceState, data.myOutputState));
    }
    final List<Item> processedItems = new ArrayList<>();
    final List<File> filesToRefresh = new ArrayList<>();
    final List<File> dirsToRefresh = new ArrayList<>();
    instance.processItems(target, toProcess, obsoleteItems, new GenericCompilerInstance.OutputConsumer<Item>() {

        @Override
        public void addFileToRefresh(@NotNull File file) {
            filesToRefresh.add(file);
        }

        @Override
        public void addDirectoryToRefresh(@NotNull File dir) {
            dirsToRefresh.add(dir);
        }

        @Override
        public void addProcessedItem(@NotNull Item sourceItem) {
            processedItems.add(sourceItem);
        }
    });
    checkForErrorsOrCanceled();
    CompilerUtil.runInContext(myContext, CompilerBundle.message("progress.updating.caches"), () -> {
        for (Key key : toRemove) {
            cache.remove(targetId, key);
        }
        CompilerUtil.refreshIOFiles(filesToRefresh);
        CompilerUtil.refreshIODirectories(dirsToRefresh);
        if (LOG.isDebugEnabled()) {
            LOG.debug("refreshed " + filesToRefresh.size() + " files and " + dirsToRefresh.size() + " dirs");
            for (int i = 0; i < getItemsCountToShowInLog(filesToRefresh.size()); i++) {
                LOG.debug("file: " + filesToRefresh.get(i));
            }
            for (int i = 0; i < getItemsCountToShowInLog(dirsToRefresh.size()); i++) {
                LOG.debug("dir: " + dirsToRefresh.get(i));
            }
        }
        final RunResult runResult = new ReadAction() {

            protected void run(@NotNull final Result result) throws Throwable {
                for (Item item : processedItems) {
                    SourceState sourceState = sourceStates.get(item);
                    if (sourceState == null) {
                        sourceState = item.computeSourceState();
                    }
                    cache.putState(targetId, item.getKey(), sourceState, item.computeOutputState());
                }
            }
        }.executeSilently();
        Throwable throwable = runResult.getThrowable();
        if (throwable instanceof IOException)
            throw (IOException) throwable;
        else if (throwable != null)
            throw new RuntimeException(throwable);
    });
    return true;
}
Also used : GenericCompilerCache(com.intellij.compiler.impl.generic.GenericCompilerCache) Result(com.intellij.openapi.application.Result) RunResult(com.intellij.openapi.application.RunResult) ReadAction(com.intellij.openapi.application.ReadAction) RunResult(com.intellij.openapi.application.RunResult) IOException(java.io.IOException) THashSet(gnu.trove.THashSet) File(java.io.File)

Aggregations

GenericCompilerCache (com.intellij.compiler.impl.generic.GenericCompilerCache)2 ReadAction (com.intellij.openapi.application.ReadAction)2 Result (com.intellij.openapi.application.Result)2 RunResult (com.intellij.openapi.application.RunResult)2 THashSet (gnu.trove.THashSet)2 GenericCompilerPersistentData (com.intellij.compiler.impl.generic.GenericCompilerPersistentData)1 CommonProcessors (com.intellij.util.CommonProcessors)1 File (java.io.File)1 IOException (java.io.IOException)1 NotNull (org.jetbrains.annotations.NotNull)1