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;
  }
 public static CompileScope createScopeWithArtifacts(
     final CompileScope baseScope,
     @NotNull Collection<Artifact> artifacts,
     boolean useCustomContentId) {
   baseScope.putUserData(ARTIFACTS_KEY, artifacts.toArray(new Artifact[artifacts.size()]));
   if (useCustomContentId) {
     baseScope.putUserData(CompilerManager.CONTENT_ID_KEY, ARTIFACTS_CONTENT_ID_KEY);
   }
   return baseScope;
 }
  private boolean cleanupChildrenRecursively(
      @NotNull final Object fromElement,
      final @Nullable CompileScope scope,
      @NotNull UUID currentSessionId) {
    final ErrorViewStructure structure = myPanel.getErrorViewStructure();
    ErrorTreeElement[] elements = structure.getChildElements(fromElement);
    if (elements.length == 0) return true;

    boolean result = false;
    for (ErrorTreeElement element : elements) {
      if (element instanceof GroupingElement) {
        if (scope != null) {
          final VirtualFile file = ((GroupingElement) element).getFile();
          if (file != null && !scope.belongs(file.getUrl())) {
            continue;
          }
        }
        if (!currentSessionId.equals(element.getData())) {
          structure.removeElement(element);
          result = true;
        } else {
          result |= cleanupChildrenRecursively(element, scope, currentSessionId);
        }
      } else {
        if (!currentSessionId.equals(element.getData())) {
          structure.removeElement(element);
          result = true;
        }
      }
    }
    return result;
  }
 private boolean needTransformCopying(CompileScope compileScope) {
   final CompilerConfiguration configuration = CompilerConfiguration.getInstance(myProject);
   final ProjectFileIndex index = ProjectRootManager.getInstance(myProject).getFileIndex();
   for (VirtualFile file :
       FilenameIndex.getVirtualFilesByName(
           myProject, AST_TRANSFORM_FILE_NAME, GlobalSearchScope.projectScope(myProject))) {
     if (compileScope.belongs(file.getUrl())
         && index.isInSource(file)
         && !configuration.isResourceFile(file)) {
       return true;
     }
   }
   return false;
 }
  public boolean validateConfiguration(CompileScope compileScope) {
    VirtualFile[] files = compileScope.getFiles(GroovyFileType.GROOVY_FILE_TYPE, true);
    if (files.length == 0) return true;

    final Set<String> scriptExtensions = GroovyFileTypeLoader.getCustomGroovyScriptExtensions();

    final CompilerManager compilerManager = CompilerManager.getInstance(myProject);
    Set<Module> modules = new HashSet<Module>();
    for (VirtualFile file : files) {
      if (scriptExtensions.contains(file.getExtension())
          || compilerManager.isExcludedFromCompilation(file)
          || CompilerConfiguration.getInstance(myProject).isResourceFile(file)) {
        continue;
      }

      ProjectRootManager rootManager = ProjectRootManager.getInstance(myProject);
      Module module = rootManager.getFileIndex().getModuleForFile(file);
      if (module != null) {
        modules.add(module);
      }
    }

    Set<Module> nojdkModules = new HashSet<Module>();
    for (Module module : modules) {
      if (!GroovyUtils.isAcceptableModuleType(ModuleType.get(module))) continue;
      final Sdk sdk = ModuleRootManager.getInstance(module).getSdk();
      if (sdk == null || !(sdk.getSdkType() instanceof JavaSdkType)) {
        nojdkModules.add(module);
        continue;
      }

      if (!LibrariesUtil.hasGroovySdk(module)) {
        if (!GroovyConfigUtils.getInstance().tryToSetUpGroovyFacetOntheFly(module)) {
          Messages.showErrorDialog(
              myProject,
              GroovyBundle.message("cannot.compile.groovy.files.no.facet", module.getName()),
              GroovyBundle.message("cannot.compile"));
          ModulesConfigurator.showDialog(
              module.getProject(), module.getName(), ClasspathEditor.NAME);
          return false;
        }
      }
    }

    if (!nojdkModules.isEmpty()) {
      final Module[] noJdkArray = nojdkModules.toArray(new Module[nojdkModules.size()]);
      if (noJdkArray.length == 1) {
        Messages.showErrorDialog(
            myProject,
            GroovyBundle.message("cannot.compile.groovy.files.no.sdk", noJdkArray[0].getName()),
            GroovyBundle.message("cannot.compile"));
      } else {
        StringBuilder modulesList = new StringBuilder();
        for (int i = 0; i < noJdkArray.length; i++) {
          if (i > 0) modulesList.append(", ");
          modulesList.append(noJdkArray[i].getName());
        }
        Messages.showErrorDialog(
            myProject,
            GroovyBundle.message("cannot.compile.groovy.files.no.sdk.mult", modulesList.toString()),
            GroovyBundle.message("cannot.compile"));
      }
      return false;
    }

    final GroovyCompilerConfiguration configuration =
        GroovyCompilerConfiguration.getInstance(myProject);
    if (!configuration.transformsOk && needTransformCopying(compileScope)) {
      final int result =
          Messages.showYesNoDialog(
              myProject,
              "You seem to have global Groovy AST transformations defined in your project,\n"
                  + "but they won't be applied to your code because they are not marked as compiler resources.\n"
                  + "Do you want to add them to compiler resource list?\n"
                  + "(you can do it yourself later in Settings | Compiler | Resource patterns)",
              "AST Transformations found",
              GroovyIcons.GROOVY_ICON_32x32);
      if (result == 0) {
        CompilerConfiguration.getInstance(myProject)
            .addResourceFilePattern(AST_TRANSFORM_FILE_NAME);
      } else {
        configuration.transformsOk = true;
      }
    }

    return true;
  }
 @Nullable
 public static Artifact[] getArtifacts(CompileScope compileScope) {
   return compileScope.getUserData(ARTIFACTS_KEY);
 }
  @Override
  protected void commitForNext() throws CommitStepException {
    final String apkPath = myApkPathField.getText().trim();
    if (apkPath.length() == 0) {
      throw new CommitStepException(
          AndroidBundle.message("android.extract.package.specify.apk.path.error"));
    }

    AndroidFacet facet = myWizard.getFacet();
    PropertiesComponent properties = PropertiesComponent.getInstance(myWizard.getProject());
    properties.setValue(
        ChooseModuleStep.MODULE_PROPERTY, facet != null ? facet.getModule().getName() : "");
    properties.setValue(APK_PATH_PROPERTY, apkPath);

    File folder = new File(apkPath).getParentFile();
    if (folder == null) {
      throw new CommitStepException(
          AndroidBundle.message("android.cannot.create.file.error", apkPath));
    }
    try {
      if (!folder.exists()) {
        folder.mkdirs();
      }
    } catch (Exception e) {
      throw new CommitStepException(e.getMessage());
    }

    final CompilerManager manager = CompilerManager.getInstance(myWizard.getProject());
    final CompileScope compileScope = manager.createModuleCompileScope(facet.getModule(), true);
    AndroidCompileUtil.setReleaseBuild(compileScope);

    properties.setValue(RUN_PROGUARD_PROPERTY, Boolean.toString(myProguardCheckBox.isSelected()));

    if (myProguardCheckBox.isSelected()) {
      final String proguardCfgPath = myProguardConfigFilePathField.getText().trim();
      if (proguardCfgPath.length() == 0) {
        throw new CommitStepException(
            AndroidBundle.message("android.extract.package.specify.proguard.cfg.path.error"));
      }
      properties.setValue(PROGUARD_CFG_PATH_PROPERTY, proguardCfgPath);

      if (!new File(proguardCfgPath).isFile()) {
        throw new CommitStepException("Cannot find file " + proguardCfgPath);
      }

      compileScope.putUserData(AndroidProguardCompiler.PROGUARD_CFG_PATH_KEY, proguardCfgPath);
    }

    manager.make(
        compileScope,
        new CompileStatusNotification() {
          public void finished(
              boolean aborted, int errors, int warnings, CompileContext compileContext) {
            if (aborted || errors != 0) {
              return;
            }

            final String title = AndroidBundle.message("android.extract.package.task.title");
            ProgressManager.getInstance()
                .run(
                    new Task.Backgroundable(myWizard.getProject(), title, true, null) {
                      public void run(@NotNull ProgressIndicator indicator) {
                        createAndAlignApk(apkPath);
                      }
                    });
          }
        });
  }
 public static void setReleaseBuild(@NotNull CompileScope compileScope) {
   compileScope.putUserData(RELEASE_BUILD_KEY, Boolean.TRUE);
 }