/*
 * Copyright 2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.gradle.language.base.plugins;

import org.gradle.api.Incubating;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.plugins.ExtensionContainer;
import org.gradle.api.tasks.TaskContainer;
import org.gradle.internal.reflect.Instantiator;
import org.gradle.language.base.ProjectSourceSet;
import org.gradle.language.base.internal.DefaultProjectSourceSet;
import org.gradle.model.Model;
import org.gradle.model.Mutate;
import org.gradle.model.RuleSource;
import org.gradle.model.collection.internal.PolymorphicDomainObjectContainerModelProjection;
import org.gradle.model.internal.core.ModelPath;
import org.gradle.model.internal.registry.ModelRegistry;
import org.gradle.model.internal.type.ModelType;
import org.gradle.platform.base.BinaryContainer;
import org.gradle.platform.base.BinarySpec;
import org.gradle.platform.base.internal.BinarySpecInternal;
import org.gradle.platform.base.internal.DefaultBinaryContainer;

import javax.inject.Inject;
import java.util.Collections;

/**
 * Base plugin for language support.
 *
 * Adds a {@link org.gradle.platform.base.BinaryContainer} named {@code binaries} to the project.
 * Adds a {@link org.gradle.language.base.ProjectSourceSet} named {@code sources} to the project.
 *
 * For each binary instance added to the binaries container, registers a lifecycle task to create that binary.
 */
@Incubating
public class LanguageBasePlugin implements Plugin<Project> {

    private final Instantiator instantiator;
    private final ModelRegistry modelRegistry;

    @Inject
    public LanguageBasePlugin(Instantiator instantiator, ModelRegistry modelRegistry) {
        this.instantiator = instantiator;
        this.modelRegistry = modelRegistry;
    }

    public void apply(final Project target) {
        target.apply(Collections.singletonMap("plugin", LifecycleBasePlugin.class));

        target.getExtensions().create("sources", DefaultProjectSourceSet.class);

        DefaultBinaryContainer binaries = target.getExtensions().create("binaries", DefaultBinaryContainer.class, instantiator);
        modelRegistry.create(
                PolymorphicDomainObjectContainerModelProjection.bridgeNamedDomainObjectCollection(
                        ModelType.of(DefaultBinaryContainer.class),
                        ModelType.of(DefaultBinaryContainer.class),
                        ModelType.of(BinarySpec.class),
                        ModelPath.path("binaries"),
                        binaries,
                        getClass().getSimpleName() + ".apply()"));
    }

    /**
     * Model rules.
     */
    @SuppressWarnings("UnusedDeclaration")
    @RuleSource
    static class Rules {
        @Model
        ProjectSourceSet sources(ExtensionContainer extensions) {
            return extensions.getByType(ProjectSourceSet.class);
        }

        @Mutate
        void createLifecycleTaskForBinary(TaskContainer tasks, BinaryContainer binaries) {
            Task assembleTask = tasks.getByName(LifecycleBasePlugin.ASSEMBLE_TASK_NAME);
            for (BinarySpecInternal binary : binaries.withType(BinarySpecInternal.class)) {
                if (!binary.isLegacyBinary()) {
                    Task binaryLifecycleTask = tasks.create(binary.getName());
                    binaryLifecycleTask.setGroup(LifecycleBasePlugin.BUILD_GROUP);
                    binaryLifecycleTask.setDescription(String.format("Assembles %s.", binary));
                    binary.setBuildTask(binaryLifecycleTask);

                    if (binary.isBuildable()) {
                        assembleTask.dependsOn(binary);
                    }
                }
            }
        }
    }
}

@@ -678,107 +678,9 @@
 api.
-Incubating;%0Aimport org.gradle.api.Plugin;%0Aimport org.gradle.api.Project;%0Aimport org.gradle.api.Task
+*
 ;%0Aim
@@ -1034,16 +1034,70 @@
 Source;%0A
+import org.gradle.model.collection.CollectionBuilder;%0A
 import o
@@ -1756,19 +1756,16 @@
 project.
-%0A *
  Adds a 
@@ -3542,97 +3542,8 @@
 ) %7B%0A
-            Task assembleTask = tasks.getByName(LifecycleBasePlugin.ASSEMBLE_TASK_NAME);%0A
     
@@ -3998,16 +3998,33 @@
 eTask);%0A
+                %7D
 %0A       
@@ -4023,32 +4023,35 @@
 %0A           
+ %7D%0A
         
  if (binary.
@@ -4038,21 +4038,471 @@
         
- if (
+%7D%0A%0A        @Mutate%0A        void attachBinariesToAssembleLifecycle(CollectionBuilder%3CTask%3E tasks, final BinaryContainer binaries) %7B%0A            tasks.named(LifecycleBasePlugin.ASSEMBLE_TASK_NAME, new Action%3CTask%3E() %7B%0A                @Override%0A                public void execute(Task assembleTask) %7B%0A                    for (BinarySpecInternal binary : binaries.withType(BinarySpecInternal.class)) %7B%0A                        if (!binary.isLegacyBinary() && 
 binary.i
@@ -4513,24 +4513,28 @@
 ldable()) %7B%0A
+    
             
@@ -4577,16 +4577,42 @@
 inary);%0A
+                        %7D%0A
         
@@ -4648,32 +4648,34 @@
  %7D%0A            %7D
+);
 %0A        %7D%0A    %7D

Split the rule that defines the assemble task for a binary from the rule that wires binaries in as dependencies of 'assemble'.