<commit_before>package com.facebook.testing.screenshot.build

import org.gradle.api.*

class ScreenshotsPluginExtension {
    def adb = "adb"
    def testApkTarget = ":packageDebugAndroidTest"
    def customTestRunner = false
    def recordDir = "screenshots"
    def addCompileDeps = true
}

class ScreenshotsPlugin implements Plugin<Project> {
  void apply(Project project) {
    project.extensions.create("screenshots", ScreenshotsPluginExtension)

    def depTarget = project.screenshots.testApkTarget
    def recordMode = false
    def verifyMode = false

    def codeSource = ScreenshotsPlugin.class.getProtectionDomain().getCodeSource();
    def jarFile = new File(codeSource.getLocation().toURI().getPath());

    println("Found jar file at " + jarFile.getAbsolutePath())

    def implementationVersion = getClass().getPackage().getImplementationVersion()

    if (project.screenshots.addCompileDeps) {
      project.dependencies.androidTestCompile('com.facebook.testing.screenshot:core:' + implementationVersion)
    }

    project.task('pullScreenshots', dependsOn: depTarget) << {
      def output = project.tasks.getByPath(depTarget).getOutputs().getFiles().getSingleFile().getAbsolutePath()
      println output
      project.exec {
        executable = 'python'

        // I don't know how to only *add* variables, so I'm just propagating all. :(
        environment = [
            'PYTHONPATH': jarFile,
            'ANDROID_HOME': System.getenv('ANDROID_HOME'),
            'ANDROID_SDK': System.getenv('ANDROID_SDK'),
            'PATH': System.getenv('PATH'),
            'ANDROID_SERIAL': System.getenv('ANDROID_SERIAL'),
        ]
        args = ['-m', 'android_screenshot_tests.pull_screenshots', "--apk", output.toString()]

        if (recordMode) {
          args += ["--record", project.screenshots.recordDir]
        } else if (verifyMode) {
          args += ["--verify", project.screenshots.recordDir]
        }
      }
    }

    project.task("clearScreenshots") << {
      project.exec {
        executable = project.screenshots.adb
        args = ["shell", "rm", "-rf", "/sdcard/screenshots"]
        ignoreExitValue = true
      }
    }

    project.task("screenshotTests", dependsOn: [
                   ":clearScreenshots",
                   ":connectedAndroidTest",
                   ":pullScreenshots"])

    if (!project.screenshots.customTestRunner) {
       project.android.defaultConfig {
           testInstrumentationRunner = 'com.facebook.testing.screenshot.ScreenshotTestRunner'
       }
    }

    project.task("recordMode") << {
      recordMode = true
    }

    project.task("verifyMode") << {
      verifyMode = true
    }
  }
}<commit_msg>handle nested projects<commit_after>package com.facebook.testing.screenshot.build

import org.gradle.api.*

class ScreenshotsPluginExtension {
    def adb = "adb"
    def testApkTarget = ":packageDebugAndroidTest"
    def customTestRunner = false
    def recordDir = "screenshots"
    def addCompileDeps = true
}

class ScreenshotsPlugin implements Plugin<Project> {
  void apply(Project project) {
    project.extensions.create("screenshots", ScreenshotsPluginExtension)

    def depTarget = project.screenshots.testApkTarget
    def recordMode = false
    def verifyMode = false

    def codeSource = ScreenshotsPlugin.class.getProtectionDomain().getCodeSource();
    def jarFile = new File(codeSource.getLocation().toURI().getPath());

    println("Found jar file at " + jarFile.getAbsolutePath())

    def implementationVersion = getClass().getPackage().getImplementationVersion()

    if (project.screenshots.addCompileDeps) {
      project.dependencies.androidTestCompile('com.facebook.testing.screenshot:core:' + implementationVersion)
    }

    project.task('pullScreenshots', dependsOn: depTarget) << {
      def output = project.tasks.getByPath(depTarget).getOutputs().getFiles().getSingleFile().getAbsolutePath()
      println output
      project.exec {
        executable = 'python'

        // I don't know how to only *add* variables, so I'm just propagating all. :(
        environment = [
            'PYTHONPATH': jarFile,
            'ANDROID_HOME': System.getenv('ANDROID_HOME'),
            'ANDROID_SDK': System.getenv('ANDROID_SDK'),
            'PATH': System.getenv('PATH'),
            'ANDROID_SERIAL': System.getenv('ANDROID_SERIAL'),
        ]
        args = ['-m', 'android_screenshot_tests.pull_screenshots', "--apk", output.toString()]

        if (recordMode) {
          args += ["--record", project.screenshots.recordDir]
        } else if (verifyMode) {
          args += ["--verify", project.screenshots.recordDir]
        }
      }
    }

    project.task("clearScreenshots") << {
      project.exec {
        executable = project.screenshots.adb
        args = ["shell", "rm", "-rf", "/sdcard/screenshots"]
        ignoreExitValue = true
      }
    }

    project.afterEvaluate {
      project.task("screenshotTests")
      project.screenshotTests.dependsOn project.clearScreenshots
      project.screenshotTests.dependsOn project.connectedAndroidTest
      project.screenshotTests.dependsOn project.pullScreenshots
    }

    if (!project.screenshots.customTestRunner) {
       project.android.defaultConfig {
           testInstrumentationRunner = 'com.facebook.testing.screenshot.ScreenshotTestRunner'
       }
    }


    project.task("recordMode") << {
      recordMode = true
    }

    project.task("verifyMode") << {
      verifyMode = true
    }
  }
}
{'fraction_non_alphanumeric': 0.1191992720655141, 'fraction_numerical': 0.0, 'mean_word_length': 2.962509012256669, 'pattern_counts': {'":': 5, '<': 21, '<?xml version=': 0, '>': 5, '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': '2565802', 'n_tokens_mistral': 1672, 'n_tokens_neox': 1565, 'n_words': 277}