Skip to content

Commit

Permalink
Support configuration cache (second attempt) (#307)
Browse files Browse the repository at this point in the history
* Avoid using global variable because Gradle has no idea about it and cannot recreate when configuration cache is reused. Use explicit task input instead.
* Annotate other task inputs.
* Convert internal "shot" property to function. It's used only once by each task anyway.
  • Loading branch information
MyDogTom authored May 15, 2022
1 parent e2e1fb0 commit 656f6a0
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 55 deletions.
4 changes: 0 additions & 4 deletions core/src/main/scala/com/karumi/shot/Shot.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ class Shot(
consoleReporter: ConsoleReporter,
envVars: EnvVars
) {
def configureAdbPath(adbPath: Folder): Unit = {
Adb.adbBinaryPath = adbPath
}

def downloadScreenshots(appId: AppId, shotFolder: ShotFolder, orchestrated: Boolean): Unit = {
console.show("⬇️ Pulling screenshots from your connected devices!")
pullScreenshots(appId, shotFolder, orchestrated)
Expand Down
9 changes: 5 additions & 4 deletions core/src/main/scala/com/karumi/shot/android/Adb.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import com.karumi.shot.domain.model.{AppId, Folder}
import scala.sys.process._

object Adb {
var adbBinaryPath: String = ""
// To be able to support API 29+ with scoped storage we need to change
// the base url where the app saves our screenshots inside the device.
// This value is computed in runtime in shot-android AndroidStorageInfo.
private val baseStoragePath = "/storage/emulated/0/Download"
}

class Adb {
class Adb(
adbPath: String
) {

private final val CR_ASCII_DECIMAL = 13
private val logger = ProcessLogger(
Expand Down Expand Up @@ -94,10 +95,10 @@ class Adb {
}

private def executeAdbCommand(command: String): Int =
s"${Adb.adbBinaryPath} $command" ! logger
s"${adbPath} $command" ! logger

private def executeAdbCommandWithResult(command: String): String =
s"${Adb.adbBinaryPath} $command" !! logger
s"${adbPath} $command" !! logger

private def isCarriageReturnASCII(device: String): Boolean =
device.charAt(0) == CR_ASCII_DECIMAL
Expand Down
8 changes: 0 additions & 8 deletions core/src/test/scala/com/karumi/shot/ShotSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,6 @@ class ShotSpec
)
}

it should "configure adb path" in {
val anyAdbPath = "/Library/androidsdk/bin/adb"

shot.configureAdbPath(anyAdbPath)

Adb.adbBinaryPath shouldBe anyAdbPath
}

it should "should delegate screenshots cleaning to Adb using the specified ANDROID_SERIAL env var" in {
val appId: AppId = AppIdMother.anyAppId
val device1: String = "emulator-5554"
Expand Down
33 changes: 9 additions & 24 deletions shot/src/main/scala/com/karumi/shot/ShotPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,8 @@ package com.karumi.shot
import com.android.build.gradle.api.BaseVariant
import com.android.build.gradle.{AppExtension, LibraryExtension}
import com.android.builder.model.{BuildType, ProductFlavor}
import com.karumi.shot.android.Adb
import com.karumi.shot.base64.Base64Encoder
import com.karumi.shot.domain.Config
import com.karumi.shot.exceptions.ShotException
import com.karumi.shot.reports.{ConsoleReporter, ExecutionReporter}
import com.karumi.shot.screenshots.{
ScreenshotsComparator,
ScreenshotsDiffGenerator,
ScreenshotsSaver
}
import com.karumi.shot.system.EnvVars
import com.karumi.shot.tasks.{
DownloadScreenshotsTask,
ExecuteScreenshotTests,
Expand All @@ -29,33 +20,23 @@ import scala.util.Try
class ShotPlugin extends Plugin[Project] {

private val console = new Console
private lazy val shot: Shot =
new Shot(
new Adb,
new Files,
new ScreenshotsComparator,
new ScreenshotsDiffGenerator(new Base64Encoder),
new ScreenshotsSaver,
console,
new ExecutionReporter,
new ConsoleReporter(console),
new EnvVars()
)

override def apply(project: Project): Unit = {
addExtensions(project)
addAndroidTestDependency(project)
project.afterEvaluate { project =>
{
configureAdb(project)
addTasks(project)
}
}
}

private def configureAdb(project: Project): Unit = {
val adbPath = AdbPathExtractor.extractPath(project)
shot.configureAdbPath(adbPath)
}

private def findAdbPath(project: Project): String = {
AdbPathExtractor.extractPath(project)
}

private def addTasks(project: Project): Unit = {
Expand Down Expand Up @@ -170,7 +151,7 @@ class ShotPlugin extends Plugin[Project] {
RemoveScreenshotsTask.name(flavor, buildType, beforeExecution = true),
classOf[RemoveScreenshotsTask]
)

val adbPath = findAdbPath(project)
removeScreenshotsAfterExecution.configure { task =>
task.setDescription(RemoveScreenshotsTask.description(flavor, buildType))
task.flavor = flavor
Expand All @@ -187,6 +168,7 @@ class ShotPlugin extends Plugin[Project] {
task.recordScreenshots = project.hasProperty("record")
task.printBase64 = project.hasProperty("printBase64")
task.projectName = project.getName
task.adbPath = adbPath
}
removeScreenshotsBeforeExecution.configure { task =>
task.setDescription(RemoveScreenshotsTask.description(flavor, buildType))
Expand All @@ -204,6 +186,7 @@ class ShotPlugin extends Plugin[Project] {
task.recordScreenshots = project.hasProperty("record")
task.printBase64 = project.hasProperty("printBase64")
task.projectName = project.getName
task.adbPath = adbPath
}

val downloadScreenshots = tasks
Expand All @@ -224,6 +207,7 @@ class ShotPlugin extends Plugin[Project] {
task.recordScreenshots = project.hasProperty("record")
task.printBase64 = project.hasProperty("printBase64")
task.projectName = project.getName
task.adbPath = adbPath
}
val executeScreenshot = tasks
.register(ExecuteScreenshotTests.name(flavor, buildType), classOf[ExecuteScreenshotTests])
Expand All @@ -243,6 +227,7 @@ class ShotPlugin extends Plugin[Project] {
task.recordScreenshots = project.hasProperty("record")
task.printBase64 = project.hasProperty("printBase64")
task.projectName = project.getName
task.adbPath = adbPath
}

if (runInstrumentation(project, extension)) {
Expand Down
32 changes: 17 additions & 15 deletions shot/src/main/scala/com/karumi/shot/tasks/Tasks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,28 @@ import com.karumi.shot.screenshots.{
import com.karumi.shot.system.EnvVars
import com.karumi.shot.ui.Console
import com.karumi.shot.{Files, Shot, ShotExtension}
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.{Input, TaskAction}
import org.gradle.api.{DefaultTask, GradleException}

import java.io.File

abstract class ShotTask extends DefaultTask {
var appId: String = _
var flavor: Option[String] = _
var buildTypeName: String = _
var orchestrated: Boolean = false
var projectPath: String = _
var buildPath: String = _
var shotExtension: ShotExtension = _
var directorySuffix: Option[String] = _
var recordScreenshots: Boolean = _
var printBase64: Boolean = _
var projectName: String = _
private val console = new Console
protected val shot: Shot =
@Input var appId: String = _
@Input var flavor: Option[String] = _
@Input var buildTypeName: String = _
@Input var orchestrated: Boolean = false
@Input var projectPath: String = _
@Input var buildPath: String = _
@Input var shotExtension: ShotExtension = _
@Input var directorySuffix: Option[String] = _
@Input var recordScreenshots: Boolean = _
@Input var printBase64: Boolean = _
@Input var projectName: String = _
@Input var adbPath: String = _
protected def shot: Shot = {
val console = new Console
new Shot(
new Adb,
new Adb(adbPath),
new Files,
new ScreenshotsComparator,
new ScreenshotsDiffGenerator(new Base64Encoder),
Expand All @@ -43,6 +44,7 @@ abstract class ShotTask extends DefaultTask {
new ConsoleReporter(console),
new EnvVars()
)
}

protected def shotFolder: ShotFolder = {
ShotFolder(
Expand Down

0 comments on commit 656f6a0

Please sign in to comment.