
package de.jcup.egradle.eclipse.ide.execution;

import static de.jcup.egradle.eclipse.ide.IDEUtil.*;
import static org.apache.commons.lang3.Validate.*;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;

import de.jcup.egradle.eclipse.ide.IDEActivator;

public class GradleJob extends Job {
    private GradleExecutionDelegate execution;

    public GradleJob(String name, GradleExecutionDelegate execution) {
        super(name);
        notNull(execution, "Execution delegate may not be null!");
        this.execution = execution;
    }

    @Override
    protected IStatus run(IProgressMonitor monitor) {
        try {
            execution.execute(monitor);
            if (!execution.getResult().isOkay()) {
                getDialogSupport().showBuildFailed(execution.getResult().createDescription());
            }
        } catch (Exception e) {
            return new Status(Status.ERROR, IDEActivator.PLUGIN_ID, "Cannot execute " + getName(), e);
        }
        return Status.OK_STATUS;
    }

}