Skip to content

Commit

Permalink
feat: add console command wrapper for bin/console
Browse files Browse the repository at this point in the history
  • Loading branch information
shyim committed Nov 18, 2024
1 parent e07c747 commit 05235ec
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 0 deletions.
92 changes: 92 additions & 0 deletions cmd/project/project_console.go
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)
}
78 changes: 78 additions & 0 deletions shop/console.go
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
}

0 comments on commit 05235ec

Please sign in to comment.