<commit_before>package com.uber.okbuck.core.model.android;

import static com.uber.okbuck.core.util.KotlinUtil.KOTLIN_ANDROID_EXTENSIONS_MODULE;
import static com.uber.okbuck.core.util.KotlinUtil.KOTLIN_KAPT_PLUGIN;

import com.android.build.gradle.BaseExtension;
import com.android.build.gradle.api.BaseVariant;
import com.android.build.gradle.api.TestVariant;
import com.android.build.gradle.api.UnitTestVariant;
import com.android.build.gradle.internal.api.TestedVariant;
import com.android.builder.core.VariantType;
import com.android.builder.model.ClassField;
import com.android.builder.model.LintOptions;
import com.android.builder.model.SourceProvider;
import com.android.manifmerger.ManifestMerger2;
import com.android.manifmerger.MergingReport;
import com.android.utils.ILogger;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.uber.okbuck.OkBuckGradlePlugin;
import com.uber.okbuck.core.model.base.AnnotationProcessorCache;
import com.uber.okbuck.core.model.base.RuleType;
import com.uber.okbuck.core.model.base.Scope;
import com.uber.okbuck.core.model.jvm.JvmTarget;
import com.uber.okbuck.core.model.jvm.TestOptions;
import com.uber.okbuck.core.util.FileUtil;
import com.uber.okbuck.core.util.ProjectUtil;
import com.uber.okbuck.core.util.XmlUtil;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.tasks.compile.JavaCompile;
import org.gradle.api.tasks.testing.Test;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.gradle.internal.AndroidExtensionsExtension;
import org.jetbrains.kotlin.gradle.plugin.KotlinAndroidPluginWrapper;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/** An Android target */
public abstract class AndroidTarget extends JvmTarget {

  private static final EmptyLogger EMPTY_LOGGER = new EmptyLogger();

  private static final String DEFAULT_SDK = "1";
  private final String applicationId;
  private final String applicationIdSuffix;
  private final String versionName;
  private final Integer versionCode;
  private final String minSdk;
  private final String targetSdk;
  private final boolean debuggable;
  private final boolean generateR2;
  private final String genDir;
  private final boolean isKotlin;
  private final boolean isKapt;
  private final boolean hasKotlinAndroidExtensions;
  private final boolean hasExperimentalKotlinAndroidExtensions;
  private final boolean lintExclude;
  private final boolean testExclude;
  private final boolean isTest;
  private String manifestPath;
  private String packageName;

  public AndroidTarget(Project project, String name, boolean isTest) {
    super(project, name);

    this.isTest = isTest;

    String suffix = "";
    if (getBaseVariant().getMergedFlavor().getApplicationIdSuffix() != null) {
      suffix += getBaseVariant().getMergedFlavor().getApplicationIdSuffix();
    }

    if (getBaseVariant().getBuildType().getApplicationIdSuffix() != null) {
      suffix += getBaseVariant().getBuildType().getApplicationIdSuffix();
    }

    applicationIdSuffix = suffix;
    if (isTest) {
      String applicationIdString =
          minus(minus(getBaseVariant().getApplicationId(), ".test"), applicationIdSuffix);
      applicationId = minus(applicationIdString, applicationIdSuffix);
    } else {
      applicationId = minus(getBaseVariant().getApplicationId(), applicationIdSuffix);
    }

    versionName = getBaseVariant().getMergedFlavor().getVersionName();
    versionCode = getBaseVariant().getMergedFlavor().getVersionCode();

    debuggable = getBaseVariant().getBuildType().isDebuggable();

    // Butterknife support
    generateR2 = project.getPlugins().hasPlugin("com.jakewharton.butterknife");

    // Create gen dir
    genDir = Paths.get(OkBuckGradlePlugin.OKBUCK_GEN, getPath(), name).toString();
    FileUtil.copyResourceToProject(
        "gen/BUCK_FILE",
        getRootProject().file(genDir).toPath().resolve(OkBuckGradlePlugin.BUCK).toFile());

    // Check if kotlin
    isKotlin = project.getPlugins().hasPlugin(KotlinAndroidPluginWrapper.class);
    isKapt = project.getPlugins().hasPlugin(KOTLIN_KAPT_PLUGIN);
    hasKotlinAndroidExtensions = project.getPlugins().hasPlugin(KOTLIN_ANDROID_EXTENSIONS_MODULE);

    // Check if any rules are excluded
    lintExclude = getProp(getOkbuck().lintExclude, ImmutableList.of()).contains(name);
    testExclude = getProp(getOkbuck().testExclude, ImmutableList.of()).contains(name);

    boolean hasKotlinExtension;
    try {
      AndroidExtensionsExtension androidExtensions =
          project.getExtensions().getByType(AndroidExtensionsExtension.class);
      hasKotlinExtension = hasKotlinAndroidExtensions && androidExtensions.isExperimental();
    } catch (Exception ignored) {
      hasKotlinExtension = false;
    }
    hasExperimentalKotlinAndroidExtensions = hasKotlinExtension;

    if (getBaseVariant().getMergedFlavor().getMinSdkVersion() == null
        || getBaseVariant().getMergedFlavor().getTargetSdkVersion() == null) {
      minSdk = targetSdk = DEFAULT_SDK;
      throw new IllegalStateException(
          "module `"
              + project.getName()
              + "` must specify minSdkVersion and targetSdkVersion in build.gradle");
    } else {
      minSdk = getBaseVariant().getMergedFlavor().getMinSdkVersion().getApiString();
      targetSdk = getBaseVariant().getMergedFlavor().getTargetSdkVersion().getApiString();
    }
  }

