/*
 * Copyright 2014 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.Action;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.file.Directory;
import org.gradle.api.internal.project.ProjectInternal;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.Delete;
import org.gradle.internal.cleanup.BuildOutputCleanupRegistry;
import org.gradle.language.base.internal.plugins.CleanRule;

/**
 * <p>A {@link org.gradle.api.Plugin} which defines a basic project lifecycle.</p>
 */
public class LifecycleBasePlugin implements Plugin<Project> {
    public static final String CLEAN_TASK_NAME = "clean";
    public static final String ASSEMBLE_TASK_NAME = "assemble";
    public static final String CHECK_TASK_NAME = "check";
    public static final String BUILD_TASK_NAME = "build";
    public static final String BUILD_GROUP = "build";
    public static final String VERIFICATION_GROUP = "verification";

    @Override
    public void apply(final Project project) {
        final ProjectInternal projectInternal = (ProjectInternal) project;
        addClean(projectInternal);
        addCleanRule(project);
        addAssemble(project);
        addCheck(project);
        addBuild(project);
    }

    private void addClean(final ProjectInternal project) {
        Provider<Directory> buildDir = project.getLayout().getBuildDirectory();

        // Register at least the project buildDir as a directory to be deleted.
        final BuildOutputCleanupRegistry buildOutputCleanupRegistry = project.getServices().get(BuildOutputCleanupRegistry.class);
        buildOutputCleanupRegistry.registerOutputs(buildDir);

        final Provider<Delete> clean = project.getTasks().register(CLEAN_TASK_NAME, Delete.class, cleanTask -> {
            cleanTask.setDescription("Deletes the build directory.");
            cleanTask.setGroup(BUILD_GROUP);
            cleanTask.delete(buildDir);
        });
        buildOutputCleanupRegistry.registerOutputs(clean.map(cl -> cl.getTargetFiles()));
    }

    private void addCleanRule(Project project) {
        project.getTasks().addRule(new CleanRule(project.getTasks()));
    }

    private void addAssemble(Project project) {
        project.getTasks().register(ASSEMBLE_TASK_NAME, new Action<Task>() {
            @Override
            public void execute(Task assembleTask) {
                assembleTask.setDescription("Assembles the outputs of this project.");
                assembleTask.setGroup(BUILD_GROUP);
            }
        });
    }

    private void addCheck(Project project) {
        project.getTasks().register(CHECK_TASK_NAME, new Action<Task>() {
            @Override
            public void execute(Task checkTask) {
                checkTask.setDescription("Runs all checks.");
                checkTask.setGroup(VERIFICATION_GROUP);
            }
        });
    }

    private void addBuild(final Project project) {
        project.getTasks().register(BUILD_TASK_NAME, new Action<Task>() {
            @Override
            public void execute(Task buildTask) {
                buildTask.setDescription("Assembles and tests this project.");
                buildTask.setGroup(BUILD_GROUP);
                buildTask.dependsOn(ASSEMBLE_TASK_NAME);
                buildTask.dependsOn(CHECK_TASK_NAME);
            }
        });
    }
}

@@ -657,38 +657,8 @@
 s;%0A%0A
-import org.gradle.api.Action;%0A
 impo
@@ -718,36 +718,8 @@
 ct;%0A
-import org.gradle.api.Task;%0A
 impo
@@ -2841,88 +2841,8 @@
 AME,
- new Action%3CTask%3E() %7B%0A            @Override%0A            public void execute(Task
  ass
@@ -2846,32 +2846,30 @@
 assembleTask
-) %7B%0A    
+ -%3E %7B%0A
             
@@ -2943,36 +2943,32 @@
 %22);%0A            
-    
 assembleTask.set
@@ -2983,38 +2983,24 @@
 ILD_GROUP);%0A
-            %7D%0A
         %7D);%0A
@@ -3107,88 +3107,8 @@
 AME,
- new Action%3CTask%3E() %7B%0A            @Override%0A            public void execute(Task
  che
@@ -3109,32 +3109,30 @@
 E, checkTask
-) %7B%0A    
+ -%3E %7B%0A
             
@@ -3181,36 +3181,32 @@
 %22);%0A            
-    
 checkTask.setGro
@@ -3229,30 +3229,16 @@
 GROUP);%0A
-            %7D%0A
         
@@ -3355,88 +3355,8 @@
 AME,
- new Action%3CTask%3E() %7B%0A            @Override%0A            public void execute(Task
  bui
@@ -3361,24 +3361,22 @@
 uildTask
-) %7B%0A    
+ -%3E %7B%0A
         
@@ -3446,36 +3446,32 @@
 %22);%0A            
-    
 buildTask.setGro
@@ -3491,36 +3491,32 @@
 P);%0A            
-  
-  
 buildTask.depend
@@ -3544,36 +3544,32 @@
 E);%0A            
-    
 buildTask.depend
@@ -3594,22 +3594,8 @@
 E);%0A
-            %7D%0A
     

Replace anonymous by lambda in `LifecycleBasePlugin`