-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcmd_twig_upgrade.go
216 lines (165 loc) · 5.42 KB
/
cmd_twig_upgrade.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"fmt"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"github.com/charmbracelet/log"
"github.com/shopware/extension-verifier/internal/llm"
"github.com/shopware/extension-verifier/internal/tool"
"github.com/shopware/extension-verifier/internal/twig"
"github.com/shopware/shopware-cli/extension"
"github.com/spf13/cobra"
)
const systemPrompt = `
You are a helper agent to help to upgrade Twig templates. I will give you the old and new template happend in the Software and as third the extended template. Apply the changes happen between old and new template to the extended template.
- Do only the necessary changes to the extended template.
- Do only modify the content inside the block and dont add new blocks
- Please also only output the modified extended template nothing more.
- Adjust also HTML elements to be more accessibility friendly.
- If in a {% block %} is {{ parent() }}, ignore it and dont modify the content of the block
`
var twigUpgradeCommand = &cobra.Command{
Use: "twig-upgrade [path] [old-shopware-version] [new-shopware-version]",
Short: "Experimental upgrade of Twig templates using AI",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
verbose, _ := cmd.Flags().GetBool("verbose")
ext, err := extension.GetExtensionByFolder(args[0])
if err != nil {
return err
}
if verbose {
fmt.Printf("\nSystem Prompt:\n%s\n", systemPrompt)
}
toolCfg, err := tool.ConvertExtensionToToolConfig(ext)
if err != nil {
return err
}
client, err := llm.NewLLMClient(cmd.Flag("provider").Value.String())
if err != nil {
return err
}
options := &llm.LLMOptions{
Model: cmd.Flag("model").Value.String(),
SystemPrompt: systemPrompt,
}
for _, sourceDirectory := range toolCfg.SourceDirectories {
twigFolder := path.Join(sourceDirectory, "Resources", "views", "storefront")
if _, err := os.Stat(twigFolder); os.IsNotExist(err) {
return nil
}
oldVersion, err := cloneShopwareStorefront(args[1])
if err != nil {
return err
}
defer func() {
if err := os.RemoveAll(oldVersion); err != nil {
fmt.Fprintf(os.Stderr, "Failed to remove old version directory: %v\n", err)
}
}()
newVersion, err := cloneShopwareStorefront(args[2])
if err != nil {
return err
}
defer func() {
if err := os.RemoveAll(newVersion); err != nil {
fmt.Fprintf(os.Stderr, "Failed to remove new version directory: %v\n", err)
}
}()
err = filepath.Walk(twigFolder, func(file string, info os.FileInfo, _ error) error {
if info.IsDir() {
return nil
}
if filepath.Ext(file) != ".twig" {
return nil
}
content, err := os.ReadFile(file)
if err != nil {
return err
}
ast, err := twig.ParseTemplate(string(content))
if err != nil {
return err
}
extends := ast.Extends()
if extends == nil {
return nil
}
tpl := extends.Template
if tpl[0] == '@' {
tplParts := strings.Split(tpl, "/")
tplParts = tplParts[1:]
tpl = strings.Join(tplParts, "/")
}
oldTemplateText, err := os.ReadFile(path.Join(oldVersion, "Resources", "views", tpl))
if err != nil {
fmt.Printf("Template %s not found in old version\n", tpl)
return nil
}
newTemplateText, err := os.ReadFile(path.Join(newVersion, "Resources", "views", tpl))
if err != nil {
fmt.Printf("Template %s not found in new version\n", tpl)
return nil
}
var str strings.Builder
str.WriteString("This was the old template:\n")
str.WriteString("```twig\n")
str.WriteString(string(oldTemplateText))
str.WriteString("\n```\n")
str.WriteString("and this is the new one:\n")
str.WriteString("```twig\n")
str.WriteString(string(newTemplateText))
str.WriteString("\n```\n")
str.WriteString("and this is my template:\n")
str.WriteString("```twig\n")
str.WriteString(string(content))
str.WriteString("\n```")
log.Info("Processing file", "file", file)
if verbose {
fmt.Printf("\nInput to LLM for file %s:\n%s\n", file, str.String())
}
text, err := client.Generate(cmd.Context(), str.String(), options)
if err != nil {
return err
}
start := strings.Index(text, "```twig")
end := strings.LastIndex(text, "```")
if start == -1 || end == -1 {
return nil
}
text = strings.TrimPrefix(text[start+7:end], "\n")
contentStr := string(content)
if strings.TrimSpace(text) == strings.TrimSpace(contentStr) {
return nil
}
return os.WriteFile(file, []byte(text), os.ModePerm)
})
if err != nil {
return err
}
}
return nil
},
}
func cloneShopwareStorefront(version string) (string, error) {
tempDir, err := os.MkdirTemp(os.TempDir(), "shopware")
if err != nil {
return "", err
}
git := exec.Command("git", "-c", "advice.detachedHead=false", "clone", "-q", "--branch", "v"+version, "https://github.com/shopware/storefront", tempDir, "--depth", "1")
git.Stdout = os.Stdout
git.Stderr = os.Stderr
if err := git.Run(); err != nil {
return "", err
}
return tempDir, nil
}
func init() {
twigUpgradeCommand.Flags().String("model", "gemma3:4b", "The model to use for the upgrade")
twigUpgradeCommand.Flags().String("provider", "ollama", "The provider to use for the upgrade")
twigUpgradeCommand.Flags().BoolP("verbose", "v", false, "Print verbose information including LLM inputs")
rootCmd.AddCommand(twigUpgradeCommand)
}