  public AndroidTarget(Project project, String name) {
    this(project, name, false);
  }

  protected abstract BaseVariant getBaseVariant();

  protected abstract ManifestMerger2.MergeType getMergeType();

  @Override
  public Scope getMain() {
    return Scope.builder(getProject())
        .configuration(getBaseVariant().getRuntimeConfiguration())
        .sourceDirs(getSources(getBaseVariant()))
        .javaResourceDirs(getJavaResources(getBaseVariant()))
        .compilerOptions(Scope.Builder.COMPILER.JAVA, getJavaCompilerOptions(getBaseVariant()))
        .build();
  }

  @Override
  public Scope getTest() {
    Scope.Builder builder = Scope.builder(getProject());
    UnitTestVariant unitTestVariant = getUnitTestVariant();
    if (unitTestVariant != null) {
      builder.configuration(unitTestVariant.getRuntimeConfiguration());
      builder.sourceDirs(getSources(getUnitTestVariant()));
      builder.compilerOptions(Scope.Builder.COMPILER.JAVA, getJavaCompilerOptions(unitTestVariant));
    }
    return builder.build();<commit_msg>Minor fixes from scope builder refactor<commit_after>package com.uber.okbuck.core.model.android;

import static com.uber.okbuck.core.util.KotlinUtil.KOTLIN_ANDROID_EXTENSIONS_MODULE;
import static com.uber.okbuck.core.util.KotlinUtil.KOTLIN_KAPT_PLUGIN;

import com.android.build.gradle.BaseExtension;
import com.android.build.gradle.api.BaseVariant;
import com.android.build.gradle.api.TestVariant;
import com.android.build.gradle.api.UnitTestVariant;
import com.android.build.gradle.internal.api.TestedVariant;
import com.android.builder.core.VariantType;
import com.android.builder.model.ClassField;
import com.android.builder.model.LintOptions;
import com.android.builder.model.SourceProvider;
import com.android.manifmerger.ManifestMerger2;
import com.android.manifmerger.MergingReport;
import com.android.utils.ILogger;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.uber.okbuck.OkBuckGradlePlugin;
import com.uber.okbuck.core.model.base.AnnotationProcessorCache;
import com.uber.okbuck.core.model.base.RuleType;
import com.uber.okbuck.core.model.base.Scope;
import com.uber.okbuck.core.model.jvm.JvmTarget;
import com.uber.okbuck.core.model.jvm.TestOptions;
import com.uber.okbuck.core.util.FileUtil;
import com.uber.okbuck.core.util.ProjectUtil;
import com.uber.okbuck.core.util.XmlUtil;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.tasks.compile.JavaCompile;
import org.gradle.api.tasks.testing.Test;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.gradle.internal.AndroidExtensionsExtension;
import org.jetbrains.kotlin.gradle.plugin.KotlinAndroidPluginWrapper;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/** An Android target */
public abstract class AndroidTarget extends JvmTarget {

  private static final EmptyLogger EMPTY_LOGGER = new EmptyLogger();

  private static final String DEFAULT_SDK = "1";
  private final String applicationId;
  private final String applicationIdSuffix;
  private final String versionName;
  private final Integer versionCode;
  private final String minSdk;
  private final String targetSdk;
  private final boolean debuggable;
  private final boolean generateR2;
  private final String genDir;
  private final boolean isKotlin;
  private final boolean isKapt;
  private final boolean hasKotlinAndroidExtensions;
  private final boolean hasExperimentalKotlinAndroidExtensions;
  private final boolean lintExclude;
  private final boolean testExclude;
  private final boolean isTest;
  private String manifestPath;
  private String packageName;

  public AndroidTarget(Project project, String name, boolean isTest) {
    super(project, name);

    this.isTest = isTest;

    String suffix = "";
    if (getBaseVariant().getMergedFlavor().getApplicationIdSuffix() != null) {
      suffix += getBaseVariant().getMergedFlavor().getApplicationIdSuffix();
    }

    if (getBaseVariant().getBuildType().getApplicationIdSuffix() != null) {
      suffix += getBaseVariant().getBuildType().getApplicationIdSuffix();
    }

    applicationIdSuffix = suffix;
    if (isTest) {
      String applicationIdString =
          minus(minus(getBaseVariant().getApplicationId(), ".test"), applicationIdSuffix);
      applicationId = minus(applicationIdString, applicationIdSuffix);
    } else {
      applicationId = minus(getBaseVariant().getApplicationId(), applicationIdSuffix);
    }

    versionName = getBaseVariant().getMergedFlavor().getVersionName();
    versionCode = getBaseVariant().getMergedFlavor().getVersionCode();

    debuggable = getBaseVariant().getBuildType().isDebuggable();

    // Butterknife support
    generateR2 = project.getPlugins().hasPlugin("com.jakewharton.butterknife");

    // Create gen dir
    genDir = Paths.get(OkBuckGradlePlugin.OKBUCK_GEN, getPath(), name).toString();
    FileUtil.copyResourceToProject(
        "gen/BUCK_FILE",
        getRootProject().file(genDir).toPath().resolve(OkBuckGradlePlugin.BUCK).toFile());

    // Check if kotlin
    isKotlin = project.getPlugins().hasPlugin(KotlinAndroidPluginWrapper.class);
    isKapt = project.getPlugins().hasPlugin(KOTLIN_KAPT_PLUGIN);
    hasKotlinAndroidExtensions = project.getPlugins().hasPlugin(KOTLIN_ANDROID_EXTENSIONS_MODULE);

    // Check if any rules are excluded
    lintExclude = getProp(getOkbuck().lintExclude, ImmutableList.of()).contains(name);
    testExclude = getProp(getOkbuck().testExclude, ImmutableList.of()).contains(name);

    boolean hasKotlinExtension;
    try {
      AndroidExtensionsExtension androidExtensions =
          project.getExtensions().getByType(AndroidExtensionsExtension.class);
      hasKotlinExtension = hasKotlinAndroidExtensions && androidExtensions.isExperimental();
    } catch (Exception ignored) {
      hasKotlinExtension = false;
    }
    hasExperimentalKotlinAndroidExtensions = hasKotlinExtension;

    if (getBaseVariant().getMergedFlavor().getMinSdkVersion() == null
        || getBaseVariant().getMergedFlavor().getTargetSdkVersion() == null) {
      minSdk = targetSdk = DEFAULT_SDK;
      throw new IllegalStateException(
          "module `"
              + project.getName()
              + "` must specify minSdkVersion and targetSdkVersion in build.gradle");
    } else {
      minSdk = getBaseVariant().getMergedFlavor().getMinSdkVersion().getApiString();
      targetSdk = getBaseVariant().getMergedFlavor().getTargetSdkVersion().getApiString();
    }
  }

  public AndroidTarget(Project project, String name) {
    this(project, name, false);
  }

  protected abstract BaseVariant getBaseVariant();

  protected abstract ManifestMerger2.MergeType getMergeType();

  @Override
  public Scope getMain() {
    return Scope.builder(getProject())
        .configuration(getBaseVariant().getRuntimeConfiguration())
        .sourceDirs(getSources(getBaseVariant()))
        .javaResourceDirs(getJavaResources(getBaseVariant()))
        .compilerOptions(Scope.Builder.COMPILER.JAVA, getJavaCompilerOptions(getBaseVariant()))
        .build();
  }

  @Override
  public Scope getTest() {
    Scope.Builder builder = Scope.builder(getProject());
    UnitTestVariant unitTestVariant = getUnitTestVariant();
    if (unitTestVariant != null) {
      builder.configuration(unitTestVariant.getRuntimeConfiguration());
      builder.sourceDirs(getSources(unitTestVariant));
      builder.javaResourceDirs(getJavaResources(unitTestVariant));
      builder.compilerOptions(Scope.Builder.COMPILER.JAVA, getJavaCompilerOptions(unitTestVariant));
    }
    return builder.build();
{'fraction_non_alphanumeric': 0.10612302883932377, 'fraction_numerical': 0.0012785907089075152, 'mean_word_length': 4.876043405676127, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5046452', 'n_tokens_mistral': 4217, 'n_tokens_neox': 4089, 'n_words': 737}