package jetbrains.mps.tool.builder.make;

/*Generated by MPS */

import jetbrains.mps.annotations.GeneratedClass;
import jetbrains.mps.make.service.AbstractMakeService;
import jetbrains.mps.make.IMakeService;
import java.util.concurrent.Future;
import jetbrains.mps.make.script.IResult;
import jetbrains.mps.make.MakeSession;
import jetbrains.mps.make.resources.IResource;
import jetbrains.mps.make.script.IScript;
import jetbrains.mps.make.script.IScriptController;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.mps.openapi.util.ProgressMonitor;
import jetbrains.mps.make.IMakeNotificationListener;
import jetbrains.mps.make.dependencies.MakeSequence;
import jetbrains.mps.make.service.CoreMakeTask;
import jetbrains.mps.internal.make.runtime.util.FutureValue;

@GeneratedClass(node = "r:2758abb3-4e9a-4fac-8e72-2fadd8b5c3d7(jetbrains.mps.tool.builder.make)/878521226301293123", model = "r:2758abb3-4e9a-4fac-8e72-2fadd8b5c3d7(jetbrains.mps.tool.builder.make)")
public class BuildMakeService extends AbstractMakeService implements IMakeService {
  public BuildMakeService() {
    // FIXME once we install it as IMakeService and use through CoreComponent, don't need to 
    // re-export module dependencies for AMS and IMakeService (j.m.make and j.m.make.runtime)
  }
  @Override
  public Future<IResult> make(MakeSession session, Iterable<? extends IResource> resources, IScript script, IScriptController controller, @NotNull ProgressMonitor monitor) {
    return doMake(session, resources, script, controller, monitor);
  }
  @Override
  public boolean isSessionActive() {
    return false;
  }
  @Override
  public boolean openNewSession(MakeSession session) {
    return false;
  }
  @Override
  public void closeSession(MakeSession session) {
  }
  @Override
  public void addListener(IMakeNotificationListener listener) {
    throw new UnsupportedOperationException();
  }
  @Override
  public void removeListener(IMakeNotificationListener listener) {
    throw new UnsupportedOperationException();
  }
  private Future<IResult> doMake(MakeSession makeSession, Iterable<? extends IResource> inputRes, IScript defaultScript, IScriptController controller, @NotNull ProgressMonitor monitor) {
    MakeSequence makeSeq = new MakeSequence(inputRes, defaultScript, makeSession);

    IScriptController ctl = this.completeController(makeSession, controller);

    CoreMakeTask task = new CoreMakeTask(makeSeq, ctl, makeSession.getMessageHandler());
    task.run(monitor);
    return new FutureValue<IResult>(task.getResult());
  }

  private IScriptController completeController(MakeSession msess, final IScriptController ctl) {
    // Assume if client supplied IScriptController, he knows what he's doing and bears full responsibility
    // to supply correct IConfigMonitor and IJobMonitor instances
    if (ctl != null) {
      return ctl;
    }
    // Although there's knowledge about default make targets (see #defaultMakeScript(), below),
    // I don't want IMakeService to configure any target here explicitly. It's up to caller to configure
    // actual targets (yes, they are not necessarily the same as in default script) with desired values, not
    // service's to guess defaults. Id rather fail and see which targets were not configured.
    return new IScriptController.Stub2(msess);
  }
}
