/
platform.go
315 lines (251 loc) · 8.21 KB
/
platform.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package extension
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"github.com/FriendsOfShopware/shopware-cli/internal/phplint"
"github.com/FriendsOfShopware/shopware-cli/logging"
"github.com/FriendsOfShopware/shopware-cli/version"
)
type PlatformPlugin struct {
path string
composer platformComposerJson
config *Config
}
// GetRootDir returns the src directory of the plugin.
func (p PlatformPlugin) GetRootDir() string {
return path.Join(p.path, "src")
}
// GetResourcesDir returns the resources directory of the plugin.
func (p PlatformPlugin) GetResourcesDir() string {
return path.Join(p.GetRootDir(), "Resources")
}
func newPlatformPlugin(path string) (*PlatformPlugin, error) {
composerJsonFile := fmt.Sprintf("%s/composer.json", path)
if _, err := os.Stat(composerJsonFile); err != nil {
return nil, err
}
jsonFile, err := os.ReadFile(composerJsonFile)
if err != nil {
return nil, fmt.Errorf("newPlatformPlugin: %v", err)
}
var composerJson platformComposerJson
err = json.Unmarshal(jsonFile, &composerJson)
if err != nil {
return nil, fmt.Errorf("newPlatformPlugin: %v", err)
}
if composerJson.Type != ComposerTypePlugin {
return nil, fmt.Errorf("newPlatformPlugin: composer.json is not type of shopware-platform-plugin")
}
cfg, err := readExtensionConfig(path)
if err != nil {
return nil, fmt.Errorf("newPlatformPlugin: %v", err)
}
extension := PlatformPlugin{
composer: composerJson,
path: path,
config: cfg,
}
return &extension, nil
}
type platformComposerJson struct {
Name string `json:"name"`
Keywords []string `json:"keywords"`
Description string `json:"description"`
Version string `json:"version"`
Type string `json:"type"`
License string `json:"license"`
Authors []struct {
Name string `json:"name"`
Homepage string `json:"homepage"`
} `json:"authors"`
Require map[string]string `json:"require"`
Extra platformComposerJsonExtra `json:"extra"`
Autoload struct {
Psr0 map[string]string `json:"psr-0"`
Psr4 map[string]string `json:"psr-4"`
} `json:"autoload"`
}
type platformComposerJsonExtra struct {
ShopwarePluginClass string `json:"shopware-plugin-class"`
Label map[string]string `json:"label"`
Description map[string]string `json:"description"`
ManufacturerLink map[string]string `json:"manufacturerLink"`
SupportLink map[string]string `json:"supportLink"`
PluginIcon string `json:"plugin-icon"`
}
func (p PlatformPlugin) GetName() (string, error) {
if p.composer.Extra.ShopwarePluginClass == "" {
return "", fmt.Errorf("extension name is empty")
}
parts := strings.Split(p.composer.Extra.ShopwarePluginClass, "\\")
return parts[len(parts)-1], nil
}
func (p PlatformPlugin) GetExtensionConfig() *Config {
return p.config
}
func (p PlatformPlugin) GetShopwareVersionConstraint() (*version.Constraints, error) {
if p.config != nil && p.config.Build.ShopwareVersionConstraint != "" {
constraint, err := version.NewConstraint(p.config.Build.ShopwareVersionConstraint)
if err != nil {
return nil, err
}
return &constraint, nil
}
shopwareConstraintString, ok := p.composer.Require["shopware/core"]
if !ok {
return nil, fmt.Errorf("require.shopware/core is required")
}
shopwareConstraint, err := version.NewConstraint(shopwareConstraintString)
if err != nil {
return nil, err
}
return &shopwareConstraint, err
}
func (PlatformPlugin) GetType() string {
return TypePlatformPlugin
}
func (p PlatformPlugin) GetVersion() (*version.Version, error) {
return version.NewVersion(p.composer.Version)
}
func (p PlatformPlugin) GetChangelog() (*extensionTranslated, error) {
return parseExtensionMarkdownChangelog(p)
}
func (p PlatformPlugin) GetLicense() (string, error) {
return p.composer.License, nil
}
func (p PlatformPlugin) GetPath() string {
return p.path
}
func (p PlatformPlugin) GetMetaData() *extensionMetadata {
return &extensionMetadata{
Name: p.composer.Name,
Label: extensionTranslated{
German: p.composer.Extra.Label["de-DE"],
English: p.composer.Extra.Label["en-GB"],
},
Description: extensionTranslated{
German: p.composer.Extra.Description["de-DE"],
English: p.composer.Extra.Description["en-GB"],
},
}
}
func (p PlatformPlugin) Validate(c context.Context, ctx *ValidationContext) {
if p.composer.Name == "" {
ctx.AddError("Key `name` is required")
}
if p.composer.Type == "" {
ctx.AddError("Key `type` is required")
} else if p.composer.Type != ComposerTypePlugin {
ctx.AddError("The composer type must be shopware-platform-plugin")
}
if p.composer.Description == "" {
ctx.AddError("Key `description` is required")
}
if p.composer.License == "" {
ctx.AddError("Key `license` is required")
}
if p.composer.Version == "" {
ctx.AddError("Key `version` is required")
}
if len(p.composer.Authors) == 0 {
ctx.AddError("Key `authors` is required")
}
if len(p.composer.Require) == 0 {
ctx.AddError("Key `require` is required")
} else {
_, exists := p.composer.Require["shopware/core"]
if !exists {
ctx.AddError("You need to require \"shopware/core\" package")
}
}
requiredKeys := []string{"de-DE", "en-GB"}
for _, key := range requiredKeys {
_, hasLabel := p.composer.Extra.Label[key]
_, hasDescription := p.composer.Extra.Description[key]
_, hasManufacturer := p.composer.Extra.ManufacturerLink[key]
_, hasSupportLink := p.composer.Extra.SupportLink[key]
if !hasLabel {
ctx.AddError(fmt.Sprintf("extra.label for language %s is required", key))
}
if !hasDescription {
ctx.AddError(fmt.Sprintf("extra.description for language %s is required", key))
}
if !hasManufacturer {
ctx.AddError(fmt.Sprintf("extra.manufacturerLink for language %s is required", key))
}
if !hasSupportLink {
ctx.AddError(fmt.Sprintf("extra.supportLink for language %s is required", key))
}
}
if len(p.composer.Autoload.Psr0) == 0 && len(p.composer.Autoload.Psr4) == 0 {
ctx.AddError("At least one of the properties psr-0 or psr-4 are required in the composer.json")
}
pluginIcon := p.composer.Extra.PluginIcon
if pluginIcon == "" {
pluginIcon = "src/Resources/config/plugin.png"
}
// check if the plugin icon exists
if _, err := os.Stat(filepath.Join(p.GetPath(), pluginIcon)); os.IsNotExist(err) {
ctx.AddError(fmt.Sprintf("The plugin icon %s does not exist", pluginIcon))
}
validateTheme(ctx)
validatePHPFiles(c, ctx)
}
func validatePHPFiles(c context.Context, ctx *ValidationContext) {
constraint, err := ctx.Extension.GetShopwareVersionConstraint()
if err != nil {
ctx.AddError(fmt.Sprintf("Could not parse shopware version constraint: %s", err.Error()))
return
}
phpVersion, err := GetPhpVersion(c, constraint)
if err != nil {
ctx.AddWarning(fmt.Sprintf("Could not find min php version for plugin: %s", err.Error()))
return
}
if phpVersion == "7.2" {
phpVersion = "7.3"
logging.FromContext(c).Infof("PHP 7.2 is not supported for PHP linting, using 7.3 now")
}
phpErrors, err := phplint.LintFolder(c, phpVersion, ctx.Extension.GetRootDir())
if err != nil {
ctx.AddWarning(fmt.Sprintf("Could not lint php files: %s", err.Error()))
return
}
for _, error := range phpErrors {
ctx.AddError(fmt.Sprintf("%s: %s", error.File, error.Message))
}
}
func GetPhpVersion(ctx context.Context, constraint *version.Constraints) (string, error) {
r, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "https://raw.githubusercontent.com/FriendsOfShopware/shopware-static-data/main/data/php-version.json", http.NoBody)
resp, err := http.DefaultClient.Do(r)
if err != nil {
return "", err
}
defer func() {
if err := resp.Body.Close(); err != nil {
logging.FromContext(ctx).Errorf("GetPhpVersion: %v", err)
}
}()
var shopwareToPHPVersion map[string]string
err = json.NewDecoder(resp.Body).Decode(&shopwareToPHPVersion)
if err != nil {
return "", err
}
for shopwareVersion, phpVersion := range shopwareToPHPVersion {
shopwareVersionConstraint, err := version.NewVersion(shopwareVersion)
if err != nil {
continue
}
if constraint.Check(shopwareVersionConstraint) {
return phpVersion, nil
}
}
return "", errors.New("could not find php version for shopware version")
}