generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
deps.go
138 lines (125 loc) · 3.28 KB
/
deps.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
package buildengine
import (
"bufio"
"context"
"fmt"
"go/parser"
"go/token"
"io/fs"
"os"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"golang.org/x/exp/maps"
"github.com/TBD54566975/ftl/internal/log"
)
// UpdateDependencies finds the dependencies for a module and returns a
// Module with those dependencies populated.
func UpdateDependencies(ctx context.Context, module Module) (Module, error) {
logger := log.FromContext(ctx)
logger.Debugf("Extracting dependencies for %q", module.Config.Module)
dependencies, err := extractDependencies(module)
if err != nil {
return Module{}, err
}
containsBuiltin := false
for _, dep := range dependencies {
if dep == "builtin" {
containsBuiltin = true
break
}
}
if !containsBuiltin {
dependencies = append(dependencies, "builtin")
}
out := module.CopyWithDependencies(dependencies)
return out, nil
}
func extractDependencies(module Module) ([]string, error) {
switch module.Config.Language {
case "go":
return extractGoFTLImports(module.Config.Module, module.Config.Dir)
case "kotlin":
return extractKotlinFTLImports(module.Config.Module, module.Config.Dir)
default:
return nil, fmt.Errorf("unsupported language: %s", module.Config.Language)
}
}
func extractGoFTLImports(moduleName, dir string) ([]string, error) {
dependencies := map[string]bool{}
fset := token.NewFileSet()
err := WalkDir(dir, func(path string, d fs.DirEntry) error {
if !d.IsDir() {
return nil
}
if strings.HasPrefix(d.Name(), "_") || d.Name() == "testdata" {
return ErrSkip
}
pkgs, err := parser.ParseDir(fset, path, nil, parser.ImportsOnly)
if pkgs == nil {
return err
}
for _, pkg := range pkgs {
for _, file := range pkg.Files {
for _, imp := range file.Imports {
path, err := strconv.Unquote(imp.Path.Value)
if err != nil {
continue
}
if !strings.HasPrefix(path, "ftl/") {
continue
}
module := strings.Split(strings.TrimPrefix(path, "ftl/"), "/")[0]
if module == moduleName {
continue
}
dependencies[module] = true
}
}
}
return nil
})
if err != nil {
return nil, fmt.Errorf("%s: failed to extract dependencies from Go module: %w", moduleName, err)
}
modules := maps.Keys(dependencies)
sort.Strings(modules)
return modules, nil
}
func extractKotlinFTLImports(self, dir string) ([]string, error) {
dependencies := map[string]bool{}
kotlinImportRegex := regexp.MustCompile(`^import ftl\.([A-Za-z0-9_.]+)`)
err := filepath.WalkDir(filepath.Join(dir, "src/main/kotlin"), func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() || !(strings.HasSuffix(path, ".kt") || strings.HasSuffix(path, ".kts")) {
return nil
}
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
matches := kotlinImportRegex.FindStringSubmatch(scanner.Text())
if len(matches) > 1 {
module := strings.Split(matches[1], ".")[0]
if module == self {
continue
}
dependencies[module] = true
}
}
return scanner.Err()
})
if err != nil {
return nil, fmt.Errorf("%s: failed to extract dependencies from Kotlin module: %w", self, err)
}
modules := maps.Keys(dependencies)
sort.Strings(modules)
return modules, nil
}