package io.quarkus.gradle.tasks;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.gradle.api.GradleException;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.options.Option;

import io.quarkus.bootstrap.model.AppArtifact;
import io.quarkus.bootstrap.resolver.AppModelResolver;
import io.quarkus.bootstrap.resolver.AppModelResolverException;
import io.quarkus.creator.AppCreatorException;
import io.quarkus.creator.CuratedApplicationCreator;
import io.quarkus.creator.phase.augment.AugmentTask;

/**
 * @author <a href="mailto:stalep@gmail.com">Ståle Pedersen</a>
 */
public class QuarkusBuild extends QuarkusTask {

    private String transformedClassesDirectory;

    private String wiringClassesDirectory;

    private String libDir;

    private String mainClass = "io.quarkus.runner.GeneratedMain";

    private boolean useStaticInit = true;

    private boolean uberJar = false;

    private List<String> ignoredEntries = new ArrayList<>();

    public QuarkusBuild() {
        super("Quarkus builds a runner jar based on the build jar");
    }

    public File getTransformedClassesDirectory() {
        if (transformedClassesDirectory == null)
            return extension().transformedClassesDirectory();
        else
            return new File(transformedClassesDirectory);
    }

    @Option(description = "The directory for application classes transformed by processing.", option = "transformed-classes-directory")
    public void setTransformedClassesDirectory(String transformedClassesDirectory) {
        this.transformedClassesDirectory = transformedClassesDirectory;
    }

    @Optional
    @Input
    public File getWiringClassesDirectory() {
        if (wiringClassesDirectory == null)
            return extension().wiringClassesDirectory();
        else
            return new File(wiringClassesDirectory);
    }

    @Option(description = "The directory for classes generated by processing", option = "wiring-classes-directory")
    public void setWiringClassesDirectory(String wiringClassesDirectory) {
        this.wiringClassesDirectory = wiringClassesDirectory;
    }

    @Optional
    @Input
    public File getLibDir() {
        if (libDir == null)
            return extension().libDir();
        else
            return new File(libDir);
    }

    @Option(description = "The directory for library jars", option = "lib-dir")
    public void setLibDir(String libDir) {
        this.libDir = libDir;
    }

    @Input
    @Optional
    public String getMainClass() {
        return mainClass;
    }

    @Option(description = "Name of the main class generated by the quarkus build process", option = "main-class")
    public void setMainClass(String mainClass) {
        this.mainClass = mainClass;
    }

    @Optional
    @Input
    public boolean isUseStaticInit() {
        return useStaticInit;
    }

    @Option(description = "", option = "use-static-init")
    public void setUseStaticInit(boolean useStaticInit) {
        this.useStaticInit = useStaticInit;
    }

    @Optional
    @Input
    public boolean isUberJar() {
        return uberJar;
    }

    @Option(description = "Set to true if the build task should build an uberjar", option = "uber-jar")
    public void setUberJar(boolean uberJar) {
        this.uberJar = uberJar;
    }

    @Optional
    @Input
    public List<String> getIgnoredEntries() {
        return ignoredEntries;
    }

    @Option(description = "When using the uber-jar option, this option can be used to "
            + "specify one or more entries that should be excluded from the final jar", option = "ignored-entry")
    public void setIgnoredEntries(List<String> ignoredEntries) {
        this.ignoredEntries.addAll(ignoredEntries);
    }

