8000 - · symfony-cli/symfony-cli@2502c34 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2502c34

Browse files
committed
-
1 parent 019265f commit 2502c34

File tree

6 files changed

+55
-42
lines changed

6 files changed

+55
-42
lines changed

commands/completion_others.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import (
88
"github.com/symfony-cli/console"
99
)
1010

11-
func autocompleteWrapper(context *console.Context, args complete.Args) []string {
11+
func autocompleteComposerWrapper(context *console.Context, args complete.Args) []string {
12+
return []string{}
13+
}
14+
15+
func autocompleteSymfonyConsoleWrapper(context *console.Context, args complete.Args) []string {
1216
return []string{}
1317
}

commands/completion_posix.go

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package commands
55
import (
66
"embed"
77
"fmt"
8+
"io"
89
"os"
910
"path"
1011
"strconv"
@@ -13,6 +14,7 @@ import (
1314
"github.com/posener/complete"
1415
"github.com/symfony-cli/console"
1516
"github.com/symfony-cli/symfony-cli/local/php"
17+
"github.com/symfony-cli/terminal"
1618
)
1719

1820
// completionTemplates holds our custom shell completions templates.
@@ -25,35 +27,39 @@ func init() {
2527
console.CompletionTemplates = completionTemplates
2628
}
2729

28-
func autocompleteWrapper(context *console.Context, words complete.Args) []string {
29-
//debug, _ := os.OpenFile("/Users/tucksaun/Work/src/github.com/symfony-cli/symfony-cli/complete.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
30-
//dumper.Fdump(debug, os.Environ())
30+
func autocompleteSymfonyConsoleWrapper(context *console.Context, words complete.Args) []string {
31+
args := buildSymfonyAutocompleteArgs("console", words)
32+
// Composer does not support those options yet, so we only use them for Symfony Console
33+
args = append(args, "-a1", fmt.Sprintf("-s%s", guessShell()))
3134

32-
// TODO: make this works for Composer as well ;)
35+
os.Exit(php.SymonyConsoleExecutor(args).Execute(false))
3336

34-
consolePath := "bin/console"
35-
if _, err := os.Stat("app/console"); err == nil {
36-
consolePath = "app/console"
37-
} else if _, err := os.Stat(consolePath); os.IsNotExist(err) {
38-
return []string{}
39-
}
37+
// unreachable
38+
return []string{}
39+
}
40+
41+
func autocompleteComposerWrapper(context *console.Context, words complete.Args) []string {
42+
args := buildSymfonyAutocompleteArgs("composer", words)
43+
// Composer does not support multiple shell yet, so we only use the default one
44+
args = append(args, "-sbash")
45+
46+
res := php.Composer("", args, []string{}, context.App.Writer, context.App.ErrWriter, io.Discard, terminal.Logger)
47+
os.Exit(res.ExitCode())
48+
49+
// unreachable
50+
return []string{}
51+
}
4052

53+
func buildSymfonyAutocompleteArgs(wrappedCommand string, words complete.Args) []string {
4154
current, err := strconv.Atoi(os.Getenv("CURRENT"))
4255
if err != nil {
43-
return []string{}
56+
current = 1
4457
} else {
4558
// we decrease one position corresponding to `symfony` command
4659
current -= 1
4760
}
4861

49-
args := []string{
50-
console.CurrentBinaryName(), consolePath, "_complete",
51-
"--no-interaction", "-a1",
52-
fmt.Sprintf("-s%s", guessShell()),
53-
fmt.Sprintf("-c%d", current),
54-
fmt.Sprintf("-i%s", consolePath),
55-
}
56-
62+
args := make([]string, 0, len(words.All))
5763
// build the inputs command line that Symfony expects
5864
for _, input := range words.All {
5965
if input = strings.TrimSpace(input); input != "" {
@@ -69,15 +75,11 @@ func autocompleteWrapper(context *console.Context, words complete.Args) []string
6975
}
7076
}
7177

72-
// execute Symfony autocompletion command
73-
e := &php.Executor{
74-
BinName: "php",
75-
Args: args,
76-
}
77-
os.Exit(e.Execute(false))
78-
79-
// unreachable
80-
return []string{}
78+
return append([]string{
79+
"_complete", "--no-interaction",
80+
fmt.Sprintf("-c%d", current),
81+
fmt.Sprintf("-i%s", wrappedCommand),
82+
}, args...)
8183
}
8284

83 1E0A 85
// TODO: replace by console.GuessShell() when it's exposed

commands/wrappers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ var (
3232
// we use an alias to avoid the command being shown in the help but
3333
// still be available for completion
3434
Aliases: []*console.Alias{{Name: "composer"}},
35-
ShellComplete: autocompleteWrapper,
35+
ShellComplete: autocompleteComposerWrapper,
3636
Action: func(c *console.Context) error {
3737
return console.IncorrectUsageError{ParentError: errors.New(`This command can only be run as "symfony composer"`)}
3838
},
@@ -46,7 +46,7 @@ var (
4646
Action: func(c *console.Context) error {
4747
return console.IncorrectUsageError{ParentError: errors.New(`This command can only be run as "symfony console"`)}
4848
},
49-
ShellComplete: autocompleteWrapper,
49+
ShellComplete: autocompleteSymfonyConsoleWrapper,
5050
}
5151
phpWrapper = &console.Command{
5252
Usage: "Runs the named binary using the configured PHP version",

local/php/executor.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,6 @@ func (e *Executor) Execute(loadDotEnv bool) int {
348348
if e.Dir != "" {
349349
cmd.Dir = e.Dir
350350
}
351-
//debug, _ := os.OpenFile("/Users/tucksaun/Work/src/github.com/symfony-cli/symfony-cli/complete.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
352-
//dumper.Fdump(debug, cmd)
353351
if err := cmd.Start(); err != nil {
354352
fmt.Fprintln(os.Stderr, err)
355353
return 1

local/php/symfony.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package php
2+
3+
import "os"
4+
5+
// ComposerExecutor returns an Executor prepared to run Symfony Console
6+
func SymonyConsoleExecutor(args []string) *Executor {
7+
consolePath := "bin/console"
8+
if _, err := os.Stat("app/console"); err == nil {
9+
consolePath = "app/console"
10+
}
11+
12+
return &Executor{
13+
BinName: "php",
14+
Args: append([]string{"php", consolePath}, args...),
15+
}
16+
}

main.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,8 @@ func main() {
7373
}
7474
// called via "symfony console"?
7575
if len(args) >= 2 && args[1] == "console" {
76-
args[1] = "bin/console"
77-
if _, err := os.Stat("app/console"); err == nil {
78-
args[1] = "app/console"
79-
}
80-
e := &php.Executor{
81-
BinName: "php",
82-
Args: args,
83-
ExtraEnv: getCliExtraEnv(),
84-
}
76+
e := php.SymonyConsoleExecutor(args[2:])
77+
e.ExtraEnv = getCliExtraEnv()
8578
os.Exit(e.Execute(false))
8679
}
8780
// called via "symfony composer"?

0 commit comments

Comments
 (0)
0