/*
 * Copyright 2016 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.cloud.tools.app.impl.cloudsdk;

import com.google.cloud.tools.app.api.AppEngineException;
import com.google.cloud.tools.app.api.deploy.AppEngineStandardStaging;
import com.google.cloud.tools.app.api.deploy.StageStandardConfiguration;
import com.google.cloud.tools.app.impl.cloudsdk.internal.args.AppCfgArgs;
import com.google.cloud.tools.app.impl.cloudsdk.internal.process.ProcessRunnerException;
import com.google.cloud.tools.app.impl.cloudsdk.internal.sdk.CloudSdk;
import com.google.common.base.Preconditions;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;

/**
 * An implementation of {@link AppEngineStandardStaging} that uses App Engine SDK bundled with the
 * Cloud SDK.
 */
public class CloudSdkAppEngineStandardStaging implements AppEngineStandardStaging {

  private CloudSdk cloudSdk;

  public CloudSdkAppEngineStandardStaging(
      CloudSdk cloudSdk) {
    this.cloudSdk = cloudSdk;
  }

  @Override
  public void stageStandard(StageStandardConfiguration config) throws AppEngineException {
    Preconditions.checkNotNull(config);
    Preconditions.checkNotNull(config.getSourceDirectory());
    Preconditions.checkNotNull(config.getStagingDirectory());
    Preconditions.checkNotNull(cloudSdk);

    List<String> arguments = new ArrayList<>();

    arguments.addAll(AppCfgArgs.get("enable_quickstart", config.getEnableQuickstart()));
    arguments.addAll(AppCfgArgs.get("disable_update_check", config.getDisableUpdateCheck()));
    arguments.addAll(AppCfgArgs.get("enable_jar_splitting", config.getEnableJarSplitting()));
    arguments.addAll(AppCfgArgs.get("jar_splitting_excludes", config.getJarSplittingExcludes()));
    arguments.addAll(AppCfgArgs.get("compile_encoding", config.getCompileEncoding()));
    arguments.addAll(AppCfgArgs.get("delete_jsps", config.getDeleteJsps()));
    arguments.addAll(AppCfgArgs.get("enable_jar_classes", config.getEnableJarClasses()));
    arguments.addAll(AppCfgArgs.get("disable_jar_jsps", config.getDisableJarJsps()));

    arguments.add("stage");
    arguments.add(config.getSourceDirectory().toPath().toString());
    arguments.add(config.getStagingDirectory().toPath().toString());

    Path dockerfile =
        config.getDockerfile() == null ? null : config.getDockerfile().toPath();
    Path dockerfileDestination = config.getStagingDirectory().toPath();

    try {

      cloudSdk.runAppCfgCommand(arguments);

      if (dockerfile != null && dockerfile.toFile().exists()) {
        Files.copy(dockerfile, dockerfileDestination, StandardCopyOption.REPLACE_EXISTING);
      }

    } catch (IOException | ProcessRunnerException e) {
      throw new AppEngineException(e);
    }

  }

}

@@ -2900,24 +2900,16 @@
 erfile =
-%0A       
  config.
@@ -2978,135 +2978,18 @@
 ();%0A
-    Path dockerfileDestination = config.getStagingDirectory().toPath();%0A%0A    try %7B%0A%0A      cloudSdk.runAppCfgCommand(arguments);
+%0A    try %7B
 %0A%0A  
@@ -3085,30 +3085,57 @@
 le, 
-dockerfileDestination,
+config.getSourceDirectory().toPath(),%0A           
  Sta
@@ -3178,16 +3178,61 @@
      %7D%0A%0A
+      cloudSdk.runAppCfgCommand(arguments);%0A%0A
     %7D ca

Copy dockerfile into source directory before staging (#82)