Skip to content

Instantly share code, notes, and snippets.

@sandipchitale
Last active October 31, 2020 19:53
Show Gist options
  • Save sandipchitale/1809f44ffea1bacb679ae2342adeb57c to your computer and use it in GitHub Desktop.
Save sandipchitale/1809f44ffea1bacb679ae2342adeb57c to your computer and use it in GitHub Desktop.
Specify which order tasks should run in specified or natural
// Apply this from main build script using:
// if (rootProject.hasProperty('only-tasks')) {
// apply from: 'build-only-tasks.gradle'
// }
//
// Usage:
//
// gradlew -Ponly-tasks
// gradlew -Ponly-tasks -Pprompt-for-tasks
// gradlew -Ponly-tasks -Pprompt-for-tasks -Pspecified-order
// gradlew -Ponly-tasks -Pspecified-order
import java.awt.Dimension
import java.awt.event.MouseEvent
import java.awt.event.MouseAdapter
import java.awt.GridLayout
import javax.swing.*
import java.util.Collection
import java.util.stream.Collectors
// Does the user wants to run only specified tasks?
if (rootProject.hasProperty('only-tasks')) {
gradle.projectsEvaluated {
// Does the user wants to be prompted to select the task?
if (rootProject.hasProperty('prompt-for-tasks')) {
// Get all tasks in all projects - rootProject tasks first
def ts = rootProject.getAllTasks(true)
.values()
.flatten()
.collect()
.stream()
.map { t -> (t.path.chars().filter { ch -> ch == PATH_SEPARATOR }.count()) + t.path }
.sorted()
.map { t -> t.substring(1) }
.toArray()
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())
DefaultListModel<String> fromTasksModel = new DefaultListModel<>()
ts.each {
fromTasksModel.addElement(it)
}
JList fromTasksList = new JList(fromTasksModel)
fromTasksList.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION)
fromTasksList.setToolTipText("Double click to add task to run")
JScrollPane fromSrollPane = new JScrollPane(fromTasksList)
fromSrollPane.setPreferredSize(new Dimension(300, 500))
DefaultListModel<String> toTasksModel = new DefaultListModel<>()
JList toTasksList = new JList(toTasksModel)
toTasksList.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION)
toTasksList.setToolTipText("Run in ${ rootProject.hasProperty('specified-order') ? 'specified' : 'dependency' } order. Double click to remove task.")
JScrollPane toTasksScrollPane = new JScrollPane(toTasksList)
toTasksScrollPane.setPreferredSize(new Dimension(300, 500))
fromTasksList.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
JList list = (JList)evt.getSource()
if (evt.getClickCount() == 2) {
int index = list.locationToIndex(evt.getPoint())
if (index != -1) {
def task = fromTasksModel.get(index)
if (!toTasksModel.contains(task)) {
int toSelectedIndex = toTasksList.getSelectedIndex()
// Add task to run
if (toSelectedIndex == -1) {
toTasksModel.addElement(task)
} else {
if (evt.isControlDown()) {
toTasksModel.add(toSelectedIndex, task)
return
} else if (evt.isShiftDown()) {
toTasksModel.add(toSelectedIndex + 1, task)
return
}
toTasksModel.addElement(task)
}
}
}
}
}
})
toTasksList.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
JList list = (JList)evt.getSource()
if (evt.getClickCount() == 2) {
int index = list.locationToIndex(evt.getPoint())
if (index != -1) {
def task = toTasksModel.get(index)
// Remove task to run
if (evt.isControlDown()) {
if (index > 0) {
toTasksModel.remove(index)
toTasksModel.add(index - 1, task)
}
} else if (evt.isShiftDown()) {
toTasksModel.remove(index)
toTasksModel.add(Math.min(toTasksModel.size(), index + 1), task)
} else {
toTasksModel.remove(index)
}
}
}
}
})
JSplitPane wrapperPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, fromSrollPane, toTasksScrollPane)
// Prompt for selection of tasks
def option = JOptionPane.showOptionDialog(null,
,wrapperPanel
,'Run selected tasks without dependee tasks'
,JOptionPane.DEFAULT_OPTION
,JOptionPane.PLAIN_MESSAGE
,null
,['None', 'Run selected'].toArray()
,'None')
// Did user select some tasks?
if (option == 1) {
def selectedTasks = []
toTasksModel.toArray().each {
selectedTasks += it
}
if (selectedTasks.size > 0) {
gradle.startParameter.setTaskNames(selectedTasks)
}
}
}
def startParameterTaskNames = gradle.startParameter.getTaskNames().stream().map { it.startsWith(':') ? it : ":${it}".toString() }.collect().toList()
if (rootProject.hasProperty('specified-order')) {
def ts = rootProject.getAllTasks(true).values().flatten().collect().stream().map { t -> t.path }.sorted().toList()
ts.each {
if (!(it in startParameterTaskNames)) {
gradle.startParameter.excludedTaskNames += it
}
}
// Force report of task run
gradle.taskGraph.beforeTask {
println ''
}
} else {
// Force report of task run
gradle.taskGraph.beforeTask {
if (it.path in startParameterTaskNames) {
println ''
} else {
it.enabled = false
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment