Search in sources :

Example 6 with ReadAction

use of com.intellij.openapi.application.ReadAction 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)

Example 7 with ReadAction

use of com.intellij.openapi.application.ReadAction in project intellij-community by JetBrains.

the class PackageFileWorker method startPackagingFiles.

public static ActionCallback startPackagingFiles(final Project project, final List<VirtualFile> files, final Artifact[] artifacts, final boolean packIntoArchives) {
    final ActionCallback callback = new ActionCallback();
    ProgressManager.getInstance().run(new Task.Backgroundable(project, "Packaging Files") {

        @Override
        public void run(@NotNull ProgressIndicator indicator) {
            try {
                for (final VirtualFile file : files) {
                    indicator.checkCanceled();
                    new ReadAction() {

                        @Override
                        protected void run(@NotNull final Result result) {
                            try {
                                packageFile(file, project, artifacts, packIntoArchives);
                            } catch (IOException e) {
                                String message = CompilerBundle.message("message.tect.package.file.io.error", e.toString());
                                Notifications.Bus.notify(new Notification("Package File", "Cannot package file", message, NotificationType.ERROR));
                            }
                        }
                    }.execute();
                    callback.setDone();
                }
            } finally {
                if (!callback.isDone()) {
                    callback.setRejected();
                }
            }
        }
    });
    return callback;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Task(com.intellij.openapi.progress.Task) ActionCallback(com.intellij.openapi.util.ActionCallback) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) ReadAction(com.intellij.openapi.application.ReadAction) IOException(java.io.IOException) NotNull(org.jetbrains.annotations.NotNull) Notification(com.intellij.notification.Notification) Result(com.intellij.openapi.application.Result)

Example 8 with ReadAction

use of com.intellij.openapi.application.ReadAction in project android by JetBrains.

the class GradleSyncTest method shouldUseLibrary.

@Test
public void shouldUseLibrary() throws IOException {
    guiTest.importSimpleApplication();
    IdeFrameFixture ideFrame = guiTest.ideFrame();
    Project project = ideFrame.getProject();
    // Make sure the library was added.
    LibraryTable libraryTable = ProjectLibraryTable.getInstance(project);
    String libraryName = "org.apache.http.legacy-" + TestUtils.getLatestAndroidPlatform();
    Library library = libraryTable.getLibraryByName(libraryName);
    // Verify that the library has the right j
    VirtualFile[] jarFiles = library.getFiles(CLASSES);
    assertThat(jarFiles).asList().hasSize(1);
    VirtualFile jarFile = jarFiles[0];
    assertEquals("org.apache.http.legacy.jar", jarFile.getName());
    // Verify that the module depends on the library
    Module appModule = ideFrame.getModule("app");
    AtomicBoolean dependencyFound = new AtomicBoolean();
    new ReadAction() {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            ModifiableRootModel modifiableModel = ModuleRootManager.getInstance(appModule).getModifiableModel();
            try {
                for (OrderEntry orderEntry : modifiableModel.getOrderEntries()) {
                    if (orderEntry instanceof LibraryOrderEntry) {
                        LibraryOrderEntry libraryDependency = (LibraryOrderEntry) orderEntry;
                        if (libraryDependency.getLibrary() == library) {
                            dependencyFound.set(true);
                        }
                    }
                }
            } finally {
                modifiableModel.dispose();
            }
        }
    }.execute();
    assertTrue("Module app should depend on library '" + library.getName() + "'", dependencyFound.get());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Result(com.intellij.openapi.application.Result) Project(com.intellij.openapi.project.Project) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ProjectLibraryTable(com.intellij.openapi.roots.impl.libraries.ProjectLibraryTable) LibraryTable(com.intellij.openapi.roots.libraries.LibraryTable) ReadAction(com.intellij.openapi.application.ReadAction) Library(com.intellij.openapi.roots.libraries.Library) Module(com.intellij.openapi.module.Module) Test(org.junit.Test)

Example 9 with ReadAction

use of com.intellij.openapi.application.ReadAction in project android by JetBrains.

the class IdeFrameFixture method parseBuildFileForModule.

