package io.freefair.gradle.plugins.maven;

import io.freefair.gradle.plugins.maven.javadoc.JavadocJarPlugin;
import lombok.Getter;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.component.SoftwareComponent;
import org.gradle.api.publish.PublishingExtension;
import org.gradle.api.publish.maven.MavenPublication;
import org.gradle.api.publish.maven.plugins.MavenPublishPlugin;
import org.gradle.plugins.signing.SigningExtension;
import org.gradle.plugins.signing.SigningPlugin;

import static org.codehaus.groovy.runtime.StringGroovyMethods.capitalize;

@Getter
public abstract class MavenPublishBasePlugin implements Plugin<Project> {

    private Project project;
    private MavenPublication publication;

    @Override
    public void apply(Project project) {
        this.project = project;
        project.getPlugins().apply(getPluginClass());
        project.getPlugins().apply(MavenPublishPlugin.class);

        publication = project.getExtensions().getByType(PublishingExtension.class)
                .getPublications()
                .create(getPublicationName(), MavenPublication.class);

        project.afterEvaluate(p -> {
            publication.from(getSoftwareComponent());

            project.getPlugins().withType(SourcesJarPlugin.class, sourcesJarPlugin ->
                    publication.artifact(sourcesJarPlugin.getSourcesJar().get())
            );

            project.getPlugins().withType(JavadocJarPlugin.class, javadocJarPlugin -> {
                if (javadocJarPlugin.getJavadocJar() != null) {
                    publication.artifact(javadocJarPlugin.getJavadocJar().get());
                }
                if (javadocJarPlugin.getAggregateJavadocJar() != null) {
                    publication.artifact(javadocJarPlugin.getAggregateJavadocJar().get());
                }
            });

            project.getPlugins().withType(SigningPlugin.class, signingPlugin -> project.getExtensions()
                    .getByType(SigningExtension.class)
                    .sign(publication)
            );
        });
    }

    protected abstract Class<? extends Plugin> getPluginClass();

    public String getPublicationName() {
        return "maven" + capitalize((CharSequence) getComponentName());
    }

    public SoftwareComponent getSoftwareComponent() {
        return getProject().getComponents().getByName(getComponentName());
    }

    abstract String getComponentName();
}

@@ -397,32 +397,74 @@
 nPublishPlugin;%0A
+import org.gradle.api.tasks.bundling.Jar;%0A
 import org.gradl
@@ -1350,16 +1350,18 @@
 lugin -%3E
+ %7B
 %0A       
@@ -1373,72 +1373,250 @@
     
-    publication.artifact(sourcesJarPlugin.getSourcesJar().get())
+Jar sourcesJar = sourcesJarPlugin.getSourcesJar().get();%0A                String classifier = sourcesJar.getArchiveClassifier().getOrElse(%22sources%22);%0A                publication.artifact(sourcesJar, config -%3E config.setClassifier(classifier));
 %0A   
@@ -1624,16 +1624,17 @@
         
+%7D
 );%0A%0A    
@@ -1737,97 +1737,25 @@
     
-if (javadocJarPlugin.getJavadocJar() != null) %7B%0A                    publication.artifact(
+Jar javadocJar = 
 java
@@ -1780,33 +1780,32 @@
 vadocJar().get()
-)
 ;%0A              
@@ -1810,191 +1810,177 @@
     
-%7D%0A                if (javadocJarPlugin.getAggregateJavadocJar() != null) %7B%0A                    publication.artifact(javadocJarPlugin.getAggregateJavadocJar().get());%0A                %7D
+String classifier = javadocJar.getArchiveClassifier().getOrElse(%22javadoc%22);%0A                publication.artifact(javadocJar, config -%3E config.setClassifier(classifier));
 %0A   

Fix javadoc and sources publishing