    @TaskAction
    public void buildQuarkus() {
        getLogger().lifecycle("building quarkus runner");

        final AppArtifact appArtifact = extension().getAppArtifact();
        final AppModelResolver modelResolver = extension().resolveAppModel();
        try {
            // this needs to be done otherwise the app artifact doesn't get a proper path
            modelResolver.resolveModel(appArtifact);
        } catch (AppModelResolverException e) {
            throw new GradleException("Failed to resolve application model " + appArtifact + " dependencies", e);
        }
        final Map<String, ?> properties = getProject().getProperties();
        final Properties realProperties = new Properties();
        for (Map.Entry<String, ?> entry : properties.entrySet()) {
            final String key = entry.getKey();
            final Object value = entry.getValue();
            if (key != null && value instanceof String && key.startsWith("quarkus.")) {
                realProperties.setProperty(key, (String) value);
            }
        }
        realProperties.putIfAbsent("quarkus.application.name", appArtifact.getArtifactId());
        realProperties.putIfAbsent("quarkus.application.version", appArtifact.getVersion());

        boolean clear = false;
        if (uberJar && System.getProperty("quarkus.package.uber-jar") == null) {
            System.setProperty("quarkus.package.uber-jar", "true");
            clear = true;
        }
        try (CuratedApplicationCreator appCreationContext = CuratedApplicationCreator.builder()
                .setWorkDir(getProject().getBuildDir().toPath())
                .setModelResolver(modelResolver)
                .setBaseName(extension().finalName())
                .setAppArtifact(appArtifact).build()) {

            AugmentTask task = AugmentTask.builder().setBuildSystemProperties(realProperties)
                    .setAppClassesDir(extension().outputDirectory().toPath())
                    .setConfigDir(extension().outputConfigDirectory().toPath()).build();
            appCreationContext.runTask(task);

        } catch (AppCreatorException e) {
            throw new GradleException("Failed to build a runnable JAR", e);
        } finally {
            if (clear) {
                System.clearProperty("quarkus.package.uber-jar");
            }
        }
    }
}

@@ -31,29 +31,8 @@
 s;%0A%0A
-import java.io.File;%0A
 impo
@@ -772,129 +772,8 @@
  %7B%0A%0A
-    private String transformedClassesDirectory;%0A%0A    private String wiringClassesDirectory;%0A%0A    private String libDir;%0A%0A
     
@@ -860,65 +860,14 @@
 an u
-seStaticInit = true;%0A%0A    private boolean uberJar = false
+berJar
 ;%0A%0A 
@@ -1035,1394 +1035,8 @@
  %7D%0A%0A
-    public File getTransformedClassesDirectory() %7B%0A        if (transformedClassesDirectory == null)%0A            return extension().transformedClassesDirectory();%0A        else%0A            return new File(transformedClassesDirectory);%0A    %7D%0A%0A    @Option(description = %22The directory for application classes transformed by processing.%22, option = %22transformed-classes-directory%22)%0A    public void setTransformedClassesDirectory(String transformedClassesDirectory) %7B%0A        this.transformedClassesDirectory = transformedClassesDirectory;%0A    %7D%0A%0A    @Optional%0A    @Input%0A    public File getWiringClassesDirectory() %7B%0A        if (wiringClassesDirectory == null)%0A            return extension().wiringClassesDirectory();%0A        else%0A            return new File(wiringClassesDirectory);%0A    %7D%0A%0A    @Option(description = %22The directory for classes generated by processing%22, option = %22wiring-classes-directory%22)%0A    public void setWiringClassesDirectory(String wiringClassesDirectory) %7B%0A        this.wiringClassesDirectory = wiringClassesDirectory;%0A    %7D%0A%0A    @Optional%0A    @Input%0A    public File getLibDir() %7B%0A        if (libDir == null)%0A            return extension().libDir();%0A        else%0A            return new File(libDir);%0A    %7D%0A%0A    @Option(description = %22The directory for library jars%22, option = %22lib-dir%22)%0A    public void setLibDir(String libDir) %7B%0A        this.libDir = libDir;%0A    %7D%0A%0A
     
@@ -1334,276 +1334,8 @@
  %7D%0A%0A
-    @Optional%0A    @Input%0A    public boolean isUseStaticInit() %7B%0A        return useStaticInit;%0A    %7D%0A%0A    @Option(description = %22%22, option = %22use-static-init%22)%0A    public void setUseStaticInit(boolean useStaticInit) %7B%0A        this.useStaticInit = useStaticInit;%0A    %7D%0A%0A
     

Remove unused gradle build parameters