Skip to content

Commit

Permalink
Mark all initFlow-related constructs as @deprecated (#1935)
Browse files Browse the repository at this point in the history
* mark all initFlow-related constructs as @deprecated

* remove all initFlow-related code
  • Loading branch information
bartekpacia authored Aug 23, 2024
1 parent 9e2ae11 commit 8ba33f7
Show file tree
Hide file tree
Showing 23 changed files with 25 additions and 391 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

package maestro.cli.runner

import io.ktor.client.utils.EmptyContent.status
import maestro.Maestro
import maestro.MaestroException
import maestro.cli.device.Device
Expand All @@ -31,9 +30,7 @@ import maestro.cli.runner.resultview.UiState
import maestro.orchestra.ApplyConfigurationCommand
import maestro.orchestra.CompositeCommand
import maestro.orchestra.MaestroCommand
import maestro.orchestra.MaestroConfig
import maestro.orchestra.Orchestra
import maestro.orchestra.OrchestraAppState
import maestro.orchestra.yaml.YamlCommandReader
import maestro.utils.Insight
import org.slf4j.LoggerFactory
Expand All @@ -50,9 +47,8 @@ object MaestroCommandRunner {
view: ResultView,
commands: List<MaestroCommand>,
debug: FlowDebugMetadata
): Result {
): Boolean {
val config = YamlCommandReader.getConfig(commands)
val initFlow = config?.initFlow
val onFlowComplete = config?.onFlowComplete
val onFlowStart = config?.onFlowStart

Expand Down Expand Up @@ -92,11 +88,6 @@ object MaestroCommandRunner {
view.setState(
UiState.Running(
device = device,
initCommands = toCommandStates(
initFlow?.commands ?: emptyList(),
commandStatuses,
commandMetadata
),
onFlowStartCommands = toCommandStates(
onFlowStart?.commands ?: emptyList(),
commandStatuses,
Expand Down Expand Up @@ -183,8 +174,7 @@ object MaestroCommandRunner {
)

val flowSuccess = orchestra.runFlow(commands)

return Result(flowSuccess = flowSuccess, cachedAppState = null)
return flowSuccess
}

private fun toCommandStates(
Expand Down Expand Up @@ -216,10 +206,4 @@ object MaestroCommandRunner {
)
}
}

data class Result(
val flowSuccess: Boolean,
val cachedAppState: OrchestraAppState?
)
}

34 changes: 5 additions & 29 deletions maestro-cli/src/main/java/maestro/cli/runner/TestRunner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,13 @@ import maestro.cli.runner.resultview.ResultView
import maestro.cli.runner.resultview.UiState
import maestro.cli.util.PrintUtils
import maestro.cli.view.ErrorViewUtils
import maestro.debuglog.DebugLogStore
import maestro.debuglog.LogConfig
import maestro.orchestra.MaestroCommand
import maestro.orchestra.MaestroInitFlow
import maestro.orchestra.OrchestraAppState
import maestro.orchestra.util.Env.withEnv
import maestro.orchestra.yaml.YamlCommandReader
import org.slf4j.LoggerFactory
import java.io.File
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import kotlin.concurrent.thread
import kotlin.io.path.absolutePathString

object TestRunner {

Expand Down Expand Up @@ -63,7 +54,7 @@ object TestRunner {
TestDebugReporter.saveFlow(flowFile.name, debug, debugOutputPath)
if (debug.exception != null) PrintUtils.err("${debug.exception?.message}")

return if (result.get()?.flowSuccess == true) 0 else 1
return if (result.get() == true) 0 else 1
}

fun runContinuous(
Expand All @@ -77,8 +68,6 @@ object TestRunner {
val fileWatcher = FileWatcher()

var previousCommands: List<MaestroCommand>? = null
var previousInitFlow: MaestroInitFlow? = null
var previousResult: MaestroCommandRunner.Result? = null

var ongoingTest: Thread? = null
do {
Expand All @@ -88,25 +77,16 @@ object TestRunner {
join()
}

val commands = YamlCommandReader.readCommands(flowFile.toPath())
val commands = YamlCommandReader
.readCommands(flowFile.toPath())
.withEnv(env)
val initFlow = getInitFlow(commands)

// Restart the flow if anything has changed
if (commands != previousCommands || initFlow != previousInitFlow) {
if (commands != previousCommands) {
ongoingTest = thread {
// If previous init flow was successful and there were no changes to the init flow,
// then reuse cached app state (and skip the init commands)
val cachedAppState: OrchestraAppState? = if (initFlow == previousInitFlow) {
previousResult?.cachedAppState
} else {
null
}

previousCommands = commands
previousInitFlow = initFlow

previousResult = runCatching(resultView, maestro) {
runCatching(resultView, maestro) {
MaestroCommandRunner.runCommands(
maestro,
device,
Expand All @@ -132,10 +112,6 @@ object TestRunner {
} while (true)
}

private fun getInitFlow(commands: List<MaestroCommand>): MaestroInitFlow? {
return YamlCommandReader.getConfig(commands)?.initFlow
}

private fun <T> runCatching(
view: ResultView,
maestro: Maestro,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import io.ktor.util.encodeBase64
import maestro.cli.runner.CommandState
import maestro.cli.runner.CommandStatus
import maestro.utils.Insight
import maestro.utils.Insights
import maestro.utils.chunkStringByWordCount
import org.fusesource.jansi.Ansi

Expand Down Expand Up @@ -66,12 +65,6 @@ class AnsiResultView(
render("Running on ${state.device.description}\n")
}
render("\n")
if (state.initCommands.isNotEmpty()) {
render("\n")
render(" ║ > Init Flow\n")
render("\n")
renderCommands(state.initCommands)
}
if (state.onFlowStartCommands.isNotEmpty()) {
render("\n")
render(" ║ > On Flow Start\n")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,6 @@ class PlainTextResultView: ResultView {
}
}

if (state.initCommands.isNotEmpty()) {
if (shouldPrintStep()) {
println(" > Init Flow")
}

renderCommandsPlainText(state.initCommands)
}

if (state.onFlowStartCommands.isNotEmpty()) {
if (shouldPrintStep()) {
println(" > On Flow Start")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ sealed class UiState {

data class Running(
val device: Device? = null,
val initCommands: List<CommandState> = emptyList(),
val onFlowStartCommands: List<CommandState> = emptyList(),
val onFlowCompleteCommands: List<CommandState> = emptyList(),
val commands: List<CommandState>,
Expand Down
4 changes: 1 addition & 3 deletions maestro-cli/src/main/java/maestro/cli/view/ErrorViewUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package maestro.cli.view

import maestro.MaestroException
import maestro.orchestra.error.InvalidFlowFile
import maestro.orchestra.error.InvalidInitFlowFile
import maestro.orchestra.error.NoInputException
import maestro.orchestra.error.UnicodeNotSupportedError
import maestro.orchestra.error.ValidationError
Expand All @@ -14,7 +13,6 @@ object ErrorViewUtils {
return when (e) {
is ValidationError -> e.message
is NoInputException -> "No commands found in Flow file"
is InvalidInitFlowFile -> "initFlow file is invalid: ${e.initFlowPath}"
is InvalidFlowFile -> "Flow file is invalid: ${e.flowPath}"
is UnicodeNotSupportedError -> "Unicode character input is not supported: ${e.text}. Please use ASCII characters. Follow the issue: https://github.com/mobile-dev-inc/maestro/issues/146"
is InterruptedException -> "Interrupted"
Expand All @@ -24,4 +22,4 @@ object ErrorViewUtils {
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ data class MaestroConfig(
val appId: String? = null,
val name: String? = null,
val tags: List<String>? = emptyList(),
val initFlow: MaestroInitFlow? = null,
val ext: Map<String, Any?> = emptyMap(),
val onFlowStart: MaestroOnFlowStart? = null,
val onFlowComplete: MaestroOnFlowComplete? = null,
Expand All @@ -19,27 +18,13 @@ data class MaestroConfig(
return copy(
appId = appId?.evaluateScripts(jsEngine),
name = name?.evaluateScripts(jsEngine),
initFlow = initFlow?.evaluateScripts(jsEngine),
onFlowComplete = onFlowComplete?.evaluateScripts(jsEngine),
onFlowStart = onFlowStart?.evaluateScripts(jsEngine),
)
}

}

data class MaestroInitFlow(
val appId: String,
val commands: List<MaestroCommand>,
) {

fun evaluateScripts(jsEngine: JsEngine): MaestroInitFlow {
return copy(
appId = appId.evaluateScripts(jsEngine),
)
}

}

data class MaestroOnFlowComplete(val commands: List<MaestroCommand>) {
fun evaluateScripts(jsEngine: JsEngine): MaestroOnFlowComplete {
return this
Expand All @@ -50,4 +35,4 @@ data class MaestroOnFlowStart(val commands: List<MaestroCommand>) {
fun evaluateScripts(jsEngine: JsEngine): MaestroOnFlowStart {
return this
}
}
}
61 changes: 11 additions & 50 deletions maestro-orchestra/src/main/java/maestro/orchestra/Orchestra.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,15 @@

package maestro.orchestra

import maestro.*
import maestro.DeviceInfo
import maestro.ElementFilter
import maestro.Filters
import maestro.Filters.asFilter
import maestro.FindElementResult
import maestro.Maestro
import maestro.MaestroException
import maestro.ScreenRecording
import maestro.ViewHierarchy
import maestro.js.GraalJsEngine
import maestro.js.JsEngine
import maestro.js.RhinoJsEngine
Expand All @@ -30,6 +37,7 @@ import maestro.orchestra.filter.TraitFilters
import maestro.orchestra.geo.Traveller
import maestro.orchestra.util.Env.evaluateScripts
import maestro.orchestra.yaml.YamlCommandReader
import maestro.toSwipeDirection
import maestro.utils.Insight
import maestro.utils.Insights
import maestro.utils.MaestroTimer
Expand All @@ -39,7 +47,6 @@ import okio.buffer
import okio.sink
import java.io.File
import java.lang.Long.max
import java.nio.file.Files

class Orchestra(
private val maestro: Maestro,
Expand Down Expand Up @@ -68,28 +75,13 @@ class Orchestra(

private val rawCommandToMetadata = mutableMapOf<MaestroCommand, CommandMetadata>()

/**
* If initState is provided, initialize app disk state with the provided OrchestraAppState and skip
* any initFlow execution. Otherwise, initialize app state with initFlow if defined.
*/
fun runFlow(
commands: List<MaestroCommand>,
initState: OrchestraAppState? = null,
): Boolean {
fun runFlow(commands: List<MaestroCommand>): Boolean {
timeMsOfLastInteraction = System.currentTimeMillis()

val config = YamlCommandReader.getConfig(commands)

initJsEngine(config)

val state = initState ?: config?.initFlow?.let {
runInitFlow(it) ?: return false
}

if (state != null) {
maestro.clearAppState(state.appId)
}

onFlowStart(commands)

executeDefineVariablesCommands(commands, config)
Expand Down Expand Up @@ -134,32 +126,6 @@ class Orchestra(
}
}

/**
* Run the initFlow and return the resulting app OrchestraAppState which can be used to initialize
* app disk state when past into Orchestra.runFlow.
*/
fun runInitFlow(
initFlow: MaestroInitFlow,
): OrchestraAppState? {
val success = runFlow(
initFlow.commands,
)
if (!success) return null

maestro.stopApp(initFlow.appId)

val stateFile = if (stateDir == null) {
Files.createTempFile(null, ".state")
} else {
Files.createTempFile(stateDir.toPath(), null, ".state")
}

return OrchestraAppState(
appId = initFlow.appId,
file = stateFile.toFile(),
)
}

fun executeCommands(
commands: List<MaestroCommand>,
config: MaestroConfig? = null,
Expand Down Expand Up @@ -356,7 +322,7 @@ class Orchestra(
)

// We do not actually know if there were any mutations, but we assume there were
return true
true
} else {
throw CommandSkipped
}
Expand Down Expand Up @@ -1152,8 +1118,3 @@ class Orchestra(
private const val MAX_ERASE_CHARACTERS = 50
}
}

data class OrchestraAppState(
val appId: String,
val file: File,
)

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,9 @@ object YamlCommandReader {

// Files to watch for changes. Includes any referenced files.
fun getWatchFiles(flowPath: Path): List<Path> = mapParsingErrors(flowPath) {
val (config, commands) = readConfigAndCommands(flowPath)
val configWatchFiles = config.getWatchFiles(flowPath)
val (_, commands) = readConfigAndCommands(flowPath)
val commandWatchFiles = commands.flatMap { it.getWatchFiles(flowPath) }
(listOf(flowPath) + configWatchFiles + commandWatchFiles)
(listOf(flowPath) + commandWatchFiles)
.filter { it.absolute().parent?.isDirectory() ?: false }
}

Expand Down
Loading

0 comments on commit 8ba33f7

Please sign in to comment.