This message was deleted.
# community-support
s
This message was deleted.
a
move
serviceOf<>()
out of
doLast {}
v
You are hopefully aware that you are using internal classes, so this could break anytime anyway. πŸ™‚ Besides taht, as far as I know
serviceOf
is also to be considered internal even without it being in an
*.internal.*
package. The
serviceOf
call should also be the problem as far as I can see, as it is called on the build script reference which can then not be serialized by the configuation cache.
a
Copy code
tasks.named<Jar>("jar") {
    val styledText = serviceOf<StyledTextOutputFactory>()

    doLast {
        val out = styledText.create("detekt-rules")
        // ...
    }
}
v
Are you sure this will not fail then for not being able to serialized the factory?
Yep, big failure
s
Copy code
* What went wrong:
Configuration cache state could not be cached: field `dateFormat` of `org.gradle.internal.logging.console.StyledTextOutputBackedRenderer` bean found in field `stdoutChain` of `org.gradle.internal.logging.sink.LogEventDispatcher` bean found in field `delegate` of `org.gradle.internal.logging.sink.OutputEventRenderer$LazyListener` bean found in field `target` of `org.gradle.internal.dispatch.ReflectionDispatch` bean found in field `dispatch` of `org.gradle.internal.event.BroadcastDispatch$SingletonDispatch` bean found in field `dispatchers` of `org.gradle.internal.event.BroadcastDispatch$CompositeDispatch` bean found in field `broadcast` of `org.gradle.internal.event.ListenerBroadcast` bean found in field `formatters` of `org.gradle.internal.logging.sink.OutputEventRenderer` bean found in field `renderer` of `org.gradle.internal.logging.sink.OutputEventListenerManager` bean found in field `this$0` of `org.gradle.internal.logging.sink.OutputEventListenerManager$1` bean found in field `listener` of `org.gradle.internal.logging.services.TextStreamOutputEventListener` bean found in field `outputEventListener` of `org.gradle.internal.logging.services.DefaultStyledTextOutputFactory` bean found in field `$styledText` of `Build_gradle$$$result$1$1` bean found in field `action` of `org.gradle.api.internal.AbstractTask$TaskActionWrapper` bean found in field `actions` of task `:detekt-rules:jar` of type `org.gradle.api.tasks.bundling.Jar`: error writing value of type 'java.text.SimpleDateFormat'
> Unable to make private void java.text.SimpleDateFormat.readObject(java.io.ObjectInputStream) throws java.io.IOException,java.lang.ClassNotFoundException accessible: module java.base does not "opens java.text" to unnamed module @2511e3ef
🀯 1
v
My recommendation is, do not use internal classes and functions πŸ™‚
s
Sure, just tell me how to then print colored output πŸ˜‰
v
Use use standard ANSI escapes for color and live with them being output to console if running piped?
s
What about terminals (like Git Bash's mintty on Windows) that do not support ANSI sequences?
v
I think it does, but well, that's the part with "live with it being printed instead of causing color" πŸ™‚
j
You should move the task implementation into a separate class. Then you can access the service via a getter annotated with
@Inject
and do not need to access the project for the serviceOf (which is also kind of internal I think)
v
What you could actually do is making a proper task class where you inject the internal factory
Copy code
abstract class Foo : DefaultTask() {
    @get:Inject
    abstract val foo: StyledTextOutputFactory

    @TaskAction
    fun foo() {
        val out = foo.create("detekt-rules")
        val message = "The detekt-rules have changed. You need to stop the Gradle daemon to allow the detekt plugin " +
                "to reload for the rule changes to take effect."
        out.withStyle(<http://StyledTextOutput.Style.Info|StyledTextOutput.Style.Info>).println(message)
    }
}

val foo by tasks.registering(Foo::class)
πŸ‘ 1
s
Hmm, but then this needs to be a separate task, instead of further configuring an existing task...
v
Copy code
interface Foo {
    @get:Inject
    val foo: StyledTextOutputFactory
}

val help by tasks.existing {
    val objects = objects
    doLast {
        val out = objects.newInstance<Foo>().foo.create("detekt-rules")
        val message = "The detekt-rules have changed. You need to stop the Gradle daemon to allow the detekt plugin " +
                "to reload for the rule changes to take effect."
        out.withStyle(<http://StyledTextOutput.Style.Info|StyledTextOutput.Style.Info>).println(message)
    }
}
πŸ‘ 1
a
What went wrong: Configuration cache state could not be cached: field
dateFormat
of …
d’oh, okay, also put the message in a provider
Copy code
val styleText by tasks.registering {
  val styledText = serviceOf<StyledTextOutputFactory>()
  val msg = providers.provider {
    val out = styledText.create("detekt-rules")
    val message = "The detekt-rules have changed. You need to stop the Gradle daemon to allow the detekt plugin " +
        "to reload for the rule changes to take effect."
    out.append(message).withStyle(StyledTextOutput.Style.Error).println(message)
  }

  doLast {
    msg.get()
  }
}
maybe there’s a better way to get a string rather than calling
println(message)
, but it seems to work
s
@Adam I get the same lengthy error message from above when using a provider.
a
ahh my mistake, I tried it out in a project that had CC disabled πŸ™ƒ
πŸ˜„ 1
v
I tested it in a proper repo, use my version and it should work πŸ™‚
a
for non-Gradle text styling you could try out something like Mordant https://github.com/ajalt/mordant. Perhaps OTT for a single log line, but it’s nice and Kotlin-esque
πŸ‘ 1
s
Yes, it does @Vampire, thanks!
πŸ‘Œ 1
Yeah, I'll be using Mordant implicitly via clikt 4 soon πŸ˜‰
πŸš€ 1