Skip to content

Commit

Permalink
Include the Scripts into the Gradle Build (#2076)
Browse files Browse the repository at this point in the history
* Use gradle for the scripts.

* Build the scripts as part of the tests
  • Loading branch information
PaulWoitaschek committed Aug 6, 2023
1 parent 384b1da commit 63901b4
Show file tree
Hide file tree
Showing 15 changed files with 103 additions and 78 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/voice.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
run: ./ci_signing_setup.sh

- name: Test
run: ./gradlew testDebugUnitTest lintKotlin app:lintDebug app:assembleDebug
run: ./gradlew testDebugUnitTest lintKotlin :app:lintDebug :app:assembleDebug :scripts:build

- uses: actions/upload-artifact@v3
if: always()
Expand Down
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

plugins {
alias(libs.plugins.kotlin.android) apply false
alias(libs.plugins.kotlin.jvm) apply false
alias(libs.plugins.android.app) apply false
alias(libs.plugins.android.library) apply false
id("voice.ktlint")
Expand Down
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ uiautomator = "androidx.test.uiautomator:uiautomator:2.2.0"
okhttp = "com.squareup.okhttp3:okhttp:4.11.0"
review = "com.google.android.play:review-ktx:2.0.1"
lottie = "com.airbnb.android:lottie-compose:6.1.0"
clikt = "com.github.ajalt.clikt:clikt:4.2.0"

coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutines" }
coroutines-guava = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-guava", version.ref = "coroutines" }
Expand Down
2 changes: 2 additions & 0 deletions new_module.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
./scripts/run.sh new-feature-module "$@"
2 changes: 2 additions & 0 deletions populate_test_files.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
./scripts/run.sh populate-test-files "$@"
12 changes: 12 additions & 0 deletions scripts/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
plugins {
alias(libs.plugins.kotlin.jvm)
application
}

application {
mainClass.set("voice.scripts.ScriptKt")
}

dependencies {
implementation(libs.clikt)
}
4 changes: 4 additions & 0 deletions scripts/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
set -e
./gradlew :scripts:installDist --quiet --console=plain
./scripts/build/install/scripts/bin/scripts "$@"
14 changes: 2 additions & 12 deletions new_module.main.kts → .../kotlin/voice/scripts/NewFeatureModule.kt
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#!/usr/bin/env kotlin
@file:DependsOn("com.github.ajalt.clikt:clikt-jvm:4.2.0")
@file:CompilerOptions("-Xopt-in=kotlin.RequiresOptIn")
package voice.scripts

import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.parameters.arguments.argument
import com.github.ajalt.clikt.parameters.arguments.validate
import java.io.File

class NewFeatureModule : CliktCommand(name = "Creates a new feature module") {
class NewFeatureModule : CliktCommand(help = "Creates a new feature module") {

private val name: String by argument("The new module name, i.e. :unicorn:wings")
.validate { name ->
Expand Down Expand Up @@ -46,12 +44,6 @@ class NewFeatureModule : CliktCommand(name = "Creates a new feature module") {
settingsGradle.writeText(lines.joinToString(separator = "\n"))
}

private fun String.toSnakeCase(): String {
return "([a-z])([A-Z]+)".toRegex()
.replace(this, "$1_$2")
.lowercase()
}

private fun gradleContent(): String {
val plugins = buildList {
add("voice.library")
Expand All @@ -75,5 +67,3 @@ class NewFeatureModule : CliktCommand(name = "Creates a new feature module") {
}
}
}

NewFeatureModule().main(args)
20 changes: 2 additions & 18 deletions populate_test_files.main.kts → ...kotlin/voice/scripts/PopulateTestFiles.kt
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#!/usr/bin/env kotlin
@file:DependsOn("com.github.ajalt.clikt:clikt-jvm:4.2.0")
@file:CompilerOptions("-Xopt-in=kotlin.RequiresOptIn")
package voice.scripts

import com.github.ajalt.clikt.core.CliktCommand
import java.io.File

class PopulateTestFiles : CliktCommand(name = "Populates test files using adb") {
class PopulateTestFiles : CliktCommand(help = "Populates test files using adb") {

private val testFilesRoot = File("build/testfiles/")

Expand Down Expand Up @@ -80,18 +78,4 @@ class PopulateTestFiles : CliktCommand(name = "Populates test files using adb")
output.absolutePath,
)
}

private fun runCommand(vararg command: String) {
println("runCommand=${command.toList()}")
val process = ProcessBuilder(command.toList())
.redirectOutput(ProcessBuilder.Redirect.INHERIT)
.start()
process.waitFor().also {
check(it == 0) {
"output=${process.errorStream.bufferedReader().readText()}"
}
}
}
}

PopulateTestFiles().main(args)
12 changes: 12 additions & 0 deletions scripts/src/main/kotlin/voice/scripts/RunCommand.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package voice.scripts

fun runCommand(vararg command: String) {
val process = ProcessBuilder(command.toList())
.redirectOutput(ProcessBuilder.Redirect.INHERIT)
.start()
process.waitFor().also {
check(it == 0) {
"output=${process.errorStream.bufferedReader().readText()}"
}
}
}
18 changes: 18 additions & 0 deletions scripts/src/main/kotlin/voice/scripts/Script.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package voice.scripts

import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.core.subcommands

class Script : CliktCommand() {
override fun run() {}
}

fun main(args: Array<String>) {
Script()
.subcommands(
UpdateScreenshots(),
NewFeatureModule(),
PopulateTestFiles(),
)
.main(args)
}
43 changes: 43 additions & 0 deletions scripts/src/main/kotlin/voice/scripts/UpdateScreenshots.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package voice.scripts

import com.github.ajalt.clikt.core.CliktCommand
import java.io.File

class UpdateScreenshots : CliktCommand() {

override fun run() {
// https://issuetracker.google.com/issues/193118030
runCommand("./gradlew", ":app:packageDebugAndroidTest")
runCommand(
"./gradlew",
":app:screenshotDevicesGroupDebugAndroidTest",
"-Pandroid.testInstrumentationRunnerArguments.class=voice.app.misc.ScreenshotCapture",
"-Dorg.gradle.workers.max=1",
)

mapOf(
"pixel5" to "phone",
"nexus7" to "tablet",
"nexus10" to "large-tablet",
)
.flatMap { (device, qualifier) ->
val deviceOutput = File("app/build/outputs/managed_device_android_test_additional_output", device)
listOf(
"book_overview_grid",
"book_overview_list",
"settings",
"folder_picker",
)
.mapIndexed { index, screenshotName ->
val sourceFile = File(deviceOutput, "$screenshotName.png")
val qualifierFolder = File("app/src/main/play/listings/en-US/graphics", "$qualifier-screenshots")
val targetFile = File(qualifierFolder, "${index + 1}.png")
sourceFile to targetFile
}
}
.forEach { (sourceFile, targetFile) ->
println("copy $sourceFile to $targetFile")
sourceFile.copyTo(targetFile, overwrite = true)
}
}
}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ include(":datastore")
include(":folderPicker")
include(":bookOverview")
include(":migration")
include(":scripts")
include(":logging:core")
include(":logging:debug")
include(":logging:crashlytics")
Expand Down
47 changes: 0 additions & 47 deletions update_screenshots.main.kts

This file was deleted.

2 changes: 2 additions & 0 deletions update_screenshots.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
./scripts/run.sh update-screenshots "$@"

0 comments on commit 63901b4

Please sign in to comment.