package com.intellij.packaging.impl.compiler;

import com.intellij.compiler.impl.ModuleCompileScope;
import com.intellij.compiler.impl.ProjectCompileScope;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ui.configuration.artifacts.ArtifactUtil;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.compiler.CompileScope;
import com.intellij.packaging.artifacts.Artifact;
import com.intellij.packaging.artifacts.ArtifactManager;
import com.intellij.packaging.elements.PackagingElementResolvingContext;
import com.intellij.packaging.impl.elements.ModuleOutputElementType;
import com.intellij.packaging.impl.elements.ModuleOutputPackagingElement;
import com.intellij.util.Processor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.HashSet;
import java.util.Set;
import java.util.Collection;
import java.util.Arrays;

/**
 * @author nik
 */
public class ArtifactCompileScope {
  private static final Key<Artifact[]> ARTIFACTS_KEY = Key.create("artifacts");

  private ArtifactCompileScope() {
  }

  @NotNull
  public static ModuleCompileScope create(@NotNull Project project, @NotNull Artifact artifact) {
    final Set<Module> modules = new HashSet<Module>();
    final PackagingElementResolvingContext context = ArtifactManager.getInstance(project).getResolvingContext();
    ArtifactUtil.processPackagingElements(artifact, ModuleOutputElementType.MODULE_OUTPUT_ELEMENT_TYPE, new Processor<ModuleOutputPackagingElement>() {
      public boolean process(ModuleOutputPackagingElement moduleOutputPackagingElement) {
        final Module module = moduleOutputPackagingElement.findModule(context);
        if (module != null) {
          modules.add(module);
        }
        return true;
      }
    }, context, true);

    final ModuleCompileScope scope = new ModuleCompileScope(project, modules.toArray(new Module[modules.size()]), true);
    scope.putUserData(ARTIFACTS_KEY, new Artifact[]{artifact});
    return scope;
  }

  public static ProjectCompileScope create(@NotNull Project project, @NotNull Collection<Artifact> artifacts) {
    final ProjectCompileScope scope = new ProjectCompileScope(project);
    scope.putUserData(ARTIFACTS_KEY, artifacts.toArray(new Artifact[artifacts.size()]));
    return scope;
  }

  @Nullable
  public static Artifact[] getArtifacts(@NotNull CompileScope compileScope) {
    return compileScope.getUserData(ARTIFACTS_KEY);
  }

  public static Set<Artifact> getArtifactsToBuild(final Project project, final CompileScope compileScope) {
    final Artifact[] artifactsFromScope = getArtifacts(compileScope);
    if (artifactsFromScope != null) {
      return new HashSet<Artifact>(Arrays.asList(artifactsFromScope));
    }
    Set<Artifact> artifacts = new HashSet<Artifact>();
    for (Artifact artifact : ArtifactManager.getInstance(project).getArtifacts()) {
      if (artifact.isBuildOnMake()) {
        artifacts.add(artifact);
      }
    }
    return artifacts;
  }
}

@@ -2076,23 +2076,16 @@
  static 
-Project
 CompileS
@@ -2169,21 +2169,33 @@
 ) %7B%0A    
-final
+return create(new
  Project
@@ -2210,56 +2210,154 @@
 cope
- scope = new ProjectCompileScope(project);%0A    s
+(project), artifacts);%0A  %7D%0A%0A  public static CompileScope create(final CompileScope baseScope, @NotNull Collection%3CArtifact%3E artifacts) %7B%0A    baseS
 cope
@@ -2439,33 +2439,37 @@
 %5D));%0A    return 
-s
+baseS
 cope;%0A  %7D%0A%0A  @Nu

build on frame deactivation for artifacts