rename handle to evaluate

This commit is contained in:
ansgarz 2022-04-24 19:29:34 +02:00
parent 13cad5b170
commit 968c5d01d6
2 changed files with 29 additions and 28 deletions

View file

@ -7,7 +7,7 @@ import org.slf4j.LoggerFactory
enum class ProgressType { NONE, DOTS, BASIC, FULL_LOG } enum class ProgressType { NONE, DOTS, BASIC, FULL_LOG }
enum class ResultMode { NONE, LAST, ALL, FAILEXIT } enum class ResultMode { OPTIONAL, LAST, ALL, FAILEXIT }
enum class OS { LINUX } enum class OS { LINUX }
@ -17,8 +17,8 @@ private const val NOT_IMPLEMENTED = "Not implemented"
/** /**
* This main class offers methods to execute shell commands. * This main class offers methods to execute shell commands.
* The commands are executed locally, remotely (via ssh) or in a docker container depending on * The commands are executed by the provided processor,
* the processor which is passed to the constructor. * e.g. a LocalProcessor will execute them locally, a RemoteUbuntuProcessor remotely (via ssh), etc.
*/ */
open class Prov protected constructor( open class Prov protected constructor(
private val processor: Processor, private val processor: Processor,
@ -70,16 +70,16 @@ open class Prov protected constructor(
* A task is the base execution unit in provs. In the results overview it is represented by one line resp. result (of either success or failure). * A task is the base execution unit in provs. In the results overview it is represented by one line resp. result (of either success or failure).
* Returns success if no sub-tasks are called or if all subtasks finish with success. * Returns success if no sub-tasks are called or if all subtasks finish with success.
*/ */
fun task(name: String? = null, taskLambda: Prov.() -> Unit): ProvResult { fun task(name: String? = null, task: Prov.() -> Unit): ProvResult {
return handle(ResultMode.ALL, name) { taskLambda(); ProvResult(true) } return evaluate(ResultMode.ALL, name) { task(); ProvResult(true) }
} }
/** /**
* Same as task but the provided lambda is explicitly required to provide a ProvResult to be returned. * Same as task but the provided lambda is explicitly required to provide a ProvResult to be returned.
* The returned result is included in the evaluation. * The returned result is included in the evaluation.
*/ */
fun taskWithResult(name: String? = null, taskLambda: Prov.() -> ProvResult): ProvResult { fun taskWithResult(name: String? = null, task: Prov.() -> ProvResult): ProvResult {
return handle(ResultMode.ALL, name) { taskLambda() } return evaluate(ResultMode.ALL, name) { task() }
} }
/** /**
@ -88,21 +88,21 @@ open class Prov protected constructor(
*/ */
@Deprecated("Use function task instead", replaceWith = ReplaceWith("task()")) @Deprecated("Use function task instead", replaceWith = ReplaceWith("task()"))
fun def(a: Prov.() -> ProvResult): ProvResult { fun def(a: Prov.() -> ProvResult): ProvResult {
return handle(ResultMode.ALL) { a() } return evaluate(ResultMode.ALL) { a() }
} }
/** /**
* defines a task, which returns the returned result, the results of sub-tasks are not considered * defines a task, which returns the returned result, the results of sub-tasks are not considered
*/ */
fun requireLast(a: Prov.() -> ProvResult): ProvResult { fun requireLast(a: Prov.() -> ProvResult): ProvResult {
return handle(ResultMode.LAST) { a() } return evaluate(ResultMode.LAST) { a() }
} }
/** /**
* defines a task, which always returns success * defines a task, which always returns success
*/ */
fun optional(a: Prov.() -> ProvResult): ProvResult { fun optional(a: Prov.() -> ProvResult): ProvResult {
return handle(ResultMode.NONE) { a() } return evaluate(ResultMode.OPTIONAL) { a() }
} }
/** /**
@ -110,14 +110,14 @@ open class Prov protected constructor(
*/ */
@Deprecated("Use function task instead", replaceWith = ReplaceWith("task()")) @Deprecated("Use function task instead", replaceWith = ReplaceWith("task()"))
fun requireAll(a: Prov.() -> ProvResult): ProvResult { fun requireAll(a: Prov.() -> ProvResult): ProvResult {
return handle(ResultMode.ALL) { a() } return evaluate(ResultMode.ALL) { a() }
} }
/** /**
* defines a task, which exits the overall execution on failure * defines a task, which exits the overall execution on failure
*/ */
fun exitOnFailure(a: Prov.() -> ProvResult): ProvResult { fun exitOnFailure(a: Prov.() -> ProvResult): ProvResult {
return handle(ResultMode.FAILEXIT) { a() } return evaluate(ResultMode.FAILEXIT) { a() }
} }
/** /**
@ -125,7 +125,7 @@ open class Prov protected constructor(
*/ */
fun taskInContainer(containerName: String, task: Prov.() -> ProvResult): ProvResult { fun taskInContainer(containerName: String, task: Prov.() -> ProvResult): ProvResult {
runInContainerWithName = containerName runInContainerWithName = containerName
val res = handle(ResultMode.ALL) { task() } val res = evaluate(ResultMode.ALL) { task() }
runInContainerWithName = null runInContainerWithName = null
return res return res
} }
@ -251,9 +251,10 @@ open class Prov protected constructor(
} }
/** /**
* Provides result handling, e.g. gather results for result summary * Provides task evaluation, i.e. computes a ProvResult based on the provided resultMode,
* on the returned ProvResult from the task as well as on the results from executed subtasks (if there are).
*/ */
private fun handle(mode: ResultMode, name: String? = null, a: Prov.() -> ProvResult): ProvResult { private fun evaluate(resultMode: ResultMode, name: String? = null, task: Prov.() -> ProvResult): ProvResult {
// init // init
if (level == 0) { if (level == 0) {
@ -273,11 +274,11 @@ open class Prov protected constructor(
level++ level++
// call the actual function // call the actual task lambda
val sublevelResult = if (!exit) { val resultOfTaskLambda = if (!exit) {
progress(internalResult) progress(internalResult)
@Suppress("UNUSED_EXPRESSION") // false positive @Suppress("UNUSED_EXPRESSION") // false positive
a() task()
} else { } else {
ProvResult(false, out = "Exiting due to failure and mode FAILEXIT") ProvResult(false, out = "Exiting due to failure and mode FAILEXIT")
} }
@ -287,25 +288,25 @@ open class Prov protected constructor(
// post-handling // post-handling
// determine result // determine result
val returnValue = val returnValue =
if (mode == ResultMode.LAST) { if (resultMode == ResultMode.LAST) {
if (internalResultIsLeaf(resultIndex) || taskName == "cmd" || taskName?.replace(" (requireLast)", "") == "repeatTaskUntilSuccess") { if (internalResultIsLeaf(resultIndex) || taskName == "cmd" || taskName?.replace(" (requireLast)", "") == "repeatTaskUntilSuccess") {
// for a leaf (task with mo subtask) or tasks "cmd" resp. "repeatUntilTrue" provide also out and err of original results // for a leaf (task with mo subtask) or tasks "cmd" resp. "repeatUntilTrue" provide also out and err of original results
// because results of cmd and leafs are not included in the reporting // because results of cmd and leafs are not included in the reporting
// and the caller of repeatUntilTrue might need to see the complete result (incl. out and err) and not only success value // and the caller of repeatUntilTrue might need to see the complete result (incl. out and err) and not only success value
sublevelResult.copy() resultOfTaskLambda.copy()
} else { } else {
// just pass success value, no other data of the original result // just pass success value, no other data of the original result
ProvResult(sublevelResult.success) ProvResult(resultOfTaskLambda.success)
} }
} else if (mode == ResultMode.ALL) { } else if (resultMode == ResultMode.ALL) {
// leaf // leaf
if (internalResultIsLeaf(resultIndex)) sublevelResult.copy() if (internalResultIsLeaf(resultIndex)) resultOfTaskLambda.copy()
// evaluate subcalls' results // evaluate subcalls' results
else ProvResult((cumulativeSuccessSublevel(resultIndex) ?: false) && sublevelResult.success) else ProvResult((cumulativeSuccessSublevel(resultIndex) ?: false) && resultOfTaskLambda.success)
} else if (mode == ResultMode.NONE) { } else if (resultMode == ResultMode.OPTIONAL) {
ProvResult(true) ProvResult(true)
} else if (mode == ResultMode.FAILEXIT) { } else if (resultMode == ResultMode.FAILEXIT) {
return if (sublevelResult.success) { return if (resultOfTaskLambda.success) {
ProvResult(true) ProvResult(true)
} else { } else {
exit = true exit = true

View file

@ -18,7 +18,7 @@ import java.net.InetAddress
*/ */
internal fun getCallingMethodName(): String? { internal fun getCallingMethodName(): String? {
val offsetVal = 1 val offsetVal = 1
val exclude = arrayOf("task", "task\$default", "taskWithResult\$default", "taskWithResult", "def", "def\$default", "record", "invoke", "invoke0", "handle", "handle\$default", ) val exclude = arrayOf("task", "task\$default", "taskWithResult\$default", "taskWithResult", "def", "def\$default", "record", "invoke", "invoke0", "evaluate", "evaluate\$default", )
// suffixes are also ignored as method names but will be added as suffix in the evaluation results // suffixes are also ignored as method names but will be added as suffix in the evaluation results
val suffixes = arrayOf("optional", "requireAll", "requireLast", "inContainer") val suffixes = arrayOf("optional", "requireAll", "requireLast", "inContainer")