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;
}
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;
}
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());
}
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());
}
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;
}
Aggregations