package gradle /** Displays the results of `gradlew tasks` in a tree structure. This is GradleBehaviour's last secondary-tab. */ class GradleTasksBehaviour( val folder : File ) : TreeBehaviour( "Tasks", Context(Context.FOLDER, folder) ) { init { iconName = "tasks" } override meth attached( container : Container ) { super.attached( container ) refresh() } meth refresh() { message( "Building tasks" ) $( ./gradlew tasks ) .dir(folder) .evalInNewThread( "gradlew_tasks", this:>parseOutput, this:>showMessage ) } // TODO Feather doesn't look in super-classes when looking for Function :-( Remove when fixed. meth showMessage( message : String ) { message( message ) } /** * Parses the output from: `gradlew tasks` and populate the tree. * * We detect the start of a `section` when we find a line of dashes, which is the same length as the * previous line. The previous line is the section's name. * * If we have found a section, and then find a line with " - " in the middle, * then to the left of the dash is the task name. To the right, the task's description. * The description will be used as a tooltip. * */ meth parseOutput(output: String) { var sectionCount = 0 root = TreeItem("tasks") root.leaf = false var section: TreeItem = null var previousLine: String = null val lines = output.split("\n") for (l in lines) { val line = l.trim() // Was the previous line a "section"? if (line.startsWith("-") && previousLine.length() == line.length()) { if (previousLine == "Rules") { section = null } else { section = TreeItem(previousLine) section.leaf = false root.add(section) section.expanded = sectionCount < 2 sectionCount ++ } } else if (section != null && line != "") { val dash = line.indexOf(" - ") if (dash > 0) { val taskName = line.substring(0, dash).trim() val description = line.substring(dash + 3) val context = Context(Gradle.GRADLE_TASK, GradleTask(folder,taskName) ) section.add( TreeItem( taskName, context ).description( description ) ) } } previousLine = line } message = "" } }