package io.freefair.gradle.plugins.maven.central;

import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.publish.maven.tasks.GenerateMavenPom;
import org.gradle.api.tasks.TaskProvider;
import org.gradle.language.base.plugins.LifecycleBasePlugin;

/**
 * @author Lars Grefer
 */
public class ValidatePomsPlugin implements Plugin<Project> {

    @Override
    public void apply(Project project) {
        TaskProvider<Task> validateAll = project.getTasks().register("validatePomFiles", t -> {
            t.setGroup(LifecycleBasePlugin.VERIFICATION_GROUP);
        });

        project.getTasks().withType(GenerateMavenPom.class, generateMavenPom -> {

            String checkTaskName;

            if (generateMavenPom.getName().startsWith("generate")) {
                checkTaskName = "validate" + generateMavenPom.getName().substring(8);
            }
            else {
                checkTaskName = "validate" + generateMavenPom.getName();
            }

            TaskProvider<ValidateMavenPom> validateMavenPom = project.getTasks().register(checkTaskName, ValidateMavenPom.class, t -> {
                t.setGroup(LifecycleBasePlugin.VERIFICATION_GROUP);
                String description = generateMavenPom.getDescription();
                if (description != null) {
                    t.setDescription(description.replace("Generates", "Validates"));
                }
            });

            validateAll.configure(all -> all.dependsOn(validateMavenPom));

            project.getPlugins().withType(LifecycleBasePlugin.class, lbp -> {
                project.getTasks().named(LifecycleBasePlugin.CHECK_TASK_NAME).configure(check -> {
                    check.dependsOn(validateMavenPom);
                });
            });

            project.afterEvaluate(p -> {
                validateMavenPom.configure(v -> {
                    v.dependsOn(generateMavenPom);
                    v.getPomFile().set(generateMavenPom.getDestination());
                });
            });
        });
    }
}

@@ -1800,53 +1800,8 @@
 );%0A%0A
-            project.afterEvaluate(p -%3E %7B%0A    
     
@@ -1858,20 +1858,16 @@
         
-    
 v.depend
@@ -1905,20 +1905,16 @@
         
-    
 v.getPom
@@ -1924,16 +1924,58 @@
 e().set(
+project.getLayout().file(project.provider(
 generate
@@ -1978,25 +1978,26 @@
 rateMavenPom
-.
+::
 getDestinati
@@ -2002,30 +2002,10 @@
 tion
-(
 ))
-;%0A                %7D
 );%0A 

Fix afterEvaluate Bug