How to make a task that does what is in defaultTasks? This would facilitate running a non-default task followed by the default tasks


(krgkg) #1

Does this look right?

allprojects {
    task defTasks {
         //TODO make this work with any project hierarchy depth
        def foundTasks = tasks.findAll {defaultTasks.contains(it.name)}
        if (foundTasks.isEmpty()) {
            foundTasks = tasks.findAll {parent.defaultTasks.contains(it.name)}
        }
        dependsOn foundTasks
          def usableDefaultTasks = null
        if (defaultTasks.isEmpty()){
            usableDefaultTasks = parent.defaultTasks
        }
        else {
            usableDefaultTasks = defaultTasks
        }
             //this assumes that the order of default tasks is correct
        def prevTask = null
        usableDefaultTasks.each { defTaskName ->
            def getFoundTask = { defTaskName == it.name }
            if (foundTasks.any( getFoundTask )) {
                if (prevTask) {
                    foundTasks.find(getFoundTask).shouldRunAfter ( prevTask)
                }
                prevTask = foundTasks.find(getFoundTask)
             }
        }
    }
}