-
Notifications
You must be signed in to change notification settings - Fork 943
Expand file tree
/
Copy pathMaestroFlowParser.kt
More file actions
584 lines (545 loc) · 21.9 KB
/
Copy pathMaestroFlowParser.kt
File metadata and controls
584 lines (545 loc) · 21.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
/*
*
* Copyright (c) 2022 mobile.dev inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*/
package maestro.orchestra.yaml
import com.fasterxml.jackson.core.JsonLocation
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.core.JsonProcessingException
import com.fasterxml.jackson.core.JsonToken
import com.fasterxml.jackson.core.TreeNode
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.JsonDeserializer
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.exc.MismatchedInputException
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException
import com.fasterxml.jackson.databind.module.SimpleModule
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator
import com.fasterxml.jackson.module.kotlin.KotlinModule
import com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException
import maestro.orchestra.MaestroCommand
import maestro.orchestra.WorkspaceConfig
import maestro.orchestra.error.InvalidFlowFile
import maestro.orchestra.error.MediaFileNotFound
import maestro.orchestra.util.Env.withEnv
import org.intellij.lang.annotations.Language
import java.nio.file.Path
import java.nio.file.Paths
import kotlin.io.path.absolute
import kotlin.io.path.isDirectory
import kotlin.io.path.readText
import kotlin.reflect.full.primaryConstructor
import kotlin.reflect.jvm.javaType
// TODO:
// - Sanity check: Parsed workspace equality check for 10 users on cloud
private val yamlFluentCommandConstructor = YamlFluentCommand::class.primaryConstructor!!
private val yamlFluentCommandParameters = yamlFluentCommandConstructor.parameters
private val yamlFluentCommandLocationParameter = yamlFluentCommandParameters.first { it.name == "_location" }
private val objectCommands = yamlFluentCommandConstructor.parameters.map { it.name!! }
private val stringCommands = mapOf<String, (JsonLocation) -> YamlFluentCommand>(
"launchApp" to { location -> YamlFluentCommand(
_location = location,
launchApp = YamlLaunchApp(
appId = null,
clearState = null,
clearKeychain = null,
stopApp = null,
permissions = null,
arguments = null,
),
)},
"stopApp" to { location -> YamlFluentCommand(
_location = location,
stopApp = YamlStopApp()
)},
"killApp" to { location -> YamlFluentCommand(
_location = location,
killApp = YamlKillApp()
)},
"clearState" to { location -> YamlFluentCommand(
_location = location,
clearState = YamlClearState(
appId = null,
)
)},
"clearKeychain" to { location -> YamlFluentCommand(
_location = location,
clearKeychain = YamlActionClearKeychain(),
)},
"eraseText" to { location -> YamlFluentCommand(
_location = location,
eraseText = YamlEraseText(charactersToErase = null)
)},
"inputRandomText" to { location -> YamlFluentCommand(
_location = location,
inputRandomText = YamlInputRandomText(length = 8),
)},
"inputRandomNumber" to { location -> YamlFluentCommand(
_location = location,
inputRandomNumber = YamlInputRandomNumber(length = 8),
)},
"inputRandomEmail" to { location -> YamlFluentCommand(
_location = location,
inputRandomEmail = YamlInputRandomEmail(),
)},
"inputRandomPersonName" to { location -> YamlFluentCommand(
_location = location,
inputRandomPersonName = YamlInputRandomPersonName(),
)},
"back" to { location -> YamlFluentCommand(
_location = location,
back = YamlActionBack(),
)},
"hideKeyboard" to { location -> YamlFluentCommand(
_location = location,
hideKeyboard = YamlActionHideKeyboard(),
)},
"hide keyboard" to { location -> YamlFluentCommand(
_location = location,
hideKeyboard = YamlActionHideKeyboard(),
)},
"pasteText" to { location -> YamlFluentCommand(
_location = location,
pasteText = YamlActionPasteText(),
)},
"scroll" to { location -> YamlFluentCommand(
_location = location,
scroll = YamlActionScroll(),
)},
"waitForAnimationToEnd" to { location -> YamlFluentCommand(
_location = location,
waitForAnimationToEnd = YamlWaitForAnimationToEndCommand(timeout = null)
)},
"stopRecording" to { location -> YamlFluentCommand(
_location = location,
stopRecording = YamlStopRecording()
)},
"toggleAirplaneMode" to { location -> YamlFluentCommand(
_location = location,
toggleAirplaneMode = YamlToggleAirplaneMode()
)},
"toggleDarkMode" to { location -> YamlFluentCommand(
_location = location,
toggleDarkMode = YamlToggleDarkMode()
)},
"assertNoDefectsWithAI" to { location -> YamlFluentCommand(
_location = location,
assertNoDefectsWithAI = YamlAssertNoDefectsWithAI()
)},
)
private val allCommands = (stringCommands.keys + objectCommands).distinct()
private const val DOCS_FIRST_FLOW = "https://docs.maestro.dev/getting-started/writing-your-first-flow"
private const val DOCS_COMMANDS = "https://docs.maestro.dev/api-reference/commands"
private class ParseException(
val location: JsonLocation,
val title: String,
@Language("markdown") val errorMessage: String,
val docs: String? = null,
) : RuntimeException("$title: $errorMessage")
private inline fun <reified T : Throwable> findException(e: Throwable): T? {
return findException(e, T::class.java)
}
private fun <T : Throwable> findException(e: Throwable, type: Class<T>): T? {
return if (type.isInstance(e)) type.cast(e) else e.cause?.let { findException(it, type) }
}
private fun wrapException(error: Throwable, parser: JsonParser, contentPath: Path, content: String): Exception {
findException<FlowParseException>(error)?.let { return it }
findException<ToCommandsException>(error)?.let { e ->
return when (e.cause) {
is InvalidFlowFile -> FlowParseException(
location = e.location,
contentPath = contentPath,
content = content,
title = "Invalid File Path",
errorMessage = e.cause.message,
)
is MediaFileNotFound -> FlowParseException(
location = e.location,
contentPath = contentPath,
content = content,
title = "Media File Not Found",
errorMessage = e.cause.message,
)
else -> FlowParseException(
location = e.location,
contentPath = contentPath,
content = content,
title = "Parsing Failed",
errorMessage = e.message ?: "Failed to parse content",
)
}
}
findException<ParseException>(error)?.let { e ->
return FlowParseException(
location = e.location,
contentPath = contentPath,
content = content,
title = e.title,
errorMessage = e.errorMessage,
docs = e.docs
)
}
findException<MissingKotlinParameterException>(error)?.let { e ->
return FlowParseException(
location = e.location ?: parser.currentLocation(),
contentPath = contentPath,
content = content,
title = "Config Field Required: ${e.parameter.name}",
errorMessage = """
|The config section is missing a required field: `${e.parameter.name}`. Eg.
|
|```yaml
|appId: com.example.app
|---
|- launchApp
|```
""".trimMargin("|"),
docs = DOCS_FIRST_FLOW,
)
}
findException<UnrecognizedPropertyException>(error)?.let { e ->
val propertyName = e.path.lastOrNull()?.fieldName ?: "<unknown>"
return FlowParseException(
location = e.location ?: parser.currentLocation(),
contentPath = contentPath,
content = content,
title = "Unknown Property: $propertyName",
errorMessage = """
|The property `$propertyName` is not recognized
""".trimMargin("|"),
)
}
findException<MismatchedInputException>(error)?.let { e ->
val path = e.path.joinToString(".") { it.fieldName }
return FlowParseException(
location = e.location ?: parser.currentLocation(),
contentPath = contentPath,
content = content,
title = "Incorrect Format: ${e.path.last().fieldName}",
errorMessage = """
|The format for $path is incorrect
""".trimMargin("|"),
)
}
return FlowParseException(
parser = parser,
contentPath = contentPath,
content = content,
title = "Parsing Failed",
errorMessage = error.message ?: "Failed to parse content",
)
}
private fun String.levenshtein(other: String): Int {
val dp = Array(length + 1) { IntArray(other.length + 1) }
for (i in 0..length) dp[i][0] = i
for (j in 0..other.length) dp[0][j] = j
for (i in 1..length)
for (j in 1..other.length)
dp[i][j] = if (this[i - 1] == other[j - 1]) dp[i - 1][j - 1]
else 1 + minOf(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1])
return dp[length][other.length]
}
private fun String.findSimilar(others: Iterable<String>, threshold: Int) =
others.sortedBy { levenshtein(it) }.takeWhile { it.levenshtein(this) <= threshold }
private object YamlCommandDeserializer : JsonDeserializer<YamlFluentCommand>() {
override fun deserialize(p: JsonParser, ctxt: DeserializationContext): YamlFluentCommand {
return when (p.currentToken) {
JsonToken.VALUE_STRING -> parseStringCommand(p)
JsonToken.START_OBJECT -> parseObjectCommand(p)
else -> throw ParseException(
location = p.currentLocation(),
title = "Invalid Command",
errorMessage = """
|Invalid command format. Expected: "<commandName>: <options>" eg. "tapOn: submit"
""".trimMargin("|"),
docs = DOCS_COMMANDS,
)
}
}
private fun parseStringCommand(parser: JsonParser): YamlFluentCommand {
val commandLocation = parser.currentLocation()
val commandText = parser.text
val command = stringCommands[commandText]
if (command != null) return command(parser.currentLocation())
if (commandText in objectCommands) {
throw ParseException(
location = commandLocation,
title = "Missing Command Options",
errorMessage = """
|The command `$commandText` requires additional options.
""".trimMargin("|"),
// TODO: Add docs link
)
}
throw ParseException(
location = commandLocation,
title = "Invalid Command: $commandText",
errorMessage = """
|`$commandText` is not a valid command.
|
|${suggestCommandMessage(commandText)}
""".trimMargin("|").trim(),
docs = DOCS_COMMANDS,
)
}
private fun parseObjectCommand(parser: JsonParser): YamlFluentCommand {
val commandLocation = parser.currentLocation()
val commandName = parser.nextFieldName()
val commandParameter = yamlFluentCommandParameters.firstOrNull { it.name == commandName }
if (commandParameter == null) {
throw ParseException(
location = parser.currentLocation(),
title = "Invalid Command: $commandName",
errorMessage = """
|`$commandName` is not a valid command.
|
|${suggestCommandMessage(commandName)}
""".trimMargin("|").trim(),
)
}
if (parser.nextToken() == JsonToken.VALUE_NULL) {
throw ParseException(
location = parser.currentLocation(),
title = "Incorrect Command Format: $commandName",
errorMessage = """
|The command `$commandName` requires additional options.
""".trimMargin("|"),
)
}
val commandType = (parser.codec as ObjectMapper).constructType(commandParameter.type.javaType)
val command = parser.codec.readValue<Any>(parser, commandType)
val fluentCommand = yamlFluentCommandConstructor.callBy(mapOf(
yamlFluentCommandLocationParameter to commandLocation,
commandParameter to command,
))
val nextToken = parser.nextToken()
if (nextToken == JsonToken.END_OBJECT) return fluentCommand
if (nextToken == JsonToken.FIELD_NAME) {
val fieldName = parser.currentName()
throw ParseException(
location = parser.currentLocation(),
title = "Invalid Command Format: $commandName",
errorMessage = """
|Found unexpected top-level field: `$fieldName`. Missing an indent or dash?
|
|Example of correctly formatted list of commands:
|```yaml
|- tapOn:
| text: submit
| optional: true
|- inputText: hello
|```
""".trimMargin("|"),
)
}
throw ParseException(
location = commandLocation,
title = "Invalid Command Format: $commandName",
errorMessage = """
|Commands must be in the format: `<commandName>: <options>` eg. `tapOn: submit`
|
|Example of correctly formatted list of commands:
|```yaml
|- tapOn:
| text: submit
| optional: true
|- inputText: hello
|```
""".trimMargin("|"),
)
}
private fun suggestCommandMessage(invalidCommand: String): String {
val prefixCommands = if (invalidCommand.length < 3) emptyList() else allCommands.filter { it.startsWith(invalidCommand) || invalidCommand.startsWith(it) }
val substringCommmands = if (invalidCommand.length < 3) emptyList() else allCommands.filter { it.contains(invalidCommand) || invalidCommand.contains(it) }
val similarCommands = invalidCommand.findSimilar(allCommands, threshold = 3)
val suggestions = (prefixCommands + similarCommands + substringCommmands).distinct()
return when {
suggestions.isEmpty() -> ""
suggestions.size == 1 -> "Did you mean `${suggestions.first()}`?"
else -> "Did you mean one of: ${suggestions.joinToString(", ")}"
}
}
}
class FlowParseException(
location: JsonLocation,
val contentPath: Path,
val content: String,
val title: String,
@Language("markdown") val errorMessage: String,
val docs: String? = null,
) : JsonProcessingException("$title\n$errorMessage", location) {
constructor(parser: JsonParser, contentPath: Path, content: String, title: String, @Language("markdown") errorMessage: String, docs: String? = null) : this(
location = parser.currentLocation(),
contentPath = contentPath,
content = content,
title = title,
errorMessage = errorMessage,
docs = docs,
)
}
object MaestroFlowParser {
private val MAPPER = ObjectMapper(YAMLFactory().apply {
disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER)
}).apply {
registerModule(KotlinModule.Builder().build())
registerModule(SimpleModule().apply {
addDeserializer(YamlFluentCommand::class.java, YamlCommandDeserializer)
})
}
fun parseFlow(flowPath: Path, flow: String): List<MaestroCommand> {
MAPPER.createParser(flow).use { parser ->
try {
val config = parseConfig(parser)
val commands = parseCommands(parser)
val maestroCommands = commands
.flatMap { it.toCommands(flowPath, config.appId) }
.withEnv(config.env)
return listOfNotNull(config.toCommand(flowPath), *maestroCommands.toTypedArray())
} catch (e: Throwable) {
throw wrapException(e, parser, flowPath, flow)
}
}
}
fun parseCommand(flowPath: Path, appId: String, command: String): List<MaestroCommand> {
MAPPER.createParser(command).use { parser ->
try {
return parser.readValueAs(YamlFluentCommand::class.java).toCommands(flowPath, appId)
} catch (e: Throwable) {
throw wrapException(e, parser, flowPath, command)
}
}
}
fun parseConfigOnly(flowPath: Path, flow: String): YamlConfig {
MAPPER.createParser(flow).use { parser ->
try {
return parseConfig(parser)
} catch (e: Throwable) {
throw wrapException(e, parser, flowPath, flow)
}
}
}
fun parseWorkspaceConfig(configPath: Path, workspaceConfig: String): WorkspaceConfig {
MAPPER.createParser(workspaceConfig).use { parser ->
try {
return parser.readValueAs(WorkspaceConfig::class.java)
} catch (e: Throwable) {
throw wrapException(e, parser, configPath, workspaceConfig)
}
}
}
fun parseWatchFiles(flowPath: Path): List<Path> {
val flow = flowPath.readText()
MAPPER.createParser(flow).use { parser ->
try {
parseConfig(parser)
val commands = parseCommands(parser)
val commandWatchFiles = commands.flatMap { it.getWatchFiles(flowPath) }
return (listOf(flowPath) + commandWatchFiles)
.filter { it.absolute().parent?.isDirectory() ?: false }
} catch (e: Throwable) {
throw wrapException(e, parser, flowPath, flow)
}
}
}
fun formatCommands(commands: List<String>): String {
return MAPPER.writeValueAsString(commands.map { MAPPER.readTree(it) })
}
fun checkSyntax(maestroCode: String) {
MAPPER.createParser(maestroCode).use { parser ->
val node = parser.readValueAsTree<TreeNode>()
if (node.isArray) {
checkCommandListSyntax(maestroCode)
} else if (node.isObject && parser.nextToken() != null) {
checkFlowSyntax(maestroCode)
} else {
checkCommandSyntax(maestroCode)
}
}
}
private fun checkCommandListSyntax(maestroCode: String) {
MAPPER.createParser(maestroCode).use { parser ->
try {
parseCommands(parser)
} catch (e: Throwable) {
throw wrapException(e, parser, Paths.get("/syntax-checker"), maestroCode)
}
}
}
private fun checkCommandSyntax(command: String) {
MAPPER.createParser(command).use { parser ->
try {
parser.readValueAs(YamlFluentCommand::class.java)
} catch (e: Throwable) {
throw wrapException(e, parser, Paths.get("/syntax-checker"), command)
}
}
}
private fun checkFlowSyntax(flow: String) {
MAPPER.createParser(flow).use { parser ->
try {
parseConfig(parser)
parseCommands(parser)
} catch (e: Throwable) {
throw wrapException(e, parser, Paths.get("/syntax-checker"), flow)
}
}
}
private fun parseCommands(parser: JsonParser): List<YamlFluentCommand> {
if (parser.nextToken() != JsonToken.START_ARRAY) {
throw ParseException(
location = parser.currentLocation(),
title = "Commands Section Required",
errorMessage = """
|Flow files must have a list of commands after the config section. Eg:
|
|```yaml
|appId: com.example.app
|---
|- launchApp
|```
""".trimMargin("|"),
docs = DOCS_FIRST_FLOW,
)
}
val commands = mutableListOf<YamlFluentCommand>()
while (parser.nextToken() != JsonToken.END_ARRAY) {
val command = parser.readValueAs(YamlFluentCommand::class.java)
commands.add(command)
}
return commands
}
private fun parseConfig(parser: JsonParser): YamlConfig {
if (parser.nextToken() != JsonToken.START_OBJECT) {
throw ParseException(
location = parser.currentLocation(),
title = "Config Section Required",
errorMessage = """
|Flow files must start with a config section. Eg:
|
|```yaml
|appId: com.example.app # <-- config section
|---
|- launchApp
|```
""".trimMargin("|"),
docs = DOCS_FIRST_FLOW,
)
}
return parser.readValueAs(YamlConfig::class.java)
}
}