-
Notifications
You must be signed in to change notification settings - Fork 9
/
stack.go
201 lines (176 loc) · 5.52 KB
/
stack.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
package question
import (
"context"
"fmt"
"os"
"strings"
"golang.org/x/exp/slices"
"github.com/AlecAivazis/survey/v2"
"github.com/platformsh/platformify/internal/colors"
"github.com/platformsh/platformify/internal/question/models"
"github.com/platformsh/platformify/internal/questionnaire"
"github.com/platformsh/platformify/internal/utils"
)
const (
settingsPyFile = "settings.py"
managePyFile = "manage.py"
composerJSONFile = "composer.json"
packageJSONFile = "package.json"
symfonyLockFile = "symfony.lock"
)
type Stack struct{}
func (q *Stack) Ask(ctx context.Context) error {
answers, ok := models.FromContext(ctx)
if !ok {
return nil
}
defer func() {
_, stderr, ok := colors.FromContext(ctx)
if !ok {
return
}
if answers.Stack != models.GenericStack {
fmt.Fprintf(
stderr,
"%s %s\n",
colors.Colorize(colors.GreenCode, "✓"),
colors.Colorize(
colors.BrandCode,
fmt.Sprintf("Detected stack: %s", answers.Stack.Title()),
),
)
}
}()
answers.Stack = models.GenericStack
hasSettingsPy := utils.FileExists(answers.WorkingDirectory, settingsPyFile)
hasManagePy := utils.FileExists(answers.WorkingDirectory, managePyFile)
if hasSettingsPy && hasManagePy {
answers.Stack = models.Django
return nil
}
requirementsPath := utils.FindFile(answers.WorkingDirectory, "requirements.txt")
if requirementsPath != "" {
f, err := os.Open(requirementsPath)
if err == nil {
defer f.Close()
if ok, _ := utils.ContainsStringInFile(f, "flask", true); ok {
answers.Stack = models.Flask
return nil
}
}
}
pyProjectPath := utils.FindFile(answers.WorkingDirectory, "pyproject.toml")
if pyProjectPath != "" {
if _, ok := utils.GetTOMLValue([]string{"tool", "poetry", "dependencies", "flask"}, pyProjectPath, true); ok {
answers.Stack = models.Flask
return nil
}
}
pipfilePath := utils.FindFile(answers.WorkingDirectory, "Pipfile")
if pipfilePath != "" {
if _, ok := utils.GetTOMLValue([]string{"packages", "flask"}, pipfilePath, true); ok {
answers.Stack = models.Flask
return nil
}
}
composerJSONPaths := utils.FindAllFiles(answers.WorkingDirectory, composerJSONFile)
for _, composerJSONPath := range composerJSONPaths {
if _, ok := utils.GetJSONValue([]string{"require", "laravel/framework"}, composerJSONPath, true); ok {
answers.Stack = models.Laravel
return nil
}
}
packageJSONPaths := utils.FindAllFiles(answers.WorkingDirectory, packageJSONFile)
for _, packageJSONPath := range packageJSONPaths {
if _, ok := utils.GetJSONValue([]string{"dependencies", "next"}, packageJSONPath, true); ok {
answers.Stack = models.NextJS
return nil
}
if _, ok := utils.GetJSONValue([]string{"dependencies", "@strapi/strapi"}, packageJSONPath, true); ok {
answers.Stack = models.Strapi
return nil
}
if _, ok := utils.GetJSONValue([]string{"dependencies", "strapi"}, packageJSONPath, true); ok {
answers.Stack = models.Strapi
return nil
}
if _, ok := utils.GetJSONValue([]string{"dependencies", "express"}, packageJSONPath, true); ok {
answers.Stack = models.Express
return nil
}
}
hasSymfonyLock := utils.FileExists(answers.WorkingDirectory, symfonyLockFile)
hasSymfonyBundle := false
hasIbexaDependencies := false
hasShopwareDependencies := false
for _, composerJSONPath := range composerJSONPaths {
if _, ok := utils.GetJSONValue([]string{"autoload", "psr-0", "shopware"}, composerJSONPath, true); ok {
hasShopwareDependencies = true
break
}
if _, ok := utils.GetJSONValue([]string{"autoload", "psr-4", "shopware\\core\\"}, composerJSONPath, true); ok {
hasShopwareDependencies = true
break
}
if _, ok := utils.GetJSONValue([]string{"autoload", "psr-4", "shopware\\appbundle\\"}, composerJSONPath, true); ok {
hasShopwareDependencies = true
break
}
if keywords, ok := utils.GetJSONValue([]string{"keywords"}, composerJSONPath, true); ok {
if keywordsVal, ok := keywords.([]string); ok && slices.Contains(keywordsVal, "shopware") {
hasShopwareDependencies = true
break
}
}
if requirements, ok := utils.GetJSONValue([]string{"require"}, composerJSONPath, true); ok {
if requirementsVal, requirementsOK := requirements.(map[string]interface{}); requirementsOK {
if _, hasSymfonyFrameworkBundle := requirementsVal["symfony/framework-bundle"]; hasSymfonyFrameworkBundle {
hasSymfonyBundle = true
}
for requirement := range requirementsVal {
if strings.HasPrefix(requirement, "shopware/") {
hasShopwareDependencies = true
break
}
if strings.HasPrefix(requirement, "ibexa/") {
hasIbexaDependencies = true
break
}
if strings.HasPrefix(requirement, "ezsystems/") {
hasIbexaDependencies = true
break
}
}
}
}
}
isSymfony := hasSymfonyBundle || hasSymfonyLock
if isSymfony && !hasIbexaDependencies && !hasShopwareDependencies {
_, stderr, ok := colors.FromContext(ctx)
if !ok {
return questionnaire.ErrSilent
}
confirm := true
err := survey.AskOne(
&survey.Confirm{
Message: "It seems like this project uses Symfony full-stack. For a better experience, you should use Symfony CLI. Would you like to use it to deploy your project instead?", //nolint:lll
Default: confirm,
},
&confirm,
)
if err != nil {
return err
}
if confirm {
fmt.Fprintln(
stderr,
colors.Colorize(
colors.WarningCode,
"Check out the Symfony CLI documentation here: https://docs.platform.sh/guides/symfony/get-started.html",
),
)
return questionnaire.ErrSilent
}
}
return nil
}