Skip to content

Commit 957d44d

Browse files
committed
feat: add project validation commands for fixing, formatting and validating code
1 parent cff8f39 commit 957d44d

3 files changed

Lines changed: 225 additions & 0 deletions

File tree

cmd/project/project_fix.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package project
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
8+
"github.com/spf13/cobra"
9+
"golang.org/x/sync/errgroup"
10+
11+
"github.com/shopware/shopware-cli/internal/verifier"
12+
)
13+
14+
var projectFixCmd = &cobra.Command{
15+
Use: "fix",
16+
Short: "Fix project",
17+
RunE: func(cmd *cobra.Command, args []string) error {
18+
allowNonGit, _ := cmd.Flags().GetBool("allow-non-git")
19+
gitPath := filepath.Join(args[0], ".git")
20+
if !allowNonGit {
21+
if stat, err := os.Stat(gitPath); err != nil || !stat.IsDir() {
22+
return fmt.Errorf("provided folder is not a git repository. Use --allow-non-git flag to run anyway")
23+
}
24+
}
25+
26+
var err error
27+
only, _ := cmd.Flags().GetString("only")
28+
29+
projectPath := ""
30+
31+
if len(args) > 0 {
32+
projectPath = args[0]
33+
} else {
34+
projectPath, err = findClosestShopwareProject()
35+
if err != nil {
36+
return err
37+
}
38+
}
39+
40+
projectPath, err = filepath.Abs(projectPath)
41+
if err != nil {
42+
return fmt.Errorf("cannot find path: %w", err)
43+
}
44+
45+
toolCfg, err := verifier.GetConfigFromProject(projectPath)
46+
if err != nil {
47+
return err
48+
}
49+
50+
var gr errgroup.Group
51+
52+
tools := verifier.GetTools()
53+
54+
tools, err = tools.Only(only)
55+
if err != nil {
56+
return err
57+
}
58+
59+
for _, tool := range tools {
60+
tool := tool
61+
gr.Go(func() error {
62+
return tool.Fix(cmd.Context(), *toolCfg)
63+
})
64+
}
65+
66+
return gr.Wait()
67+
},
68+
}
69+
70+
func init() {
71+
projectRootCmd.AddCommand(projectFixCmd)
72+
projectFixCmd.PersistentFlags().String("only", "", "Run only specific tools by name (comma-separated, e.g. phpstan,eslint)")
73+
projectFixCmd.PersistentFlags().Bool("allow-non-git", false, "Allow running on non git repositories")
74+
}

cmd/project/project_format.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package project
2+
3+
import (
4+
"fmt"
5+
"path/filepath"
6+
7+
"github.com/spf13/cobra"
8+
"golang.org/x/sync/errgroup"
9+
10+
"github.com/shopware/shopware-cli/internal/verifier"
11+
)
12+
13+
var projectFormatCmd = &cobra.Command{
14+
Use: "format",
15+
Short: "Format project",
16+
RunE: func(cmd *cobra.Command, args []string) error {
17+
var err error
18+
only, _ := cmd.Flags().GetString("only")
19+
dryRun, _ := cmd.Flags().GetBool("dry-run")
20+
21+
projectPath := ""
22+
23+
if len(args) > 0 {
24+
projectPath = args[0]
25+
} else {
26+
projectPath, err = findClosestShopwareProject()
27+
if err != nil {
28+
return err
29+
}
30+
}
31+
32+
projectPath, err = filepath.Abs(projectPath)
33+
if err != nil {
34+
return fmt.Errorf("cannot find path: %w", err)
35+
}
36+
37+
toolCfg, err := verifier.GetConfigFromProject(projectPath)
38+
if err != nil {
39+
return err
40+
}
41+
42+
var gr errgroup.Group
43+
44+
tools := verifier.GetTools()
45+
46+
tools, err = tools.Only(only)
47+
if err != nil {
48+
return err
49+
}
50+
51+
for _, tool := range tools {
52+
tool := tool
53+
gr.Go(func() error {
54+
return tool.Format(cmd.Context(), *toolCfg, dryRun)
55+
})
56+
}
57+
58+
return gr.Wait()
59+
},
60+
}
61+
62+
func init() {
63+
projectRootCmd.AddCommand(projectFormatCmd)
64+
projectFormatCmd.PersistentFlags().String("only", "", "Run only specific tools by name (comma-separated, e.g. phpstan,eslint)")
65+
projectFormatCmd.PersistentFlags().Bool("dry-run", false, "Run tools in dry run mode")
66+
}

cmd/project/project_validate.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package project
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
8+
"github.com/spf13/cobra"
9+
"golang.org/x/sync/errgroup"
10+
11+
"github.com/shopware/shopware-cli/internal/system"
12+
"github.com/shopware/shopware-cli/internal/verifier"
13+
)
14+
15+
var projectValidateCmd = &cobra.Command{
16+
Use: "validate",
17+
Short: "Validate project",
18+
RunE: func(cmd *cobra.Command, args []string) error {
19+
reportingFormat, _ := cmd.Flags().GetString("reporter")
20+
only, _ := cmd.Flags().GetString("only")
21+
tmpDir, err := os.MkdirTemp(os.TempDir(), "analyse-project-*")
22+
if err != nil {
23+
return fmt.Errorf("cannot create temporary directory: %w", err)
24+
}
25+
26+
projectPath := ""
27+
28+
if len(args) > 0 {
29+
projectPath = args[0]
30+
} else {
31+
projectPath, err = findClosestShopwareProject()
32+
if err != nil {
33+
return err
34+
}
35+
}
36+
37+
projectPath, err = filepath.Abs(projectPath)
38+
if err != nil {
39+
return fmt.Errorf("cannot find path: %w", err)
40+
}
41+
42+
if reportingFormat == "" {
43+
reportingFormat = verifier.DetectDefaultReporter()
44+
}
45+
46+
if err := system.CopyFiles(projectPath, tmpDir); err != nil {
47+
return err
48+
}
49+
50+
toolCfg, err := verifier.GetConfigFromProject(tmpDir)
51+
if err != nil {
52+
return err
53+
}
54+
55+
result := verifier.NewCheck()
56+
57+
var gr errgroup.Group
58+
59+
tools := verifier.GetTools()
60+
61+
tools, err = tools.Only(only)
62+
if err != nil {
63+
return err
64+
}
65+
66+
for _, tool := range tools {
67+
tool := tool
68+
gr.Go(func() error {
69+
return tool.Check(cmd.Context(), result, *toolCfg)
70+
})
71+
}
72+
73+
if err := gr.Wait(); err != nil {
74+
return err
75+
}
76+
77+
return verifier.DoCheckReport(result.RemoveByIdentifier(toolCfg.ValidationIgnores), reportingFormat)
78+
},
79+
}
80+
81+
func init() {
82+
projectRootCmd.AddCommand(projectValidateCmd)
83+
projectValidateCmd.PersistentFlags().String("reporter", "", "Reporting format (summary, json, github, junit, markdown)")
84+
projectValidateCmd.PersistentFlags().String("only", "", "Run only specific tools by name (comma-separated, e.g. phpstan,eslint)")
85+
}

0 commit comments

Comments
 (0)