-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add console command wrapper for bin/console
- Loading branch information
Showing
2 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package project | ||
|
||
import ( | ||
"github.com/FriendsOfShopware/shopware-cli/extension" | ||
"github.com/FriendsOfShopware/shopware-cli/internal/phpexec" | ||
"github.com/FriendsOfShopware/shopware-cli/shop" | ||
"github.com/spf13/cobra" | ||
"slices" | ||
) | ||
|
||
var pluginCommands = []string{"plugin:install", "plugin:uninstall", "plugin:update", "plugin:activate", "plugin:deactivate"} | ||
var appCommands = []string{"app:install", "app:update", "app:activate", "app:deactivate"} | ||
|
||
var projectConsoleCmd = &cobra.Command{ | ||
Use: "console", | ||
Short: "Runs the Symfony Console (bin/console) for current project", | ||
Args: cobra.MinimumNArgs(1), | ||
DisableFlagParsing: true, | ||
ValidArgsFunction: func(cmd *cobra.Command, input []string, _ string) ([]string, cobra.ShellCompDirective) { | ||
projectRoot, err := findClosestShopwareProject() | ||
if err != nil { | ||
return nil, cobra.ShellCompDirectiveDefault | ||
} | ||
|
||
parsedCommands, err := shop.GetConsoleCompletion(cmd.Context(), projectRoot) | ||
|
||
if err != nil { | ||
return nil, cobra.ShellCompDirectiveDefault | ||
} | ||
completions := make([]string, 0) | ||
|
||
if len(input) == 0 { | ||
for _, command := range parsedCommands.Commands { | ||
if !command.Hidden { | ||
completions = append(completions, command.Name) | ||
} | ||
} | ||
} else { | ||
completions = parsedCommands.GetCommandOptions(input[0]) | ||
|
||
isAppCommand := slices.Contains(appCommands, input[0]) | ||
isPluginCommand := slices.Contains(pluginCommands, input[0]) | ||
|
||
if isAppCommand || isPluginCommand { | ||
extensions := extension.FindExtensionsFromProject(cmd.Context(), projectRoot) | ||
|
||
for _, extension := range extensions { | ||
if (extension.GetType() == "plugin" && isPluginCommand) || (extension.GetType() == "app" && isAppCommand) { | ||
name, err := extension.GetName() | ||
|
||
if err != nil { | ||
continue | ||
} | ||
|
||
completions = append(completions, name) | ||
} | ||
} | ||
} | ||
|
||
filtered := make([]string, 0) | ||
for _, completion := range completions { | ||
if slices.Contains(input, completion) { | ||
continue | ||
} | ||
|
||
filtered = append(filtered, completion) | ||
} | ||
|
||
completions = filtered | ||
} | ||
|
||
return completions, cobra.ShellCompDirectiveDefault | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
projectRoot, err := findClosestShopwareProject() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
consoleCmd := phpexec.ConsoleCommand(cmd.Context(), args...) | ||
consoleCmd.Dir = projectRoot | ||
consoleCmd.Stdin = cmd.InOrStdin() | ||
consoleCmd.Stdout = cmd.OutOrStdout() | ||
consoleCmd.Stderr = cmd.ErrOrStderr() | ||
|
||
return consoleCmd.Run() | ||
}, | ||
} | ||
|
||
func init() { | ||
projectRootCmd.AddCommand(projectConsoleCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package shop | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"github.com/FriendsOfShopware/shopware-cli/internal/phpexec" | ||
"os" | ||
"path" | ||
) | ||
|
||
type ConsoleResponse struct { | ||
Commands []struct { | ||
Name string `json:"name"` | ||
Hidden bool `json:"hidden"` | ||
Definition struct { | ||
Arguments interface{} `json:"arguments"` | ||
Options map[string]struct { | ||
Shortcut string `json:"shortcut"` | ||
} `json:"options"` | ||
} `json:"definition"` | ||
} `json:"commands"` | ||
} | ||
|
||
func (c ConsoleResponse) GetCommandOptions(name string) []string { | ||
for _, command := range c.Commands { | ||
if !command.Hidden && command.Name == name { | ||
options := make([]string, 0) | ||
for optionName := range command.Definition.Options { | ||
options = append(options, fmt.Sprintf("--%s", optionName)) | ||
} | ||
|
||
return options | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func GetConsoleCompletion(ctx context.Context, projectRoot string) (*ConsoleResponse, error) { | ||
cachePath := path.Join(projectRoot, "var", "cache", "console_commands.json") | ||
|
||
if _, err := os.Stat(cachePath); err == nil { | ||
var resp ConsoleResponse | ||
|
||
bytes, err := os.ReadFile(cachePath) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := json.Unmarshal(bytes, &resp); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &resp, nil | ||
} | ||
|
||
consoleCommand := phpexec.ConsoleCommand(ctx, "list", "--format=json") | ||
consoleCommand.Dir = projectRoot | ||
|
||
commandJson, err := consoleCommand.Output() | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var resp ConsoleResponse | ||
|
||
if err := json.Unmarshal(commandJson, &resp); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := os.WriteFile(cachePath, commandJson, os.ModePerm); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &resp, nil | ||
} |
05235ec
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shyim I'm getting the following, when running
maybe because of that commit?
If I go to the first lines of bin/console and put a breakpoint that's what I get:
Shopware version:
6.6.7.0
Which end up being the same as:
05235ec
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I cannot reproduce it on
0.4.61
. it works for me 🤔