Chachako
06/23/2022, 9:33 AMDomainObjectCollection
?
Is this Kotlin code correct?
val dependencies = channelFlow {
project.configurations.configureEach {
it.dependencies.forEach(::trySend)
}
}
// On "configureEach" completion
println(dependencies)
Sam
06/23/2022, 9:38 AMThe resulting flow completes as soon as the code in the block and all its children completes. Use awaitClose as the last statement to keep it running.https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/channel-flow.html
Chachako
06/23/2022, 9:44 AMDomainObjectCollection.configureEach
with a clumsy trick like channelFlow
? and any correct guidance would be appreciated!Sam
06/23/2022, 9:46 AMSam
06/23/2022, 9:47 AMproject.afterEvaluate
Sam
06/23/2022, 9:50 AMresolutionStrategy
instead. That will be invoked at the point when the configuration is resolved, which sounds like what you want.Chachako
06/23/2022, 9:50 AMproject.configurations
shouldn’t have anything new added, so I want to do something while it’s at a point where nothing will be added.
project.afterEvaluate
is a practice, but I feel this may be a little lateSam
06/23/2022, 10:02 AMconfiguration.incoming.beforeResolve
to register some code to run just before the configuration is resolvedSam
06/23/2022, 10:02 AMconfiguration.incoming
are)Chachako
06/23/2022, 10:04 AMproject.afterEvaluate
will be the last resortVampire
06/23/2022, 1:24 PMIt is not only a practice, it is a bad practice. And you can never know whether the collection is already complete at that time, becauseis a practiceproject.afterEvaluate
afterEvaluate
blocks are added t a queue and the ones later in the queue than your block might again change what you are evaluating.
afterEvaluate
is not a solution to anything, but a hack and one that will fail on you as soon as someone configures the thing you try to look at in an afterEvaluate
block that is evaluated after your afterEvaluate
block.
The only thing afterEvaluate
does, is doing symptom treatment, delaying a problem to arise later again, but that time even harder to debug.
It always introduces nasty race conditions you cannot really avoid except by not using afterEvaluate
.Vampire
06/23/2022, 1:25 PMChachako
06/23/2022, 2:51 PMAs you most probably not want to just print all dependencies, please describe what your actual use-case is you are trying to solve, then maybe someone can advice a proper way to solve it. 🙂I actually have a common version catalog file that is very large, so I need a way to get all Gradle in-use dependencies, and write the corresponding dependencies from the common file to
libraries.versions.toml
to avoid other unnecessary sight distractions.
So in this case , afterEvaluate
should work