Reputation: 3072
I am trying to write an sbt task that checks if the code compilation succeeds or fails and based on that information, does something. So far I have this:
When compilation failed, it printed out this:
[warn] Compile: Inc(Incomplete(node=Some(Task((taskDefinitionKey: ScopedKey(Scope(Select(ProjectRef(file:/Users/john-michaelreed/Downloads/NewDownloads/sbt-0.13/lesson/HelloScala1/,helloscala1)),Select(ConfigKey(compile)),Global,Global),compile)))), tpe=Error, msg=None, causes=List(Incomplete(node=Some(Task((tags: Map(Tag(compile) -> 1, Tag(cpu) -> 1), taskDefinitionKey: ScopedKey(Scope(Select(ProjectRef(file:/Users/john-michaelreed/Downloads/NewDownloads/sbt-0.13/lesson/HelloScala1/,helloscala1)),Select(ConfigKey(compile)),Global,Global),compileIncremental)))), tpe=Error, msg=None, causes=List(), directCause=Some(Compilation failed))), directCause=None)) !
which contains the String "Compilation failed". I could check to see if that String is present and based on the result of that, do something.
Example:
val monitorTask = taskKey[Unit]("A task that gets the result of compile.")
monitorTask in Scope.GlobalScope := {
// monitorTask dependencies:
val log = streams.value.log // streams task happens-before monitorTask
val compileResult = (compile.in(Compile)).result.value // compile task happens-before monitorTask
// ---- monitorTask begins here ----
if(compileResult.toString.contains("Compilation failed")) {
log.warn("Compilation failed!")
// Do stuff
} else {
log.info("Compilation succeeded!")
// Do other stuff
}
}
But that looks a little fragile. Is there a better way to do that?
p.s. In the process of testing monitorTask, I ran into this bug: https://github.com/sbt/sbt/issues/4444
Upvotes: 1
Views: 336
Reputation: 95624
If you want binary success or fail information why not call toEither
on .result.value
? You can then check if it's isRight
.
Upvotes: 0