<commit_before>/*
 * Copyright (C) 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.maven;

import com.google.cloud.tools.appengine.api.deploy.AppEngineDeployment;
import com.google.cloud.tools.appengine.api.deploy.AppEngineFlexibleStaging;
import com.google.cloud.tools.appengine.api.deploy.AppEngineStandardStaging;
import com.google.cloud.tools.appengine.api.devserver.AppEngineDevServer;
import com.google.cloud.tools.appengine.cloudsdk.CloudSdk;
import com.google.cloud.tools.appengine.cloudsdk.CloudSdkAppEngineDeployment;
import com.google.cloud.tools.appengine.cloudsdk.CloudSdkAppEngineDevServer;
import com.google.cloud.tools.appengine.cloudsdk.CloudSdkAppEngineFlexibleStaging;
import com.google.cloud.tools.appengine.cloudsdk.CloudSdkAppEngineStandardStaging;
import com.google.cloud.tools.appengine.cloudsdk.process.ProcessOutputLineListener;

import org.apache.maven.plugin.logging.Log;

/**
 * Factory for App Engine dependencies.
 */
public class CloudSdkAppEngineFactory implements AppEngineFactory {

  protected CloudSdkFactory cloudSdkFactory;
  private CloudSdkMojo mojo;

  public CloudSdkAppEngineFactory(CloudSdkMojo mojo) {
    this(mojo, new CloudSdkFactory());
  }

  public CloudSdkAppEngineFactory(CloudSdkMojo mojo, CloudSdkFactory cloudSdkFactory) {
    this.mojo = mojo;
    this.cloudSdkFactory = cloudSdkFactory;
  }

  @Override
  public AppEngineStandardStaging standardStaging() {
    return cloudSdkFactory.standardStaging(defaultCloudSdkBuilder().build());
  }

  @Override
  public AppEngineFlexibleStaging flexibleStaging() {
    return cloudSdkFactory.flexibleStaging();
  }

  @Override
  public AppEngineDeployment deployment() {
    return cloudSdkFactory.deployment(defaultCloudSdkBuilder().build());
  }

  @Override
  public AppEngineDevServer devServerRunSync() {
    return cloudSdkFactory.devServer(defaultCloudSdkBuilder().build());
  }

  @Override
  public AppEngineDevServer devServerRunAsync(int startSuccessTimeout) {
    CloudSdk.Builder builder = defaultCloudSdkBuilder()
        .async(true)
        .runDevAppServerWait(startSuccessTimeout);
    return cloudSdkFactory.devServer(builder.build());
  }

  @Override
  public AppEngineDevServer devServerStop() {
    return cloudSdkFactory.devServer(null);
  }

  protected CloudSdk.Builder defaultCloudSdkBuilder() {

    ProcessOutputLineListener lineListener = new DefaultProcessOutputLineListener(mojo.getLog());

    return cloudSdkFactory.cloudSdkBuilder()
        .sdkPath(mojo.getCloudSdkPath())
        .addStdOutLineListener(lineListener)
        .addStdErrLineListener(lineListener)
        .appCommandMetricsEnvironment(mojo.getArtifactId())
        .appCommandMetricsEnvironmentVersion(mojo.getArtifactVersion());
  }

  /**
   * Default output listener that copies output to the Maven Mojo logger with a 'GCLOUD: ' prefix.<commit_msg>fail on non-zero process exit code (#33)<commit_after>/*
 * Copyright (C) 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.maven;

import com.google.cloud.tools.appengine.api.deploy.AppEngineDeployment;
import com.google.cloud.tools.appengine.api.deploy.AppEngineFlexibleStaging;
import com.google.cloud.tools.appengine.api.deploy.AppEngineStandardStaging;
import com.google.cloud.tools.appengine.api.devserver.AppEngineDevServer;
import com.google.cloud.tools.appengine.cloudsdk.CloudSdk;
import com.google.cloud.tools.appengine.cloudsdk.CloudSdkAppEngineDeployment;
import com.google.cloud.tools.appengine.cloudsdk.CloudSdkAppEngineDevServer;
import com.google.cloud.tools.appengine.cloudsdk.CloudSdkAppEngineFlexibleStaging;
import com.google.cloud.tools.appengine.cloudsdk.CloudSdkAppEngineStandardStaging;
import com.google.cloud.tools.appengine.cloudsdk.process.NonZeroExceptionExitListener;
import com.google.cloud.tools.appengine.cloudsdk.process.ProcessOutputLineListener;

import org.apache.maven.plugin.logging.Log;

/**
 * Factory for App Engine dependencies.
 */
public class CloudSdkAppEngineFactory implements AppEngineFactory {

  protected CloudSdkFactory cloudSdkFactory;
  private CloudSdkMojo mojo;

  public CloudSdkAppEngineFactory(CloudSdkMojo mojo) {
    this(mojo, new CloudSdkFactory());
  }

  public CloudSdkAppEngineFactory(CloudSdkMojo mojo, CloudSdkFactory cloudSdkFactory) {
    this.mojo = mojo;
    this.cloudSdkFactory = cloudSdkFactory;
  }

  @Override
  public AppEngineStandardStaging standardStaging() {
    return cloudSdkFactory.standardStaging(defaultCloudSdkBuilder().build());
  }

  @Override
  public AppEngineFlexibleStaging flexibleStaging() {
    return cloudSdkFactory.flexibleStaging();
  }

  @Override
  public AppEngineDeployment deployment() {
    return cloudSdkFactory.deployment(defaultCloudSdkBuilder().build());
  }

  @Override
  public AppEngineDevServer devServerRunSync() {
    return cloudSdkFactory.devServer(defaultCloudSdkBuilder().build());
  }

  @Override
  public AppEngineDevServer devServerRunAsync(int startSuccessTimeout) {
    CloudSdk.Builder builder = defaultCloudSdkBuilder()
        .async(true)
        .runDevAppServerWait(startSuccessTimeout);
    return cloudSdkFactory.devServer(builder.build());
  }

  @Override
  public AppEngineDevServer devServerStop() {
    return cloudSdkFactory.devServer(null);
  }

  protected CloudSdk.Builder defaultCloudSdkBuilder() {

    ProcessOutputLineListener lineListener = new DefaultProcessOutputLineListener(mojo.getLog());

    return cloudSdkFactory.cloudSdkBuilder()
        .sdkPath(mojo.getCloudSdkPath())
        .addStdOutLineListener(lineListener)
        .addStdErrLineListener(lineListener)
        .exitListener(new NonZeroExceptionExitListener())
        .appCommandMetricsEnvironment(mojo.getArtifactId())
        .appCommandMetricsEnvironmentVersion(mojo.getArtifactVersion());
  }

  /**
   * Default output listener that copies output to the Maven Mojo logger with a 'GCLOUD: ' prefix.
{'fraction_non_alphanumeric': 0.08661529680365297, 'fraction_numerical': 0.0025684931506849314, 'mean_word_length': 5.000856164383562, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3229979', 'n_tokens_mistral': 2031, 'n_tokens_neox': 1990, 'n_words': 429}