Skip to content

Commit

Permalink
Add deleteOldScreenshots option
Browse files Browse the repository at this point in the history
  • Loading branch information
takahirom committed Nov 4, 2024
1 parent c908cf3 commit 2960963
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,13 @@ dependencies {
""".trimIndent()
)
}
buildFile.appendText(
"""
roborazzi {
deleteOldScreenshots = true
}
""".trimIndent()
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTargetWithTests
import org.jetbrains.kotlin.gradle.targets.jvm.KotlinJvmTarget
import org.jetbrains.kotlin.gradle.targets.native.KotlinNativeBinaryTestRun
import org.jetbrains.kotlin.gradle.targets.native.tasks.KotlinNativeTest
import java.io.File
import java.io.FileNotFoundException
import java.util.Locale
import javax.inject.Inject
Expand All @@ -45,6 +46,9 @@ private const val DEFAULT_TEMP_DIR = "intermediates/roborazzi"

open class RoborazziExtension @Inject constructor(objects: ObjectFactory) {
val outputDir: DirectoryProperty = objects.directoryProperty()
@ExperimentalRoborazziApi
val deleteOldScreenshots: Property<Boolean> = objects.property(Boolean::class.java)
.convention(false)

@ExperimentalRoborazziApi
val generateComposePreviewRobolectricTests: GenerateComposePreviewRobolectricTestsExtension =
Expand Down Expand Up @@ -77,7 +81,7 @@ abstract class RoborazziPlugin : Plugin<Project> {
else -> throw IllegalStateException("Unsupported test task type: $this")
}

@OptIn(InternalRoborazziApi::class)
@OptIn(InternalRoborazziApi::class, ExperimentalRoborazziApi::class)
override fun apply(project: Project) {
val extension = project.extensions.create("roborazzi", RoborazziExtension::class.java)

Expand Down Expand Up @@ -292,6 +296,20 @@ abstract class RoborazziPlugin : Plugin<Project> {
val roborazziResults = CaptureResults.from(results)
finalizeTestTask.infoln("Roborazzi: Save result to ${resultsSummaryFile.absolutePath} with results:${results.size} summary:${roborazziResults.resultSummary}")

if (extension.deleteOldScreenshots.get()) {
// Remove all files not in the results
val removingFiles: MutableList<File> = outputDir.get().asFile
.listFiles()?.toList().orEmpty().toMutableList()
roborazziResults.captureResults.forEach { result ->
removingFiles.removeIf { file ->
file.name == result.actualFile || file.name == result.compareFile || file.name == result.goldenFile
}
}
removingFiles.forEach { file ->
file.delete()
}
}

val jsonResult = roborazziResults.toJson()
resultsSummaryFile.parentFile.mkdirs()
resultsSummaryFile.writeText(jsonResult)
Expand Down

0 comments on commit 2960963

Please sign in to comment.