Skip to content

Commit

Permalink
Merge pull request #1860 from rsteube/env-condition
Browse files Browse the repository at this point in the history
env: conditions
  • Loading branch information
rsteube authored Sep 23, 2023
2 parents 3ce9b6f + 295221d commit 8eb60fc
Show file tree
Hide file tree
Showing 35 changed files with 748 additions and 122 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ cmd/carapace/cmd/completers/description.go
cmd/carapace/cmd/completers.go
cmd/carapace/cmd/completers/name.go
cmd/carapace/cmd/completers_release.go
cmd/carapace/cmd/macros.go
cmd/carapace/cmd/shim/*.exe
cmd/carapace-fmt/carapace-fmt
cmd/carapace-generate/carapace-generate
Expand All @@ -18,3 +17,5 @@ completers_release
dist
docs/book
out.gif
pkg/actions/actions_generated.go
pkg/conditions/conditions_generated.go
94 changes: 88 additions & 6 deletions cmd/carapace-generate/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

func main() {
macros()
conditions()

names, descriptions := readCompleters()

Expand Down Expand Up @@ -269,23 +270,104 @@ func macros() {
}
sort.Strings(sortedDescriptions)

content := fmt.Sprintf(`package cmd
content := fmt.Sprintf(`package actions
import (
%v
spec "github.com/rsteube/carapace-spec"
)
var macros = map[string]spec.Macro{
func init() {
MacroMap = map[string]spec.Macro{
%v
}
}
var macroDescriptions = map[string]string {
MacroDescriptions = map[string]string {
%v
}
}
`, strings.Join(sortedImports, "\n"), strings.Join(macros, "\n"), strings.Join(sortedDescriptions, "\n"))

os.WriteFile(root+"/cmd/carapace/cmd/macros.go", []byte(content), 0644)
execabs.Command("go", "fmt", root+"/cmd/carapace/cmd/macros.go").Run()
os.WriteFile(root+"/pkg/actions/actions_generated.go", []byte(content), 0644)
execabs.Command("go", "fmt", root+"/pkg/actions/actions_generated.go").Run()

}

func conditions() {
root, err := rootDir()
if err != nil {
panic(err.Error)
}

macros := make([]string, 0)
descriptions := make(map[string]string, 0)

r := regexp.MustCompile(`^func Condition(?P<name>[^(]+)\((?P<arg>[^(]*)\) condition.Condition {$`)
filepath.WalkDir(root+"/pkg/conditions", func(path string, d fs.DirEntry, err error) error { // TODO walkdir not necessary
path = filepath.ToSlash(path)
if !d.IsDir() && strings.HasSuffix(path, ".go") {
file, err := os.Open(path)
if err != nil {
log.Fatal(err)
}
defer file.Close()

scanner := bufio.NewScanner(file)
for scanner.Scan() {
if t := scanner.Text(); strings.HasPrefix(t, "func Condition") {
if r.MatchString(t) {
matches := r.FindStringSubmatch(t)

_func := fmt.Sprintf("Condition%v", matches[1])

if arg := matches[2]; strings.Contains(arg, ",") {
macros = append(macros, "// TODO unsupported signature: "+t)
continue
} else if arg == "" {
macros = append(macros, fmt.Sprintf(`"%v": condition.MacroN(%v),`, matches[1], _func))
} else if strings.Contains(arg, "...") {
macros = append(macros, fmt.Sprintf(`"%v": condition.MacroV(%v),`, matches[1], _func))
} else {
macros = append(macros, fmt.Sprintf(`"%v": condition.MacroI(%v),`, matches[1], _func))
}
}
} else if strings.HasPrefix(t, "// Condition") {
if splitted := strings.SplitN(strings.TrimPrefix(t, "// Condition"), " ", 2); len(splitted) > 1 {
descriptions[splitted[0]] = splitted[1]
}
}
}

}
return nil
})

sortedDescriptions := make([]string, 0)
for key, value := range descriptions {
sortedDescriptions = append(sortedDescriptions, fmt.Sprintf(`%#v: %#v,`, key, value))
}
sort.Strings(sortedDescriptions)

content := fmt.Sprintf(`package conditions
import (
"github.com/rsteube/carapace-bin/internal/condition"
"github.com/rsteube/carapace-spec/pkg/macro"
)
func init() {
MacroMap = macro.MacroMap[condition.Macro]{
%v
}
MacroDescriptions = map[string]string {
%v
}
}
`, strings.Join(macros, "\n"), strings.Join(sortedDescriptions, "\n"))

os.WriteFile(root+"/pkg/conditions/conditions_generated.go", []byte(content), 0644)
execabs.Command("go", "fmt", root+"/pkg/conditions/conditions_generated.go").Run()

}
69 changes: 54 additions & 15 deletions cmd/carapace/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/cmd/carapace/cmd/completers"
"github.com/rsteube/carapace-bin/cmd/carapace/cmd/shim"
"github.com/rsteube/carapace-bin/pkg/actions"
"github.com/rsteube/carapace-bin/pkg/conditions"
spec "github.com/rsteube/carapace-spec"
"github.com/rsteube/carapace/pkg/ps"
"github.com/rsteube/carapace/pkg/style"
Expand Down Expand Up @@ -63,6 +65,12 @@ var rootCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
// since flag parsing is disabled do this manually
switch args[0] {
case "--conditions":
if len(args) > 1 {
//printCondition(args[1]) // TODO
} else {
printConditions() // TODO
}
case "--macros":
if len(args) > 1 {
printMacro(args[1])
Expand Down Expand Up @@ -121,27 +129,42 @@ var rootCmd = &cobra.Command{
fmt.Fprintln(cmd.ErrOrStderr(), err.Error())
}
}

completers := completers.Names()
if os.Getenv("CARAPACE_ENV") == "0" {
filtered := make([]string, 0, len(completers))
for _, name := range completers {
switch name {
case "get-env", "set-env", "unset-env":
default:
filtered = append(filtered, name)

}
}
completers = filtered
}

switch shell {
case "bash":
fmt.Fprintln(cmd.OutOrStdout(), bash_lazy(completers.Names()))
fmt.Fprintln(cmd.OutOrStdout(), bash_lazy(completers))
case "bash-ble":
fmt.Fprintln(cmd.OutOrStdout(), bash_ble_lazy(completers.Names()))
fmt.Fprintln(cmd.OutOrStdout(), bash_ble_lazy(completers))
case "elvish":
fmt.Fprintln(cmd.OutOrStdout(), elvish_lazy(completers.Names()))
fmt.Fprintln(cmd.OutOrStdout(), elvish_lazy(completers))
case "fish":
fmt.Fprintln(cmd.OutOrStdout(), fish_lazy(completers.Names()))
fmt.Fprintln(cmd.OutOrStdout(), fish_lazy(completers))
case "nushell":
fmt.Fprintln(cmd.OutOrStdout(), nushell_lazy(completers.Names()))
fmt.Fprintln(cmd.OutOrStdout(), nushell_lazy(completers))
case "oil":
fmt.Fprintln(cmd.OutOrStdout(), oil_lazy(completers.Names()))
fmt.Fprintln(cmd.OutOrStdout(), oil_lazy(completers))
case "powershell":
fmt.Fprintln(cmd.OutOrStdout(), powershell_lazy(completers.Names()))
fmt.Fprintln(cmd.OutOrStdout(), powershell_lazy(completers))
case "tcsh":
fmt.Fprintln(cmd.OutOrStdout(), tcsh_lazy(completers.Names()))
fmt.Fprintln(cmd.OutOrStdout(), tcsh_lazy(completers))
case "xonsh":
fmt.Fprintln(cmd.OutOrStdout(), xonsh_lazy(completers.Names()))
fmt.Fprintln(cmd.OutOrStdout(), xonsh_lazy(completers))
case "zsh":
fmt.Fprintln(cmd.OutOrStdout(), zsh_lazy(completers.Names()))
fmt.Fprintln(cmd.OutOrStdout(), zsh_lazy(completers))
default:
fmt.Fprintln(os.Stderr, "could not determine shell")
}
Expand Down Expand Up @@ -286,10 +309,26 @@ func printCompletersJson() {
}
}

func printConditions() {
maxlen := 0
names := make([]string, 0)
for name := range conditions.MacroMap {
names = append(names, name)
if len := len(name); len > maxlen {
maxlen = len
}
}

sort.Strings(names)
for _, name := range names {
fmt.Printf("%-"+strconv.Itoa(maxlen)+"v %v\n", name, conditions.MacroDescriptions[name])
}
}

func printMacros() {
maxlen := 0
names := make([]string, 0)
for name := range macros {
for name := range actions.MacroMap {
names = append(names, name)
if len := len(name); len > maxlen {
maxlen = len
Expand All @@ -298,12 +337,12 @@ func printMacros() {

sort.Strings(names)
for _, name := range names {
fmt.Printf("%-"+strconv.Itoa(maxlen)+"v %v\n", name, macroDescriptions[name])
fmt.Printf("%-"+strconv.Itoa(maxlen)+"v %v\n", name, actions.MacroDescriptions[name])
}
}

func printMacro(name string) {
if m, ok := macros[name]; ok {
if m, ok := actions.MacroMap[name]; ok {
path := strings.Replace(name, ".", "/", -1)
signature := ""
if s := m.Signature(); s != "" {
Expand All @@ -313,7 +352,7 @@ func printMacro(name string) {
fmt.Printf(`signature: $_%v%v
description: %v
reference: https://pkg.go.dev/github.com/rsteube/carapace-bin/pkg/actions/%v#Action%v
`, name, signature, macroDescriptions[name], filepath.Dir(path), filepath.Base(path))
`, name, signature, actions.MacroDescriptions[name], filepath.Dir(path), filepath.Base(path))
}
}

Expand Down Expand Up @@ -406,7 +445,7 @@ func init() {
rootCmd.Flags().String("scrape", "", "scrape spec to go code")
rootCmd.Flags().String("style", "", "set style")

for m, f := range macros {
for m, f := range actions.MacroMap {
spec.AddMacro(m, f)
}
}
1 change: 1 addition & 0 deletions docs/src/development/tools/carapace-fmt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# carapace-fmt
1 change: 1 addition & 0 deletions docs/src/development/tools/carapace-generate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# carapace-generate
1 change: 1 addition & 0 deletions docs/src/development/tools/carapace-lint.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# carapace-lint
1 change: 1 addition & 0 deletions docs/src/development/tools/carapace-parse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# carapace-parse
Loading

0 comments on commit 8eb60fc

Please sign in to comment.