Skip to content

Commit

Permalink
gradle: group tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Dec 15, 2022
1 parent 6627271 commit 14f5d04
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions completers/gradle_completer/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package cmd

import (
"regexp"
"sort"
"strings"

"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/pkg/util"
"github.com/rsteube/carapace/pkg/cache"
"github.com/rsteube/carapace/pkg/style"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -104,11 +106,22 @@ func locateBuildConfig() (target string, err error) {

func ActionTasks() carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
if tasks, err := parseTasks(c); err != nil {
tasks, err := parseTasks(c)
if err != nil {
return carapace.ActionMessage(err.Error())
} else {
return carapace.ActionValuesDescribed(tasks...)
}

sorted := make([]string, 0, len(tasks))
for key := range tasks {
sorted = append(sorted, key)
}
sort.Strings(sorted)

batch := carapace.Batch()
for index, group := range sorted {
batch = append(batch, carapace.ActionValuesDescribed(tasks[group]...).Style(style.Carapace.Highlight(index)).Tag(group))
}
return batch.ToA()
}).Cache(-1, func() (string, error) {
if buildConfig, err := locateBuildConfig(); err != nil {
return "", err
Expand All @@ -118,22 +131,23 @@ func ActionTasks() carapace.Action {
})
}

func parseTasks(c carapace.Context) ([]string, error) {
func parseTasks(c carapace.Context) (map[string][]string, error) {
output, err := c.Command("gradle", "tasks", "--all").Output()
if err != nil {
return nil, err
}

patternTaskGroups := regexp.MustCompile(`[^ ]+ tasks\n-----+\n(?P<tasks>(.+\n)*)`)
patternTaskGroups := regexp.MustCompile(`\n(?P<group>[^ \n]+ tasks)\n-----+\n(?P<tasks>(.+\n)*)`)
patternTask := regexp.MustCompile(`^(?P<task>[^ ]+)( - (?P<description>.*))?`)

result := make([]string, 0)
result := make(map[string][]string, 0)
taskGroups := patternTaskGroups.FindAllStringSubmatch(string(output), -1)
for _, taskGroup := range taskGroups {
for _, line := range strings.Split(taskGroup[1], "\n") {
result[taskGroup[1]] = make([]string, 0)
for _, line := range strings.Split(taskGroup[2], "\n") {
if patternTask.MatchString(line) {
task := patternTask.FindStringSubmatch(line)
result = append(result, task[1], task[3])
result[taskGroup[1]] = append(result[taskGroup[1]], task[1], task[3])
}
}
}
Expand Down

0 comments on commit 14f5d04

Please sign in to comment.