// Copyright 2016 Google Inc. All Rights Reserved.
package com.google.copybara.testing;

import com.google.common.collect.ImmutableList;
import com.google.common.jimfs.Jimfs;
import com.google.copybara.GeneralOptions;
import com.google.copybara.Options;
import com.google.copybara.WorkflowNameOptions;
import com.google.copybara.git.GerritOptions;
import com.google.copybara.git.GitOptions;
import com.google.copybara.localdir.LocalDestinationOptions;
import com.google.copybara.util.console.LogConsole;

import java.io.IOException;
import java.nio.file.Files;

/**
 * Allows building complete and sane {@link Options} instances succinctly.
 */
public final class OptionsBuilder {

  public GeneralOptions general =
      new GeneralOptions(Jimfs.newFileSystem().getPath("/"), /*verbose=*/true, /*lastRevision=*/
          null, new LogConsole(System.out));
  public LocalDestinationOptions localDestination =
      new LocalDestinationOptions();
  public GitOptions git = new GitOptions();
  public GerritOptions gerrit = new GerritOptions();
  public WorkflowNameOptions workflowName = new WorkflowNameOptions("default");

  public OptionsBuilder setWorkdirToRealTempDir() throws IOException {
    general = new GeneralOptions(Files.createTempDirectory("OptionsBuilder"), /*verbose=*/true,
        /*lastRevision=*/null, new LogConsole(System.out));
    return this;
  }

  public Options build() {
    return new Options(ImmutableList.of(general, localDestination, git, gerrit, workflowName));
  }
}

@@ -203,32 +203,67 @@
 GeneralOptions;%0A
+import com.google.copybara.Option;%0A
 import com.googl
@@ -274,32 +274,32 @@
 pybara.Options;%0A
-
 import com.googl
@@ -588,16 +588,69 @@
 e.Files;
+%0Aimport java.util.ArrayList;%0Aimport java.util.Arrays;
 %0A%0A/**%0A *
@@ -737,14 +737,8 @@
 lic 
-final 
 clas
@@ -1205,24 +1205,30 @@
 ;%0A%0A  public 
+final 
 OptionsBuild
@@ -1461,125 +1461,424 @@
 %0A%0A  
-public Options build() %7B%0A    return new Options(ImmutableList.of(general, localDestination, git, gerrit, workflowName
+/**%0A   * Returns all options to include in the built %7B@link Options%7D instance. This can be overridden by%0A   * child classes, in which case it should also include the superclass' instances.%0A   */%0A  protected Iterable%3COption%3E allOptions() %7B%0A    return ImmutableList.of(general, localDestination, git, gerrit, workflowName);%0A  %7D%0A%0A  public final Options build() %7B%0A    return new Options(ImmutableList.copyOf(allOptions()
 ));%0A

Allow OptionsBuilder to be subclassed