Java源码示例:com.intellij.openapi.compiler.CompileScope

示例1
/**
 * Compiles the project and runs the task only if there are no compilation errors.
 */
private static void runAfterCompilationCheck(ProjectInfo projectInfo, Task task) {
    ApplicationManager.getApplication().invokeLater(() -> {
        List<PsiClass> classes = projectInfo.getClasses();

        if (!classes.isEmpty()) {
            VirtualFile[] virtualFiles = classes.stream()
                    .map(classObject -> classObject.getContainingFile().getVirtualFile()).toArray(VirtualFile[]::new);
            Project project = projectInfo.getProject();

            CompilerManager compilerManager = CompilerManager.getInstance(project);
            CompileStatusNotification callback = (aborted, errors, warnings, compileContext) -> {
                if (errors == 0 && !aborted) {
                    ProgressManager.getInstance().run(task);
                } else {
                    task.onCancel();
                    AbstractRefactoringPanel.showCompilationErrorNotification(project);
                }
            };
            CompileScope compileScope = compilerManager.createFilesCompileScope(virtualFiles);
            compilerManager.make(compileScope, callback);
        } else {
            ProgressManager.getInstance().run(task);
        }
    });
}
 
示例2
public void actionPerformed(final AnActionEvent e) {
    final CreateIpaDialog dialog = new CreateIpaDialog(e.getProject());
    dialog.show();
    if(dialog.getExitCode() == DialogWrapper.OK_EXIT_CODE) {
        // create IPA
        IpaConfig ipaConfig = dialog.getIpaConfig();
        CompileScope scope = CompilerManager.getInstance(e.getProject()).createModuleCompileScope(ipaConfig.module, true);
        scope.putUserData(IPA_CONFIG_KEY, ipaConfig);
        CompilerManager.getInstance(e.getProject()).compile(scope, new CompileStatusNotification() {
            @Override
            public void finished(boolean aborted, int errors, int warnings, CompileContext compileContext) {
                RoboVmPlugin.logInfo(e.getProject(), "IPA creation complete, %d errors, %d warnings", errors, warnings);
            }
        });
    }
}
 
示例3
private void addScope(CompileScope scope) {
  if (scope instanceof CompositeScope) {
    final CompositeScope compositeScope = (CompositeScope)scope;
    for (CompileScope childScope : compositeScope.myScopes) {
      addScope(childScope);
    }
  }
  else {
    myScopes.add(scope);
  }

  Map<Key, Object> map = scope.exportUserData();
  for (Map.Entry<Key, Object> entry : map.entrySet()) {
    putUserData(entry.getKey(), entry.getValue());
  }
}
 
示例4
private void compile(@NotNull CompileScope scope) {
  try {
    CompilerTester tester = new CompilerTester(myProject, Arrays.asList(scope.getAffectedModules()), null);
    try {
      List<CompilerMessage> messages = tester.make(scope);
      for (CompilerMessage message : messages) {
        switch (message.getCategory()) {
          case ERROR:
            fail("Compilation failed with error: " + message.getMessage());
            break;
          case WARNING:
            System.out.println("Compilation warning: " + message.getMessage());
            break;
          case INFORMATION:
            break;
          case STATISTICS:
            break;
        }
      }
    }
    finally {
      tester.tearDown();
    }
  }
  catch (Exception e) {
    ExceptionUtilRt.rethrow(e);
  }
}
 
示例5
private CompileScope createModulesCompileScope(final String[] moduleNames) {
  final List<Module> modules = new ArrayList<>();
  for (String name : moduleNames) {
    modules.add(getModule(name));
  }
  return new ModuleCompileScope(myProject, modules.toArray(Module.EMPTY_ARRAY), false);
}
 
示例6
private CompileScope createArtifactsScope(String[] artifactNames) {
  List<Artifact> artifacts = new ArrayList<>();
  for (String name : artifactNames) {
    artifacts.add(ArtifactsTestUtil.findArtifact(myProject, name));
  }
  return ArtifactCompileScope.createArtifactsScope(myProject, artifacts);
}
 
示例7
private static void compileIfChanged(VirtualFileEvent event, final Project project) {
    if(!RoboVmGlobalConfig.isCompileOnSave()) {
        return;
    }
    VirtualFile file = event.getFile();
    Module module = null;
    for(Module m: ModuleManager.getInstance(project).getModules()) {
        if(ModuleRootManager.getInstance(m).getFileIndex().isInContent(file)) {
            module = m;
            break;
        }
    }

    if(module != null) {
        if(isRoboVmModule(module)) {
            final Module foundModule = module;
            OrderEntry orderEntry = ModuleRootManager.getInstance(module).getFileIndex().getOrderEntryForFile(file);
            if(orderEntry != null && orderEntry.getFiles(OrderRootType.SOURCES).length != 0) {
                ApplicationManager.getApplication().invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        if(!CompilerManager.getInstance(project).isCompilationActive()) {
                            CompileScope scope = CompilerManager.getInstance(project).createModuleCompileScope(foundModule, true);
                            CompilerManager.getInstance(project).compile(scope, null);
                        }
                    }
                });
            }
        }
    }
}
 
