package pl.polgrabia.demos.mojos;

import ch.qos.logback.core.util.StringUtil;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import pl.polgrabia.demos.PluginRunner;
import pl.polgrabia.demos.PluginRunnerConfig;

@Mojo(name = "runner")
public class PluginRunnerMojo implements org.apache.maven.plugin.Mojo {
    private Log log;

    @Parameter(name = "id")
    private String id;

    @Parameter(name = "name")
    private String name;

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        if (StringUtil.isNullOrEmpty(id)) {
            id = System.getProperty("maven.runner.id");
        }

        if (StringUtil.isNullOrEmpty(name)) {
            name = System.getProperty("maven.runner.name");
        }

        new PluginRunner(new PluginRunnerConfig(
                id,
                name
        ))
                .execute();
    }

    @Override
    public void setLog(Log log) {
        this.log = log;
    }

    @Override
    public Log getLog() {
        return log;
    }
}
