-
Notifications
You must be signed in to change notification settings - Fork 170
/
appdetect.go
317 lines (261 loc) · 7.19 KB
/
appdetect.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
316
317
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package appdetect
import (
"context"
"errors"
"fmt"
"io/fs"
"log"
"os"
"path/filepath"
"github.com/azure/azure-dev/cli/azd/pkg/exec"
"github.com/azure/azure-dev/cli/azd/pkg/tools/dotnet"
"github.com/bmatcuk/doublestar/v4"
)
type Language string
const (
DotNet Language = "dotnet"
DotNetAppHost Language = "dotnet-apphost"
Java Language = "java"
JavaScript Language = "js"
TypeScript Language = "ts"
Python Language = "python"
)
func (pt Language) Display() string {
switch pt {
case DotNet:
return ".NET"
case DotNetAppHost:
return ".NET (Aspire)"
case Java:
return "Java"
case JavaScript:
return "JavaScript"
case TypeScript:
return "TypeScript"
case Python:
return "Python"
}
return ""
}
type Dependency string
const (
JsReact Dependency = "react"
JsAngular Dependency = "angular"
JsJQuery Dependency = "jquery"
JsVite Dependency = "vite"
JsNext Dependency = "next"
PyFlask Dependency = "flask"
PyDjango Dependency = "django"
PyFastApi Dependency = "fastapi"
)
var WebUIFrameworks = map[Dependency]struct{}{
JsReact: {},
JsAngular: {},
JsJQuery: {},
JsVite: {},
}
func (f Dependency) Language() Language {
switch f {
case JsReact, JsAngular, JsJQuery, JsVite:
return JavaScript
}
return ""
}
func (f Dependency) Display() string {
switch f {
case JsReact:
return "React"
case JsAngular:
return "Angular"
case JsJQuery:
return "JQuery"
case JsVite:
return "Vite"
case JsNext:
return "Next.js"
}
return ""
}
func (f Dependency) IsWebUIFramework() bool {
if _, ok := WebUIFrameworks[f]; ok {
return true
}
return false
}
// A type of database that is inferred through heuristics while scanning project information.
type DatabaseDep string
const (
// Database dependencies
DbPostgres DatabaseDep = "postgres"
DbMongo DatabaseDep = "mongo"
DbMySql DatabaseDep = "mysql"
DbSqlServer DatabaseDep = "sqlserver"
DbRedis DatabaseDep = "redis"
)
func (db DatabaseDep) Display() string {
switch db {
case DbPostgres:
return "PostgreSQL"
case DbMongo:
return "MongoDB"
case DbMySql:
return "MySQL"
case DbSqlServer:
return "SQL Server"
case DbRedis:
return "Redis"
}
return ""
}
type Project struct {
// The language associated with the project.
Language Language
// Dependencies scanned in the project.
Dependencies []Dependency
// Experimental: Database dependencies inferred through heuristics while scanning dependencies in the project.
DatabaseDeps []DatabaseDep
// The path to the project directory.
Path string
// A short description of the detection rule applied.
DetectionRule string
// If true, the project uses Docker for packaging. This is inferred through the presence of a Dockerfile.
Docker *Docker
}
func (p *Project) HasWebUIFramework() bool {
for _, f := range p.Dependencies {
if f.IsWebUIFramework() {
return true
}
}
return false
}
type Docker struct {
Path string
}
type projectDetector interface {
Language() Language
DetectProject(ctx context.Context, path string, entries []fs.DirEntry) (*Project, error)
}
var allDetectors = []projectDetector{
// Order here determines precedence when two projects are in the same directory.
// This is unlikely to occur in practice, but reordering could help to break the tie in these cases.
&javaDetector{},
&dotNetAppHostDetector{
// TODO(ellismg): Remove ambient authority.
dotnetCli: dotnet.NewDotNetCli(exec.NewCommandRunner(nil)),
},
&dotNetDetector{
dotnetCli: dotnet.NewDotNetCli(exec.NewCommandRunner(nil)),
},
&pythonDetector{},
&javaScriptDetector{},
}
// Detect detects projects located under a directory.
func Detect(ctx context.Context, root string, options ...DetectOption) ([]Project, error) {
config := newConfig(options...)
return detectUnder(ctx, root, config)
}
// DetectDirectory detects the project located in a directory.
func DetectDirectory(ctx context.Context, directory string, options ...DetectDirectoryOption) (*Project, error) {
config := newDirectoryConfig(options...)
entries, err := os.ReadDir(directory)
if err != nil {
return nil, fmt.Errorf("reading directory: %w", err)
}
return detectAny(ctx, config.detectors, directory, entries)
}
func detectUnder(ctx context.Context, root string, config detectConfig) ([]Project, error) {
projects := []Project{}
walkFunc := func(path string, entries []fs.DirEntry) error {
relativePath, err := filepath.Rel(root, path)
if err != nil {
return err
}
// Normalize all paths to use forward slash '/' for glob matching
relativePathForMatch := filepath.ToSlash(relativePath)
for _, p := range config.ExcludePatterns {
match, err := doublestar.Match(p, relativePathForMatch)
if err != nil {
return err
}
if match {
return filepath.SkipDir
}
}
project, err := detectAny(ctx, config.detectors, path, entries)
if err != nil {
return err
}
if project != nil {
// Once a project is detected, we skip possible inner projects.
projects = append(projects, *project)
return filepath.SkipDir
}
return nil
}
err := walkDirectories(root, walkFunc)
if err != nil {
return nil, fmt.Errorf("scanning directories: %w", err)
}
return projects, nil
}
// Detects if a directory belongs to any projects.
func detectAny(ctx context.Context, detectors []projectDetector, path string, entries []fs.DirEntry) (*Project, error) {
log.Printf("Detecting projects in directory: %s", path)
for _, detector := range detectors {
project, err := detector.DetectProject(ctx, path, entries)
if err != nil {
return nil, fmt.Errorf("detecting %s project: %w", string(detector.Language()), err)
}
if project != nil {
log.Printf("Found project %s at %s", project.Language, path)
// docker is an optional property of a project, and thus is different than other detectors
docker, err := detectDocker(path, entries)
if err != nil {
return nil, fmt.Errorf("detecting docker project: %w", err)
}
project.Docker = docker
return project, nil
}
}
return nil, nil
}
// walkDirFunc is the type of function that is called whenever a directory is visited by WalkDirectories.
//
// path is the directory being visited. entries are the file entries (including directories) in that directory.
type walkDirFunc func(path string, entries []fs.DirEntry) error
// walkDirectories walks the file tree rooted at root, calling fn for each directory in the tree, including root.
// The directories are walked in lexical order.
func walkDirectories(root string, fn walkDirFunc) error {
info, err := os.Lstat(root)
if err != nil {
return err
}
return walkDirRecursive(root, fs.FileInfoToDirEntry(info), fn)
}
func walkDirRecursive(path string, d fs.DirEntry, fn walkDirFunc) error {
entries, err := os.ReadDir(path)
if err != nil {
return fmt.Errorf("reading directory: %w", err)
}
err = fn(path, entries)
if errors.Is(err, filepath.SkipDir) {
// skip the directory
return nil
}
if err != nil {
return err
}
for _, entry := range entries {
if entry.IsDir() {
dir := filepath.Join(path, entry.Name())
err = walkDirRecursive(dir, entry, fn)
if err != nil {
return err
}
}
}
return nil
}