示例8
public static CompileScope createScopeWithArtifacts(final CompileScope baseScope,
                                                    @Nonnull Collection<Artifact> artifacts,
                                                    boolean useCustomContentId,
                                                    final boolean forceArtifactBuild) {
  baseScope.putUserData(ARTIFACTS_KEY, artifacts.toArray(new Artifact[artifacts.size()]));
  if (useCustomContentId) {
    baseScope.putUserData(CompilerManager.CONTENT_ID_KEY, ARTIFACTS_CONTENT_ID_KEY);
  }
  if (forceArtifactBuild) {
    baseScope.putUserData(FORCE_ARTIFACT_BUILD, Boolean.TRUE);
  }
  return baseScope;
}
 
示例9
public static Set<Artifact> getArtifactsToBuild(final Project project,
                                                final CompileScope compileScope,
                                                final boolean addIncludedArtifactsWithOutputPathsOnly) {
  final Artifact[] artifactsFromScope = getArtifacts(compileScope);
  final ArtifactManager artifactManager = ArtifactManager.getInstance(project);
  PackagingElementResolvingContext context = artifactManager.getResolvingContext();
  if (artifactsFromScope != null) {
    return addIncludedArtifacts(Arrays.asList(artifactsFromScope), context, addIncludedArtifactsWithOutputPathsOnly);
  }

  final Set<Artifact> cached = compileScope.getUserData(CACHED_ARTIFACTS_KEY);
  if (cached != null) {
    return cached;
  }

  Set<Artifact> artifacts = new HashSet<Artifact>();
  final Set<Module> modules = new HashSet<Module>(Arrays.asList(compileScope.getAffectedModules()));
  final List<Module> allModules = Arrays.asList(ModuleManager.getInstance(project).getModules());
  for (Artifact artifact : artifactManager.getArtifacts()) {
    if (artifact.isBuildOnMake()) {
      if (modules.containsAll(allModules)
          || containsModuleOutput(artifact, modules, context)) {
        artifacts.add(artifact);
      }
    }
  }
  Set<Artifact> result = addIncludedArtifacts(artifacts, context, addIncludedArtifactsWithOutputPathsOnly);
  compileScope.putUserData(CACHED_ARTIFACTS_KEY, result);
  return result;
}
 
示例10
@Nonnull
public VirtualFile[] getFiles(FileType fileType, boolean inSourceOnly) {
  Set<VirtualFile> allFiles = new THashSet<VirtualFile>();
  for (CompileScope scope : myScopes) {
    final VirtualFile[] files = scope.getFiles(fileType, inSourceOnly);
    if (files.length > 0) {
      ContainerUtil.addAll(allFiles, files);
    }
  }
  return VfsUtil.toVirtualFileArray(allFiles);
}
 
示例11
public boolean belongs(String url) {
  for (CompileScope scope : myScopes) {
    if (scope.belongs(url)) {
      return true;
    }
  }
  return false;
}
 
示例12
@Nonnull
public Module[] getAffectedModules() {
  Set<Module> modules = new HashSet<Module>();
  for (final CompileScope compileScope : myScopes) {
    ContainerUtil.addAll(modules, compileScope.getAffectedModules());
  }
  return modules.toArray(new Module[modules.size()]);
}
 
示例13
public <T> T getUserData(@Nonnull Key<T> key) {
  for (CompileScope compileScope : myScopes) {
    T userData = compileScope.getUserData(key);
    if (userData != null) {
      return userData;
    }
  }
  return super.getUserData(key);
}
 
示例14
private static void doBuild(@Nonnull Project project, final @Nonnull List<ArtifactPopupItem> items, boolean rebuild) {
  final Set<Artifact> artifacts = getArtifacts(items, project);
  final CompileScope scope = ArtifactCompileScope.createArtifactsScope(project, artifacts, rebuild);

  ArtifactsWorkspaceSettings.getInstance(project).setArtifactsToBuild(artifacts);
  if (!rebuild) {
    //in external build we can set 'rebuild' flag per target type
    CompilerManager.getInstance(project).make(scope, null);
  }
  else {
    CompilerManager.getInstance(project).compile(scope, null);
  }
}
 