@NotNull
public GradleBuildModelFixture parseBuildFileForModule(@NotNull String moduleName) {
    Module module = getModule(moduleName);
    VirtualFile buildFile = getGradleBuildFile(module);
    Ref<GradleBuildModel> buildModelRef = new Ref<>();
    new ReadAction() {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            buildModelRef.set(GradleBuildModel.parseBuildFile(buildFile, getProject()));
        }
    }.execute();
    return new GradleBuildModelFixture(buildModelRef.get());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Ref(com.intellij.openapi.util.Ref) GradleBuildModel(com.android.tools.idea.gradle.dsl.model.GradleBuildModel) ReadAction(com.intellij.openapi.application.ReadAction) Module(com.intellij.openapi.module.Module) Result(com.intellij.openapi.application.Result) GradleInvocationResult(com.android.tools.idea.gradle.project.build.invoker.GradleInvocationResult) GradleBuildModelFixture(com.android.tools.idea.tests.gui.framework.fixture.gradle.GradleBuildModelFixture) Assert.assertNotNull(junit.framework.Assert.assertNotNull) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with ReadAction

use of com.intellij.openapi.application.ReadAction in project intellij-community by JetBrains.

the class ArtifactBuildTargetScopeProvider method getBuildTargetScopes.

@NotNull
@Override
public List<TargetTypeBuildScope> getBuildTargetScopes(@NotNull final CompileScope baseScope, @NotNull CompilerFilter filter, @NotNull final Project project, final boolean forceBuild) {
    final ArtifactsCompiler compiler = ArtifactsCompiler.getInstance(project);
    if (compiler == null || !filter.acceptCompiler(compiler)) {
        return Collections.emptyList();
    }
    final List<TargetTypeBuildScope> scopes = new ArrayList<>();
    new ReadAction() {

        protected void run(@NotNull final Result result) {
            final Set<Artifact> artifacts = ArtifactCompileScope.getArtifactsToBuild(project, baseScope, false);
            if (ArtifactCompileScope.getArtifacts(baseScope) == null) {
                Set<Module> modules = ArtifactUtil.getModulesIncludedInArtifacts(artifacts, project);
                CompileScopeUtil.addScopesForModules(modules, scopes, forceBuild);
            }
            if (!artifacts.isEmpty()) {
                TargetTypeBuildScope.Builder builder = TargetTypeBuildScope.newBuilder().setTypeId(ArtifactBuildTargetType.INSTANCE.getTypeId()).setForceBuild(forceBuild || ArtifactCompileScope.isArtifactRebuildForced(baseScope));
                for (Artifact artifact : artifacts) {
                    builder.addTargetId(artifact.getName());
                }
                scopes.add(builder.build());
            }
        }
    }.execute();
    return scopes;
}
Also used : Set(java.util.Set) ReadAction(com.intellij.openapi.application.ReadAction) ArrayList(java.util.ArrayList) TargetTypeBuildScope(org.jetbrains.jps.api.CmdlineRemoteProto.Message.ControllerMessage.ParametersMessage.TargetTypeBuildScope) Artifact(com.intellij.packaging.artifacts.Artifact) Result(com.intellij.openapi.application.Result) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ReadAction (com.intellij.openapi.application.ReadAction)14 Result (com.intellij.openapi.application.Result)14 VirtualFile (com.intellij.openapi.vfs.VirtualFile)7 NotNull (org.jetbrains.annotations.NotNull)7 Module (com.intellij.openapi.module.Module)3 GenericCompilerCache (com.intellij.compiler.impl.generic.GenericCompilerCache)2 Notification (com.intellij.notification.Notification)2 RunResult (com.intellij.openapi.application.RunResult)2 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)2 Project (com.intellij.openapi.project.Project)2 Artifact (com.intellij.packaging.artifacts.Artifact)2 THashSet (gnu.trove.THashSet)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 GradleBuildModel (com.android.tools.idea.gradle.dsl.model.GradleBuildModel)1 GradleInvocationResult (com.android.tools.idea.gradle.project.build.invoker.GradleInvocationResult)1 GradleBuildModelFixture (com.android.tools.idea.tests.gui.framework.fixture.gradle.GradleBuildModelFixture)1 GenericCompilerPersistentData (com.intellij.compiler.impl.generic.GenericCompilerPersistentData)1 NotificationListener (com.intellij.notification.NotificationListener)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1