|
| 1 | +//go:build darwin || linux || freebsd || openbsd |
| 2 | + |
| 3 | +package commands |
| 4 | + |
| 5 | +import ( |
| 6 | + "embed" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "os" |
| 10 | + "strconv" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "github.com/posener/complete" |
| 14 | + "github.com/symfony-cli/console" |
| 15 | + "github.com/symfony-cli/symfony-cli/local/php" |
| 16 | + "github.com/symfony-cli/terminal" |
| 17 | +) |
| 18 | + |
| 19 | +// completionTemplates holds our custom shell completions templates. |
| 20 | +// |
| 21 | +//go:embed resources/completion.* |
| 22 | +var completionTemplates embed.FS |
| 23 | + |
| 24 | +func init() { |
| 25 | + // override console completion templates with our custom ones |
| 26 | + console.CompletionTemplates = completionTemplates |
| 27 | +} |
| 28 | + |
| 29 | +func autocompleteSymfonyConsoleWrapper(context *console.Context, words complete.Args) []string { |
| 30 | + args := buildSymfonyAutocompleteArgs("console", words) |
| 31 | + // Composer does not support those options yet, so we only use them for Symfony Console |
| 32 | + args = append(args, "-a1", fmt.Sprintf("-s%s", console.GuessShell())) |
| 33 | + |
| 34 | + os.Exit(php.SymonyConsoleExecutor(args).Execute(false)) |
| 35 | + |
| 36 | + // unreachable |
| 37 | + return []string{} |
| 38 | +} |
| 39 | + |
| 40 | +func autocompleteComposerWrapper(context *console.Context, words complete.Args) []string { |
| 41 | + args := buildSymfonyAutocompleteArgs("composer", words) |
| 42 | + // Composer does not support multiple shell yet, so we only use the default one |
| 43 | + args = append(args, "-sbash") |
| 44 | + |
| 45 | + res := php.Composer("", args, []string{}, context.App.Writer, context.App.ErrWriter, io.Discard, terminal.Logger) |
| 46 | + os.Exit(res.ExitCode()) |
| 47 | + |
| 48 | + // unreachable |
| 49 | + return []string{} |
| 50 | +} |
| 51 | + |
| 52 | +func buildSymfonyAutocompleteArgs(wrappedCommand string, words complete.Args) []string { |
| 53 | + current, err := strconv.Atoi(os.Getenv("CURRENT")) |
| 54 | + if err != nil { |
| 55 | + current = 1 |
| 56 | + } else { |
| 57 | + // we decrease one position corresponding to `symfony` command |
| 58 | + current -= 1 |
| 59 | + } |
| 60 | + |
| 61 | + args := make([]string, 0, len(words.All)) |
| 62 | + // build the inputs command line that Symfony expects |
| 63 | + for _, input := range words.All { |
| 64 | + if input = strings.TrimSpace(input); input != "" { |
| 65 | + |
| 66 | + // remove quotes from typed values |
| 67 | + quote := input[0] |
| 68 | + if quote == '\'' || quote == '"' { |
| 69 | + input = strings.TrimPrefix(input, string(quote)) |
| 70 | + input = strings.TrimSuffix(input, string(quote)) |
| 71 | + } |
| 72 | + |
| 73 | + args = append(args, fmt.Sprintf("-i%s", input)) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return append([]string{ |
| 78 | + "_complete", "--no-interaction", |
| 79 | + fmt.Sprintf("-c%d", current), |
| 80 | + fmt.Sprintf("-i%s", wrappedCommand), |
| 81 | + }, args...) |
| 82 | +} |
0 commit comments