feat(commands): refactor and add more commands
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
package runner
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/versenilvis/iris/commands/core"
|
||||
)
|
||||
|
||||
func init() {
|
||||
core.Register(&core.Spec{
|
||||
Name: "just",
|
||||
Description: "command runner",
|
||||
Generator: func(tokens []string, prefix string, partial string) []core.Suggestion {
|
||||
file, err := os.Open("justfile")
|
||||
if err != nil {
|
||||
// try uppercase
|
||||
file, err = os.Open("Justfile")
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
var suggestions []core.Suggestion
|
||||
seen := make(map[string]bool)
|
||||
scanner := bufio.NewScanner(file)
|
||||
// matching recipes like `build:`
|
||||
recipeRegex := regexp.MustCompile(`^([a-zA-Z0-9_-]+):`)
|
||||
|
||||
lastComment := ""
|
||||
for scanner.Scan() {
|
||||
line := strings.TrimSpace(scanner.Text())
|
||||
|
||||
// Handle comments
|
||||
if strings.HasPrefix(line, "#") {
|
||||
lastComment = strings.TrimSpace(strings.TrimPrefix(line, "#"))
|
||||
continue
|
||||
}
|
||||
|
||||
// Skip blank lines or group attributes
|
||||
if line == "" || strings.HasPrefix(line, "[") {
|
||||
continue
|
||||
}
|
||||
|
||||
if matches := recipeRegex.FindStringSubmatch(line); len(matches) > 1 {
|
||||
recipe := matches[1]
|
||||
if seen[recipe] {
|
||||
lastComment = ""
|
||||
continue
|
||||
}
|
||||
seen[recipe] = true
|
||||
|
||||
cmd := recipe
|
||||
if prefix != "" {
|
||||
cmd = prefix + " " + recipe
|
||||
}
|
||||
|
||||
desc := "just recipe"
|
||||
if lastComment != "" {
|
||||
desc = lastComment
|
||||
}
|
||||
|
||||
suggestions = append(suggestions, core.Suggestion{
|
||||
Cmd: cmd,
|
||||
Desc: desc,
|
||||
})
|
||||
lastComment = ""
|
||||
} else {
|
||||
// If it's something else (like a command line), reset comment
|
||||
// unless it's just indented (which we already skip or handle)
|
||||
if !strings.HasPrefix(scanner.Text(), " ") && !strings.HasPrefix(scanner.Text(), "\t") {
|
||||
lastComment = ""
|
||||
}
|
||||
}
|
||||
}
|
||||
return suggestions
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package runner
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"os"
|
||||
"regexp"
|
||||
|
||||
"github.com/versenilvis/iris/commands/core"
|
||||
)
|
||||
|
||||
func init() {
|
||||
core.Register(&core.Spec{
|
||||
Name: "make",
|
||||
Description: "build automation",
|
||||
Generator: func(tokens []string, prefix string, partial string) []core.Suggestion {
|
||||
// Read the Makefile
|
||||
file, err := os.Open("Makefile")
|
||||
if err != nil {
|
||||
// No Makefile, don't pretend we have one
|
||||
return nil
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
var suggestions []core.Suggestion
|
||||
seen := make(map[string]bool)
|
||||
scanner := bufio.NewScanner(file)
|
||||
// Matches targets like `build:`, ` run :`, etc.
|
||||
// Ignores lines starting with whitespace followed by command (tab)
|
||||
targetRegex := regexp.MustCompile(`^[ \t]*([a-zA-Z0-9_-]+)[ \t]*:`)
|
||||
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if matches := targetRegex.FindStringSubmatch(line); len(matches) > 1 {
|
||||
target := matches[1]
|
||||
// Ignore standard non-target keywords
|
||||
if target == "PHONY" || seen[target] {
|
||||
continue
|
||||
}
|
||||
seen[target] = true
|
||||
|
||||
cmd := target
|
||||
if prefix != "" {
|
||||
cmd = prefix + " " + target
|
||||
}
|
||||
suggestions = append(suggestions, core.Suggestion{
|
||||
Cmd: cmd,
|
||||
Desc: "make target",
|
||||
})
|
||||
}
|
||||
}
|
||||
return suggestions
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user