示例15
@Override
public boolean validateConfiguration(CompileScope scope) {
  return true;
}
 
示例16
public static CompileScope createArtifactsScope(@Nonnull Project project,
                                                @Nonnull Collection<Artifact> artifacts) {
  return createArtifactsScope(project, artifacts, false);
}
 
示例17
public static CompileScope createArtifactsScope(@Nonnull Project project,
                                                @Nonnull Collection<Artifact> artifacts,
                                                final boolean forceArtifactBuild) {
  return createScopeWithArtifacts(createScopeForModulesInArtifacts(project, artifacts), artifacts, true, forceArtifactBuild);
}
 
示例18
public static CompileScope createScopeWithArtifacts(final CompileScope baseScope,
                                                    @Nonnull Collection<Artifact> artifacts,
                                                    boolean useCustomContentId) {
  return createScopeWithArtifacts(baseScope, artifacts, useCustomContentId, false);
}
 
示例19
@Nullable
public static Artifact[] getArtifacts(CompileScope compileScope) {
  return compileScope.getUserData(ARTIFACTS_KEY);
}
 
示例20
public static boolean isArtifactRebuildForced(@Nonnull CompileScope scope) {
  return Boolean.TRUE.equals(scope.getUserData(FORCE_ARTIFACT_BUILD));
}
 
示例21
public CompositeScope(CompileScope scope1, CompileScope scope2) {
  addScope(scope1);
  addScope(scope2);
}
 
示例22
public CompositeScope(CompileScope[] scopes) {
  for (CompileScope scope : scopes) {
    addScope(scope);
  }
}
 
示例23
public CompositeScope(Collection<? extends CompileScope> scopes) {
  for (CompileScope scope : scopes) {
    addScope(scope);
  }
}
 
示例24
public Collection<CompileScope> getScopes() {
  return Collections.unmodifiableList(myScopes);
}
 
示例25
@javax.annotation.Nullable
public abstract CompileScope getAdditionalScope(@Nonnull CompileScope baseScope,
                                                @Nonnull Condition<com.intellij.openapi.compiler.Compiler> filter,
                                                @Nonnull Project project);
 
示例26
@Override
public void addScope(final CompileScope additionalScope) {
  myDelegate.addScope(additionalScope);
}
 
示例27
@Override
public CompileScope getCompileScope() {
  return myDelegate.getCompileScope();
}
 
示例28
static AsyncResult<Void> doMake(UIAccess uiAccess, final Project myProject, final RunConfiguration configuration, final boolean ignoreErrors) {
  if (!(configuration instanceof RunProfileWithCompileBeforeLaunchOption)) {
    return AsyncResult.rejected();
  }

  if (configuration instanceof RunConfigurationBase && ((RunConfigurationBase)configuration).excludeCompileBeforeLaunchOption()) {
    return AsyncResult.resolved();
  }

  final RunProfileWithCompileBeforeLaunchOption runConfiguration = (RunProfileWithCompileBeforeLaunchOption)configuration;
  AsyncResult<Void> result = AsyncResult.undefined();
  try {
    final CompileStatusNotification callback = (aborted, errors, warnings, compileContext) -> {
      if ((errors == 0 || ignoreErrors) && !aborted) {
        result.setDone();
      }
      else {
        result.setRejected();
      }
    };

    TransactionGuard.submitTransaction(myProject, () -> {
      CompileScope scope;
      final CompilerManager compilerManager = CompilerManager.getInstance(myProject);
      if (Comparing.equal(Boolean.TRUE.toString(), System.getProperty(MAKE_PROJECT_ON_RUN_KEY))) {
        // user explicitly requested whole-project make
        scope = compilerManager.createProjectCompileScope();
      }
      else {
        final Module[] modules = runConfiguration.getModules();
        if (modules.length > 0) {
          for (Module module : modules) {
            if (module == null) {
              LOG.error("RunConfiguration should not return null modules. Configuration=" + runConfiguration.getName() + "; class=" + runConfiguration.getClass().getName());
            }
          }
          scope = compilerManager.createModulesCompileScope(modules, true);
        }
        else {
          scope = compilerManager.createProjectCompileScope();
        }
      }

      if (!myProject.isDisposed()) {
        compilerManager.make(scope, callback);
      }
      else {
        result.setRejected();
      }
    });
  }
  catch (Exception e) {
    result.rejectWithThrowable(e);
  }

  return result;
}
 
示例29
@Override
public boolean validateConfiguration(CompileScope scope) {
  return true;
}
 
示例30
void addScope(CompileScope additionalScope);