public Map<String, String> buildEnvironment(RubyLabel rubyRuntimeLabel, ConfigurationMap config)
      throws RuntimeLocatorException {

    this.log.info(
        "Using manager {} runtime {}",
        rubyRuntimeLabel.getRubyRuntimeManager(),
        rubyRuntimeLabel.getRubyRuntime());

    final Map<String, String> currentEnvVars = getEnvironmentVariableAccessor().getEnvironment();

    // Get the variables from our configuration
    final String taskEnvironmentVariables = config.get(ENVIRONMENT);
    final String globalEnvironmentVariables =
        RubyBuildConfigurationPlugin.getRubyEnvironmentVariables(getBuildDefinition());

    Map<String, String> configEnvVars =
        getEnvironmentVariableAccessor()
            .splitEnvironmentAssignments(
                globalEnvironmentVariables + " " + taskEnvironmentVariables);

    final RubyLocator rubyLocator = getRubyLocator(rubyRuntimeLabel.getRubyRuntimeManager());

    return rubyLocator.buildEnv(
        rubyRuntimeLabel.getRubyRuntime(),
        getRubyExecutablePath(rubyRuntimeLabel),
        ImmutableMap.<String, String>builder()
            .putAll(currentEnvVars)
            .putAll(configEnvVars)
            .build());
  }
  protected String getRubyExecutablePath(final RubyLabel rubyRuntimeLabel) {

    final Capability capability =
        getCapabilityContext().getCapabilitySet().getCapability(rubyRuntimeLabel.toCapabilityKey());
    Preconditions.checkNotNull(
        capability,
        "Capability  for ruby "
            + rubyRuntimeLabel.toCapabilityKey()
            + ".  Please be sure to \"Detect server capabilities\" in the Administration console, and the path is valid.");
    final String rubyRuntimeExecutable = capability.getValue();
    Preconditions.checkNotNull(rubyRuntimeExecutable, "rubyRuntimeExecutable");
    return rubyRuntimeExecutable;
  }
  @Override
  @NotNull
  public TaskResult execute(@NotNull CommonTaskContext commonTaskContext) throws TaskException {

    final TaskResultBuilder taskResultBuilder = TaskResultBuilder.newBuilder(commonTaskContext);
    final TaskContext taskContext = Narrow.to(commonTaskContext, TaskContext.class);
    final BuildLogger buildLogger = taskContext.getBuildLogger();

    // move this crazy resolution out of here.
    resolveContext(taskContext, commonTaskContext);

    final String rubyRuntimeLabel =
        RubyBuildConfigurationPlugin.getRubyRuntime(getBuildDefinition());

    try {

      if (rubyRuntimeLabel == null) {
        throw new RuntimeLocatorException(
            "A ruby runtime has not been chosen for this plan.  Please see the miscellaneous tab to choose a plan-wide ruby runtime.");
      }

      final RubyLabel rubyLabel = RubyLabel.fromString(rubyRuntimeLabel);

      final ConfigurationMap config = commonTaskContext.getConfigurationMap();
      Map<String, String> envVars = buildEnvironment(rubyLabel, config);

      List<String> commandsList = buildCommandList(rubyLabel, config);

      ExternalProcess externalProcess =
          getProcessService()
              .createExternalProcess(
                  commonTaskContext,
                  new ExternalProcessBuilder()
                      .env(envVars)
                      .command(commandsList)
                      .workingDirectory(commonTaskContext.getWorkingDirectory()));

      externalProcess.execute();

      taskResultBuilder.checkReturnCode(externalProcess, 0);
    } catch (IllegalArgumentException e) {
      buildLogger.addErrorLogEntry("Could not run ruby task: " + e.getMessage(), e);
      taskResultBuilder.failed();
    } catch (PathNotFoundException e) {
      buildLogger.addErrorLogEntry("Could not run ruby task: " + e.getMessage(), e);
      taskResultBuilder.failed();
    } catch (RuntimeLocatorException e) {
      buildLogger.addErrorLogEntry("Could not run ruby task: " + e.getMessage(), e);
      taskResultBuilder.failed();
    }

    return taskResultBuilder.build();
  }